Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > postfix evaluation

Thread Tools

postfix evaluation

maha_khaled maha_khaled is offline
Junior Member
Join Date: Apr 2012
Location: Egypt
Posts: 1
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,the problem he can't read operators neither their ascii ,any help?
here's my code
PHP Code:
#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];"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; } 
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
[EVALUATION] - E04 - jamPersist Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 18 04-09-2005 04:45 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 74 04-04-2005 05:29 PM
Function call, postfix inc and exception behavior Goran Sliskovic C++ 2 05-15-2004 07:06 PM
infix to postfix expression string for evalution. KidLogik C++ 5 02-03-2004 05:20 PM