•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,512 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,694 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4011 | Replies: 1
![]() |
| |
•
•
Join Date: Sep 2007
Posts: 3
Reputation:
Rep Power: 0
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,288
Reputation:
Rep Power: 9
Solved Threads: 175
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- help with a infix to postfix program (C++)
- calling Python function from C/C++ (Python)
- Need Help!!! (Computer Science and Software Design)
- 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



Hybrid Mode