Advertisement
Guest User

Pre-Calc

a guest
Oct 31st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.60 KB | None | 0 0
  1. use std::io;
  2.  
  3. // Program evaluates in the form (operator operand1 operand2)
  4. // Example: (+ 1 (* 2 3)) = 7
  5. // Can take positive and negative integers as arguments, or expressions as seen above
  6.  
  7. fn main() {
  8.     // Main program loop
  9.     loop {
  10.         let input = gather_exp();
  11.         if input == "QQ" {
  12.             break
  13.         }
  14.         else {
  15.             let answer = evaluate(&input);
  16.             println!("{}",answer);
  17.         }
  18.     }
  19. }
  20.  
  21. fn evaluate(exp: &String) -> f64 {
  22.     let numtest = exp.parse::<f64>().is_ok();
  23.     if numtest == true {
  24.         return exp.parse::<f64>().unwrap();
  25.     }
  26.     else {
  27.         // gets the first operand
  28.         let parse1 = find_operand(&exp, 0).unwrap();
  29.         let operand1 = parse1.0;
  30.         let next_ind = parse1.1;
  31.         // gets the second operand
  32.         let parse2 = find_operand(&exp, next_ind).unwrap();
  33.         let operand2 = parse2.0;
  34.         let operator = find_operator(&exp);
  35.         // recursive case to evaluate expressions
  36.         match operator {
  37.             Operator::Addition => evaluate(&operand1) + evaluate(&operand2),
  38.             Operator::Subtraction => evaluate(&operand1) - evaluate(&operand2),
  39.             Operator::Multiplication => evaluate(&operand1) * evaluate(&operand2),
  40.             Operator::Division => evaluate(&operand1) / evaluate(&operand2),
  41.             Operator::Null => panic!("Syntax Error!"),
  42.         }
  43.     }
  44. }
  45.  
  46. fn gather_exp() -> String {
  47.     // Funciton takes in input from std::io and returns a String
  48.     // representing the expression
  49.  
  50.     let mut input = String::new();
  51.     io::stdin()
  52.         .read_line(&mut input)
  53.         .expect("Fatal Error: Failed to read expression!");
  54.     return input.trim().to_owned();
  55. }
  56.  
  57. fn find_operator(exp: &String) -> Operator {
  58.     // Function takes in an expression and returns an enum
  59.     // representing the operator type
  60.  
  61.     let op_char = exp.chars().nth(1).unwrap_or('0');
  62.     match op_char {
  63.         '+' => Operator::Addition,
  64.         '-' => Operator::Subtraction,
  65.         '*' => Operator::Multiplication,
  66.         '/' => Operator::Division,
  67.         _ => Operator::Null,
  68.     }
  69. }
  70.  
  71.  
  72. fn find_operand(input: &String, index: usize) -> Option<(String, usize)> {
  73.     let mut exp = input.clone();
  74.     let len = exp.len();
  75.     exp = exp[1..(len - 1)].to_string();
  76.     for i in index..len {
  77.         // Checks for negative numbers
  78.         if (exp.chars().nth(i).unwrap() == '-') && (exp.chars().nth(i + 1).unwrap().is_digit(10)) {
  79.             for digit_ind in i + 1..exp.len() {
  80.                 if exp.chars().nth(digit_ind).unwrap().is_digit(10) && (digit_ind == exp.len()-1) {
  81.                     let num = &exp[i..=digit_ind];
  82.                     return Some((num.to_string(), digit_ind));
  83.                 }
  84.                 else if exp.chars().nth(digit_ind).unwrap().is_digit(10) {
  85.                     continue;
  86.                 }
  87.                 else {
  88.                     let num = &exp[i..digit_ind];
  89.                     return Some((num.to_string(), digit_ind));
  90.                 }
  91.             }
  92.         }
  93.         // Checks for positive numbers here
  94.         if exp.chars().nth(i).unwrap().is_digit(10) {
  95.             for digit_ind in i..exp.len() {
  96.                 if exp.chars().nth(digit_ind).unwrap().is_digit(10) && (digit_ind == exp.len() - 1)
  97.                 {
  98.                     let num = &exp[i..=digit_ind];
  99.                     return Some((num.to_string(), digit_ind));
  100.                 } else if exp.chars().nth(digit_ind).unwrap().is_digit(10) {
  101.                     continue;
  102.                 } else {
  103.                     let num = &exp[i..digit_ind];
  104.                     return Some((num.to_string(), digit_ind));
  105.                 }
  106.             }
  107.         }
  108.         // Checks for expressions (things in parenthesis) here
  109.         if exp.chars().nth(i).unwrap() == '(' {
  110.             let mut open: u32 = 0;
  111.             for exp_ind in i+1..exp.len() {
  112.                 if exp.chars().nth(exp_ind).unwrap() == '(' {
  113.                     open = open + 1;
  114.                     continue;
  115.                 }
  116.                 if exp.chars().nth(exp_ind).unwrap() == ')' && (open > 0) {
  117.                     open = open - 1;
  118.                     continue;
  119.                 }
  120.                 if exp.chars().nth(exp_ind).unwrap() == ')' && (open == 0) {
  121.                     let expression = &exp[i..=exp_ind];
  122.                     return Some((expression.to_string(), exp_ind));
  123.                 }
  124.  
  125.             }
  126.         }
  127.     }
  128.     None
  129. }
  130.  
  131. #[derive(PartialEq)]
  132. enum Operator {
  133.     Addition,
  134.     Subtraction,
  135.     Multiplication,
  136.     Division,
  137.     Null,
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement