| | |
postfix expression evaluation
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 3
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
1) Welcome to DaniWeb!
2) When posting code to this board use code tags. How to use them is explained in the watermark in the message input box and in one of the "sticky" messages at the top of the board.
Some of the people here won't respond to your message if you don't use them, especially if you've been here a while.
3) Even if your compiler allows you to declare main() with a void return type, don't do it, at least not for any code you post here. Its not standard, it's not portable to many other compilers, and int is one keystroke less than void anyway.
4) Using files isn't difficult, in theory. You include the fstream header file, declare an instance of an appropriate stream object, associate a file with the stream, be sure the stream is open for use, then use the same operators/methods (like >> or << or getline(), etc) with the stream object that you would use with cin/cout (which are just stream objects that are predeclared for you) to do the reading/writing from the file. It's true that just about each of those steps has it's own foibles, so in addition to practice and reading other code samples, you should really have a good reference source available to use.
Once you get the above generics of file reading under control the problem becomes that you can only read the file if you know how the file was written. That is, does the file contain a single char per line, a single token per line, a single prefix expression per line, a single line in the whole file with each prefix expression delimited by some token, etc. All files are stored in binary and viewed by some technique, usually as something other than binary, usually as text or hexidecimal code. The program will use the binary data found in the file and use it as the variable type that your code indicates, whether it's int, char, double, etc.
2) When posting code to this board use code tags. How to use them is explained in the watermark in the message input box and in one of the "sticky" messages at the top of the board.
Some of the people here won't respond to your message if you don't use them, especially if you've been here a while.
3) Even if your compiler allows you to declare main() with a void return type, don't do it, at least not for any code you post here. Its not standard, it's not portable to many other compilers, and int is one keystroke less than void anyway.
4) Using files isn't difficult, in theory. You include the fstream header file, declare an instance of an appropriate stream object, associate a file with the stream, be sure the stream is open for use, then use the same operators/methods (like >> or << or getline(), etc) with the stream object that you would use with cin/cout (which are just stream objects that are predeclared for you) to do the reading/writing from the file. It's true that just about each of those steps has it's own foibles, so in addition to practice and reading other code samples, you should really have a good reference source available to use.
Once you get the above generics of file reading under control the problem becomes that you can only read the file if you know how the file was written. That is, does the file contain a single char per line, a single token per line, a single prefix expression per line, a single line in the whole file with each prefix expression delimited by some token, etc. All files are stored in binary and viewed by some technique, usually as something other than binary, usually as text or hexidecimal code. The program will use the binary data found in the file and use it as the variable type that your code indicates, whether it's int, char, double, etc.
![]() |
Similar Threads
- calling Python function from C/C++ (Python)
- help with a infix to postfix program (C++)
- Need Help!!! (Computer Science)
- having problems to complete implement a simple calculator program in c (C)
- postfix evaluation (C++)
- Any Boolean Expression to Sum of Minterms (C++)
- Mathematics In A Stack (C++)
Other Threads in the C++ Forum
- Previous Thread: Uninitialized Local Variable help
- Next Thread: File open globally and write to it
Views: 8950 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






