0

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;
	}
}
Attachments
#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;
}
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by ben8118
0

What seems to be the problem?


And next time, do not attach the code as files. Usually it is an extra trouble for us to download it and open them seperately. We would prefer it included with the post so we can look at it directly. I have done it for now since it is your first time. I have also used code tags, so next time remember that when you are posting code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.