Daksh7

Benchmark Results

Feb 27th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.11 KB | None | 0 0
  1. Prompt: Make a decent, ready to run CLI calculator in Rust.
  2.  
  3. Sonnet 3.7 (thinking):
  4.  
  5. use std::io::{self, Write};
  6.  
  7. fn main() {
  8. println!("Simple Rust Calculator");
  9. println!("Enter mathematical expressions to calculate (e.g. 2 + 3 * 4)");
  10. println!("Supported operations: +, -, *, / and parentheses");
  11. println!("Type 'exit' to quit");
  12.  
  13. loop {
  14. print!("> ");
  15. io::stdout().flush().unwrap();
  16.  
  17. let mut input = String::new();
  18. io::stdin().read_line(&mut input).expect("Failed to read input");
  19.  
  20. let input = input.trim();
  21.  
  22. if input.eq_ignore_ascii_case("exit") {
  23. break;
  24. }
  25.  
  26. if input.is_empty() {
  27. continue;
  28. }
  29.  
  30. match evaluate_expression(input) {
  31. Ok(result) => println!("= {}", result),
  32. Err(err) => println!("Error: {}", err),
  33. }
  34. }
  35. }
  36.  
  37. // Function to evaluate a mathematical expression
  38. fn evaluate_expression(expr: &str) -> Result<f64, String> {
  39. let mut chars = expr.chars().peekable();
  40. let result = parse_expression(&mut chars)?;
  41.  
  42. // Check if there's any unexpected input left
  43. while let Some(&c) = chars.peek() {
  44. if c.is_whitespace() {
  45. chars.next();
  46. } else {
  47. return Err(format!("Unexpected character at end: {}", c));
  48. }
  49. }
  50.  
  51. Ok(result)
  52. }
  53.  
  54. // Parse an expression (addition and subtraction)
  55. fn parse_expression<I>(chars: &mut std::iter::Peekable<I>) -> Result<f64, String>
  56. where
  57. I: Iterator<Item = char>,
  58. {
  59. let mut left = parse_term(chars)?;
  60.  
  61. while let Some(&c) = chars.peek() {
  62. if c == '+' || c == '-' {
  63. let op = c;
  64. chars.next(); // Consume the operator
  65.  
  66. let right = parse_term(chars)?;
  67.  
  68. match op {
  69. '+' => left += right,
  70. '-' => left -= right,
  71. _ => unreachable!(),
  72. }
  73. } else {
  74. break;
  75. }
  76. }
  77.  
  78. Ok(left)
  79. }
  80.  
  81. // Parse a term (multiplication and division)
  82. fn parse_term<I>(chars: &mut std::iter::Peekable<I>) -> Result<f64, String>
  83. where
  84. I: Iterator<Item = char>,
  85. {
  86. let mut left = parse_factor(chars)?;
  87.  
  88. while let Some(&c) = chars.peek() {
  89. if c == '*' || c == '/' {
  90. let op = c;
  91. chars.next(); // Consume the operator
  92.  
  93. let right = parse_factor(chars)?;
  94.  
  95. match op {
  96. '*' => left *= right,
  97. '/' => {
  98. if right == 0.0 {
  99. return Err("Division by zero".to_string());
  100. }
  101. left /= right;
  102. },
  103. _ => unreachable!(),
  104. }
  105. } else {
  106. break;
  107. }
  108. }
  109.  
  110. Ok(left)
  111. }
  112.  
  113. // Parse a factor (number or parenthesized expression)
  114. fn parse_factor<I>(chars: &mut std::iter::Peekable<I>) -> Result<f64, String>
  115. where
  116. I: Iterator<Item = char>,
  117. {
  118. // Skip whitespace
  119. while let Some(&c) = chars.peek() {
  120. if c.is_whitespace() {
  121. chars.next();
  122. } else {
  123. break;
  124. }
  125. }
  126.  
  127. if let Some(&c) = chars.peek() {
  128. if c == '(' {
  129. chars.next(); // Consume '('
  130. let result = parse_expression(chars)?;
  131.  
  132. // Skip whitespace
  133. while let Some(&c) = chars.peek() {
  134. if c.is_whitespace() {
  135. chars.next();
  136. } else {
  137. break;
  138. }
  139. }
  140.  
  141. // Expect closing parenthesis
  142. if chars.next() != Some(')') {
  143. return Err("Expected closing parenthesis".to_string());
  144. }
  145.  
  146. Ok(result)
  147. } else if c.is_digit(10) || c == '.' || c == '-' {
  148. parse_number(chars)
  149. } else {
  150. Err(format!("Unexpected character: {}", c))
  151. }
  152. } else {
  153. Err("Unexpected end of input".to_string())
  154. }
  155. }
  156.  
  157. // Parse a number (integer or decimal)
  158. fn parse_number<I>(chars: &mut std::iter::Peekable<I>) -> Result<f64, String>
  159. where
  160. I: Iterator<Item = char>,
  161. {
  162. let mut num_str = String::new();
  163.  
  164. // Handle negative numbers
  165. if let Some(&c) = chars.peek() {
  166. if c == '-' {
  167. num_str.push(c);
  168. chars.next();
  169. }
  170. }
  171.  
  172. // Parse digits before decimal
  173. let mut has_digits = false;
  174. while let Some(&c) = chars.peek() {
  175. if c.is_digit(10) {
  176. has_digits = true;
  177. num_str.push(c);
  178. chars.next();
  179. } else {
  180. break;
  181. }
  182. }
  183.  
  184. // Parse decimal point and digits after
  185. if let Some(&c) = chars.peek() {
  186. if c == '.' {
  187. num_str.push(c);
  188. chars.next();
  189.  
  190. let mut has_decimal_digits = false;
  191.  
  192. while let Some(&c) = chars.peek() {
  193. if c.is_digit(10) {
  194. has_decimal_digits = true;
  195. num_str.push(c);
  196. chars.next();
  197. } else {
  198. break;
  199. }
  200. }
  201.  
  202. if !has_decimal_digits {
  203. return Err("Expected digits after decimal point".to_string());
  204. }
  205. }
  206. }
  207.  
  208. // Convert to f64
  209. if num_str == "-" {
  210. Err("Invalid number format".to_string())
  211. } else {
  212. num_str.parse::<f64>().map_err(|_| "Invalid number format".to_string())
  213. }
  214. }
  215.  
  216. Sonnet 3.7, base:
  217.  
  218. Here's a ready-to-run CLI calculator in Rust:
  219.  
  220. ```rust
  221. use std::io::{self, Write};
  222.  
  223. fn main() {
  224. println!("Simple Calculator");
  225. println!("Enter expressions like '2 + 2' or type 'exit' to quit");
  226.  
  227. loop {
  228. print!("> ");
  229. io::stdout().flush().unwrap();
  230.  
  231. let mut input = String::new();
  232. io::stdin().read_line(&mut input).unwrap();
  233.  
  234. let input = input.trim();
  235. if input.eq_ignore_ascii_case("exit") {
  236. break;
  237. }
  238.  
  239. match evaluate_expression(input) {
  240. Ok(result) => println!("= {}", result),
  241. Err(err) => println!("Error: {}", err),
  242. }
  243. }
  244. }
  245.  
  246. fn evaluate_expression(expr: &str) -> Result<f64, String> {
  247. let parts: Vec<&str> = expr.split_whitespace().collect();
  248.  
  249. if parts.len() != 3 {
  250. return Err("Invalid format. Use: number operator number".to_string());
  251. }
  252.  
  253. let left: f64 = parts[0].parse().map_err(|_| "Invalid first number".to_string())?;
  254. let operator = parts[1];
  255. let right: f64 = parts[2].parse().map_err(|_| "Invalid second number".to_string())?;
  256.  
  257. match operator {
  258. "+" => Ok(left + right),
  259. "-" => Ok(left - right),
  260. "*" => Ok(left * right),
  261. "/" => {
  262. if right == 0.0 {
  263. Err("Division by zero".to_string())
  264. } else {
  265. Ok(left / right)
  266. }
  267. },
  268. "^" => Ok(left.powf(right)),
  269. "%" => Ok(left % right),
  270. _ => Err(format!("Unknown operator: {}", operator)),
  271. }
  272. }
  273. ```
  274.  
  275. o1-pro:
  276.  
  277. use std::io::{self, Write};
  278. use meval::eval_str;
  279.  
  280. fn main() {
  281. println!("Welcome to the CLI Calculator!");
  282. println!("Type an expression to evaluate or 'exit' to quit.\n");
  283.  
  284. loop {
  285. // Print a prompt
  286. print!("> ");
  287. // Flush stdout so prompt appears immediately
  288. io::stdout().flush().unwrap();
  289.  
  290. // Read input from the user
  291. let mut input = String::new();
  292. if io::stdin().read_line(&mut input).is_err() {
  293. eprintln!("Error reading input. Exiting.");
  294. break;
  295. }
  296.  
  297. // Remove trailing newline and surrounding whitespace
  298. let expression = input.trim();
  299.  
  300. // Exit if the user types "exit"
  301. if expression.eq_ignore_ascii_case("exit") {
  302. println!("Goodbye!");
  303. break;
  304. }
  305.  
  306. // Evaluate the expression using meval
  307. match eval_str(expression) {
  308. Ok(result) => println!("{}", result),
  309. Err(err) => eprintln!("Error: {}", err),
  310. }
  311. }
  312. }
  313.  
  314. GPT-4.5:
  315.  
  316. use std::io;
  317.  
  318. fn main() {
  319. println!("Simple CLI Calculator");
  320. println!("Available operations: +, -, *, /");
  321. println!("Enter 'exit' to quit\n");
  322.  
  323. loop {
  324. // Get first number
  325. let num1 = match get_number("Enter first number: ") {
  326. Some(n) => n,
  327. None => continue,
  328. };
  329.  
  330. // Get operation
  331. let operation = match get_operation() {
  332. Some(op) => op,
  333. None => continue,
  334. };
  335.  
  336. // Get second number
  337. let num2 = match get_number("Enter second number: ") {
  338. Some(n) => n,
  339. None => continue,
  340. };
  341.  
  342. // Calculate and display result
  343. match calculate(num1, operation, num2) {
  344. Ok(result) => println!("Result: {}\n", result),
  345. Err(e) => println!("Error: {}\n", e),
  346. }
  347. }
  348. }
  349.  
  350. fn get_number(prompt: &str) -> Option<f64> {
  351. println!("{}", prompt);
  352. let mut input = String::new();
  353.  
  354. io::stdin()
  355. .read_line(&mut input)
  356. .expect("Failed to read input");
  357.  
  358. let input = input.trim();
  359.  
  360. if input.to_lowercase() == "exit" {
  361. std::process::exit(0);
  362. }
  363.  
  364. match input.parse::<f64>() {
  365. Ok(num) => Some(num),
  366. Err(_) => {
  367. println!("Please enter a valid number!");
  368. None
  369. }
  370. }
  371. }
  372.  
  373. fn get_operation() -> Option<char> {
  374. println!("Enter operation (+, -, *, /): ");
  375. let mut input = String::new();
  376.  
  377. io::stdin()
  378. .read_line(&mut input)
  379. .expect("Failed to read input");
  380.  
  381. let input = input.trim();
  382.  
  383. if input.to_lowercase() == "exit" {
  384. std::process::exit(0);
  385. }
  386.  
  387. match input.chars().next() {
  388. Some(op) if "+-*/".contains(op) => Some(op),
  389. _ => {
  390. println!("Please enter a valid operation (+, -, *, /)!");
  391. None
  392. }
  393. }
  394. }
  395.  
  396. fn calculate(num1: f64, operation: char, num2: f64) -> Result<f64, String> {
  397. match operation {
  398. '+' => Ok(num1 + num2),
  399. '-' => Ok(num1 - num2),
  400. '*' => Ok(num1 * num2),
  401. '/' => {
  402. if num2 == 0.0 {
  403. Err("Division by zero is not allowed!".to_string())
  404. } else {
  405. Ok(num1 / num2)
  406. }
  407. }
  408. _ => Err("Invalid operation!".to_string()),
  409. }
  410. }
  411.  
  412. o3-mini-high:
  413.  
  414. use std::env;
  415. use std::process;
  416.  
  417. fn main() {
  418. // Collect command-line arguments.
  419. let args: Vec<String> = env::args().collect();
  420.  
  421. // Expect exactly three arguments: operand1, operator, operand2.
  422. if args.len() != 4 {
  423. eprintln!("Usage: {} <operand1> <operator> <operand2>", args[0]);
  424. process::exit(1);
  425. }
  426.  
  427. // Parse the operands.
  428. let operand1: f64 = match args[1].parse() {
  429. Ok(num) => num,
  430. Err(_) => {
  431. eprintln!("Error: '{}' is not a valid number.", args[1]);
  432. process::exit(1);
  433. }
  434. };
  435.  
  436. let operand2: f64 = match args[3].parse() {
  437. Ok(num) => num,
  438. Err(_) => {
  439. eprintln!("Error: '{}' is not a valid number.", args[3]);
  440. process::exit(1);
  441. }
  442. };
  443.  
  444. let operator = &args[2];
  445.  
  446. // Perform the calculation based on the operator.
  447. let result = match operator.as_str() {
  448. "+" => operand1 + operand2,
  449. "-" => operand1 - operand2,
  450. "*" => operand1 * operand2,
  451. "/" => {
  452. if operand2 == 0.0 {
  453. eprintln!("Error: Division by zero is not allowed.");
  454. process::exit(1);
  455. }
  456. operand1 / operand2
  457. },
  458. _ => {
  459. eprintln!("Error: Unknown operator '{}'. Supported operators are +, -, *, and /.", operator);
  460. process::exit(1);
  461. }
  462. };
  463.  
  464. println!("Result: {}", result);
  465. }
  466.  
  467. Grok-3 thinking:
  468. use std::io::{self, Write};
  469.  
  470. fn main() {
  471. // Display welcome message and instructions
  472. println!("Welcome to the Rust CLI Calculator!");
  473. println!("Enter expressions in the form: number operator number");
  474. println!("Supported operators: +, -, *, /");
  475. println!("Type 'quit' or 'exit' to exit.");
  476.  
  477. // Main loop for continuous operation
  478. loop {
  479. // Print prompt and ensure it displays immediately
  480. print!(">> ");
  481. io::stdout().flush().unwrap();
  482.  
  483. // Read user input
  484. let mut input = String::new();
  485. match io::stdin().read_line(&mut input) {
  486. Ok(_) => {
  487. // Trim whitespace from input
  488. let input = input.trim();
  489.  
  490. // Check for exit commands
  491. if input == "quit" || input == "exit" {
  492. break;
  493. }
  494.  
  495. // Parse the expression into parts
  496. let parts: Vec<&str> = input.split_whitespace().collect();
  497. if parts.len() != 3 {
  498. println!("Invalid expression format. Please enter in the form: number operator number");
  499. continue;
  500. }
  501.  
  502. // Parse the first operand
  503. let operand1: f64 = match parts[0].parse() {
  504. Ok(num) => num,
  505. Err(_) => {
  506. println!("Invalid number: {}", parts[0]);
  507. continue;
  508. }
  509. };
  510.  
  511. // Get the operator
  512. let operator = parts[1];
  513.  
  514. // Parse the second operand
  515. let operand2: f64 = match parts[2].parse() {
  516. Ok(num) => num,
  517. Err(_) => {
  518. println!("Invalid number: {}", parts[2]);
  519. continue;
  520. }
  521. };
  522.  
  523. // Perform the calculation based on the operator
  524. let result = match operator {
  525. "+" => operand1 + operand2,
  526. "-" => operand1 - operand2,
  527. "*" => operand1 * operand2,
  528. "/" => {
  529. if operand2 == 0.0 {
  530. println!("Error: Division by zero");
  531. continue;
  532. } else {
  533. operand1 / operand2
  534. }
  535. }
  536. _ => {
  537. println!("Invalid operator: {}", operator);
  538. continue;
  539. }
  540. };
  541.  
  542. // Display the result
  543. println!("{}", result);
  544. }
  545. Err(error) => {
  546. println!("Error reading input: {}", error);
  547. break;
  548. }
  549. }
  550. }
  551.  
  552. // Farewell message
  553. println!("Goodbye!");
  554. }
Advertisement
Add Comment
Please, Sign In to add comment