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

    Joined:
    Sep 25, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice