Académique Documents
Professionnel Documents
Culture Documents
Edition By H. M. Deitel - Deitel & Associates, Inc., P. J. Deitel - Deitel & Associates, Inc.) but I
wrote it in Java Language. -mijaka_paz08@yahoo.com
21.12 Stacks are used by compilers to help in the process of evaluating expressions
and generating machine language code. In this and the next exercise, we
investigate how compilers evaluate arithmetic expressions consisting only of
constants, operators and parentheses.
In this exercise, you will write a C++ version of the infix-to-postfix conversion
algorithm. In the next exercise, you will write a C++ version of the postfix
expression evaluation algorithm. Later in the chapter, you will discover that
code you write in this exercise can help you implement a complete working
compiler.
(6 + 2) * 5 - 8 / 4
62+5*84/-
21.13 Write a program that evaluates a postfix expression (assume it is valid) such as
62+5*84/-
The program should read a postfix expression consisting of digits and operators
into a character array. Using modified versions of the stack functions
implemented earlier in this chapter, the program should scan the expression
and evaluate it.
Result:
38
Codes
/**
* @(#)StackTest.java
*no comments … I provided the code and you do your part to understand it.. =>
*
* @author ME
* @version 1.00 2010/8/3
*/
import java.io.*;
import java.util.*;
input=input+")";
Stack stack=new Stack(input.length());
stack.push('(');
String post=stack.convert(input);
StringTokenizer output=new StringTokenizer(post);
System.out.println("\npostfix expression:");
while(output.hasMoreTokens())
{
System.out.print(output.nextToken()+" ");
}
int answer=evaluate.produce(post);
System.out.println("\nresult: ");
System.out.println(answer);
System.exit(0);
/**
* @(#)Stack.java
*This is a stack in int array
*
* @author ME
* @version 1.00 2010/8/3
*/
import java.io.*;
import java.util.*;
top++;
values[top] = data;
}
}
return retVal;
}
if(!isEmpty()) {
retVal=values[temp];
temp--;
}
return retVal;
else if(it=='/')
return 5;
else if(it=='%')
return 4;
else if(it=='+')
return 3;
else if(it=='-')
return 2;
else
return 1;
}
int i=0;
char c;
String word=" ";
while(i<in_put.length())
{
c=in_put.charAt(i);
if(Character.isDigit(c))
postfix.append(c).append(" ");
if(c=='(')
push('(');
if(c=='*'||c=='/'||c=='%'||c=='+'||c=='-')
{
char v=stackTop();
if(v=='*'||v=='/'||v=='%'||v=='+'||v=='-')
{
if(precedence(v)>=precedence(c))
{
{
postfix.append(pop()).append(" ").toString();
}
}
}
push(c);
}
if(c==')')
{ char st=stackTop();
while(st!='('&&!isEmpty())
{
postfix.append(pop()).append(" ").toString();
st=stackTop();
}
pop();
}
i++;
}
return postfix.toString();
/**
* @(#)evaluator.java
*This is a stack in char array
*
* @author ME
* @version 1.00 2010/8/4
*/
import java.io.*;
import java.util.*;
top++;
values[top] = data;
}
}
return retVal;
}
post_fix=post_fix+"\0";
int i=0;
int ans;
int x,y;
while(i<post_fix.length())
{
char c=post_fix.charAt(i);
if(Character.isDigit(c))
{
int l=(c-'0');
push(l);
if(c=='*'||c=='/'||c=='%'||c=='+'||c=='-')
{
x=pop();
y=pop();
//calculating
if(c=='*'){
ans=x*y;
push(ans);
}
if (c=='/'){
if(x<y)
{
ans=y/x;
push(ans);
}
else{
ans=x/y;
push(ans);
}
}
if (c=='%'){
ans=x%y;
push(ans);
}
if(c=='+'){
ans=x+y;
push(ans);
}
if(c=='-'){
if(x<y)
{
ans=y-x;
push(ans);
}
else{
ans=x-y;
push(ans);
}
}
if(c=='\0')
{
while(!isEmpty()){
valEx=pop();
}
}
i++;
}
return valEx;
}