fractiontest.cpp

来自「BigC++的源码」· C++ 代码 · 共 30 行

CPP
30
字号
#include "fraction.h"

int main()
{
   // Test constructors
   Fraction a;  // Value is 0/1
   Fraction b(4);  // Value is 4/1
   Fraction c(6,8);  // Value is 6/8, which is converted to 3/4
   cout << "Constructed values " << a << " " << b << " " << c << "\n";
   Fraction d(c);  // Value is copy of c, also 3/4
   cout << "Value of d is " << d.numerator() << "/" 
      << d.denominator() << "\n";
   // Test arithmetic instructions
   d = b + c;
   cout << "4 + 3/4 is " << d << "\n";
   d = b - c;
   cout << "4 - 3/4 is " << d << "\n";
   Fraction e = (b + (-c));
   cout << e << "done with negation\n";
   if (d == e)
      cout << "subtraction test successful\n";
   else
      cout << "subtraction test failed\n";
   a = Fraction(6,8);
   b = Fraction(7,8);
   if (a < b)
      cout << "Compare successful\n";
   return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?