本文整理汇总了C++中Money::cents方法的典型用法代码示例。如果您正苦于以下问题:C++ Money::cents方法的具体用法?C++ Money::cents怎么用?C++ Money::cents使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Money
的用法示例。
在下文中一共展示了Money::cents方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: UnitTest
// For testing (DO NOT ALTER)
void UnitTest() {
cout << string(40, '-') << endl;
cout << "UNIT TEST:\n" << string(40, '-') << endl;
if (num_of_tests != 0)
cout << "Total Number of Tests: " << num_of_tests << endl;
string yours = "", actual = "";
// Tests
Money amount1(123, 45), amount2(10, 9);
Money sum = amount1 + amount2;
Money diff1 = amount1 - amount2;
Money diff2 = amount2 - amount1;
Test(sum.dollars() == 133 && sum.cents() == 54, __LINE__, "$123.45 + $10.09");
Test(diff1.dollars() == 113 && diff1.cents() == 36, __LINE__,
"$123.45 - $10.09");
Test(diff2.dollars() == -113 && diff2.cents() == -36, __LINE__,
"$10.09 - $123.45");
amount1 = -amount1;
Test(amount1.dollars() == -123 && amount1.cents() == -45, __LINE__,
"-$123.45");
Test((diff2 == diff1) == false, __LINE__, "$113.36 == $-113.36");
amount1.set_dollars(-8);
amount1.set_cents(-75);
amount2.set_dollars(0);
amount2.set_cents(50);
sum = amount1 + amount2;
diff1 = amount1 - amount2;
diff2 = amount2 - amount1;
Test(sum.dollars() == -8 && sum.cents() == -25, __LINE__, "$-8.75 + $0.50");
Test(diff1.dollars() == -9 && diff1.cents() == -25, __LINE__,
"$-8.75 - $0.50");
Test(diff2.dollars() == 9 && diff2.cents() == 25, __LINE__, "$0.50 - $8.75");
diff1 = -diff1;
Test(diff1.dollars() == 9 && diff1.cents() == 25, __LINE__, "-$-9.25");
Test((diff2 == diff1) == true, __LINE__, "$9.25 == $9.25");
Money one(0, 10), two(0, -10), three(100, 2), four(-100, -2);
std::streambuf* old_cout = cout.rdbuf();
std::ostringstream capture_cout;
cout.rdbuf(capture_cout.rdbuf());
cout << Money(0, 10);
cout.rdbuf(old_cout);
yours = capture_cout.str();
actual = "$0.10";
Test(yours == actual, __LINE__, "<< Money(0, 10)", yours, actual);
capture_cout.str("");
cout.rdbuf(capture_cout.rdbuf());
cout << Money(0, -10);
cout.rdbuf(old_cout);
yours = capture_cout.str();
actual = "$-0.10";
Test(yours == actual, __LINE__, "<< Money(0, -10)", yours, actual);
capture_cout.str("");
cout.rdbuf(capture_cout.rdbuf());
cout << Money(100, 2);
cout.rdbuf(old_cout);
yours = capture_cout.str();
actual = "$100.02";
Test(yours == actual, __LINE__, "<< Money(100, 2)", yours, actual);
capture_cout.str("");
cout.rdbuf(capture_cout.rdbuf());
cout << Money(-100, -2);
cout.rdbuf(old_cout);
yours = capture_cout.str();
actual = "$-100.02";
Test(yours == actual, __LINE__, "<< Money(-100, -2)", yours, actual);
cout << string(40, '-') << endl;
cout << "Passed: " << ut_passed << " / " << ut_total << endl;
OutputFailedTests();
cout << string(40, '-') << endl;
cout << "END OF UNIT TEST!\n";
cout << string(40, '-') << endl;
cout << "Be sure to run 'make style' to check for any style errors.\n"
<< "Please note that 'make style' does NOT check variable names or"
<< " indentation" << endl << endl;
}