User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,514 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 3,945 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: 3112 | Replies: 1
Reply
Join Date: Sep 2007
Posts: 3
Reputation: sarah24 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sarah24 sarah24 is offline Offline
Newbie Poster

postfix expression evaluation

  #1  
Sep 27th, 2007
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;
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 1,104
Reputation: Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough 
Rep Power: 9
Solved Threads: 145
Lerner Lerner is offline Offline
Veteran Poster

Re: postfix expression evaluation

  #2  
Sep 27th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:59 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC