hi please i need some help..i know how to write a code for postfix expression evaluation by taking input from user,, but what if all the data is in text file,,
pls help me modify my code , i have to read all the variables and values and also the expressions from text file and then evaluate them...
#include <iostream>
#include <stack>
#include <string>
using namespace std;
void main()
{
int i, choice = 1;
string postfixExp;
char token;
float value, value1, value2;
stack<float> s; //Declare a stack of floats
while (choice != 0)
{
cout << "1. Evaluate a postfix expression" << endl;
cout << "0. Exit " << endl;
cout << "Enter the number for the option: ";
cin >> choice;
switch(choice)
{
case 1: cout << "Evaluate a postfix expression\n";
cout << "Enter the expression: ";
cin >> postfixExp;
i = 0;
token = postfixExp[i];
while((i < postfixExp.size()) && (token != '='))
{
if(isdigit(token))
{
value = token - '0';
s.push(value);
}
else
{
value2 = s.top();
s.pop();
value1 = s.top();
s.pop();
switch(token)
{
case '+': value = value1 + value2;
break;
case '-': value = value1 - value2;
break;
case '*': value = value1*value2;
break;
case '/': value = value1/value2;
break;
}
s.push(value);
}
i++;
token = postfixExp[i];
}
value = s.top();
s.pop();
cout << postfixExp << " " << value << endl;
break;
case 0: cout << "Exiting the program\n";
break;
default: cout << "Invalid option\n";
break;
}
cout << endl;
}
}