-
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;
}
-
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.
-
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?
-
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?
-
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.
-
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?!