diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index bff6201..71251e2 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,5 +1,4 @@ | |||
1 | use std::io::{ stdin, stdout }; | 1 | use std::f64; |
2 | use std::io::{self, Write}; | ||
3 | 2 | ||
4 | #[derive(Debug, Copy, Clone, PartialEq)] | 3 | #[derive(Debug, Copy, Clone, PartialEq)] |
5 | pub struct Operator { | 4 | pub struct Operator { |
@@ -111,14 +110,15 @@ fn lexer(input: &str) -> Result<Vec<Token>, String> { | |||
111 | if char_vec.len() > 0 { | 110 | if char_vec.len() > 0 { |
112 | let funct = char_vec.clone(); | 111 | let funct = char_vec.clone(); |
113 | match &funct[..] { | 112 | match &funct[..] { |
114 | "sin" | "sine" => result.push(Function::token_from_fn("sin".into(), |x| x.sin())), | 113 | "sin" | "sine" => result.push(Function::token_from_fn("sin".into(), |x| x.to_radians().sin())), |
115 | "cos" | "cosine" => result.push(Function::token_from_fn("cos".into(), |x| x.cos())), | 114 | "cos" | "cosine" => result.push(Function::token_from_fn("cos".into(), |x| x.to_radians().cos())), |
116 | "tan" | "tangent" => result.push(Function::token_from_fn("tan".into(), |x| x.tan())), | 115 | "tan" | "tangent" => result.push(Function::token_from_fn("tan".into(), |x| x.to_radians().tan())), |
117 | "csc" | "cosec" => result.push(Function::token_from_fn("csc".into(), |x| 1f64 / x.sin())), | 116 | "csc" | "cosec" => result.push(Function::token_from_fn("csc".into(), |x| 1f64 / x.to_radians().sin())), |
118 | "sec" | "secant" => result.push(Function::token_from_fn("sec".into(), |x| 1f64 / x.cos())), | 117 | "sec" | "secant" => result.push(Function::token_from_fn("sec".into(), |x| 1f64 / x.to_radians().cos())), |
119 | "cot" | "cotangent" => result.push(Function::token_from_fn("cot".into(), |x| 1f64 / x.tan())), | 118 | "cot" | "cotangent" => result.push(Function::token_from_fn("cot".into(), |x| 1f64 / x.to_radians().tan())), |
120 | "ln" => result.push(Function::token_from_fn("ln".into(), |x| x.ln())), | 119 | "ln" => result.push(Function::token_from_fn("ln".into(), |x| x.ln())), |
121 | _ => {} | 120 | "log" => result.push(Function::token_from_fn("log".into(), |x| x.log10())), |
121 | _ => {} | ||
122 | } | 122 | } |
123 | char_vec.clear(); | 123 | char_vec.clear(); |
124 | } else { | 124 | } else { |
@@ -202,15 +202,15 @@ fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> { | |||
202 | op_stack.push(token); | 202 | op_stack.push(token); |
203 | }, | 203 | }, |
204 | Token::RParen => { | 204 | Token::RParen => { |
205 | let mut push_until: bool = false; | 205 | let mut push_until_paren: bool = false; |
206 | while let Some(token) = op_stack.pop() { | 206 | while let Some(token) = op_stack.pop() { |
207 | if token == Token::LParen { | 207 | if token == Token::LParen { |
208 | push_until = true; | 208 | push_until_paren = true; |
209 | break; | 209 | break; |
210 | } | 210 | } |
211 | postfixed.push(token) | 211 | postfixed.push(token) |
212 | } | 212 | } |
213 | if !push_until { | 213 | if !push_until_paren { |
214 | return Err(String::from("Mismatched ')'")); | 214 | return Err(String::from("Mismatched ')'")); |
215 | } | 215 | } |
216 | } | 216 | } |
@@ -220,7 +220,6 @@ fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> { | |||
220 | while let Some(op) = op_stack.pop() { | 220 | while let Some(op) = op_stack.pop() { |
221 | postfixed.push(op); | 221 | postfixed.push(op); |
222 | } | 222 | } |
223 | println!("{:?}", postfixed); | ||
224 | Ok(postfixed) | 223 | Ok(postfixed) |
225 | } | 224 | } |
226 | 225 | ||