Question

In: Computer Science

(Fraction calculator c++) | Write a program that lets the user perform arithmetic operations on fractions....

(Fraction calculator c++) | Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, in which _a_ and _b_ are integers and b ≠ 0. Your program must be menu driven, allowing the user to select the operation (+, –, *, or /) and input the numerator and denominator of each fraction. Furthermore, your program must consist of at least the following functions: Function menu: This function informs the user about the program’s purpose, explains how to enter data, and allows the user to select the operation. Function addFractions: This function takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.) Function subtractFractions: This function takes as input four integers representing the numerators and denominators of two fractions, subtracts the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.) Function multiplyFractions: This function takes as input four integers representing the numerators and denominators of two fractions, multiplies the fractions, and returns the numerators and denominators of the result. (Notice that this function has a total of six parameters.) Function divideFractions: This function takes as input four integers representing the numerators and denominators of two fractions, divides the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.) Some sample outputs are: 3 / 4 + 2 / 5 = 23 / 20 2 / 3 * 3 / 5 = 6 / 15 Your answer need not be in the lowest terms.

Solutions

Expert Solution

C++ program (fraction calculator):

#include <iostream>
using namespace std;

struct fraction // structure for fraction
{
int num; // numerator
int denom; // denominator
};

int gcd(int a, int b) // gcd to calculate lcm
{
if(b == 0)
return a;
return gcd(b,a%b);
}

struct fraction add(struct fraction f1, struct fraction f2) // adds two fractions
{
struct fraction f3;
f3.denom = (f1.denom/gcd(f1.denom,f2.denom)) * f2.denom; // lcm of denominators
f3.num = f1.num*(f3.denom/f1.denom) + f2.num*(f3.denom/f2.denom);
return f3;
}

struct fraction sub(struct fraction f1, struct fraction f2) // subracts two fractions
{
struct fraction f3;
f3.denom = (f1.denom/gcd(f1.denom,f2.denom)) * f2.denom;
f3.num = f1.num*(f3.denom/f1.denom) - f2.num*(f3.denom/f2.denom);
return f3;
}

struct fraction mul(struct fraction f1, struct fraction f2) //multiplies two fractions
{
struct fraction f3;
f3.num = f1.num*f2.num;
f3.denom = f1.denom*f2.denom;
  
return f3;
}

struct fraction division(struct fraction f1, struct fraction f2) // divides two fractions
{
struct fraction f3;
f3.num = f1.num*f2.denom;
f3.denom = f1.denom*f2.num;

return f3;
}

int main()
{
struct fraction f1,f2,f3;
int op;

cout<<"Enter fraction1 numerator and denominator : ";
cin>>f1.num>>f1.denom;
cout<<"Enter fraction2 numerator and denominator : ";
cin>>f2.num>>f2.denom;
  
cout<<"\n\nFraction Calculator Menu \n"; // Menu driven program
cout<<"------------------------- \n";
cout<<"1. Addition\n";
cout<<"2. Subraction\n";
cout<<"3. Multiplication\n";
cout<<"4. Division\n\n";
  
cout<<"Choose a option 1/2/3/4 from menu : ";
cin>>op;
  
switch(op)
{
case 1:
f3 = add(f1,f2);
cout<<f1.num<<"/"<<f1.denom<<" + "<<f2.num<<"/"<<f2.denom<<" = "<<f3.num<<"/"<<f3.denom<<endl;
break;
case 2:
f3 = sub(f1,f2);
cout<<f1.num<<"/"<<f1.denom<<" - "<<f2.num<<"/"<<f2.denom<<" = "<<f3.num<<"/"<<f3.denom<<endl;
break;
case 3:
f3 = mul(f1,f2);
cout<<f1.num<<"/"<<f1.denom<<" * "<<f2.num<<"/"<<f2.denom<<" = "<<f3.num<<"/"<<f3.denom<<endl;
break;
case 4:
f3 = division(f1,f2);
cout<<f1.num<<"/"<<f1.denom<<" / "<<f2.num<<"/"<<f2.denom<<" = "<<f3.num<<"/"<<f3.denom<<endl;
break;
}
  
return 0;
}

Output :


Related Solutions

Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions.
Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a / b, in which a and b are integers and b != 0.Your program must:be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numerator and denominator of each fraction.The input must be validated for correct operation (+, -, *, or /) and b != 0. If any of these cases are...
Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and...
Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and division. This program prompts user to enter data in the following order: First data is the number, second data is an operator and the third data is the number. You are required to: Write a function for addition. Write a function for subtraction. Write a function for multiplication. Write a function for division. Write a main program which calls four functions based on the...
Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
C++ Please For this assignment, you will write a program that lets the user play against...
C++ Please For this assignment, you will write a program that lets the user play against the computer in a variation of the popular blackjack car game. In this variation of the game, two-six sided dice are used instead of cards. The dice are rolled, and the player tries to beat the computer's hidden total without going over 21. Here are some suggestions for the game's design: Each round of the game is performed as an iteration of a loop...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
Time Calculator ( PROGRAM VBA EXCEL) Create a application that lets the user enter a number...
Time Calculator ( PROGRAM VBA EXCEL) Create a application that lets the user enter a number of seconds and produces output according to the following criteria: There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT