Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fearless Leader

macrumors 68020
Original poster
Mar 21, 2006
2,360
0
Hoosiertown
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)
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.
 

SC68Cal

macrumors 68000
Feb 23, 2006
1,642
0
Look into using interface builder, which is part of the Xcode development tools.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
If you are eager to learn OpenGL you can shorten your learning curve by using GLUT. GLUT is a cross platform API that takes care of setting up a window with an OpenGL context. It also handles the main loop and keyboard/mouse input and output functions. It's very easy to use and there is an Xcode GLUT project which you can copy and modify in the example projects directory. If you're starting out in OpenGL I can recommend GLUT.

The other way of course is to use Cocoa and Interface Builder. You can drop an OpenGL panel in a window. It's a bit more work but obviously has more potential than just using GLUT.

b e n
 

slooksterPSV

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
Make subclass of NSObject and define the field for the math operation to be shown and the operations such as + - and that, and link them to buttons to be called by the NSObject subclass's functions (IBAction) and then once you do that you can just have those actions call what you've already made or you can make it to where the user types it in and it runs all the stuff from main which then all you'd have to do is when the user presses enter on the textfield it shows the results in a label/text labbl so yeah that's how you could do it.
 

Fearless Leader

macrumors 68020
Original poster
Mar 21, 2006
2,360
0
Hoosiertown
Make subclass of NSObject and define the field for the math operation to be shown and the operations such as + - and that, and link them to buttons to be called by the NSObject subclass's functions (IBAction) and then once you do that you can just have those actions call what you've already made or you can make it to where the user types it in and it runs all the stuff from main which then all you'd have to do is when the user presses enter on the textfield it shows the results in a label/text labbl so yeah that's how you could do it.


Thanks much, thats really helpful
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.