I'm writing a simple calculator program. At first I wrote program that converts infix to postfix notation and I thought it would be simple thing to evaluate exression. This calculator is working with integers numbers only for the start. However I have a bug, for example -2*4-5 will not work because of the way of string processing I've chosen. That charatcer '-' is converted to some number. I would like you to aks for help, to give me advice how to change string processing, or maybe to start writing program from the scratch. I omitted error checking for simplicity.
Can you suggest me the way to fix this without large program modifications? It seems that this idea is not very useful. how would you approach to this problem?
Thank you very much
- MickoCode:#include <iostream> #include <string> #include <algorithm> #include <cctype> #include <stack> #include <cstdlib> #include <cmath> using namespace std; inline bool is_whitespace (const char&); inline bool is_operator (const char&); inline bool is_operand (const char&); inline bool higher_priority (const char&, const char&); inline bool same_priority (const char&, const char&); int get_priority (const char&); string remove_whtsp (string&);//remove whitespace characters string convert_to_postfix (string&); long postfix_eval (string&); int main() { string a; getline (cin, a); string b = remove_whtsp (a); string c = convert_to_postfix (b); cout << (c)<<endl; cout << postfix_eval(c); return 0; } bool is_whitespace(const char& ch) { return (isspace(ch) != 0); } bool is_operator (const char& ch) { return (ch == '+' || ch == '*' || ch == '/' || ch == '-' || ch == '^'); } bool is_operand (const char& ch) { return (isdigit(ch) != 0 || isalpha(ch) != 0); } bool higher_priority (const char& ch, const char& top) { return get_priority(ch) > get_priority(top); } inline bool same_priority (const char& ch, const char& top) { return get_priority (ch) == get_priority (top); } int get_priority (const char& ch) { if (ch == '^') return 3; else if (ch == '*' || ch == '/') return 2; else if (ch == '+' || ch == '-') return 1; else return 0; } string remove_whtsp (string& a) { string b; remove_copy_if(a.begin(), a.end(), back_inserter(b), is_whitespace); return b; } string convert_to_postfix (string& str_infix) { stack <char> st; string str_postfix; string :: const_iterator it; it = str_infix.begin(); //step1. Examine the next element in the input. while (it != str_infix.end()) { if (is_operand(*it)) { str_postfix.push_back(*it);//step 2. If it is an operand, output it. } else if (*it == '(') { st.push(*it);//step 3. If it is opening parenthesis, push it on stack. } else if (is_operator(*it))//step4. If it is an operator, then { if (st.empty()) { st.push(*it); } else if (same_priority(st.top(), *it)) { str_postfix.push_back(st.top()); st.pop(); st.push(*it); } else if (higher_priority(*it, st.top())) { st.push(*it); } else { while (!higher_priority(*it, st.top())) { str_postfix.push_back(st.top()); st.pop(); } st.push(*it); } }//end step 4. if (*it == ')')//step 5.If it is a closing parenthesis, //pop operators from the stack and output them until an opening parenthesis is encountered { while (st.top() != '(') { str_postfix.push_back(st.top()); st.pop(); } st.pop(); } ++it; } while (!st.empty())//step7. If there is no more input, unstack the remaining operators to output. { str_postfix.push_back (st.top()); st.pop(); } return str_postfix; } long postfix_eval(string& str) { string tmp; string :: const_iterator it; stack <long> st; long temp1, temp2; for (it = str.begin(); it != str.end(); ++it) { if (is_operand(*it)) { tmp = *it; st.push(atol(tmp.c_str())); tmp = ""; } else if (is_operator(*it)) { temp1 = st.top(); st.pop(); temp2 = st.top(); st.pop(); if (*it == '+') { st.push(temp1 + temp2); } else if(*it == '-') { st.push(temp2 - temp1); } else if(*it == '*') { st.push(temp1 * temp2); } else if(*it == '/') { st.push(temp2 / temp1); } else if (*it == '^') { st.push(static_cast<long>(pow(temp2, temp1)));//I had to do casting } } } temp1 = st.top(); st.pop(); return temp1; }



LinkBack URL
About LinkBacks


