67 272
Assignments Done
99,1%
Successfully Done
In November 2018

Answer to Question #27495 in C++ for red rose

Question #27495
A fraction-handling program contains this menu:
A. Add two fractions
B. Convert a fraction to decimal
C. Multiply two fractions
Q. Quit
a.Write C++ code for the program with stub functions for the choices.
b.Insert the Fraction To Decimal()function from Questions into the code with
commands to pass and display t he parameters.
Expert's answer
#include <iostream>
using namespace std;

double FractionToDecimal (double numerator, double denominator) {
return denominator == 0 ? 0 : numerator / denominator; //it's bad to divide by zero
}

double Add() {
double num1;
double num2;
double den1;
double den2;

cout << "Enter 1st fraction\nnum: ";
cin >> num1;
cout << "den: ";
cin >> den1;
cout << "Enter 2st fraction\nnum: ";
cin >> num2;
cout << "den: ";
cin >> den2;
return (num1*den2 + num2*den1) / (den1*den2);
}

double Multiply() {
double num1;
double num2;
double den1;
double den2;
//stub func
//do input somewhere for fractions
return (num1*num2) / (den1*den2);
}

using namespace std;

int main() {
& cout << "A. Add two fractions\n"
"B. Convert a fraction to decimal\n"
"C. Multiply two fractions\n"
"Q. Quit\n";
& bool ok_choice = false;
& while (ok_choice != true) {
char choice;
cout << "Please enter your choice: ";
cin >> choice;
//in OK cases you can insert functions that you need
switch (choice) {
case 'A':
cout << "Add : " << Add() << endl;
ok_choice = true;
break;

case 'B':
cout << "ToDecimal: " << FractionToDecimal(1.24, 2.33) << endl; //stub
ok_choice = true;
break;
case 'C':
Multiply();
ok_choice = true;
break;
case 'Q':
cout << "Quit\n";
ok_choice = true;
break;
default:
cout << "Bad choice! Try another one\n";
break;
}
& }
& return 0;
}

/*
& * some tip:
& * it's better to use some structs for fractions, or even classes,
& * i.e
& * struct Fraction {
& * double numenator;
& * double denumenator;
& * double toDecimal() {
& * return denominator == 0 ? 0 : numerator / denominator;
& * }
& *
& * //as you can see these function return Fraction as a return-type
& * Fraction addFraction(const Fraction another_fraction);
& * Fraction multilplyFraction(const Fraction another_fraction);
& *
& * //even more, you can override some operands! such as + and *
& * //and then you can printf something like this:
& * // Fraction a,b;
& * // cout << a+b; //it's great
& * operator +(const Fraction another_fraction);
& * operator *(const Fraction another_fraction);
& * operator <<(const Fraction another_fraction); //for output
& *
& * //output fraction
& * void printFraction();
& * };
& *
& */

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions