Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Prompt: Make a decent, ready to run CLI calculator in Rust.
- Sonnet 3.7 (thinking):
- use std::io::{self, Write};
- fn main() {
- println!("Simple Rust Calculator");
- println!("Enter mathematical expressions to calculate (e.g. 2 + 3 * 4)");
- println!("Supported operations: +, -, *, / and parentheses");
- println!("Type 'exit' to quit");
- loop {
- print!("> ");
- io::stdout().flush().unwrap();
- let mut input = String::new();
- io::stdin().read_line(&mut input).expect("Failed to read input");
- let input = input.trim();
- if input.eq_ignore_ascii_case("exit") {
- break;
- }
- if input.is_empty() {
- continue;
- }
- match evaluate_expression(input) {
- Ok(result) => println!("= {}", result),
- Err(err) => println!("Error: {}", err),
- }
- }
- }
- // Function to evaluate a mathematical expression
- fn evaluate_expression(expr: &str) -> Result<f64, String> {
- let mut chars = expr.chars().peekable();
- let result = parse_expression(&mut chars)?;
- // Check if there's any unexpected input left
- while let Some(&c) = chars.peek() {
- if c.is_whitespace() {
- chars.next();
- } else {
- return Err(format!("Unexpected character at end: {}", c));
- }
- }
- Ok(result)
- }
- // Parse an expression (addition and subtraction)
- fn parse_expression<I>(chars: &mut std::iter::Peekable<I>) -> Result<f64, String>
- where
- I: Iterator<Item = char>,
- {
- let mut left = parse_term(chars)?;
- while let Some(&c) = chars.peek() {
- if c == '+' || c == '-' {
- let op = c;
- chars.next(); // Consume the operator
- let right = parse_term(chars)?;
- match op {
- '+' => left += right,
- '-' => left -= right,
- _ => unreachable!(),
- }
- } else {
- break;
- }
- }
- Ok(left)
- }
- // Parse a term (multiplication and division)
- fn parse_term<I>(chars: &mut std::iter::Peekable<I>) -> Result<f64, String>
- where
- I: Iterator<Item = char>,
- {
- let mut left = parse_factor(chars)?;
- while let Some(&c) = chars.peek() {
- if c == '*' || c == '/' {
- let op = c;
- chars.next(); // Consume the operator
- let right = parse_factor(chars)?;
- match op {
- '*' => left *= right,
- '/' => {
- if right == 0.0 {
- return Err("Division by zero".to_string());
- }
- left /= right;
- },
- _ => unreachable!(),
- }
- } else {
- break;
- }
- }
- Ok(left)
- }
- // Parse a factor (number or parenthesized expression)
- fn parse_factor<I>(chars: &mut std::iter::Peekable<I>) -> Result<f64, String>
- where
- I: Iterator<Item = char>,
- {
- // Skip whitespace
- while let Some(&c) = chars.peek() {
- if c.is_whitespace() {
- chars.next();
- } else {
- break;
- }
- }
- if let Some(&c) = chars.peek() {
- if c == '(' {
- chars.next(); // Consume '('
- let result = parse_expression(chars)?;
- // Skip whitespace
- while let Some(&c) = chars.peek() {
- if c.is_whitespace() {
- chars.next();
- } else {
- break;
- }
- }
- // Expect closing parenthesis
- if chars.next() != Some(')') {
- return Err("Expected closing parenthesis".to_string());
- }
- Ok(result)
- } else if c.is_digit(10) || c == '.' || c == '-' {
- parse_number(chars)
- } else {
- Err(format!("Unexpected character: {}", c))
- }
- } else {
- Err("Unexpected end of input".to_string())
- }
- }
- // Parse a number (integer or decimal)
- fn parse_number<I>(chars: &mut std::iter::Peekable<I>) -> Result<f64, String>
- where
- I: Iterator<Item = char>,
- {
- let mut num_str = String::new();
- // Handle negative numbers
- if let Some(&c) = chars.peek() {
- if c == '-' {
- num_str.push(c);
- chars.next();
- }
- }
- // Parse digits before decimal
- let mut has_digits = false;
- while let Some(&c) = chars.peek() {
- if c.is_digit(10) {
- has_digits = true;
- num_str.push(c);
- chars.next();
- } else {
- break;
- }
- }
- // Parse decimal point and digits after
- if let Some(&c) = chars.peek() {
- if c == '.' {
- num_str.push(c);
- chars.next();
- let mut has_decimal_digits = false;
- while let Some(&c) = chars.peek() {
- if c.is_digit(10) {
- has_decimal_digits = true;
- num_str.push(c);
- chars.next();
- } else {
- break;
- }
- }
- if !has_decimal_digits {
- return Err("Expected digits after decimal point".to_string());
- }
- }
- }
- // Convert to f64
- if num_str == "-" {
- Err("Invalid number format".to_string())
- } else {
- num_str.parse::<f64>().map_err(|_| "Invalid number format".to_string())
- }
- }
- Sonnet 3.7, base:
- Here's a ready-to-run CLI calculator in Rust:
- ```rust
- use std::io::{self, Write};
- fn main() {
- println!("Simple Calculator");
- println!("Enter expressions like '2 + 2' or type 'exit' to quit");
- loop {
- print!("> ");
- io::stdout().flush().unwrap();
- let mut input = String::new();
- io::stdin().read_line(&mut input).unwrap();
- let input = input.trim();
- if input.eq_ignore_ascii_case("exit") {
- break;
- }
- match evaluate_expression(input) {
- Ok(result) => println!("= {}", result),
- Err(err) => println!("Error: {}", err),
- }
- }
- }
- fn evaluate_expression(expr: &str) -> Result<f64, String> {
- let parts: Vec<&str> = expr.split_whitespace().collect();
- if parts.len() != 3 {
- return Err("Invalid format. Use: number operator number".to_string());
- }
- let left: f64 = parts[0].parse().map_err(|_| "Invalid first number".to_string())?;
- let operator = parts[1];
- let right: f64 = parts[2].parse().map_err(|_| "Invalid second number".to_string())?;
- match operator {
- "+" => Ok(left + right),
- "-" => Ok(left - right),
- "*" => Ok(left * right),
- "/" => {
- if right == 0.0 {
- Err("Division by zero".to_string())
- } else {
- Ok(left / right)
- }
- },
- "^" => Ok(left.powf(right)),
- "%" => Ok(left % right),
- _ => Err(format!("Unknown operator: {}", operator)),
- }
- }
- ```
- o1-pro:
- use std::io::{self, Write};
- use meval::eval_str;
- fn main() {
- println!("Welcome to the CLI Calculator!");
- println!("Type an expression to evaluate or 'exit' to quit.\n");
- loop {
- // Print a prompt
- print!("> ");
- // Flush stdout so prompt appears immediately
- io::stdout().flush().unwrap();
- // Read input from the user
- let mut input = String::new();
- if io::stdin().read_line(&mut input).is_err() {
- eprintln!("Error reading input. Exiting.");
- break;
- }
- // Remove trailing newline and surrounding whitespace
- let expression = input.trim();
- // Exit if the user types "exit"
- if expression.eq_ignore_ascii_case("exit") {
- println!("Goodbye!");
- break;
- }
- // Evaluate the expression using meval
- match eval_str(expression) {
- Ok(result) => println!("{}", result),
- Err(err) => eprintln!("Error: {}", err),
- }
- }
- }
- GPT-4.5:
- use std::io;
- fn main() {
- println!("Simple CLI Calculator");
- println!("Available operations: +, -, *, /");
- println!("Enter 'exit' to quit\n");
- loop {
- // Get first number
- let num1 = match get_number("Enter first number: ") {
- Some(n) => n,
- None => continue,
- };
- // Get operation
- let operation = match get_operation() {
- Some(op) => op,
- None => continue,
- };
- // Get second number
- let num2 = match get_number("Enter second number: ") {
- Some(n) => n,
- None => continue,
- };
- // Calculate and display result
- match calculate(num1, operation, num2) {
- Ok(result) => println!("Result: {}\n", result),
- Err(e) => println!("Error: {}\n", e),
- }
- }
- }
- fn get_number(prompt: &str) -> Option<f64> {
- println!("{}", prompt);
- let mut input = String::new();
- io::stdin()
- .read_line(&mut input)
- .expect("Failed to read input");
- let input = input.trim();
- if input.to_lowercase() == "exit" {
- std::process::exit(0);
- }
- match input.parse::<f64>() {
- Ok(num) => Some(num),
- Err(_) => {
- println!("Please enter a valid number!");
- None
- }
- }
- }
- fn get_operation() -> Option<char> {
- println!("Enter operation (+, -, *, /): ");
- let mut input = String::new();
- io::stdin()
- .read_line(&mut input)
- .expect("Failed to read input");
- let input = input.trim();
- if input.to_lowercase() == "exit" {
- std::process::exit(0);
- }
- match input.chars().next() {
- Some(op) if "+-*/".contains(op) => Some(op),
- _ => {
- println!("Please enter a valid operation (+, -, *, /)!");
- None
- }
- }
- }
- fn calculate(num1: f64, operation: char, num2: f64) -> Result<f64, String> {
- match operation {
- '+' => Ok(num1 + num2),
- '-' => Ok(num1 - num2),
- '*' => Ok(num1 * num2),
- '/' => {
- if num2 == 0.0 {
- Err("Division by zero is not allowed!".to_string())
- } else {
- Ok(num1 / num2)
- }
- }
- _ => Err("Invalid operation!".to_string()),
- }
- }
- o3-mini-high:
- use std::env;
- use std::process;
- fn main() {
- // Collect command-line arguments.
- let args: Vec<String> = env::args().collect();
- // Expect exactly three arguments: operand1, operator, operand2.
- if args.len() != 4 {
- eprintln!("Usage: {} <operand1> <operator> <operand2>", args[0]);
- process::exit(1);
- }
- // Parse the operands.
- let operand1: f64 = match args[1].parse() {
- Ok(num) => num,
- Err(_) => {
- eprintln!("Error: '{}' is not a valid number.", args[1]);
- process::exit(1);
- }
- };
- let operand2: f64 = match args[3].parse() {
- Ok(num) => num,
- Err(_) => {
- eprintln!("Error: '{}' is not a valid number.", args[3]);
- process::exit(1);
- }
- };
- let operator = &args[2];
- // Perform the calculation based on the operator.
- let result = match operator.as_str() {
- "+" => operand1 + operand2,
- "-" => operand1 - operand2,
- "*" => operand1 * operand2,
- "/" => {
- if operand2 == 0.0 {
- eprintln!("Error: Division by zero is not allowed.");
- process::exit(1);
- }
- operand1 / operand2
- },
- _ => {
- eprintln!("Error: Unknown operator '{}'. Supported operators are +, -, *, and /.", operator);
- process::exit(1);
- }
- };
- println!("Result: {}", result);
- }
- Grok-3 thinking:
- use std::io::{self, Write};
- fn main() {
- // Display welcome message and instructions
- println!("Welcome to the Rust CLI Calculator!");
- println!("Enter expressions in the form: number operator number");
- println!("Supported operators: +, -, *, /");
- println!("Type 'quit' or 'exit' to exit.");
- // Main loop for continuous operation
- loop {
- // Print prompt and ensure it displays immediately
- print!(">> ");
- io::stdout().flush().unwrap();
- // Read user input
- let mut input = String::new();
- match io::stdin().read_line(&mut input) {
- Ok(_) => {
- // Trim whitespace from input
- let input = input.trim();
- // Check for exit commands
- if input == "quit" || input == "exit" {
- break;
- }
- // Parse the expression into parts
- let parts: Vec<&str> = input.split_whitespace().collect();
- if parts.len() != 3 {
- println!("Invalid expression format. Please enter in the form: number operator number");
- continue;
- }
- // Parse the first operand
- let operand1: f64 = match parts[0].parse() {
- Ok(num) => num,
- Err(_) => {
- println!("Invalid number: {}", parts[0]);
- continue;
- }
- };
- // Get the operator
- let operator = parts[1];
- // Parse the second operand
- let operand2: f64 = match parts[2].parse() {
- Ok(num) => num,
- Err(_) => {
- println!("Invalid number: {}", parts[2]);
- continue;
- }
- };
- // Perform the calculation based on the operator
- let result = match operator {
- "+" => operand1 + operand2,
- "-" => operand1 - operand2,
- "*" => operand1 * operand2,
- "/" => {
- if operand2 == 0.0 {
- println!("Error: Division by zero");
- continue;
- } else {
- operand1 / operand2
- }
- }
- _ => {
- println!("Invalid operator: {}", operator);
- continue;
- }
- };
- // Display the result
- println!("{}", result);
- }
- Err(error) => {
- println!("Error reading input: {}", error);
- break;
- }
- }
- }
- // Farewell message
- println!("Goodbye!");
- }
Advertisement
Add Comment
Please, Sign In to add comment