Code:
//Fraction Math by homeyg
#include <iostream>
using namespace std;
//function prototypes
void displayAdd(int num1, int num2, int den1, int den2);
void displaySub(int num1, int num2, int den1, int den2);
void displayMul(int num1, int num2, int den1, int den2);
void displayDiv(int num1, int num2, int den1, int den2);
int denomChange(int run1, int run2);
int denomChangeMul(int run1, int run2);
int denomChangeDiv(int rise, int run);
int numChangeAdd(int rise1, int rise2, int run1, int run2);
int numChangeSub(int rise1, int rise2, int run1, int run2);
int numChangeMul(int rise1, int rise2);
int numChangeDiv(int rise, int run);
int main()
{
//array variable declarations (array is used to perform a check to see if one of the four operators was input
int arrayCount = 4;
char opArray[4];
opArray[0] = '*';
opArray[1] = '/';
opArray[2] = '+';
opArray[3] = '-';
bool check;
//variable declarations for the numerators and denominators, operation input, and for the loop control
int cont;
int num1;
int denom1;
int num2;
int denom2;
char operation;
cout << "This program performs the four main mathematical operations on fractions, nothing fancy."<<endl
<< "This program will not display mixed numbers, only improper fractions."<<endl
<< "It will also not simplify the answers."<<endl;
//main loop
while(cont!=0)
{
//operation input
cout << "What operation do you want to do (*,/,+,-)? ";
cin >> operation;
//test to see if one of the four valid operators is used
//if it is not, the main loop starts over
for(int ind = 0;ind<arrayCount; ind++)
{
if(operation == opArray[ind])
{
check = true;
break;
}
if(operation != opArray[ind])
{
check = false;
}
}
if(check == false)
{
cout<<"Invalid operator."<<endl;
continue;
}
//numerator and denominator input section
cout << "Enter the numerator and denominator of the first fraction to be performed on."<<endl;
cout << "Numerator: ";
cin >> num1;
cout << "Denominator: ";
cin >> denom1;
cout << num1 <<"/"<<denom1;
cout << "\nEnter the numerator and denominator of the second fraction to be performed on."<<endl;
cout << "Numerator: ";
cin >> num2;
cout << "Denominator: ";
cin >> denom2;
cout << num2 <<"/"<<denom2;
//the function corresponding to the operator is called
switch(operation)
{
case '+' : displayAdd(num1, num2, denom1, denom2);
break;
case '-' : displaySub(num1, num2, denom1, denom2);
break;
case '*' : displayMul(num1, num2, denom1, denom2);
break;
case '/' : displayDiv(num1, num2, denom1, denom2);
break;
}
//test if you want to continue
cout << "Press any number to continue and 0 to quit. ";
cin >> cont;
}
return 0;
}
//display functions
/////////////////////////////////////////////
void displayAdd(int num1, int num2, int den1, int den2)
{
cout << "\nThe answer is: "
<< numChangeAdd(num1, num2, den1, den2) <<"/"<<denomChange(den1, den2)<<endl;
}
void displaySub(int num1, int num2, int den1, int den2)
{
cout << "\nThe answer is: "
<< numChangeSub(num1, num2, den1, den2) <<"/"<<denomChange(den1, den2)<<endl;
}
void displayMul(int num1, int num2, int den1, int den2)
{
cout << "\nThe answer is: "
<< numChangeMul(num1, num2) <<"/"<<denomChangeMul(den1, den2)<<endl;
}
void displayDiv(int num1, int num2, int den1, int den2)
{
cout << "\nThe answer is: "
<< numChangeDiv(num1, den2) <<"/"<<denomChangeDiv(num2, den1)<<endl;
}
//denominator calculation functions
/////////////////////////////////////////////
int denomChange(int run1, int run2)
{
if(run1!=run2)
return run1 * run2;
if(run1==run2)
return run1;
}
int denomChangeMul(int run1, int run2)
{
return run1 * run2;
}
int denomChangeDiv(int rise, int run)
{
return rise * run;
}
//numerator calculation functions
/////////////////////////////////////////////
int numChangeAdd(int rise1, int rise2, int run1, int run2)
{
if(run1!=run2)
{
int firstNum = rise1 * run2;
int secondNum = rise2 * run1;
return firstNum + secondNum;
}
if(run1==run2)
return rise1 + rise2;
}
int numChangeSub(int rise1, int rise2, int run1, int run2)
{
if(run1!=run2)
{
int firstNum = rise1 * run2;
int secondNum = rise2 * run1;
return firstNum - secondNum;
}
if(run1==run2)
return rise1 - rise2;
}
int numChangeMul(int rise1, int rise2)
{
return rise1 * rise2;
}
int numChangeDiv(int rise, int run)
{
return rise * run;
}
Do any of you remember the first program you wrote unnassisted? I did write a smaller program before, but it had many problems before it finally worked. I think I had to resolve to the forums to solve it!