i not sure how to solve the problem. Pls help me. Thanks.
asg.cpp
#include<iostream>
#include<ctype.h>
#include"Stack.h"
using namespace std;
main(){
char ch;
nodeIntType operand1,operand2,result;
StackInt S;
InitializeStack(&S);
cout<<"Enter postfix expression";
cin.get(ch);
while (ch!='\n')
{
if (isdigit(ch))
{
Push ((nodeIntType)((char)ch-(char)'0'),&S);
}
else
{
Pop (&S,&operand2);
Pop (&S,&operand1);
switch (ch)
{
case'+' :result = operand1+operand2;
break;
case'-' :result = operand1-operand2;
break;
case'*' :result = operand1*operand2;
break;
case'/' :result = operand1/operand2;
break;
}
Push(result,&S);
}
cin.get(ch);
}
Pop(&S,&result);
cout<<"answer:"<<result<<endl;
return 0;
}
stack.h
#include<iostream>
using namespace std;
typedef char nodeCharType;
typedef int nodeIntType;
struct nodeChar {
nodeCharType item;
nodeChar *next;
};
struct nodeInt {
nodeIntType item;
nodeInt *next;
};
typedef nodeChar *StackChar;
typedef nodeInt *StackInt;
void InitializeStack(StackChar *S)
{
*S=NULL;
}
void InitializeStack(StackInt *S)
{
*S=NULL;
}
int FullStack(StackChar)
{
return 0;
}
int FullStack(StackInt)
{
return 0;
}
void Pop(StackChar *S, nodeCharType *r)
{
StackChar p,y;
p=*S;
y=*S;
if (p->next==NULL)
{
cout<<"Error : Stack Empty!"<<endl;
}
else
{
while (p->next!=NULL)
{
p=p->next;
}
*r=p->item;
}
}
void Pop(StackInt *S, nodeIntType *r)
{
StackInt p,y;
p=*S;
y=*S;
if (p->next==NULL)
{
cout<<"Error : Stack Empty!"<<endl;
}
else
{
while (p->next!=NULL)
{
p=p->next;
}
*r=p->item;
}
}
int EmptyStack(StackChar S)
{
return (S==NULL);
}
int EmptyStack(StackInt S)
{
return (S==NULL);
}
void Push(nodeCharType , StackChar *S)
{
StackChar *Temp;
Temp = new StackChar;
if (Temp == NULL)
{
cout << "System storage is exhausted.\n";
}
else
{
Temp ->link = S->Item ;
Temp ->Item = x;
S ->Item = Temp;
}
}
void Push(nodeIntType , StackInt *S)
{
StackInt *Temp;
Temp = new StackInt;
if (Temp == NULL)
{
cout << "System storage is exhausted.\n";
}
else
{
Temp ->link = S->Item ;
Temp ->Item = x;
S ->Item = Temp;
}
}