Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i'am trying to read postfix expression from txt file and evaluate it
the input is 10 5 * ,the output should be 50, but it reads only 10 and 5, where's the error?
here's my code

C++
#include <iostream>
 #include <iomanip>
 #include <fstream>
 #include<stdio.h>
 #include<ctype.h>
 #include<stdlib.h>
 using namespace std;
 #define SIZE 40
 int stack[SIZE];
 int top=-1;
 
void push(int n)
 {
 if(top==SIZE-1)
 {
 printf("Stack is full\n");
  system("pause");
 return;
 }
 else
 {
 top=top+1;
 stack[top]=n;
 printf("Pushed element is %d\n",n);
  system("pause");
 }
 }
 
int pop()
 {
 int n;
 if(top==-1)
 {
 printf("Stack is empty\n");
  system("pause");
 return 0;
 }
 else
 {
 n=stack[top];
 top=top-1;

 return(n);
 }
 }
 



int main() 
{
 
int str[50],ch;

 int i=0;
 int n,op1,op2;

 ifstream inFile;
 ch=str[i];

 inFile.open("D:\\me.txt");
 if (!inFile) 
{
 printf( "Unable to open file");
 system("pause");
 exit(1); // terminate with error
 }

 while (inFile >> ch) 
{
 if(ch==43 || ch==45 || ch==47 || ch==42 || ch==94 || ch==37 )
 {

	 op1=pop();
	 op2=pop();
	 if (op1<op2)
	 {
	 n=op1;
	 op1=op2;
	 op2=n;
	 }
	 switch(ch)
	 {
	 case 43:
	 n=op1+op2; break;
	 case 45:
	 n=op1-op2;break;
	 case 42:
	 n=op1*op2;break;
	 case 47:
	 n=op1/op2;break;
	 case 37:
	 n=op1%op2;break;
	 case 94:
	 n=op1^op2;break;
	
 
	 default:
	 {
	 printf("The operator is not identified\n");
	 system("pause");
	 exit(0);break;
	 }
	 }
	 printf("n=%d\n",n);
	 push(n);
	 system("pause");


 }
	 else
	 {
	 n=ch;
	 push(n);
	 }
 ch=str[++i];
 }
inFile.close();
 printf("The value of the arithmetic expression is=%d\n",pop());
 system("pause");
 return 0;
 } 
Posted
Updated 19-Apr-12 5:19am
v2
Comments
Schehaider_Aymen 19-Apr-12 11:16am    
You declared ch as int so when u read the first numbers 10 and 5 all work fine but when u try to read the operand * it goes out od the loop.
shabnile 19-Apr-12 11:27am    
thanks, but i'am trying to read the ascii code of these operators
shabnile 19-Apr-12 17:22pm    
Thanks, i solved the problem ^_^

1 solution

irst off, take that code and indent it properly - so everyone can see easily what is going on. Then add some comments. Then replace your magic numbers with constant names, or the actual characters. Which of these is easier to read and understand:
C++
if (ch == 42)

C++
if (ch == '+')

C++
if (ch == OPERATOR_ADD)

Then we can look at the problem a lot more easily.

But I'll give you a hint: Look at what is in your input string, and also look at what you push and pop on the stack. What characters are you getting from the file? What values would you be adding if your code worked? How many items are you putting on the stack?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900