Need help on my project 2

This is a discussion on Need help on my project 2 within the C++ Programming forums, part of the General Programming Boards category; Write a program in C++ that evaluates portfix strings (one per line in a text file) with the following restrictions ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Need help on my project 2

    Write a program in C++ that evaluates portfix strings (one per line in a text file) with the following restrictions on the input: Each character is either an operand- if it is a digit and the floating point value associated with the digit, or it is one of the 5 binary operators: +, -, *, / (floating point), ^ (exponentiation). You must use a stack, in particular, the linked list implementation of the stack.

    Here is what I got so far:

    Code:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    
    const int DefaultListSize =50;
    typedef float Elem;
    typedef char Link;
    
    
    class Lstack {
    private:
    	Link*top;	/*Pointer to first element*/
    	int size;	/*Count number of elements*/
    
    public:
    	Lstack(int sz =DefaultListSize) {top = NULL; size = 0;}
    	~Lstack() {clear();}	/*Destructor*/
    	void clear() {
    		while (top != NULL) { /*Delete link nodes*/
    			Link*temp = top;
    			top = top->next;
    			delete temp;
    		}
    		size = 0;
    	}
    	bool push(const Elem& item) {
    		top = new Link(item,top);
    		size++;
    		return true;
    	}
    	bool pop(Elem& item) {
    		if(size == 0) return false;
    		item = top->element;
    		Link *ltemp = top->next;
    		delete top;
    		top = ltemp;
    		size--;
    		return true;
    	}
    	bool topValue(Elem& it) const {
    		if (size ==0) return false;
    		it = top->element;
    		return true;
    	}
    	int length() const {return size;}
    	bool isEmpty() const {
    		if (top == NULL) return true;
    		else return false;
    	};	
    	float Eval(float oprndL, char oper, float oprndR){
    		if(oper == '+') return oprndL + oprndR;
    		else if(oper == '-') return oprndL - oprndR;
    		else if(oper == '*') return oprndL * oprndR;
    		else if(oper == '/') return oprndL / oprndR;
    		else if(oper == '^') return pow(oprndL,oprndR);
    		else return 0.0;
    int main(){
    
    	int v;
    	bool operand(char ch){
    		if(('0' <= ch) && (ch <= '9'))
    			return true;
    		else return false;
    	}
    	while(EOF){
    		if(ch != '/n') if(operand(ch)) S.push(Val(ch));
    		else {S.pop(oprndR);
    			S.pop(oprndL);
    			v = Eval(oprndL,ch,oprndR);
    			S.push(v);
    		}
    	}
    	else S.pop(answer);
    	cout<< answer;
    	return 0;
    }
    1>------ Build started: Project: Project 2, Configuration: Debug Win32 ------
    1> Project 2.cpp
    1>c:\users\justin\documents\visual studio 2010\projects\project 2\project 2\project 2.cpp(78): fatal error C1075: end of file found before the left brace '{' at 'c:\users\justin\documents\visual studio 2010\projects\project 2\project 2\project 2.cpp(51)' was matched
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Code:
    	};	
    	float Eval(float oprndL, char oper, float oprndR){
    		if(oper == '+') return oprndL + oprndR;
    		else if(oper == '-') return oprndL - oprndR;
    		else if(oper == '*') return oprndL * oprndR;
    		else if(oper == '/') return oprndL / oprndR;
    		else if(oper == '^') return pow(oprndL,oprndR);
    		else return 0.0;
    The error message is very revealing here. What is that semi-colon doing at the end of the function? Not to mention, you have another function after it called 'Eval', but there is no terminating '}' for that function. Those are your problems with this code.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    oh ok I see! The other thing it's giving me is that top is undefined in my Lstack. But I dont know if it should be int, char, or what.

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Perhaps I am missing something, but at the start of the source code you have these two lines of code:
    Code:
    typedef float Elem;
    typedef char Link;
    Which is fair enough, you have defined Link as a character. Then in your class you define a pointer to type 'Link', which is again ok, but in the source code you have lines like the two below:
    Code:
    item = top->element;
    Link *ltemp = top->next;
    Am I missing something really obvious? Where does type 'Link' have a member called 'next' or 'element'? It is just a character.

    Also, I was a little confused when I saw this code:
    Code:
    	bool operand(char ch){
    		if(('0' <= ch) && (ch <= '9'))
    			return true;
    		else return false;
    	}
    The reason I was confused was that it looks a lot like a function but it is defined within the int main function! Do I need to continue with the explanation of how wrong that is?

    You then have a while loop defined as this:
    Code:
    	while(EOF){
    Do you realise that EOF is a macro? You don't even use it in the right context.

    You then have lines like this:
    Code:
    S.push(Val(ch));
    I can only presume that 'S' is the object instance of your class although you don't seem to ever call the constructor of your class.
    Also, where is the function 'Val()' defined? I can't see the implementation of that function? Is implemented in another file? To the best of my knowledge it does not exist in any of the header files you included.

    Are you moving in C++ from another language?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 10:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21