What do I push onto the stack?

This is a discussion on What do I push onto the stack? 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

    What do I push onto the stack?

    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.

    I want to push the return value from my function Eval. How would I say that?

    Here's my code:
    Code:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    using namespace std;
    
    const int DefaultListSize =50;
    typedef float Elem;
    
    class Link {
    public:
    	Elem element;	/*Value for this node*/
    	Link* next;		/*Pointer to the next node in list*/
    	Link(const Elem& elemval, Link* nextval =NULL)
    	{ element = elemval; next = nextval; }
    	Link(Link* nextval =NULL) { next = nextval; }
    };
    
    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;
    	}
    };
    	bool Operand(float ch){
    		if(('0' <= ch) && (ch <= '9'))
    			return true;
    		else return false;
    	}
    	bool Operator(char ch){
    		if(ch == '+' || '-' || '*' || '/' || '^')
    			return true;
    		else return false;
    	}
    
    	float Eval(float oprndL, char oper, float oprndR);
    	
    int main(){
    
    	Lstack S;
    	string s;
    	float a;
    	char b;
    	float c;
    	float answer;
    	unsigned int i;
    	
    	cout << "Enter Postfix string:\n";
    	getline(cin, s);
    
    	for(i=0; i < s.length(); i++ ){
    		if(Operand(s.at(i)))
    			S.push(s.at(i));
    
    		if(Operator(s.at(i)))
    			b = Operator(s.at(i));
    		}
    	if(!S.isEmpty()){
    		S.pop(a);
    		S.pop(c);
    		Eval(Operand(a), Operator(b) , Operand(c));
    		S.push();		
                    S.pop(answer);
    		cout << answer;
    	}
    	return 0;
    }
    
    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;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to put the item you want to push inside the () behind the word "push". Eval(this, that, theother) is a perfectly good item.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    oh ok! That makes sense. Now My program runs but when I input any postfix strings I keep getting 0. Like I entered 23+ the first time and it return 0. Then I tried 42- and it return 0 as well.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    The program should convert 23+ to 2+3 and the same with 42-, it should become 4-2.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Operator(b) is a boolean value, not a char. Same with operand(a) and operand(c). You need to pass the floats/characters.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Code:
    	Eval(a, b, c);
              S.push(Eval(a, b, c));
    I tried this but I still get 0.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You were intended to fix it everywhere:
    Code:
    b = Operator(s.at(i));

  8. #8
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok so could I instead assign b to any of the 5 operators then and take out my bool operator?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should assign b to just be the character. You should still do the check to see whether it is an operator, but that's taken care of in the if statement.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok I did that but I still get 0 for my answers. Here's my main program:

    Code:
    int main(){
    
    	Lstack S;
    	string s;
    	float a = 0;
    	char b = 0;
    	float c = 0;
    	float answer;
    	unsigned int i;
    	
    	cout << "Enter Postfix string:\n";
    	getline(cin, s);
    
    	for(i=0; i < s.length(); i++ ){
    		if(Operand(s.at(i)))
    			S.push(s.at(i));
    
    		if(Operator(b)){
    			Eval(a, b, c);
    		}
    	}
    
    	if(!S.isEmpty()){
    		S.pop(a);
    		S.pop(c);
    		Eval(a, b, c);
    		cout << Eval(a, b, c);
    		S.push(Eval(a, b, c));
    		S.pop(answer);
    		cout << answer;
    	}
    	return 0;
    }

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should assign b to just be the character, as opposed to just ignoring it completely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

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