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
|
//Header file
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;
class TextVersionOfNumber
{
private:
double amount;
public:
string getTextVersionOfNumber();
void setAmount(double);
};
//.cpp file
#include "TextVersionOfNumber.h"
string TextVersionOfNumber::getTextVersionOfNumber()
{
string upto20[20] = { "", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen" };
string tens[10] = { "", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety" };
double _amount = (double)amount;
string ret_val = "";
if (amount < 0)
cout << "Given amount is negative.";
amount = abs(amount);
_amount = amount / 1000;
if (_amount>0)
ret_val += upto20[(int)_amount], " thousand ";
amount /= 1000;
_amount = amount / 100;
if (_amount > 0)
ret_val += upto20[(int)_amount], " hundred ";
amount /= 100;
if (amount >= 20)
{
_amount = amount / 10;
if (_amount > 0)
ret_val += tens[(int)_amount], " ";
}
else if (amount >= 10)
{
ret_val += upto20[(int)amount], " ";
}
amount /= 10;
if (amount > 0)
ret_val += upto20[(int)amount];
system("pause");
return ret_val;
};
void TextVersionOfNumber::setAmount(double _amount)
{
amount = _amount;
};
//tester file
// Chapter 12-- Assignment 14: Check Writer
// This program can convert a dollar and cents amount given in
// numerical form to a word description of the amount.
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;
#include "TextVersionOfNumber.h"
// Assume a maximum amount of $10,000
int main()
{
string date = "03/05/2016", payee = "Michael Wille";
TextVersionOfNumber checkAmount;
double testAmounts[] = { 0, .01, .25, 12, 12.45, 19, 19.02,
19.45, 20, 20.45,
34, 56.78, 100, 109, 119.78,
450, 678.90, 1000, 1009.45, 1056,
1234, 1567.98, 9999, 9999.99 };
for (int i = 0; i < sizeof(testAmounts) / sizeof(double); i++)
{
double an_amount = testAmounts[i];
checkAmount.setAmount(an_amount);
cout << setprecision(2) << fixed;
cout << setw(60) << right;
cout << "Date: " << date << endl;
cout << "Pay to the order of: " << payee << "\t\t\t";
cout << "$" << an_amount << endl;
cout << checkAmount.getTextVersionOfNumber() << endl;
cout << "---------------------------------------------------------------------\n";
}
system("pause");
return 0;
}
|