Thread: C++ Programming - stack based calculator program

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Unhappy C++ Programming - stack based calculator program

    Can anyone see what I've done incorrectly with this program. You should be able to enter a value, press return, enter then next value, press return, enter an operator, press return and you'd get the answer, then you can just continue but would need to Ctrl+Z to exit.

    [code]
    #include <list>
    #include <stack>
    #include <string>
    #include <iostream>

    using namespace std;

    void main (void)
    {
    cout << "enter value or operand" << endl;
    stack <string> stk;
    const int BUFLAN = 80;
    char szbuf[BUFLEN+2] = {0};

    for (;;)
    {
    string s;
    cin >> s;
    if (s== "+")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) + atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "-")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) - atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "*")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) * atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "/ ")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) / atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else
    {
    stk.push(s);
    }
    }
    }
    [/code/
    Last edited by tinkerbelle; 10-08-2003 at 04:28 PM.
    tinkerbelle

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: C++ Programming - stack based calculator program

    Originally posted by tinkerbelle
    [/code/
    can you fix that up?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Errors in your code:
    => Declared as BUFLAN and using as BUFLEN
    => Use sprintf, not sprint


    Better use int main() and do a return 0 at the end, instead of void main

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Still need help with this

    I corrected my typos, sorry, but now I'm getting a linking error LNK2001, _WinMain@16: Unresolved External Symbol. I looked it up, they say to go into the Output category of the Link field in the Project Setting dialog box, set the Entry Point symbol to wWinMainCRTStartup. I did this and it still gives me the same linking error....anybody seen this before?

    C++ Programming - stack based calculator program
    Can anyone see what I've done incorrectly with this program. You should be able to enter a value, press return, enter then next value, press return, enter an operator, press return and you'd get the answer, then you can just continue but would need to Ctrl+Z to exit.

    Code:
    #include <list>
    #include <stack>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    void main (void)
    {
    cout << "enter value or operand" << endl;
    stack <string> stk;
    const int BUFLEN = 80;
    char szbuf[BUFLEN+2] = {0};
    
    for (;;)
    {
    string s;
    cin >> s;
    if (s== "+")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) + atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "-")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) - atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "*")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) * atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "/ ")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) / atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else
    {
    stk.push(s);
    }
    }
    }
    tinkerbelle

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I got bored. If you want to wade through all of this fine. If not, so be it.
    [code]
    #include <list>
    #include <stack>
    #include <string>
    #include <iostream>
    //#include <cstdio> to get sprintf

    using namespace std;

    void main (void)
    //use int main() instead of void main()
    {
    cout << "enter value or operand" << endl;
    //place the cout line after declaring string s in for loop below
    stack <string> stk;
    const int BUFLEN = 80;
    char szbuf[BUFLEN+2] = {0};

    for (;
    {
    //all math operator you use are binary operator
    //declare op1 and op2 here instead of in each operator
    string s;
    cin >> s;

    //scrap everything below. It's not the syntax for a stack
    //based calculator. See comments outside of code.
    if (s== "+")
    {
    string op1 = stk.top();
    //stk is still empty, nothing has been pushed on it yet
    //therefore stk.top() is undefined
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) + atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "-")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) - atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "*")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) * atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else if (s== "/ ")
    {
    string op1 = stk.top();
    stk.pop();
    string op2 = stk.top();
    stk.pop();
    double result = atof(op1.c_str()) / atof(op2.c_str());
    sprint (szbuf, "%f", result);
    s=szbuf;
    stk.push(s);
    cout << "result: " << s << endl;
    }
    else
    {
    stk.push(s);
    }
    }
    }
    [/cout]


    simple stack based operators usually have user push op1 on stack then push op2 on stack then enter operator. When operator is identified it goes back and pops op1 to become lhs operand and op2 to become rhs operand.

    Code:
    request op1
    push op1 on stack
    request op2
    push op2 on stack
    request operator
    pop stack and store result as rhs
    pop stack and store result as lhs
    use switch based on char (not string) representing operator if you want, rather than if/else with string comparison.
    switch(operator)
    {
      case '+':
        result = lhs + rhs;
        break;
      case '-':
        result = lhs - rhs;
        break;
    //etc.
    }
    display result.
    for simple operations, probably not much advantage over non-stack based calculators. But trying to do this with a non-stack based calculator is very difficult. With a stack based calculator (and a few other tricks) it ain't a puff ball, but it ain't impossible either.

    result = (5 * (4 - 1)/(7+ 8))*3;

    post fix notation is the other trick to allow most efficient use of a stack based calculator to solve complex equations like above entered as a single string of input.

    Good luck with your assignment.

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Still need help with this

    Originally posted by tinkerbelle
    I corrected my typos, sorry, but now I'm getting a linking error LNK2001, _WinMain@16: Unresolved External Symbol.
    You need to change the target from a Win32 GUI app to a Win32 console application.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    You're right

    Thanks, when I restarted the program, you know copy and pasted it back it. I was able to get rid of that error.

    Now I still have a bug in the program, do I need to do something else with the / (divide) and the - (subtraction) operands because when I run it and use the / for division is just hangs, then when I use the - (for subtraction) it gives me a negative number...
    i.e.

    20
    20
    +
    result: 40.000000

    10
    -
    result: -30.000000

    This is the first time I've run into this and then when I attempt to use the / (division) it just hangs. Suggestions please.
    tinkerbelle

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Your subtraction is subtracting the first number from the second number: 10 - 40 = -30, instead of the second number from the first number: 40 - 10 = 30.

    Not sure about the problem with divide, but I think the operands are switched for that, too.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I think you should use a stack of numbers instead of a stack of strings.
    That would be faster and it would make more sense, since the only thing you store on the stack is numbers.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Thanks everyone

    I figured it out, of course, with all your help. Thanks much.
    tinkerbelle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM