www.webdeveloper.com
Results 1 to 3 of 3

Thread: c++ rpn expression code error

  1. #1
    Join Date
    Nov 2003
    Posts
    3

    c++ rpn expression code error

    this program calculate rpn expression
    of the form 42 3 +
    and print the result
    i want toknow what errors in code
    and if any body have any ideas
    #include<string.h>
    #include<stdlib.h>
    #include<iostream.h>
    #include<ctype.h>
    #include<conio.h>
    #include "stack.h"

    stack s;

    class RPNexp
    {
    public:
    string str;
    bool isnumb_str(const char*t);
    string calcRPN(str);
    };

    bool RPNexp::isnumb_str(const char*t)
    {
    bool isnumb=false;
    while(*t)
    {
    //for(int i=o;i<str.length();i++)
    isnumb=isdigit(*t);
    if(!isnumb)
    break;
    ++t;
    }
    return isnumb;
    }
    string RPNexp::calcRPN(str)
    {
    char tmp1,tmp2,value;
    /*check to see if it was a number*/
    for(int i=0;i<str.length();i++)
    {
    token=str[i];
    while(token!='$')
    {
    bool isnumb=isnumb_str(token);

    /*
    *if it's a number push it to a stack
    *if it's an operator then calculate both values
    */
    if(isnumb)
    {
    s.push(atoi(token));//convert string to integer
    }
    else
    switch (token)
    {
    case'+':
    value=s.pop()+s.pop();
    s.push(value);
    break;
    case'-':
    tmp2=s.pop();
    tmp1=s.pop();
    value=v1-v2;
    s.push(value);
    case'*':
    value=s.pop()*s.pop();
    case'/':
    {
    tmp2=s.pop();
    tmp1=s.pop();
    if(tmp1==0)
    {
    cout<<"error divide by zero";
    error=true;
    }
    else
    {
    value=mp2/tmp1;
    s.push(value);
    }
    }//case'/'
    break;
    default:
    cout<<"error unknown."<<endl;
    }//switch
    }
    }
    return value;
    }


    int main()
    {
    RPNexp r;
    cout<<"\nEnter RPN expression to calculate it.";
    cout<<"\n of the form 2 4 7 + *.";
    cout<<"\n dont't use parantheses.";
    cout<<"\n you must separate operators and operands by white space.";
    cout<<"\n end the expression with'$'.\n";
    cout<<"\n enter expression.";
    cin.getline(r.str,'$');
    cout<<"The total result of the expression is: "<<r.calcRPN(str)<<endl;
    getch();
    return 0;
    }

  2. #2
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998
    You're WAY off in your choice of forum. You should try posting this in the "Other" forum area.

  3. #3
    Join Date
    Nov 2003
    Posts
    655
    *sniff* Do I smell homework? If so, either your instructor is assigning overly complicated problems in the process of teaching OO in C++ or you really should have paid (a lot) more attention in your previous courses (the more likely scenario, based on my observations as both a student and an instructor).

    If nothing else, try compiling it and you'll get at least one message telling you what is wrong.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles