C++ Stack Program Implementing PostFix Notation

I'm having problems with my program.

What I'm Trying to achieve is this:

Make a program that prompts the user to enter the value of A,B,C,D, and E. Then the user must enter and Arithmetic Notation using InFix, Then prints the Result.
Must use Stack..

Sample output:

Enter numbers:
A: 2
B: 4
C: 4
D: 1
E: 2

Enter Notation: AB+C-E*/

Result: xx

Here's what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <string.h>
#include <iostream.h>
#include <conio.h>

class Stack
{
	private:
		int S[50];
		int top;
	public:
		Stack(void);
		void Push(int elem);
		int Pop(void);
		int Top(void);
		int Empty(void);
};

Stack::Stack(void)
{
	top=-1;
}

void Stack::Push(int elem)
{
	top++;
	S[top]=elem;
}

int Stack::Pop(void)
{
	int temp = S[top];
	top--;
	return temp;
}

int Stack::Top(void)
{
	return S[top];
}

int Stack::Empty(void)
{
    if(top<0)
	return 1;
    else
	return 0;
}

void main()
{
	clrscr();
	Stack S1;
	int a,b,c,d,e;
	int count=0;
	char arit[30];

	cout<<"Enter a Number: ";
	cout<<"\nA: ";
	cin>>a;
	cout<<"B: ";
	cin>>b;
	cout<<"C: ";
	cin>>c;
	cout<<"D: ";
	cin>>d;
	cout<<"E: ";
	cin>>e;

	cout<<endl;

	cout<<"Enter Arithmetic Notation: ";
	cin>>arit;

	for (int i=0;i<strlen(arit);i++)

	{

		if (arit=='a'||arit=='b'||arit=='c'||arit=='d'||arit=='e'||arit=='A'||arit=='B'||arit=='C'||arit=='D'||arit=='E')
		 {




    getch();
}



Im using TurboC++

Hope you guys can help..
Last edited on
If you read a variable, push its value onto the stack.
If you read an operator, pop the top two values off the stack, perform the operation, and push the result onto the stack.
Repeat until end of input.
At end of input, there should be only one value on the stack, and it will be the answer.
I know that, but the problem is how do i do it.. Im not really a pro when it comes to programming..

I dont know how to Pop the two variables and perform the operator..
can anyone help?
Erm, call the pop function twice to get the data out. Then check the operator and do that operation to the two variables you've just popped.
Can you guys give me a sample program about implementing a postfix notation?

I know how to Pop and Push, but my question is. as you can see in the program, the Arithmetic Notation is in Char, now like example the program reads "a" i want it to push the value the user gave on and not the character..

And Question will it automatically add the variables? when an operator is read?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main () {
    int values[5];
    for(char c='A'; c<='E'; c++) { //store the values in the array
        cin >> values[c-'A'];
    }

    //read the expression here...

    //now loop for each character in the expression...

    //if for example you've read D then push values['D'-'A']
    //sample
    cout << endl << values['D'-'A']; //showing the value of D
    return 0;
}

Topic archived. No new replies allowed.