cout un-reduced fractions??

Hi everyone. I am writing a program that is meant to do fractions. The program is simple enough because its a simple redirection for an input file which will provide the program with data.

So basically we just create a program that will so the arthmetic and redirect it to a file that will input the data.



My question is how do I write the program so that the fraction will come back in an un-reduced form.


For example the problem 3/5 + 1/5 would be 4/5 however the teacher wants it in an un-reduced form so the answer would come out looking like = 20/25

Another example. Un-reduced form = 12/10 - 1/3 = 26/30



This is what I have written so far. I know how to re-direct and everything, i just need to figure out how to put the answer in a un-reduced form.




#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
int numerator1, numerator2;
int denominator1, denominator2;
int answer1;

cout << "*********** Assignment #7 Section #1003" << endl;
cin >> numerator1;
cin >> denominator1;
cin >> numerator2;
cin >> denominator2;
answer1 = (numerator1 / denominator1) + (numerator2 / denominator2);
cout << numerator1 << "/" << denominator1 << " + " << numerator2 << "/" << denominator2 << " = " << answer1 << endl;
return 0;
}



This is basically the result:

-bash-4.1$ ./a.out *************
************* Assignment #7 Section #1003
3 //input
5 //input
1 //input
5 //input
3/5 + 1/5 = 0 // cout statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    int num1 = 3 ;
    int den1 = 10 ;
    int num2 = 1 ;
    int den2 = 15 ;

    // std::cin >> ...

    const int num_ans_plus = num1*den2 + num2*den1 ;
    const int num_ans_minus = num1*den2 - num2*den1 ;
    const int den_ans = den1 * den2 ;

    std::cout << num1 << '/' << den1 << " + " << num2 << '/' << den2
               << " == " << num_ans_plus << '/' << den_ans << '\n' ;

    std::cout << num1 << '/' << den1 << " - " << num2 << '/' << den2
               << " == " << num_ans_minus << '/' << den_ans << '\n' ;
}
I'm not sure I understand.

I think what I need is multiple if statements.

is there a way to do a continuous if statement, as to say, the program will keep searching for inputs to calculate the output. Once it has calculated output, it will once again search for an input that the user may enter. Is there a way to loop that?

There are a bunch of other if statements such as:

- if fraction is improper, convert to a whole number, or mixed number.

- if the result cannot not be calculated display problem in error message.


let me just write what the program is supposed to do:

write a C++ program to perform simple fraction problems. Must be able to add, subtract, multiply, and divide. dividing by0 will result in error message.

Program should be designed to read a series of of problems, 1 pr line. from redirected file.

Input: Numerator/denominator operator numerator/denominator
Last edited on
> the program will keep searching for inputs to calculate the output.
> Once it has calculated output, it will once again search for an input that the user may enter.
> Is there a way to loop that?

Use a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    int num1, den1, num2, den2 ;
    char oper ;
    char slash ;
    
    // loop till there is nothing left to read 
    // Input: Numerator/denominator operator numerator/denominator
    while( std::cin >> num1 >> slash >> den1 >> oper >> num2 >> slash >> den2 ) // read input
    {
        // validate input
        // calculate results
        // print results
    }
}
Last edited on
Ok I understand looping. if it's that easy I'm sure i won't have a problem.

How about the fraction result?

Could anybody help me with that?

Topic archived. No new replies allowed.