Ok I got my RPN Calculator running, it does 5 basic math operations, add, divide, subtract, Multiply, and powering.
THE CODE: (Warning may cause eye or brain cancer upon reading)
Ok so what's the best way to attach a graphical interface to this, or rewrite code and move it to a cocoa or carbon app.
I also want to learn opengl and make a version using it.
THE CODE: (Warning may cause eye or brain cancer upon reading)
Code:
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
using namespace std;
double multi(double x, double y);
double divi(double x, double y);
double add(double x, double y);
double sub(double x, double y);
double power(double x, double y);
void addstack();
stack<double> mstack;
string input;
double swapx;
double swapy;
double test();
void resetinput();
void readstack();
int main()
{
while(1) //main loop
{
while(test() == 0) // addstack loop
{
addstack();
}
if(test() == 1) //multiplication
{
readstack();
cout << multi(swapx, swapy) << endl;
mstack.push( multi(swapx, swapy) );
}
if(test() == 2) // addition
{
readstack();
cout << add(swapx, swapy) << endl;
mstack.push( add(swapx, swapy) );
}
if(test() == 3) //subtraction
{
readstack();
cout << sub(swapx, swapy) << endl;
mstack.push( sub(swapx, swapy) );
}
if(test() == 4) // division
{
readstack();
cout << divi(swapx, swapy) << endl;
mstack.push( divi(swapx, swapy) );
}
if(test() == 5) // power
{
readstack();
cout << power(swapx, swapy) << endl;
mstack.push( power(swapx, swapy) );
}
resetinput(); // to turn on addstack loop
}
}
void addstack() // add input to the stack
{
cout << ": " ;
cin >> input;
if(test() == 0) // makes sure string isn't a math operator
{
istringstream buffer(input); //checks if string, if not converts to number
double swap;
buffer >> swap;
mstack.push(swap); // add to stack
}
}
double multi(double x, double y)
{
double swap;
swap = x * y;
return swap;
}
double divi(double x, double y)
{
double swap;
swap = y / x;
return swap;
}
double add(double x, double y)
{
double swap;
swap = x + y;
return swap;
}
double sub(double x, double y)
{
double swap;
swap = y - x;
return swap;
}
double power(double y, double x)
{
double ox = x; // sets a constanst to multiply by
while( y - 1 != 0)
{
x = ox * x;
y = y - 1;
}
return x;
}
double test() // returns 0 to addstack function if not math operator
{
if(input == "*")
{
return 1;
}
if(input == "+")
{
return 2;
}
if(input == "-")
{
return 3;
}
if(input == "/")
{
return 4;
}
if(input == "^")
{
return 5;
}
return 0;
}
void resetinput() // makes input void so addstack loop can do its buisness
{
input = "void";
}
void readstack()
{
swapx = mstack.top();
mstack.pop();
swapy = mstack.top();
mstack.pop();
}
Ok so what's the best way to attach a graphical interface to this, or rewrite code and move it to a cocoa or carbon app.
I also want to learn opengl and make a version using it.