All pastes #427672 Raw Edit

Something

public text v1 · immutable
·published 2007-04-06 22:44 UTC
rendered paste body
//super duper calculator program
//max levental


#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

//individual function declaration
void add(), subtract(), multiply(), divide();


int main(int argc, char *argv[])
{
	//variable declaration for main
    const int size(100);
    char choice, decision;
    
    
	//loop that actually controls the main menu
    do
    {
		//prompts user for input to select which function they would like to use
        cout << "\t\tMath Calculator";
        cout << "\n\nSelect one of the following functions:" << endl;
        cout << "A. Add" << endl;
        cout << "B. Subtract" << endl;
        cout << "C. Multiply" << endl;
        cout << "D. Divide" << endl;
        cout << "\nYour Choice : ";
        cin >> choice;
        system("CLS");

		//calls a function depending on user input
        switch ( choice )
        {
               case 'A': 
                    add();
                    break;
               case 'a':
                    add();
                    break;
               case 'B':
                    subtract();
                    break;
               case 'b':
                    subtract();
                    break;
               case 'C':
                    multiply();
                    break;
               case 'c':
                    multiply();
                    break;
               case 'D':
                    divide();
                    break;
               case 'd':
                    divide();
                    break;
               
       }

	   //outside of switch, kills or doesn't the loop
       cout << "Would you like to use another function (Y/N)?";
       cin >> decision;
       system("CLS");
    } while ( decision == 'Y' || decision == 'y' );  
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
void add()
{
	//variable declaration for add()
	//variables are initialized appropo to function
    int position(0), position2(0), length(0), i(0);
    double number(0), sum(0);
    string str1, str2;
    
	//stream declaration so you can do math with string
    stringstream stream;
    
    //prompts user for numbers, kills input when = is entered
    cout << "Please enter some numbers, enter = to sum them.\n\n";
    getline(cin, str1, '=');
    
	//primes string so parsing algorithm works
    str1.insert(0, 1, '0');
    str1.insert(1, 1, '\n');

	//length need to control while loop below
    int b(str1.length());
    
	//summing loop 
    while ( i <= b-3 )
          { 
			//finds first '\n' which is at position 1 because of insert method
            position = str1.find('\n', i);
            
            //finds the next '\n' after the one position found
            position2 = str1.find('\n', position+1);

			//determines length of entered number between '\n', -1 need because array  starts at 0
            length = (position2 - position -1);

			//creates another string with just the number in it
            str2 = str1.substr(position+1, length);
            stream.clear();

			//streams str2 into number
            stream << str2;
            stream >> number;

			//accumulates numbers
            sum += number;

			//bumps counter up
            i=position2;
            number = 0; 
            }
    //displays sum
	cout << "The sum is : "<< sum << endl;
    system("PAUSE");
    
}
void multiply()
{
	//variable declaration for add()
	//variables are initialized appropo to function
    int position(0), position2(0), length(0), i(0);
    double number(0), product(1);
    string str1, str2;
    
	//stream declaration so you can do math with string
    stringstream stream;
    
    //prompts user for numbers, kills input when = is entered
    cout << "Please enter some numbers, enter = to multiply them.\n\n";
    cin.ignore(2, '\n');
    getline(cin, str1, '=');

	//primes string so parsing algorithm works
    str1.insert(0, 1, '0');
    str1.insert(1, 1, '\n');

	//length need to control while loop below
    int b(str1.length());
    
	//mulitplying loop 
    while ( i <= b-3 )
          { 
		    //finds first '\n' which is at position 1 because of insert method
            position = str1.find('\n', i);

			//finds the next '\n' after the one position found
            position2 = str1.find('\n', position+1);

			//determines length of entered number between '\n', -1 need because array  starts at 0
            length = (position2 - position -1);

            //creates another string with just the number in it
            str2 = str1.substr(position+1, length);
            stream.clear();
            
            //streams str2 into number
            stream << str2;
            stream >> number;
            
            //system("PAUSE");
			//accumulates numbers
            product = (product*number);
            //bumps coutner up
			i=position2;
            number = 0; 
            }
    
	//displays product
    cout << "The product is :" << product << endl;
    system("PAUSE");
    
}
void subtract()
{

    //variable declaration for add()
	//variables are initialized appropo to function
    int position(0), position2(0), length(0), i(0);
    double number(0), difference(0);
    string str1, str2;
    
	//stream declaration so you can do math with string
    stringstream stream;
    
    //prompts user for numbers, kills input when = is entered
    cout << "Please enter some numbers, enter = to subtract them from the first number you entered.\n\n";
    
	//first numbers primes the dummy variable
	cin >> difference;
    cout << "-";

	//rest of the numbers go into the string
    cin.ignore(1, '\n');
    getline(cin, str1, '=');
    
	//primes string so parsing algorithm works
    str1.insert(0, 1, '0');
    str1.insert(1, 1, '\n');
    
	//length need to control while loop below
    int b(str1.length());
    
	//subtracting loop
    while ( i <= b-3 )
          { 
		    //finds first '\n' which is at position 1 because of insert method
            position = str1.find('\n', i);

            //finds the next '\n' after the one position found
            position2 = str1.find('\n', position+1);

            //determines length of entered number between '\n', -1 need because array  starts at 0
            length = (position2 - position -1);

			//creates another string with just the number in it
            str2 = str1.substr(position+1, length);
            stream.clear();

            //streams str2 into number
            stream << str2;
            stream >> number;

            //accumulates numbers
            difference = ( difference - number );

			//bumps counter up
            i=position2;
            number = 0; 
            }
    
	//displays difference
	cout << "The difference is :" << difference << endl;
    system("PAUSE");
    
}
void divide()
{
	//variable declaration for add()
	//variables are initialized appropo to function
    int position(0), position2(0), length(0), i(0);
    double number(0), quotient(1);
    string str1, str2;
    
    //stream declaration so you can do math with string
    stringstream stream;
    
    //prompts user for numbers, kills input when = is entered
    cout << "Please enter some numbers, enter = to divide them.\n\n";

    //first numbers primes the dummy variable
    cin >> quotient;
    cout << "divided by ";

	//rest of numbers go into string
    cin.ignore(1, '\n');
    getline(cin, str1, '=');

	//primes string so parsing works
    str1.insert(0, 1, '0');
    str1.insert(1, 1, '\n');

	//length needed to control loop below
    int b(str1.length());
   
    //dividing loop
    while ( i <= b-3 )
          { 
		    //finds first '\n'
            position = str1.find('\n', i);

			//finds second '\n'
            position2 = str1.find('\n', position+1);

			//determines length of number
            length = (position2 - position -1);

			//creates string with just the number in it
            str2 = str1.substr(position+1, length);
            stream.clear();

			//streams string into number
            stream << str2;
            stream >> number;

			//checks to make sure none of the numbers are zero
            while (number==0)
            {
                  cout << "Unpossible, Can't divide by Zero!!!";
                  cout << "\nTry again :";
                  cin >> number;
            }

			//accumulator
            quotient = (quotient*(1/number));
            i=position2;
            number = 0; 
            }
    // displays quotient
    cout << "The quotient is :" << quotient << endl;
    system("PAUSE");
    
}