how i can Solution this ...?

Feb 17, 2011 at 2:11pm
hi ..
plezz how i can solution this algorethom

234*+
in Mathematics this solution
2 3*4+
2 12+
2+12
14
thx all
Feb 17, 2011 at 2:29pm
Build a postfix parser which has as side-effect the evaluation of the expression
Feb 17, 2011 at 2:31pm
thx bro but i need see the program ..
Feb 17, 2011 at 2:52pm
You'll have to write it, then.
Feb 17, 2011 at 3:58pm
closed account (z05DSL3A)
Using a stack
For each item in the postfix expression from the left:
    if the element is a number push it on the stack
    if the element is an operator (+,-,*, or /) then
        pop two numbers off the stack
        make a calculation: the second number popped-operator-first number
        push the result on the stack
When the loop is done the answer is the only item remaining on the stack.
Last edited on Feb 17, 2011 at 3:58pm
Feb 17, 2011 at 5:09pm
thats good bro ...
but : redface:
i m new learning and i start with stack ..
i dont know how i can write this program ..
Feb 17, 2011 at 5:30pm
I feel like all I do lately is link to this site on these forums.

http://www.cplusplus.com/doc/tutorial/
Feb 17, 2011 at 5:54pm
Ok, look at this code, it's something I already had:
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
#include "stdio.h"
#include "ctype.h"
int main(){
	int instr;
	int stack[100];
	int sp=0;
	while((instr=getchar())!=EOF){
		if(isdigit(instr)){
			if(sp>=100){
				fputs("stack overflow\n",stderr);
				continue;
			}else stack[sp++]=instr-'0';
		}else if(sp-1<0){
			fputs("stack underflow\n",stderr);
			continue;
		}
		else if(instr=='\n')printf("%d\n",stack[--sp]);
		else if(sp-2<0){
			fputs("stack underflow\n",stderr);
			continue;
		}
		if(instr=='+')stack[sp-2]+=stack[sp-1],sp--;
		if(instr=='-')stack[sp-2]-=stack[sp-1],sp--;
		if(instr=='*')stack[sp-2]*=stack[sp-1],sp--;
		if(instr=='/')stack[sp-2]/=stack[sp-1],sp--;
		if(instr=='%')stack[sp-2]%=stack[sp-1],sp--;
	}
}
Feb 17, 2011 at 8:03pm
thx all ...
thats vary good bro thx
Topic archived. No new replies allowed.