Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[C++] Calculator V2

Joined
Aug 4, 2010
Messages
572
Reaction score
177
Making a calculator using C++. I'm going to add more features such as addition, subtraction, and division. So far I just added 1 component in this calculator.

Since this is not strictly a tutorial but a reference, I will just use a freeway of explaining whats going on.


UPDATE: 06/04/11 08:09 // This is the last Update for this.
Features:
  • Multiplication
  • Division
  • Addition
  • Subtraction
  • Multiplication Chart ( Allows user to view the entire chart with input
  • Division Chart ( Allows user to view the entire chart with input
  • Subtraction Chart ( Allows user to view the entire chart with input
  • Addition Chart ( Allows user to view the entire chart with input
  • Completely working Menu for selections
  • Time/Date Support

Code:
#include <cstdlib>
#include <iostream>
using namespace std;

void MultiplicationChart() {
    int x;
    int y;
    
        int array[13][13];
    
        for ( x = 0; x < 13; x ++ ) {
        for ( y = 0; y < 13; y ++ )
        array[x][y] = x * y;
    }
    
    cout <<"Multiplication Chart: \n";
    for ( x = 0; x < 13; x ++ ) {
        for ( y =0; y < 13; y ++ )
        cout <<"["<<x<<"]["<<y<<"]="<< array[x][y] << " ";
        cout <<"\n";
        system("pause");
    }
}

void DivisionChart() {
    int x;
    int y;
    
        int array[13][13];
    
        for ( x = 0; x < 13; x ++ ) {
        for ( y = 0; y < 13; y ++ )
        array[x][y] = x / y;
    }
    
    cout <<"Division Chart: \n";
    for ( x = 0; x < 13; x ++ ) {
        for ( y =0; y < 13; y ++ )
        cout <<"["<<x<<"]["<<y<<"]="<< array[x][y] << " ";
        cout <<"\n";
        system("pause");
    }
    
}

void SubtractionChart() {
    int x;
    int y;
    
        int array[13][13];
    
        for ( x = 0; x < 13; x ++ ) {
        for ( y = 0; y < 13; y ++ )
        array[x][y] = x - y;
    }
    
    cout <<"Subtraction Chart: \n";
    for ( x = 0; x < 13; x ++ ) {
        for ( y =0; y < 13; y ++ )
        cout <<"["<<x<<"]["<<y<<"]="<< array[x][y] << " ";
        cout <<"\n";
        system("pause");
    }
    
}     

void AdditionChart() {
    int x;
    int y;
    
        int array[13][13];
    
        for ( x = 0; x < 13; x ++ ) {
        for ( y = 0; y < 13; y ++ )
        array[x][y] = x + y;
    }
    
    cout <<"Addition Chart: \n";
    for ( x = 0; x < 13; x ++ ) {
        for ( y =0; y < 13; y ++ )
        cout <<"["<<x<<"]["<<y<<"]="<< array[x][y] << " ";
        cout <<"\n";
        system("pause");
    }
    
}   

int main()
{
    int x;
    int y;
    int c;
    int b;

    char choice;
    for (;;){
     do {
    cout <<"\t\t\tThe Calculator 1.0";
    cout <<"\n\n\n1.Multiplication\n";
    cout <<"2.Division\n";
    cout <<"3.Addition\n";
    cout <<"4.Subtraction\n";
    cout <<"5.Time table charts";
    cout <<"\n6.Quit.";
    cout <<"\n\nSelection: "; 
    cin>>choice;
    } while ( choice < '1' || choice > '6' && choice != '6');
    if (choice == '6') break;
  
switch (choice) {

case '1' : 
    system("cls");
        cout <<"\t\t\tThe Multiplication Section.\n\n\n";
        cout << "What numbers would you like to multiply?\n";
        cout <<"Number: ";
        cin >> x;
        cout <<"\nX\n";
        cout <<"\nSecound Number: ";
        cin >> y;
        cin.ignore();
        cout <<"\nThe Answer is: " << x * y << "\n";
        system("pause");
        system("cls");
    break;

case '2' : 
        system("cls");
        cout <<"\t\t\tThe Division Section.\n\n\n";
        cout << "What numbers would you like to divide?\n";
        cout <<"\nFirst Number: ";
        cin >> c;
        cout <<"\n/\n";
        cout <<"\nSecound Number: ";
        cin >> b;    
        cout <<"The Answer is: " << c / b << "\n";
        cin.get();
        system("pause");
        system("cls");
    
    break;
  
case '3' :
    
        system("cls");
        cout <<"\t\t\tThe Addition Section.\n\n\n";
        cout << "What numbers would you like to add?\n";
        cout <<"\nFirst Number: ";
        cin >> c;
        cout <<"\n+\n";
        cout <<"\nSecound Number: ";
        cin >> b;    
        cout <<"The Answer is: " << c + b << "\n";
        cin.get();
        system("pause");
        system("cls");
    break;
 
case '4' :
        system("cls");
        cout <<"\t\t\tThe Subtraction Section.\n\n\n";
        cout << "What numbers would you like to subtract?\n";
        cout <<"\nFirst Number: ";
        cin >> c;
        cout <<"\n-\n";
        cout <<"\nSecound Number: ";
        cin >> b;    
        cout <<"The Answer is: " << c - b << "\n";
        cin.get();
        system("pause");
        system("cls");
    break;


case '5' :
int chart;    
    
cout <<"What chart would you like to display? \n";
cout <<"1.Multiplication\n";
cout <<"2.Division(Not Working)\n";
cout <<"3.Subtraction\n";
cout <<"4.Addition\n";
cout <<"Chart: ";
cin >> chart;
if ( chart == 1 ) {
    MultiplicationChart();

}

else if ( chart == 2 ) {
    DivisionChart();
}

else if ( chart == 3 ) {
    SubtractionChart();
}

else if ( chart == 4 ) {
    AdditionChart();
}


break;
}
}

return 0;


}

You can take the source code and make this calculator more advanced if you feel like.

-I removed old sources.
 
Last edited:
Joined
Oct 11, 2007
Messages
1,706
Reaction score
517
Re: [C++] Making a calculator

How about commenting the code a little more so newbies actually understand what the functions in the lines do? :p

Edit: oh and now when I looked @ the code, that wont work, you don't return any value in your main function.

Code:
#include <iostream>
using namespace std;

int mult ( int x, int y ){
        return x * y;
}

int main()
{
    int x,y;
    
    cout << "What numbers would you like to multiply?\n";
    cout <<"\nFirst Number: ";
    cin >> x;
    cout <<"\nX\n";
    cout <<"\nSecound Number: ";
    cin >> y;
    cin.ignore();
    cout <<"\nThe Answer is: " << mult ( x,y ) << "\n";
    cin.get();

	return 0;
}
 
Last edited:
Joined
Aug 4, 2010
Messages
572
Reaction score
177
Re: [C++] Making a calculator

How about commenting the code a little more so newbies actually understand what the functions in the lines do? :p

Edit: oh and now when I looked @ the code, that wont work, you don't return any value in your main function.

Thanks I will update it with on what the functions do now. Also I've tested the program and did not get errors.
 
Joined
Oct 11, 2007
Messages
1,706
Reaction score
517
Re: [C++] Making a calculator

Thanks I will update it with on what the functions do now. Also I've tested the program and did not get errors.

check my post, updated it, you have to keep in mind that just because something works in your compilator it might not work for others, and NOT adding something like a return value in a main loop will 100% not work for a shitload of people, also declaring the function before, then setting what it does after is just confusing to ppl :p.
 
Master Summoner
Joined
Jan 11, 2009
Messages
505
Reaction score
8
Re: [C++] Making a calculator

Code:
int lengthdir(double bufFirst, double bufSecond)
{
    cout << "To calculate length towards direction, requiring 2 inputs where each are coresponding coordinates from 0,0:" << endl;
    cout << "Enter the first input; x: \t";
    cin >> bufFirst;
    cout << "Enter the second input; y: \t";
    cin >>  bufSecond;
    cout << "\nThe length of x from origin towards direction: \t" << cos(bufFirst) << endl;
    cout << "The length of y from origin towards direction: \t" << sin(bufSecond) << endl;
    return 0;
}

i want to contribute something simpy dude
 
人◕ ‿‿ ◕人
Loyal Member
Joined
Jul 11, 2008
Messages
1,078
Reaction score
90
Re: [C++] Making a calculator

Why not have an introduction that you can choose what you want to do.

Still nice having the simplicity of using a calculator but still

a simple statement to use would be to do

Code:
cout << "What would you like to do\n";
cin >> input;
if ( input == + )
{
this...
} else if ( input == - )
{
this..
}
and so on.
 
Joined
Aug 4, 2010
Messages
572
Reaction score
177
Re: [C++] Making a calculator

Why not have an introduction that you can choose what you want to do.

Still nice having the simplicity of using a calculator but still

a simple statement to use would be to do

Code:
cout << "What would you like to do\n";
cin >> input;
if ( input == + )
{
this...
} else if ( input == - )
{
this..
}
and so on.

I could but I don't find being too simple well help with exploring with whats going on. I did add a introduction for people to choose what their looking for.

---------- Post added at 10:00 PM ---------- Previous post was at 09:35 PM ----------

update
 
Newbie Spellweaver
Joined
Oct 17, 2008
Messages
21
Reaction score
87
Re: [C++] Making a calculator

Use switch:

Code:
switch (selection) {

  case 1 : 
    Multiplication();
    break;

  case 2 : 
    Division();
    break;
  
  case 3 :
    Addition();
    break;
 
  case 4 :
    Subtraction();
    break;

}
 
Joined
Aug 4, 2010
Messages
572
Reaction score
177
Re: [C++] Making a calculator

Use switch:

Code:
switch (selection) {

  case 1 : 
    Multiplication();
    break;

  case 2 : 
    Division();
    break;
  
  case 3 :
    Addition();
    break;
 
  case 4 :
    Subtraction();
    break;

}

Yep I know, i'm editing it now with those codes and some others so theres a nice loop :)

---------- Post added at 01:53 PM ---------- Previous post was at 01:14 PM ----------

UPDATED Latest Log Above
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
Re: [C++] Making a calculator

Continuing on the previous post:

Code:
void Operation(string name, string action, char operator) {
   system("cls");
   cout <<"\t\t\tThe " << name << " Section.\n\n\n";
   cout << "What numbers would you like to " << action" << "?\n";
   cout <<"Number: ";
   cin >> x;
   cout <<"\nX\n";
   cout <<"\nSecound Number: ";
   cin >> y;
   cin.ignore();

   int answer = 0;
   switch (operator) {
     case '*':
       answer = x * y;
       break;
     case '/':
       answer = x / y;
       break;
     (and so on)
   }
   cout <<"\nThe Answer is: " << answer << "\n";
   system("pause");
   system("cls");
}

void Multiplication() {
  Operation("Multiplication", "multiply", '*');
}

void Division() {
  Operation("Division", "divide", '/');
}

(and so on..)

The point being, always avoid repetition. Usually if you find that you're repeating yourself, you're doing something wrong.

In reality I'd clean up the switch-case away from the Operation-function. A nice alternative would be going data-driven all the way, using the selected operation number as the index to an array of structs, each defining the name, action and a function pointer. An object-oriented choice, then, would be having a general Operation class and inheriting Multiplication, Division, etc. from it (each having the operate(x, y) -method).
 
Last edited:
Joined
Aug 4, 2010
Messages
572
Reaction score
177
Re: [C++] Making a calculator

Continuing on the previous post:

Code:
void Operation(string name, string action, char operator) {
   system("cls");
   cout <<"\t\t\tThe " << name << " Section.\n\n\n";
   cout << "What numbers would you like to " << action" << "?\n";
   cout <<"Number: ";
   cin >> x;
   cout <<"\nX\n";
   cout <<"\nSecound Number: ";
   cin >> y;
   cin.ignore();

   int answer = 0;
   switch (operator) {
     case '*':
       answer = x * y;
       break;
     case '/':
       answer = x / y;
       break;
     (and so on)
   }
   cout <<"\nThe Answer is: " << answer << "\n";
   system("pause");
   system("cls");
}

void Multiplication() {
  Operation("Multiplication", "multiply", '*');
}

void Division() {
  Operation("Division", "divide", '/');
}

(and so on..)

The point being, always avoid repetition. Usually if you find that you're repeating yourself, you're doing something wrong.

In reality I'd clean up the switch-case away from the Operation-function. A nice alternative would be going data-driven all the way, using the selected operation number as the index to an array of structs, each defining the name, action and a function pointer. An object-oriented choice, then, would be having a general Operation class and inheriting Multiplication, Division, etc. from it (each having the operate(x, y) -method).

I agree with this.

---------- Post added at 12:12 PM ---------- Previous post was at 11:39 AM ----------

Last update.
 
Last edited:
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Don't use system-dependent poop.

I made a some functions for you guys.. You can do anything using the add() function, so the other functions just use that. Division is using LONG type, not float. But you can get the remainder using the modulus operator, so everything is accurate. (tell me if you can prove otherwise, nobody's perfect..)
Code:
//math
	void add( long &a, long b );
	void subtract( long &a, long b );
	void multiply( long &a, long b );
	void exponent( long &a, long b );
	void divide( long &a, long b, bool modulus );

//Input Numbers & Operators
	void long_in( long &number );
	void operator_in( char &Char, long &a, long &b );

//Run calculation, return false when finished [ used as: while( calculate( my_number ) ); ]
	bool calculate( long &a );



void add( long &a, long b )
{
	// debug mode
	cout << a << " + " << b << " = ";
	
	// Simple addition
	a = a + b;
	
	// debug mode.. continued. thats it..
	// all other math functions use add()
	// except for certain efficiency short-cuts
	// in math.
	cout << a << "\n";
}

void subtract( long &a, long b )
{
	// Abstract Subtraction
	add( a, (-b) );
}

void multiply( long &a, long b )
{
	if( b == 0 || a == 0 )
	{
		// Anything 0 times is 0
		a = 0;
	} else if ( b < 0 )
	{
		// Negative Rules: (neg x neg = pos; pos x neg = neg)
		a = -a;
		b = -b;
	} else if ( a == 1 )
	{
		// anything 1 time is the same
		a = b;
		b = 1;
	}
	
	// copy of a
	long a_cpy = a;
	
	for( int i = 1; i < b; i++ )
	{
		// add a to itself b times
		add( a, a_cpy );
	}
}

void exponent( long &a, long b )
{
	if( b == 0 )
	{
		// Anything 0 times is 0
		a = 0;
	}
	
	// copy of a
	long a_cpy = a;
	
	for( int i = 1; i < b; i++ )
	{
		// multiply a to itself b times
		multiply( a, a_cpy );
	}
}

void divide( long &a, long b, bool modulus = false )
{
	if( b == a )
	{
		// any number divided by itself is 1
		a = 1;
		b = 1;
		
	} else if ( b < 0 )
	{
		// Negative Rules: (neg x neg = pos; pos x neg = neg)
		a = -a;
		b = -b;
		
	} else if( b == 0 )
	{
		// Division by 0 error... 
		// Because Zero goes into every number (even 0) infinite times
		cout << "Error!\n\n";
		a = 0;
	}
	
	// copy of b
	long b_cpy = b;
	
	//Times b goes into a.. yeh, I went there.
	int times_b_goes_into_a = 1;
	
	// If a is less than b, b goes into a 0 times
	// moduls is simply b - a
	if( a < b )
	{
		times_b_goes_into_a = 0;
	}
	
	// if b is 1, we have a better way than this loop
	if( b != 1 )
	{
		// How many times does b go into a?
		while( b < a )
		{
		
			// Add b to itself until it's greater than or equal to a.
			add( b, b_cpy );
			// Everytime b is less than a, b can go into a..
			// So increment this variable:
			times_b_goes_into_a++;
		}
	} else {// If b == 1
		// if 'b' is 1, it goes into 'a', 'a' times.
		// no need to loop 'a' times, we already know the answer is 'a'
		times_b_goes_into_a = a;
		b = a;
	}
	
	// Modulus sets the remainder, 
	if( modulus )
	{//Do modulus
		// in the while loop b is equal to or greater than a.
		subtract( b, a ); 
		
		// Well, 'b' is the remainder.. but 'a' is our reference variable..
		// So set 'a' to 'b'. (the remainder is the modulus; two functions in one)
		a = b; // the modulus
	} else {// Do Division
		// This is how many times b can go into a; division.
		a = times_b_goes_into_a; // It does NOT include the remainder, which can be 
					// retrieved from the modulus setting in this function.
	}
}
 
Back
Top