c++ - Trying to write a program which analyzes a mathimatical equation, convert to postfix notation and then solves it? -
so first half works fine , great job of converting equation postfix notation after that, command prompt sits there blinking. want convert postfix , solve equation.can on i'm doing wrong? thanks!
#include <iostream> #include <stack> #include <string> #include <sstream> using namespace std; int priority(string item) //prioritizing operators { if(item == "(" || item == ")") { return 0; } else if(item == "+" || item == "-") { return 1; } else { return 2; } } int main() { stack <string> mystack; // initializing stack. stack <int> mynewstack; //used part 2 char line[256]; char line2[256]; cin.getline( line, 256); // , proceeding line input. string exp = line; string item; string postitem; //used part 2 string postfix; //used part 2 istringstream iss(exp); iss >> item; while(iss) { if(item != "+" && item != "-" && item != "/" && item != "*" && item != "(" && item != ")") //if char number { cout << item; postfix = postfix + item; } else if(mystack.size() == 0) // if stack empty { mystack.push(item); } else if( item == "+" || item == "-" || item == "/" || item == "*") //if char operator { if(priority(mystack.top()) < priority(item)) // item on stack greater priority array item { mystack.push(item); } else { while(mystack.size() > 0 && priority(mystack.top()) >= priority(item)) //while stack contains something, , item on { cout << mystack.top(); postfix = postfix + mystack.top(); mystack.pop(); } mystack.push(item); } } else if(item == "(") // left peren { mystack.push(item); } else if(item == ")") // right peren { while(mystack.top() != "(") { cout << mystack.top(); postfix = postfix + mystack.top(); mystack.pop(); } mystack.pop(); } iss >> item; } while (mystack.size() > 0 ) //when nothing left evaluate { cout << mystack.top(); postfix = postfix + mystack.top(); mystack.pop(); } //part 2 int x1; int x2; int x3; istringstream iss2(postfix); iss2 >> postitem; while(iss2) { if(postitem != "+" && postitem != "-" && postitem != "/" && postitem != "*") //if number { int n; n = atoi(postitem.c_str()); mynewstack.push(n); } else if( postitem == "+" || postitem == "-" || postitem == "/" || postitem == "*") //if operator { if(postitem == "+") { x1 = mynewstack.top(); mynewstack.pop(); x2 = mynewstack.top(); mynewstack.pop(); x3 = x1 + x2; mynewstack.push(x3); } else if(postitem == "-") { x1 = mynewstack.top(); mynewstack.pop(); x2 = mynewstack.top(); mynewstack.pop(); x3 = x1 - x2; mynewstack.push(x3); } else if(postitem == "/") { x1 = mynewstack.top(); mynewstack.pop(); x2 = mynewstack.top(); mynewstack.pop(); x3 = x1 * x2; mynewstack.push(x3); } else if(postitem == "*") { x1 = mynewstack.top(); mynewstack.pop(); x2 = mynewstack.top(); mynewstack.pop(); x3 = x1 / x2; mynewstack.push(x3); } iss2 >> postitem; } } cout << "the conversion infix notation is" + mynewstack.top() << endl; }
go link, click "code related lecture" on bottom , show examples. cant write code here, long. http://sci.notbc.org/~weiss/cisc3130/lectures/04-3-applications-expressionevaluation.html
Comments
Post a Comment