I am trying to implement some scientific functions into this rpn calculator(meant to be a hp-35 sim) but i'm having trouble being able to use pi in calculations or how to implement log, sin or pow into the code. It currently works with basic algebra.
The constructor sets all the variables to zero, it stops the strange numbers appearing, you should see zero instead. The destructor doesn't need to do anything. But I put it there in case it might be needed later.
do you want to write your own pow/sin/log? Or work them into your code using the defaults?
sin is just a series.
not sure how to do fractional pow off the top of my head
logs have tons of tricks; not sure best way here but you can use a log table / interpolate same as engineers used to do with a slide rule. If you have one log, you have them all via change of base trick.
fractional powers give roots, so once that works you have sqrt etc too.
I wanted to work them into my code from the cmath library. I've been able to do that succesfully but currently I'm trying to implement a way of string the value in stack x into memory and being able to recall it. Not sure if I should use pointers
This is what I have at the moment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
elseif (token == "STO") {
double x = stack.peek();
double y = stack.peek();
double z = stack.peek();
double t = stack.peek();
t = x;
}
elseif (token == "RCL") {
double x = stack.peek();
double y = stack.peek();
double z = stack.peek();
double t = stack.peek();
x = t;
}
For that very reason I tend to make my stacks (and most other data structures) have a 'back door' for iteration. That is, any stack I have would double as an array, so I could just look at everything on it in a loop. One way to do that is just to use a vector ... it has the push-back which everyone knows, but it also has the somewhat under-used pop-back ... making the standard vector a simple stack. Then you don't need a peek (peek is just stack[stack.size()-1]) and you can look at any of the entries directly...
so you would not need a pointer this way, you can copy out of the stack to local variables on demand, and reverse-iterate on demand, etc.
depending on how you implemented your hpstack adding that functionality may be easy or difficult. If difficult, consider just swapping to a vector. If you need more advanced stack stuffs, there is std::stack, though I have not needed it and am unsure of what major differences there are between it and a vector as a stack.
It's a long time since I did any serious stuff with RPN, but I was just playing around with ideas, based on Pythagoran triples, something like this: 3 3 * 4 4 * 12 12 * 84 84 * 77 77 * - + + + + SQRT SQRT
or line-by-line
It requires a stack depth of at least six , which is no problem with something like a vector, but malfunctions with the original design using separate variables.