(10%)
Determine whether the following code has syntax erros or not. If
it is correct, predict its output. If it is incorrect,
point out the mistake(s).
#include <iostream>
using std::cout;
using std::endl;
int gcd(int a, int b)
{
if (b == 0) return a;
else return gcd(b, a % b);
}
class CRational
{
public:
int numerator; // 分子
int denominator; // 分母
CRational(int p=0, int q=1) : numerator(p), denominator(q)
{ if (q==0)
{ cout << "Error! The denominator cannot be 0.\n";
exit(1);
}
}
void Print() const
{ cout << numerator;
if (denominator != 1)
cout << "/" << denominator;
cout << endl;
}
void Reduce() const
{ int i = gcd(numerator, denominator);
numerator /= i;
denominator /=i;
}
};
int main()
{
CRational a(12,8);
CRational b(3,6);
a.Print(); a.Reduce(); a.Print();
b.Reduce(); b.Print();
return 0;
}