All pastes #412694 Raw Edit

Untitled

public text v1 · immutable
#412694 ·published 2007-03-27 21:50 UTC
rendered paste body
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void add(), subtract(), multiply(), divide();


int main(int argc, char *argv[])
{
    const int size(100);
    char choice, decision;
    double number;
    
    do
    {
        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");
        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;
               
       }
       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()
{
    int position(0), position2(0), length(0), i(0);
    double number(0), sum(0);
    string str1, str2;
    
    stringstream stream;
    
    
    cout << "Please enter some numbers, enter = to sum them.\n\n";
    getline(cin, str1, '=');
    str1.insert(0, 1, '0');
    str1.insert(1, 1, '\n');
    int b(str1.length());
     
    while ( i <= b-3 )
          {  
            position = str1.find('\n', i);
            
            
            position2 = str1.find('\n', position+1);
            length = (position2 - position -1);
            str2 = str1.substr(position+1, length);
            stream.clear();
            stream << str2;
            stream >> number;
            sum += number;
            i=position2;
            number = 0; 
            }
    
	cout << "The sum is : "<< sum << endl;
    system("PAUSE");
    
}
void multiply()
{
    int position(0), position2(0), length(0), i(0);
    double number(0), product(1);
    string str1, str2;
    
    stringstream stream;
    
    
    cout << "Please enter some numbers, enter = to multiply them.\n\n";
    getline(cin, str1, '=');
    str1.insert(0, 1, '0');
    str1.insert(1, 1, '\n');
    int b(str1.length());
    
    while ( i <= b-3 )
          {  
            position = str1.find('\n', i);
            position2 = str1.find('\n', position+1);
            length = (position2 - position -1);
            str2 = str1.substr(position+1, length);
            stream.clear();
            stream << str2;
            stream >> number;
            product = (product*number);
            i=position2;
            number = 0; 
            }
    
    cout << "The product is :" << product << endl;
    system("PAUSE");
    
}
void subtract()
{
    int position(0), position2(0), length(0), i(0);
    double number(0), difference(0);
    string str1, str2;
    
    stringstream stream;
    
    
    cout << "Please enter some numbers, enter = to subtract them from the first number you entered.\n\n";
    cin >> difference;
    cout << "-";
    cin.ignore(1, '\n');
    getline(cin, str1, '=');
    
    str1.insert(0, 1, '0');
    str1.insert(1, 1, '\n');
    int b(str1.length());
    
    while ( i <= b-3 )
          {  
            position = str1.find('\n', i);
            position2 = str1.find('\n', position+1);
            length = (position2 - position -1);
            str2 = str1.substr(position+1, length);
            stream.clear();
            stream << str2;
            stream >> number;
            difference = ( difference - number );
            i=position2;
            number = 0; 
            }
    
	cout << "The difference is :" << difference << endl;
    system("PAUSE");
    
}
void divide()
{
    int position(0), position2(0), length(0), i(0);
    double number(0), quotient(1);
    string str1, str2;
    
    stringstream stream;
    
    
    cout << "Please enter some numbers, enter = to divide them.\n\n";
    cin >> quotient;
    cout << "divided by ";
    cin.ignore(1, '\n');
    getline(cin, str1, '=');
    str1.insert(0, 1, '0');
    str1.insert(1, 1, '\n');
    int b(str1.length());
   
    while ( i <= b-3 )
          {  
            position = str1.find('\n', i);
            position2 = str1.find('\n', position+1);
            length = (position2 - position -1);
            str2 = str1.substr(position+1, length);
            stream.clear();
            stream << str2;
            stream >> number;
            while (number==0)
            {
                  cout << "Unpossible, Can't divide by Zero!!!";
                  cout << "\nTry again :";
                  cin >> number;
            }
            quotient = (quotient*(1/number));
            i=position2;
            number = 0; 
            }
    
    cout << "The quotient is :" << quotient << endl;
    system("PAUSE");
    
}