C++ class Numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number
Numbers Class
Design a C++ class Numbers that can be used to translate whole dollar amounts in the
range 0 through 9999 into an English description of the number. For example, the
number 713 would be translated into the string seven hundred thirteen, and 8203
would be translated into eight thousand two hundred three. The class should have a
single integer member variable:
int number;
and a static array of strings that specify how to translate key dollar amounts into the
desired format. For example, you might use static strings such as
char lessThan20(20)[25] = {“zero·’, “one·’ , … , ··eighteen”, ·’ nineteen”};
char hundred[ 1 – ·’hundred”;
char thousand[ 1 ‘” “thousand”;
The class should have a constructor that accepts a nonnegative integer and uses it to
initialize the Numbers object. It should have a member function print() that prints
the English description of the Numbers object. Demonstrate the class by writing a
main program [hat asks the user ro enter a number in the proper range and then prints
out its English description.
Answer:
// Numbers Class
#include <iostream>
#include <string>
using namespace std;
// Constants for array sizes
const int LT20_SIZE = 20;
const int TENS_SIZE = 10;
const int WORD_SIZE = 25;
// Declaration of Numbers class
class Numbers
{
private:
int number; // To hold a number
public:
// Static arrays to hold words
const static char
lessThan20[LT20_SIZE][WORD_SIZE];
const static char
tens[TENS_SIZE][WORD_SIZE];
const static char hundred[WORD_SIZE];
const static char thousand[WORD_SIZE];
// Constructor
Numbers(int x){ number = x;}
// Function to print the words for the number
void print();
};
// Static member variables must be defined
// outside of the class
const char Numbers::lessThan20[LT20_SIZE][WORD_SIZE] =
{"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen",
"nineteen",
};
const char Numbers::tens[TENS_SIZE][WORD_SIZE] =
{ "zero", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety",
};
const char Numbers::hundred[WORD_SIZE] = "hundred";
const char Numbers::thousand[WORD_SIZE] = "thousand";
// *********************************************
// The print fucntion prints the English words *
// for the number *
// *********************************************
void Numbers::print()
{
if (number < 0)
cout << "minus ";
// Residue holds what remains to be printed.
int residue = number;
// Make sure residue is nonnegative
if (residue < 0) residue = - residue; // Take care of thousands, if any. int n_thousands = residue/1000; residue = residue % 1000; if (n_thousands > 0)
{
cout << " " << lessThan20[n_thousands];
cout << " thousand "; } // Take care of hundreds, if any. int n_hundreds= residue /100; residue = residue % 100; if (n_hundreds > 0)
{
cout << " " << lessThan20[n_hundreds];
cout << " hundred "; } // Take care numbers less than a 100. if (residue >= 20)
{
int n_tens = residue /10;
residue = residue % 10;
if (n_tens > 0)
cout << " " << tens[n_tens];
if (residue == 0) return;
}
// Take care of anything less than 20
cout << " " << lessThan20[residue];
}
// Demo program
int main()
{
int number;
// Tell user what the program does.
cout << "This program translates whole "
<< "dollar amounts into words for the\n"
<< "for the purpose of writing checks.\n"
<< "Entering a negative number terminates"
<< "the program.\n"
<< "Enter an amount for be translated "
<< "into words: "; cin >> number;
while (number >= 0)
{
// Create a Numbers object.
Numbers n(number);
// Print the English description.
n.print();
// Get another number.
cout << "\nEnter another number: "; cin >> number;
}
return 0;
}
<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
Leave a reply