Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Can I Alter Postfix Notation to Prefix Notation with Small Tweak in C++

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 9
    Answer it

    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++.

 2 Answer(s)

  • Hello Sir, I was searching for the best blog commenting sites to comment a good reviews about an article and also include my site for this weblog, I got this article from Google. I really find it helpful. Thank you so much for sharing this article. Really a helpful article. Such lucid language and the structure of the post. Such taken-for-granted stuff often needs explanation. Nice info graphic. Best essay writing service
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: