科目名稱:程式設計 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2011.3.16 |
(考試時間: 16:30-16:45)
#include <iostream>
using std::cout;
using std::endl;
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()
{
cout << numerator;
if (denominator != 1)
cout << "/" << denominator;
cout << endl;
}
CRational operator+(const CRational x) const
{
return CRational(numerator*x.denominator + denominator*x.numerator,
denominator*x.denominator);
}
};
int main()
{
CRational a, d;
CRational b(3);
CRational c(1,7);
a = b + c + d; a.Print();
a = c + c; a.Print();
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
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()
{
cout << numerator;
if (denominator != 1)
cout << "/" << denominator;
cout << endl;
}
CRational operator=(const CRational x)
{
if (this == &x)
return *this;
numerator = x.numerator;
denominator = x.denominator;
return *this;
}
};
int main()
{
CRational a;
CRational b(3);
CRational c(1,7);
(a = b) = c; a.Print();
return 0;
}