about 5 years ago
Preferred language using C++//
Basically the expression below has been converted into a C++ postfix expression. Now i need to reckon the result.
I used deque somehow, instead of stack, because i tried using stack and it didn't seem to work. but there's bug.
7 8 9 + 1 7 - *
-102
Here's my code:
#include <fstreaM> #include <string> #include <deque>
using namespace std;
int main(){ ifstream inf("input.txt"); ofstream outf("output.txt");
string line; inf >> line;
deque <int> stack;
for(int i = 0; i < line.length(); i+=2){ // char ch = line.at(i); if(line.at(i) == '*'){ stack.push_back(stack.pop_back() * stack.pop_back()); }else if(line.at(i) == '+'){ stack.push_back(stack.pop_back() + stack.pop_back()); }else if(line.at(i) == '-'){ stack.push_back(-stack.pop_back() + stack.pop_back()); }else stack.push_back(line.at(i)- '0');
}
outf << stack.pop_back() << endl;
return 0; }
With the code above can i alter with a small tweak and have it change into a Prefix Notation.
I still prefer using the std::deque STL Library from C++.
Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).
Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.
Chrome Version Support
Are you sure, you want to delete this comment?
Sign up using
0 Answer(s)