Need help with Rational Number Calculator with Fractions

Hello, so far this is the code I have for a rational number calculator but as of right now it is basically just a regular calculator. I need help getting input like 5 / 2 + 1 / 2 then outputting the result as a simplified fraction. So I'm not sure how to implement a function to handle this type of input as well as how to simply and output as a fraction. Then I need to be able to overload the operator but help on the above would be greatly appreciated. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <conio.h>

using namespace std;

class Rational
{
    private:
        float numInput;

    public:
        Rational(): numInput(0)
        {}

        void getValues()
        {           
            cout << "Enter number: ";
            cin >> numInput;
        }

        void showValues()
        {
            cout << numInput << endl;
        }

        Rational operator + (Rational) const;
        Rational operator - (Rational) const;
        Rational operator * (Rational) const;
        Rational operator / (Rational) const;
};

Rational Rational::operator + (Rational arg2) const
{
    Rational temp;
    temp.numInput = numInput + arg2.numInput;
    return temp;
}

Rational Rational::operator - (Rational arg2) const
{
    Rational temp;
    temp.numInput = numInput - arg2.numInput;
    return temp;
}

Rational Rational::operator * (Rational arg2) const
{
    Rational temp;
    temp.numInput = numInput * arg2.numInput;
    return temp;
}

Rational Rational::operator / (Rational arg2) const
{
    Rational temp;
    temp.numInput = numInput / arg2.numInput;
    return temp;
}

int main()
{
    Rational mathOb1, mathOb2, outputOb;
    int choice;
    mathOb1.getValues();
    cout << "First number entered: ";
    mathOb1.showValues();
    cout << endl;
	cout << "Enter operator: + = 1, - = 2, * = 3, / = 4  ";
    cin >> choice;
    cout << endl;
    mathOb2.getValues();
    cout << "Second number entered: ";
    mathOb2.showValues();    cout << endl;

    switch (choice)
    {
        case '+':
            outputOb = mathOb1 + mathOb2;
            break;
        case 2:
            outputOb = mathOb1 - mathOb2;
            break;
        case 3:
            outputOb = mathOb1 * mathOb2;
            break;
        case 4:
            outputOb = mathOb1 / mathOb2;
            break;
        default:
            cout << "Invalid choice! " << endl;
    }
	
    cout << "Answer: ";
    outputOb.showValues();
    cout << endl;
	
	return 0;
}
Last edited on
It looks like ratio is pretty new. It doesn't work with my compiler. Is there a similar way of using ratio that would be more widely supported?
<ratio> is new, from C++11.
which compiler are you using?
I'm not sure. I have to be able to compile it on a linux server which is not using the C++ 11 compiler.
you can compile online >>> http://cpp.sh/

wandbox : several languages and compiler, doesn't take user input:
http://melpon.org/wandbox/
I know, but in order to submit the assignment, the program must compile on a linux server for class, so I'll need to use another method than ratio unfortunately.
ok. then you need,
1
2
3
private:
int num;
int denom;

in your class.
Last edited on
Topic archived. No new replies allowed.