converting to float

This is a discussion on converting to float 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

    converting to float

    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.

    Right now Im just tryna get postfix strings with 3 characters like 42-, 23+, 32^, 12*, and 93/. Only my subtraction works. I need to convert the characters into floats but I dont know how to do that and I cant find it in any books.

    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 == '+') || (ch =='-')|| (ch == '*') || (ch == '/') || (ch == '^'))
    			return true;
    		else return false;
    	}
    
    	float Eval(float oprndL, char oper, float oprndR);
    	
    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(s.at(i) != EOF){
    			if(Operand(s.at(i))){
    				S.push(s.at(i));
    			}
    			else if(Operator(s.at(i))){
    				b = s.at(i);
    			}
    		}
    	}
    	if(!S.isEmpty()){
    		S.pop(c);
    		S.pop(a);
    		Eval(a, b, c);
    		S.push(Eval(a, b, c));
    		S.pop(answer);
    		cout << answer << endl;
    	}
    	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
    I'm not sure why you think the answer will be different this week than last week, but: you need to subtract '0' from a character (representing a digit) to get the value of that digit.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I didnt think that I couldnt find my original post so I just reposted this. What do you mean by subtract '0' from a character?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    It's very... um... arrogant, to start your post with imperative statements, and anyone who does this usually gets no help at all from me. I am not your slave, and you may not simply dump instructions for me to follow. Respect gets respect. You need to start your post with wording that comes from yourself, and not just regurgitate what someone else intended as instructions to you. As a minimum it could be prefixed with something like "This is my assignment:", though I would typically expect a bit more than that.
    Kapish?!

    Well, you get yourself a character in some variable and you subtract '0' from it. What part of that do you not understand?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    What are you talking bout? All I did is post my instructions Im suppose to follow. Since I could not find the original posting I was looking for so I reposted it.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    I don't know anything about any previous thread that you lost. You simply started this thread here where the first sentence is commanding the reader (us) to do something.

    Sure we could read the whole first two paragraphs and work out that those instructions are meant for you to follow and not us, but it's still downright unpolite.

    Would you walk up to a stranger and hold a printed page of instructions in their face? No, you'd politely ask them to help you with something and then show them what it was you wanted help with.
    Do you get it yet?!
    Last edited by iMalc; 12-16-2010 at 09:11 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  2. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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