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
    • 0
    • 0
    • 0
    • 0
    • 512
    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++.

 0 Answer(s)

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: