/*
Write a program which continueously calls a menu function which prints the following menu and returns to main the operation selected:
1.Add
2.Subtract
3.Multiply
4.Divide
5.Compare
6.Quit
The menu function must keep printing the menu and asking for a selection, as long as the user enters an invalid entry.
Then, once a valid selection is made, it returns the selection to main.
If the selection is not to quit, main reads two fractions such as 1/2 and 3/4, and as long as the denominator is not 0, passes the
selected operation (1-4) as well as the numerator and denominator of both fractions entered to a perform function which performs
the operation (i.e: add, subtract, multiply or divide). If the operation selected by the user is 5 (compare), it passes the numerator
and denominator of both fractions to compare function which returns 0 if they are equal, 1 if the first fraction is greater than
the second, and -1 if the second one is greater than the first. Then, using the returned value, main prints which fraction is greater
or that they are equal.
After performing the selected operation, the perform function passes the resulting fraction (numerator and denominator)
to reduce function to reduce the fraction. For example, if the fraction passed to reduce is 6/8, it reduces it to 3/4. The reduce
function in turn passes the numerator and denominator of the reduced fraction to print function which prints the fraction.
The program continues until the user selects quit from the menu.
So, you must write the following functions:
main - calls the menu function and gets back the selection; and if the selection is not to quit, it then reads two fractions
and passes them to the perform function.
menu - prints the menu and returns the selection to main.
perform - receives the selection and two fractions from main, performs the operation and passes the resulting fraction to reduce function.
reduce - receives the fraction from perform, reduces it and passes the reduced fraction to print for printing.
print - receives the reduced fraction from reduce and prints it.
compare - receives two fractions from main and returns 0, 1 or -1.
The following is a sample run of the program:
Select an operation from the following menu: [This is done by the menu function]
1.Add
2.Subtract
3.Multiply
4.Divide
5.Compare
6.Quit
Enter your selection: 1
Enter two fractions (e.g.: 1/2 3/4) 1/4 1/2 [This is done in main]
1/4 + 1/2 = 3/4
[Addition is done in perform function, reduction in reduce and printing in print function]
Select an operation from the following menu:
1.Add
2.Subtract
3.Multiply
4.Divide
5.Compare
6.Quit
Enter your selection: 5
Enter two fractions (e.g.: 1/2 3/4): 3/7 9/23
3/7 is larger.
Select an operation from the following menu:
1.Add
2.Subtract
3.Multiply
4.Divide
5.Compare
6.Quit
Enter your selection: 6
Press any key to continue.
*/
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
void addition();
void subtraction();
void multiplication();
void division();
void comparison();
void reduce();
void print();
void menu();
int choice,sum;
int n1,d1,n2,d2,n3,d3;
char slash;
int main()
{
do
{
menu();
cin>>choice;
while(choice<1||choice>6)
{
cout<<endl;
cout<<"Please enter 1,2,3,4,5 or 6: ";
cin>>choice;
}
if(choice!=6)
{
cout<<"\n\nEnter the first fraction: ";
cin>>n1>>slash>>d1;
cout<<"\nEnter the second fraction: ";
cin>>n2>>slash>>d2;
while(choice<=4&&choice>=1)
{
while(d1==0||d2==0)
{
cout<<"\n\tYou cannot have a '0' as a denominator!\n";
cout<<"\nEnter the first fraction: ";
cin>>n1>>slash>>d1;
cout<<"\nEnter the second fraction: ";
cin>>n2>>slash>>d2;
}
}
}
switch(choice)
{
case 1:addition();
break;
case 2:subtraction();
break;
case 3:multiplication();
break;
case 4:division();
break;
//case 5:comparison();
//break;
}
}while(choice!=6);
cout<<endl<<endl;
cout<<"Program is ending...\n\n";
system("pause");
return 0;
}
void menu()
{
cout<<"\n\t\t\t\tMath Function(s) Menu\n\n";
cout<<"1. Add\n";
cout<<"2. Subtract\n";
cout<<"3. Multiply\n";
cout<<"4. Divide\n";
cout<<"5. Compare\n";
cout<<"6. Quit\n";
cout<<"\n\tEnter your choice: ";
}
void addition()
{
n3=(n1*d2)+(n2*d1);
d3=d1*d2;
}
void subtraction()
{
n3=(n1*d2)-(n2*d1);
d3=d1*d2;
}
void multiplication()
{
n3=n1*n2;
d3=d1*d2;
}
void division()
{
n3=(n1*d2);
d3=(n2*d1);
}
/*void comparison()
{
float answer;
if(subtraction((sub>0)))
answer=1;
if(subtraction((sub==0)))
answer=0;
if(subtraction((sub<0)))
answer=-1;
*///}
void reduce()
{
for(int i=1;i<=d3;i++)
if(n3%i==0 && d3%i==0)
{
n3=n3/i;
d3=d3/i;
}
}
void print()
{
cout<<n3<<slash<<d3;
}
newCoder1545 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.