Summing Fractions

Hi there, there is a small bug in my code that I cannot quite figure out.
- The input starts with the number of fractions you want to add (eg. 3)
- You can then input fractions in the form of
1) A while number (eg. 2)
2) A fraction (eg. 1/2)
3) A mixed number (e.g. 3,4/5)
- The program keeps a running numerator and denominator
- Then at the end it will say in the lowest form the added fraction
It appears to work but for some test cases it proves false and I do not know which ones.
Does anything appear wrong?
Thankyou


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
using namespace std;

int GCD(int a, int b)
{
    int r = a%b;
    if (r!=0)
    {
        return GCD(b, r);
    }
    return b;
}


int main()
{
    for (int b = 0; 1; b++)
    {
        int numbers;
        cin >> numbers;
        cin.ignore();
        if (numbers<1)
        {
            break;
        }
        int top = 0;
        int bottom = 1;
        for (int a = 0; a<numbers; a++)
        {
            int tmpTop;
            int tmpBottom;
            string line;
            getline(cin, line);
            stringstream is;
            if (count(line.begin(), line.end(), ',')==1)
            {
                // Is a mixed fraction
                replace(line.begin(), line.end(), ',', ' ');
                replace(line.begin(), line.end(), '/', ' ');
                is << line;
                int tmp;
                is >> tmp;
                is >> tmpTop;
                is >> tmpBottom;
                tmpTop = tmpTop + (tmp*tmpBottom);
                if (a==0)
                {
                    top = tmpTop;
                    bottom = tmpBottom;
                }
                else
                {
                    top = (top*tmpBottom)+(tmpTop*bottom);
                    bottom = (bottom*tmpBottom);
                }
            }
            else
            {
                if (count(line.begin(), line.end(), '/')==1)
                {
                    // Is a fraction
                    replace(line.begin(), line.end(), '/', ' ');
                    is << line;
                    is >> tmpTop;
                    is >> tmpBottom;
                    if (a==0)
                    {
                        top = tmpTop;
                        bottom = tmpBottom;
                    }
                    else
                    {
                        top = (top*tmpBottom)+(tmpTop*bottom);
                        bottom = (bottom*tmpBottom);
                    }
                }
                else
                {
                    // Is a whole number
                    is << line;
                    is >> tmpTop;
                    tmpBottom = 1;
                    if (a==0)
                    {
                        top = tmpTop;
                        bottom = tmpBottom;
                    }
                    else
                    {
                        top = (top*tmpBottom)+(tmpTop*bottom);
                        bottom = (bottom*tmpBottom);
                    }
                }
            }
        }
        cout << "Case " << b+1 << ": " << top << "/" << bottom << endl;
        if (top==0)
        {
            // It's one
            cout << "Test " << b+1 << ": 0" << endl;
        }
        else
        {
            if (top==bottom)
            {
                // It's zero
                cout << "Test " << b+1 << ": 1" << endl;
            }
            else
            {
                if (top>bottom)
                {
                    int tmp = top/bottom;
                    top = top - (bottom*tmp);
                    int common = GCD(top, bottom);
                    top = top/common;
                    bottom = bottom/common;
                    if (top==0)
                    {
                        cout << "Test " << b+1 << ": " << tmp << endl;
                    }
                    else
                    {
                        cout << "Test " << b+1 << ": " << tmp << "," << top << "/" << bottom << endl;
                    }
                }
                else
                {
                    int common = GCD(top, bottom);
                    top = top/common;
                    bottom = bottom/common;
                    cout << "Test " << b+1 << ": " << top << "/" << bottom << endl;
                }
            }
        }
    }
    return 0;
}
Topic archived. No new replies allowed.