1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

pls help: postfix expression evaluation by reading data from text file

Discussion in 'C++' started by sarah24, Sep 29, 2007.

  1. sarah24

    sarah24 New Member

    Hi All
    pls i need urgent help i have written a code that takes input from users and then evaluate the postfix expression but i don't know how to do it if data and even expressions are to be read from a text file...
    Code:
    #include <iostream>
    #include <stack> 
    #include <string>
    using namespace std;
    #include <fstream>
    using std::ifstream;
    #include <cstdlib>
    
    int main()
    {
        int i, choice = 1;
        string postfixExp;
        char token;
        float value, value1, value2;
        stack<float> s; //Declare a stack of floats
        
    	/* i only know how to open my file and read data from it but don't know if i need to read it in certain format*/
    	ifstream indata;
    	indata.open("C:\\temp\\575_prog2_data.txt");
    	if(!indata)  {
    		cerr << "Error: File could not be opened" << endl;
    		exit (1);
    	}
    
    	char letter;
    	while (indata.eof()==0) 
    	{indata >> letter;
    	cout <<letter;
    	cout << endl;
    	indata.close();
    	cout<< "end of file reached.." <<endl;
    	return 0;
    	}
    
    
    
        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;
        }
    }
     
    Last edited by a moderator: Sep 30, 2007

Share This Page