1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include<iostream>
using namespace std;
// function prototypes
void intro() ;
void menu(int choice) ;
void get_fractions(int numerator1,int denominator1,int numerator2,int denominator2) ;
void addition(int& numerator1, int& denominator1, int& numerator2, int& denominator2, int& new_numberator, int& new_denominator) ;
void subtraction(int& numerator1, int& denominator1, int& numerator2, int& denominator2, int& new_numberator, int& new_denominator) ;
void multiplication(int& numerator1, int& denominator1, int& numerator2, int& denominator2, int& new_numberator, int& new_denominator) ;
void division(int& numerator1, int& denominator1, int& numerator2, int& denominator2, int& new_numberator, int& new_denominator) ;
void operations(int choice,int& numerator1, int& denominator1, int& numerator2, int& denominator2, int& new_numberator, int& new_denominator) ;
int main()
{
int choice, numerator1, denominator1, numerator2, denominator2, new_numberator, new_denominator ;
intro() ;
menu(choice) ;
get_fractions(numerator1, denominator1, numerator2, denominator2) ;
operations(choice, numerator1, denominator1, numerator2, denominator2, new_numberator, new_denominator) ;
system("PAUSE") ;
return 0;
} // Close main
// functions
void intro()
{
cout << "This program will add, multiply, subtract or divide fractions." << endl
<< endl ;
}
void menu(int choice)
{
cout << " What arithmetic operation would you like to perform?" << endl << endl ;
cout << " 1.) Multiplication" << endl
<< " 2.) Division" << endl
<< " 3.) Addition" << endl
<< " 4.) Subtraction" << endl << endl ;
cout << " Please enter menu number for operation desired: ";
cin >> choice ;
} // End menu function
void get_fractions(int numerator1,int denominator1,int numerator2,int denominator2)
{
char throw_away;
cout<<"Please enter your first fraction. Example 1/2: " ;
cin >> numerator1 >> throw_away >> denominator1 ;
cout<<"Please enter your second fraction. Example 1/2: " ;
cin >> numerator2 >> throw_away >> denominator2 ;
} // close fractions function
void addition(int numerator1, int denominator1, int numerator2, int denominator2, int new_numberator, int new_denominator) //function prototype for adding
{
new_numberator = (numerator1 * denominator2) + (numerator2 * denominator1) ;
new_denominator = denominator1 * denominator2 ;
cout << new_numberator << "/" << new_denominator ;
} // close addition function
void subtraction(int numerator1, int denominator1, int numerator2, int denominator2, int new_numberator, int new_denominator)//function prototype for adding
{
new_numberator = (numerator1 * denominator2) - (numerator2 * denominator1) ;
new_denominator = denominator1 * denominator2 ;
cout << new_numberator << "/" << new_denominator ;
} // close subtraction function
void multiplication(int numerator1, int denominator1, int numerator2, int denominator2, int new_numberator, int new_denominator)//function prototype for adding
{
new_numberator = numerator1 * numerator2 ;
new_denominator = denominator1 * denominator2 ;
cout << new_numberator << "/" << new_denominator ;
} // close multiplication function
void division(int numerator1, int denominator1, int numerator2, int denominator2, int new_numberator, int new_denominator)//function prototype for adding
{
// this takes the first fraction and multiplys it by the reciprical of the second fraction
new_numberator = numerator1 * denominator2 ;
new_denominator = denominator1 * numerator2 ;
cout << new_numberator << "/" << new_denominator ;
} // close division function
void operations(int choice,int numerator1, int denominator1, int numerator2, int denominator2, int new_numberator, int new_denominator)
{
switch(choice) //switch statement that allows user to enter a choice
{
case 1: multiplication(numerator1, denominator1, numerator2, denominator2, new_numberator, new_denominator) ;
break;
case 2: division(numerator1, denominator1, numerator2, denominator2, new_numberator, new_denominator) ;
break;
case 3: addition(numerator1, denominator1, numerator2, denominator2, new_numberator, new_denominator) ;
break;
case 4: subtraction(numerator1, denominator1, numerator2, denominator2, new_numberator, new_denominator) ;
break;
default:
cout << "Please enter a vailid operation." ;
break;
} // closes switch
} // closes operation function
|