Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,100 people online right now. Registration is fast and FREE... Join Now!




Postfix Calculator

 
Reply to this topicStart new topic

Postfix Calculator

sagarcruise
20 Sep, 2006 - 03:12 AM
Post #1

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 4


My Contributions
here is my assignment can anyone help me wid dis,i m so clueless abt dis


I have used all methods,but still not workin someone plzz help me


After you have learned about stack, how stack can be used to convert an infix expression into a postfix expression, and how a postfix expression can be evaluated, you would like to check how much you really understand what you have just learned.

Requirement
Instead of buying a calculator, you now have an option to implement a program to perform simple arithmetic using +, -, *, and / in an infix expression. * and / have higher precedence over + and -, and two arithmetic operators of the same precedence will be evaluated from left to right. You are required to implement a table (or using other data structure that is efficient for this application) to represent the precedence of the 4 arithmetic operators supported. Parentheses can be used to specify the order of evaluation. To make the task simpler, all tokens in the expression are separated by blanks as shown below:
1 + 2 * 3 - 4 / 2 + 5 * 6
Note that each operand is not restricted to just single digit and it can be any value found within an int.

After evaluation, the program will print the result.

Sample input 1
1 + 2 * 3 - 4 / 2 + 5 * 6

Sample output 1
35


Sample input 2
( 10 + 20 ) * 30 - 4000 / 20 + 50 * 60

Sample output 2
3700
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Postfix Calculator
20 Sep, 2006 - 05:41 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
The site has a policy by which we prefer to see a good faith effort on the part of the user before providing source code in the matter of academic assignments. If you could post the code you have written in an attempt to complete this assignment, our members would be happy to guide you.
User is offlineProfile CardPM
+Quote Post

sagarcruise
RE: Postfix Calculator
20 Sep, 2006 - 05:48 AM
Post #3

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 4


My Contributions
CODE

#include <iostream>
#include <stack>
#include <vector>
#include <sstream>
#include <cstring>
using namespace std;
int main()
{
    int result;
    stack<string> op;
    vector<string> postfix;
    stack<int> num;
    string line,str;
    int eval_postfix(vector<string>& post,stack<int>& num1);
    int precedence(string str1);
    getline(cin,line);
    istringstream iss(line);
    while(!iss.eof())
    {
                     iss>>str;
                     if(str=="+"||str=="-"||str=="*"||str=="/")
                     {
                            
                            while(!op.isEmpty()&&op.top()!="("&&precedence(str)>=precedence(op.top()))
                            {
                                                           postfix.push_back(op.top());
                                                           op.pop();
                            }
                            op.push(str);
                     }
                     else if(str=="(")
                     {
                          op.push(str);
                     }
                     else if(str==")")
                     {
                          while(op.top()!="(")
                          {
                              postfix.push_back(op.top());
                              op.pop();
                          }
                          op.pop();
                     }
                     else
                     {
                         postfix.push_back(str);
                     }
                                
    }    
    while(!op.isEmpty())
    {
                      postfix.push_back(op.top());
                      op.pop();
    }
    result=eval_postfix(&postfix,&num);
    cout<<result;
    return 0;
}
int precedence(string str)
{
    if(str=="+"||str=="-")
    return 1;
    else if(str=="*"||str="/")
    return 2;
    else
    return 0;
}
int eval_postfix(vector<string>& postfix,stack<int>& num)
{
    for(vector<string>::const_iterator ptr=postfix.begin();ptr!=postfix.end();++ptr)
    {
                                        
              if(*ptr!=="+"||*ptr!=="-"||*ptr!=="*"||*ptr!=="/")
              {
                                     int number;
                                     number=atoi(*ptr.c_str());
                                     num.push(number);
              }
              else{
                   int arg2=num.top();
                   num.pop();
                   int arg1=num.top();
                   num.pop();
                   int result;
                   if(*ptr=="+")
                   result=arg1+arg2;
                   else if(*ptr=="-")
                   result=arg1-arg2;
                   else if(*ptr=="*")
                   result=arg1*arg2;
                   else
                   result=arg1/arg2;
                   num.push(result);
                   }
     }          
     int i=num.top();
     num.pop();
     return i;
}

Sorry here is the code,i get a lotta compilation errors can anyone temme wat is wrong
User is offlineProfile CardPM
+Quote Post

Xing
RE: Postfix Calculator
20 Sep, 2006 - 06:13 AM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Check the modified code
CODE
#include <iostream>
#include <stack>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int result;

stack<string> op;
vector<string> postfix;
stack<int> num;

string line,str;

int eval_postfix(vector<string>& post,stack<int>& num1);
int precedence(string str1);
getline(cin,line);
istringstream iss(line);

while(!iss.eof())
{
iss>>str;
if(str=="+"||str=="-"||str=="*"||str=="/")
{

while(!op.empty()&&op.top()!="("&&precedence(str)>=precedence(op.top()))
{
    postfix.push_back(op.top());
    op.pop();
}
op.push(str);
}
else if(str=="(")
{
op.push(str);
}
else if(str==")")
{
while(op.top()!="(")
{
postfix.push_back(op.top());
op.pop();
}
op.pop();
}
else
{
postfix.push_back(str);
}

}
while(!op.empty())
{
postfix.push_back(op.top());
op.pop();
}
result=eval_postfix(postfix,num);
cout<<result;
return 0;
}
int precedence(string str)
{
if(str=="+"||str=="-")
return 1;
else if(str=="*"||str=="/")
return 2;
else
return 0;
}
int eval_postfix(vector<string>& postfix,stack<int>& num)
{
for(vector<string>::const_iterator ptr=postfix.begin();ptr!=postfix.end();++ptr)
{

if(*ptr!="+"||*ptr!="-"||*ptr!="*"||*ptr!="/")
{
int number;
istringstream stm;
stm.str(*ptr);
stm>>number;

//number=atoi(*ptr.c_str());
num.push(number);
}
else{
int arg2=num.top();
num.pop();
int arg1=num.top();
num.pop();
int result;
if(*ptr=="+")
result=arg1+arg2;
else if(*ptr=="-")
result=arg1-arg2;
else if(*ptr=="*")
result=arg1*arg2;
else
result=arg1/arg2;
num.push(result);
}
}
int i=num.top();
num.pop();
return i;
}

User is offlineProfile CardPM
+Quote Post

sagarcruise
RE: Postfix Calculator
20 Sep, 2006 - 07:14 AM
Post #5

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 4


My Contributions
thks a million for ur help,my program had a lotta compilation errors,now there are no errors but the answe r is wrong,can u help me on this account ,nce again thanks a lot for going through my program.
User is offlineProfile CardPM
+Quote Post

Xing
RE: Postfix Calculator
20 Sep, 2006 - 07:26 AM
Post #6

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(sagarcruise @ 20 Sep, 2006 - 08:44 PM) *

thks a million for ur help,my program had a lotta compilation errors,now there are no errors but the answe r is wrong,can u help me on this account ,nce again thanks a lot for going through my program.

Even after having a strong feeling that it's not your code I removed compilation errors for you to get you moving. Try to understand the logic yourself now. Analyse the code by dividing it into small parts. If you are stuck in anypart then post your understanding of that specific part. We'll try to help you understand better.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:05AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month