[Algorithm] Calculate the value of expression
Inverse method Balan or expression trees will make you confused beginner.
This is an extremely simple method you can use is to read and understand
Simple expressions include 2 operations + and *
CEO: 2*5+3+4*3+2
Calculation:
If the expression has allowed + then take the first expression + Last part of the expression
Conversely, if the expression may allow * then take the first expression * end expression
Conversely expression = the number itself
Illustrations: “2*5+3+4*3+2” = “2*5″+”3+4*3+2” = 2 * 5 3 ”4*3+2″= 2 * 5 3 ”4*3″+2 = 2*5+3+4*3+2
Expressions include operations +, -, *
Before we add to calculate expression Forums + before each sign – (if there is evidence – then head to the original)
Calculated as normal without permission –
Expressions include operations +, -, *, /
More + before –
More * before /
While convert string to number if there is an error, remove the first character of the string -> continue to change string to number and then taking the inverse. Proceed as normal calculation expression only + and *
If you use this algorithm in the article you, please specify the name of the author of the algorithm qvluom
Code illustrated with 4 operations +, -, *, /
#include <sstream> #include <iostream> #include <cstdlib> #include <cstdio> #include <cctype> #include <string> using namespace std; int check_to_number(string s) //kiem tra la so { int check = 1, l = s.length(); for (int i = 1; i<l; i++) if (!isdigit(s[i]) && s[i] != '.') //duyt tu phan tu thu 2neu khong la so hoac thi check = 0 { check = 0; break; } if (s[0] == '-' && check == 1) return 2; //kiem tra dau xau co la - if (s[0] == '/' && check == 1) return 3; //kiem tra dau xau co la / if (isdigit(s[0]) && check == 1) return 1; //dau xau la so } float convert_to_number(string s) { float number, check = check_to_number(s); if (check > 0) { if (check == 1) //neu la so binhf thuong { stringstream ss (s); ss >> number; } if (check > 1) { stringstream ss (s.substr(1,s.length()-1)); ss >> number; if (check == 2) number = 0 - number; //neu la phep tru if (check == 3) number = 1/number; // neu la phep chia } return number; } } float process(string s, int l, int r) { int li = 0, ri = r; if (check_to_number(s)) return convert_to_number(s); //{ cout <<" "<<convert_to_number(s)<<endl; return convert_to_number(s);} else { int i; for (i=li; i<ri; i++) if (s[i] == '+') return process(s.substr(li, i-li), li, i) + process(s.substr(i+1, ri-i), i+1, ri-i); for (i=li; i<ri; i++) if (s[i] == '*') return process(s.substr(li, i-li), li, i) * process(s.substr(i+1, ri-i), i+1, ri-i); } return 0; } int main() { float number; string s; getline(cin,s); for (int i=0; i<s.length(); i++) { if(s[i] == '-') { s.insert(i,"+"); i++; } if(s[i] == '/') { s.insert(i,"*"); i++; } } cout<<s<<endl; number = process(s,0,s.length()); cout<<number<<endl; return 0; }
This algorithm by qvluom (Quach Van Lượm) proposed circa 2002 and is widely published with strange name “Quach Nguyen”. The correct name is “Quach Nguyen”. This is the algorithm design is quite short and easy to understand, worked very well for a school function parameters. Illustration Code Pascal.
This Code calculate the value of the function Ln; operations +, -, *, /. The addition of the sine, with, so, … is extremely simple.
Exactly so, thank you for comment.
Your c code with little problem, when you enter the expression (20+9)*(18+2)-1234/5, result is 333.2 but your program out 246.8, you review your help with, Your division seems to have his problems
AH, thank you. It is true that your expression will be sorry for your expressions containing the signs ( and ). Your program can not be calculated with the expression such that only the expression 4 operations +, -, *, / whiff.
you prize your household sentence 24 + 25 + 26 + 27 +…+122+123