# Calculator using a Stack

Page 1 of 1

## 8 Replies - 8904 Views - Last Post: 30 August 2009 - 01:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=122637&amp;s=74eacb0529b5a8b50a4e1edad70571eb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 poncho4all

Reputation: 123
• Posts: 1,413
• Joined: 15-July 09

# Calculator using a Stack

Posted 27 August 2009 - 11:38 PM

Well i guess i just cant solve this one on my own, "to noob still" =(.
I have to make a calculator implementing a stack to save files from the anwsers of each operation and then pop them out if the operation continues. The calc takes the data on this format: 1 3 + 2 * 4 /
The operation should give back a result of 2, and as you can see its a weird way to imput the operations but yeah thats what my teacher wants.
Here is what i have so far:
```#include <iostream>

using namespace std;

struct Nodo{
float val;
Nodo *sig;
};
Nodo *pinicio=NULL;
void ppush(float x){
Nodo *temp;
temp=new Nodo;
temp->val=x;
temp->sig=NULL;
if(pinicio!=NULL)
temp->sig=pinicio;
pinicio=temp;
}
float ppop(){
Nodo *temp;
temp=pinicio;
float y=temp->val;
pinicio=pinicio->sig;
delete temp;
return y;
}
int main(){
char op[50];
char temp[8];
int i=0,j=0;
float y=0.f;
float tmp=0.f;
float res=0.f;
float val1=0.f;
cout<<"Imput the operation ";
gets(op);
do{
while(op[i]!= ' '){
temp[j]=op[i];
i++;
j++;
}
if(temp[j]=='+'){
y=ppop();
val1=ppop();
res=val1+y;
}
if(temp[j]=='-'){
}
if(temp[j]=='*'){
}
if(temp[j]=='/'){
}
if(temp[j]!='+'||temp[j]!='-'||temp[j]!='*'||temp[j]!='/'||temp[j]!=' ')
tmp=atof(temp);
ppush(tmp);
temp[j]='\0';
j=0;
i++;
}while(res!=4);//this is 4 because for testing im imputing 1 3 +
cout<<endl;
cout<<res;
system("pause");
return 0;
}
```

As you can see im really lost so a good idea or a redirection would be very helpfull

Thank you

Is This A Good Question/Topic? 0

## Replies To: Calculator using a Stack

### #2 LaFayette

• D.I.C Regular

Reputation: 43
• Posts: 326
• Joined: 24-November 08

## Re: Calculator using a Stack

Posted 28 August 2009 - 12:50 AM

Not weird at all, It's a pretty standard assignment actually!

Just push everything on the stack to begin with (divided as indivdual elements). Then pop your two arguments followed by the operator, have a look at the operator, do the operation and push the result on the stack. redo the same procedure unless there's only one element on stack (if so that is you answer)!

This post has been edited by LaFayette: 28 August 2009 - 12:51 AM

### #3 poncho4all

Reputation: 123
• Posts: 1,413
• Joined: 15-July 09

## Re: Calculator using a Stack

Posted 28 August 2009 - 10:11 PM

ok im stucked i dont have a clue of how to work them, plz some help, in my code im pushing all the operation into a stack, should i push them all as chars? and when reading to do the operation the first one out would be the last char that went in but that is not what i should operate.

Need some help

```#include <iostream>
#include <conio.h>

using namespace std;

struct Nodo{
char val;
Nodo *sig;
};
void ppush(char x, Nodo **pinicio){
Nodo *temp=new Nodo;
temp->val=x;
temp->sig=*pinicio;
*pinicio=temp;
}
char ppop(Nodo **pinicio){
Nodo *temp=*pinicio;
char y=temp->val;
*pinicio=temp->sig;
delete temp;
return y;
}
int main(){
char num=' ';
Nodo *pinicio=NULL;
float y=0.f;
char val2;
char res;
char val1;
cout<<"Imput the operation ";
while(true){
cin>>num;
if(num=='=')
break;
ppush(num, &pinicio);
}
while(pinicio!=NULL){
}
/*if(=='+'){
}
if(=='-'){
}
if(=='*'){
}
if(=='/'){
}*/
system("pause");
return 0;
}
```

Thank you

### #4 aks29921

• D.I.C Regular

Reputation: 116
• Posts: 345
• Joined: 24-August 09

## Re: Calculator using a Stack

Posted 28 August 2009 - 11:38 PM

if u have to build a calculator by implementing stack, this is what you should do:
1. whatever expression you have to evaluate, convert it in postfix form(operands as well as operators)
2. this postfix expression should be saved in a stack(you can implement it as an array, remember LIFO)
3. start popping from the top of stack, when u get 2 operands and an operator, perform the reqd computation and place the result back on top of stack.
4. continue (3) until you have only one element left in the stack, this element will be your answer.

hope this helps you and you are able to implement it in code

### #5 poncho4all

Reputation: 123
• Posts: 1,413
• Joined: 15-July 09

## Re: Calculator using a Stack

Posted 28 August 2009 - 11:58 PM

It did help and ty very much i completely forgot about the postfix part, i haven't seen stacks in such a long time.....

Thank you

### #6 poncho4all

Reputation: 123
• Posts: 1,413
• Joined: 15-July 09

## Re: Calculator using a Stack

Posted 29 August 2009 - 04:00 PM

Ok i need help commenting parts of this code
```  cin >> noskipws >> ch;
int operand = 0;
while(ch == ' ')//what does this do exactly?
cin >> ch;
if((ch >= '0')&&(ch <= '9')){
while(ch != ' '){//and this?
operand = operand*10 + (ch-48);//this converts the ch into ancii right?
cin >> ch;
}

```

ok need help i dont quite understand the noskipws function

### #7 poncho4all

Reputation: 123
• Posts: 1,413
• Joined: 15-July 09

## Re: Calculator using a Stack

Posted 30 August 2009 - 12:07 AM

ok i almost got it but still doesnt work quite well, something about negative numbers that dont wanna be nice to me =(
```#include <iostream>
#include <iomanip>
using namespace std;

struct Node{
float number;
Node *next;
};

Node* push(Node *stack, float data){
Node *utility;
utility = new Node;
utility -> number = data;
utility -> next = stack;
return utility;
}

Node* pop(Node *stack, float &data){
Node *temp;
if (stack != NULL){
temp = stack;
data = stack -> number;
stack = stack -> next;
delete temp;
}
else cout << "\nERROR: Empty stack.\n";
return stack;
}

int main()
{
char ch = ' ';
int neg=0,cont=0;
Node *utility, *top;

utility = new Node;
utility -> number = 0.f;
utility -> next = NULL;
top = new Node;
top -> number = 0.f;
top -> next = utility;

cout << "Enter the operation: ";
while(ch != '\n')
{
cin >> noskipws >> ch;
int operand = 0;
while(ch == ' '){
cin >> ch;
cont++;
}
if(cont<1 && ch=='-')
neg=-1;
cout<<cont<<endl;
if((ch >= '0')&&(ch <= '9')){
while(ch != ' '){
operand = operand*10 + (ch-48);
cin >> ch;
if(neg==-1){
operand=operand * neg;
}
neg=0;
}
cout<<operand<<"  "<<endl;
top = push(top, operand);
}else{
top=pop(top, operand1);
top=pop(top, operand2);
switch(ch){
case '+': answer = operand2 + operand1;break;
case '-': answer = operand2 - operand1;break;
case '*': answer = operand2 * operand1;break;
case '/': answer = operand2 / operand1;break;
}
}
}
system("pause");
return 0;
}

```

Is there another way?
How can i fix this?

### #8 jjl

• Engineer

Reputation: 1120
• Posts: 4,647
• Joined: 09-June 09

## Re: Calculator using a Stack

Posted 30 August 2009 - 12:51 AM

poncho4all, on 29 Aug, 2009 - 03:00 PM, said:

Ok i need help commenting parts of this code
```  cin >> noskipws >> ch;
int operand = 0;
while(ch == ' ')//asks for input if empty
cin >> ch;
if((ch >= '0')&&(ch <= '9')){
while(ch != ' '){//if the char is not empty continue
operand = operand*10 + (ch-48);//this converts the ch into ancii right?
cin >> ch;
}

```

ok need help i dont quite understand the noskipws function

### #9 poncho4all

Reputation: 123
• Posts: 1,413
• Joined: 15-July 09

## Re: Calculator using a Stack

Posted 30 August 2009 - 01:13 AM

Yay finally done here is the code
```//program modified by poncho4all
//Postfix Calculator using Stack for positive and negative integers
#include <iostream>
#include <iomanip>
using namespace std;

struct Node{
float number;
Node *next;
};

Node* push(Node *stack, float data){
Node *utility;
utility = new Node;
utility -> number = data;
utility -> next = stack;
return utility;
}

Node* pop(Node *stack, float &data){
Node *temp;
if (stack != NULL){
temp = stack;
data = stack -> number;
stack = stack -> next;
delete temp;
}
else cout << "\nERROR: Empty stack.\n";
return stack;
}

int main()
{
char ch = ' ';
int neg=0,cont=0,copy;
Node *utility, *top;

utility = new Node;
utility -> number = 0.f;
utility -> next = NULL;
top = new Node;
top -> number = 0.f;
top -> next = utility;

cout << "Enter the postfix operation: ";
while(ch != '\n')
{
cin >> noskipws >> ch;
cont++;
if(cont<4&&ch=='-'){
neg=-1;
ch=' ';
}
int operand = 0;
while(ch == ' '){
cin >> ch;
}
if((ch >= '0')&&(ch <= '9')){
while(ch != ' '){
operand = operand*10 + (ch-48);
cin >> ch;
if(neg==-1 && operand1>=0 ){
operand=operand * neg;
}
}
neg=0;
top = push(top, operand);
}else{
copy=operand1;
top=pop(top, operand1);
top=pop(top, operand2);
if(operand2<0&&copy>0){
operand2*=-1;
if(cont>4)
operand2*=-1;
}
switch(ch){
case '+': answer = operand2 + operand1;break;
case '-': answer = operand2 - operand1;break;
case '*': answer = operand2 * operand1;break;
case '/': answer = operand2 / operand1;break;
}
}
}