latest code: I still cant figure out what should I do.I want to make it like the function would do the calculations for every numbers that was pushed. Still, i manage to solve it with only 2 operands and 1 operator. How to do it when I want to have more than 3 operands and the function to check all the calculation. No error checking required.
If i try to add more than 2 numbers(eg, 3 numbers with 2 operands), the function would only calculate the first 2 operands and the operator(No error in program). Not sure how to do on the subsequent operands and operators
You could read input by lines, loop through the string using find_first_of and find_first_not_of to find operators. You'd than need to use a stringstream to convert it into an int. This is only a suggestion.
What you'd want to do than is push any values you read onto the stack, than whenever you read an operator, pop the last two values off the stack, compute and push the resultant back onto the stack. At the end you should have only 1 value left in the stack if the input follows RPN.
I want to make it like the functions would calculate all of the operands regardless of any number input. Since i need 2 vairables for calculation, i have to declare 2 variables. Right now, i already confused how to implement the algorithm such that the function would do the counting, the number or charcter to be input would be calculated by the function regardless the amount of operators and operands. Anyway, my program do not need to check error checking.
if i try to code the calculation function into pop instead of using push, i cant add, substract the variables since the stack pop fucntion take no argument. So, i have to use push. Seriously, give me some tips. I already tried 4 hours but still cant do. Where is the specify code I had done wrong.
You need to use top() first to view the variable, than pop() to remove it from the stack. It's a shame that pop() doesn't return the value but I guess it's for efficiency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int calc (int op1, int op2, char expr); // can prob use templating here instead incase you want to change the type after
stack<int> values;
int a, b;
b = values.top();
values.pop();
a = values.top();
values.pop();
values.push( calc( a, b, expr ) );
The reason your program isn't working the way it is is because you aren't reading the input correctly.
Think about this, right now it's hard coded to read 2 ints and 1 operator so if you have this input:
1 1 1 + +
It'll read, 1 -> op1, 1 -> op2 and 1 -> ch.
You need to change the method in which you read input from.
Also you shouldn't be using a global variable like you have, you can easily use it as a local variable, which makes your calculation function look more like this:
If you don't understand and you've spend 4 hours without figuring something like this out, you should probably start with something a bit simpler and work your way up. Right now it seems your problem is you don't know how to get input the way you need it to. You can't assume that it'll be 2 ints followed by an expression unless you are telling the user to input only a single operation.
So, should I use templates instead. Because my program do not required error checking in my assignment, so I assume all of the number, ooperator input by the user is correct.
I know this problem but not sure how should I changed it such that the input accept charcters and integers together. This is my biggest problem that stuck me for several hours.Not sure if templates work. Should i change my functions or data typs into templates. As for global variables, if i declare it in local variables, the a.push in the function body of the calculation would be undeclared. So, no choice, i had to let it become global variables.
And i cant just code what you wrote in the function. I need to pop the any 2 numbers to do the calculation and I cant just return the amount only.
You can pass the stack by reference or pointer through to a function if that's what's required of you, so yes there is a choice.
If this is an assignment ask your prof or teacher for guidance, they are the most knowledgeable and reliable source of information. There's many methods you can choose to read input the way you need it, read the characters one by one, use a space as a delimiter.
How to like translate it into the code for reading the char one by one.
a.push( calculation( op1, op2, ch ) );
I cant do something like that for the function. I not sure of the error.
cannot convert parameter 1 from 'void' to 'const int &' - error
Wa, this code definitely looks new and hard. I dun understand the code at all.
Just asking, anyone know if it is possible to declare something that the data type can store integers or charcters both without the int which store only integers and char to store only charcters. This is the part where I had big problem since I only can do the calculation for a b+ only.
Any1 know what should I do if I want to check whether the user input is integer or characters. Currently, this is my biggest problem. Because integer is make of int data types while characters is make of char data types. how should I check if the user input enter is either integer of characters using c++ algorithms with perhaps example of source code.
You need to ask for your input, one at a time. Then if it is a number, push it onto the stack. If it is an opertor, then pop the last two numbers off the stack, evaluate them according to the operator and push the result back onto the top of the stack.