Mixed Number Program

I have a program which allows the user to input mixed numbers such as fractions and performs addition, subtraction, division, multiplication operations.

However, I have to modify the program such that it will take negative numbers and the ability to raise x to the y
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <cstdlib>
#include <sstream>
#include <string>


using namespace std;

struct mixedNumber
{
    int whole, num, denom;

    void output();

};

void mixedNumber::output()
{
    if(whole == 0)
        if(num == 0)
            cout<<0;
        else
            cout<<num<<"/"<<denom;
   else
        if(num == 0)
            cout<<whole;
        else
            cout<<whole<<" "<<num<<"/"<<denom;
}

bool input(string &line);
void process(string line);
int gcd(int p, int q);
void reduce(mixedNumber &number);
mixedNumber add(mixedNumber x, mixedNumber y);
mixedNumber mult(mixedNumber x, mixedNumber y);
mixedNumber divide(mixedNumber x, mixedNumber y);
mixedNumber subtract(mixedNumber x, mixedNumber y);
char getOperator(string line, int &pos);
void getOperands(const string &line, mixedNumber &x, mixedNumber &y, int pos);
mixedNumber convertToMixedNumber(string line);
void trim(string &line);

int main()
{
    string line;
    while(input(line))
        process(line);
    return 0;
}

bool input(string &line)
{
    cout<<"Input: ";
    getline(cin,line);
    return line != "";
    /*
      The above is identical, in result, to:

      if(line != "")
        return true;
      else
        return false;

      which is also the same result of:

      return line != "" ? true : false;
    */
}

void getOperands(const string &line, mixedNumber &x, mixedNumber &y, int pos)
{
    string first, second;
    first = line.substr(0, pos);
    second = line.substr(pos+3);
    x = convertToMixedNumber(first);
    y = convertToMixedNumber(second);
}

void trim(string &line)
{
    while(line[0] == ' ')
        line.erase(0,1);
}

mixedNumber convertToMixedNumber(string line)
{
    stringstream ss;
    char junk;
    mixedNumber answer;
    trim(line);
    int pos = line.find(' '),
        pos2 = line.find('/');
    if(pos < string::npos && pos2 < string::npos) //have a true mixed number!
    {
        ss << line;
        ss>> answer.whole>>answer.num>>junk>>answer.denom;
    }
    else
        if(pos2 >= string::npos)
        {
            ss<<line;
            ss>>answer.whole;
            answer.num = 0;
            answer.denom = 1;
        }
        else
        {
            ss<<line;
            ss>>answer.num>>junk>>answer.denom;
            answer.whole = 0;
        }
    return answer;
}


char getOperator(string line, int &pos)
{
    string choices[4] = {" + ", " - ", " * ", " / "};
    for(int i = 0; i < 4; i++)
        if( (pos = line.find(choices[i])) < string::npos)
            break;
    return pos < string::npos ? line[pos+1] : '\0';
}


void process(string line)
{
    mixedNumber one, two, answer;
    int pos;
    char op = getOperator(line, pos);
    if(op == '\0')
    {
        cout<<"No operator found! Please re-etner"<<endl;
        return;
    }
    getOperands(line, one, two, pos);
    switch(op)
    {
        case '+' : answer = add(one, two);
                   break;

        case '-' : answer = subtract(one,two);
                   break;

        case '*' : answer = mult(one,two);
                   break;

        case '/' : answer = divide(one,two);
    }
    cout<<line<<" = ";
    answer.output();
    cout<<endl;
}

int gcd(int p, int q)
{
    return q==0 ? p : gcd(q, p%q);
}

void reduce(mixedNumber &number)
{
    int newNum = number.denom*number.whole + number.num,
        newDenom = number.denom,
        divisor = gcd(newNum,newDenom );
    newNum /= divisor;
    newDenom /= divisor;
    number.whole = newNum/newDenom;
    number.num = newNum %newDenom;
    number.denom = newDenom;
}

mixedNumber add(mixedNumber x, mixedNumber y)
{
    mixedNumber answer;
    answer.whole = 0;
    answer.num = y.denom*(x.denom*x.whole + x.num) +  x.denom*(y.denom*y.whole + y.num);
    answer.denom = x.denom * y.denom;
    reduce(answer);
    return answer;
}

mixedNumber mult(mixedNumber x, mixedNumber y)
{
    mixedNumber answer;
    answer.whole = 0;
    answer.num = (x.denom*x.whole + x.num) * (y.denom*y.whole + y.num);
    answer.denom = x.denom * y.denom;
    reduce(answer);
    return answer;
}


mixedNumber divide(mixedNumber x, mixedNumber y)
{
    mixedNumber answer;
    answer.whole = 0;
    answer.num = (x.denom*x.whole + x.num) * y.denom ;
    answer.denom = x.denom * (y.denom*y.whole + y.num);
    reduce(answer);
    return answer;
}

mixedNumber subtract(mixedNumber x, mixedNumber y)
{
    mixedNumber answer;
    answer.whole = 0;
    answer.num = y.denom*(x.denom*x.whole - x.num) +  x.denom*(y.denom*y.whole + y.num);
    answer.denom = x.denom * y.denom;
    reduce(answer);
    return answer;
}
Can anyone give me any input on how to modify the program in order to take in negative numbers?
Any help? I am at a loss to how this program can intake negative numbers and be able to raise the x to the y
What happens when you use a negative number for your input?
it will not properly calculate a mixed number + a mixed number when dealing with fractions

for example, if you input -2/3 + -2/3 it will come out as -1 1/-3

the denominator seems to intake the negative sign
Topic archived. No new replies allowed.