C++ Design a class Numbers that can be used to translate whole dollar amounts in
ID: 3795399 • Letter: C
Question
C++
Design a 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 collection of static string members that specify how to translate key dollar amounts
into the desired format. For example, you might use static strings such as
string lessThan20[ ] =
{''zero'' , '' one'' , ... , '' eighteen'' , "nineteen"};
string hundred = '' hundred'';
string thousand = ''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 that asks
the user to enter a number in the proper range and then prints out its English description.
Explanation / Answer
#include <iostream>
#include <string>
#include “Numbers.h”
using namespace std;
string Numbers::lessThan20[ ] =
{“zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”,
“nine”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”, “fifteen”,
“sisteen”, “seventeen”, “eighteen”, “nineteen” };
string Numbers::bet20To100[ ] =
{” “, ” “, “twenty”, “thirty”, “forty”, “fifty”, “sixty”,
“seventy”, “eightty”, “ninety” };
string Numbers::hundred = “hundred”;
string Numbers::thousand = “thousand”;
Numbers::Numbers(int num)
{
if (num >= 0 || num <= 9999)
number = num;
else
cout << “Error! Number is outside valid range. ”;
}
void Numbers::print()
{
int K = 0,
H = 0,
T = 0,
N = 0;
string EngNum = ” “;
if (number > 999)
{
K = number / 1000;
EngNum = lessThan20[K] + ” ” + thousand + ” “;
N = K * 1000;
}
if (number > 99)
{
if (number > 999)
{
H = (number – N)/100;
}
else
H = number / 100;
EngNum += lessThan20[H] + ” ” + hundred + ” “;
N += (H * 100);
}
if (N > 0)
{
T = number – N;
}
else
T = number;
if (T > 19)
{
EngNum += bet20To100[(T/10)] + ” ” + lessThan20[(T%10)];
}
else
EngNum += lessThan20[T];
cout << “English descripton: ” << EngNum << “.” << endl;
}
Number.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <iostream>
#include <string>
using namespace std;
class Numbers
{
public:
int number;
static string lessThan20[ ];
static string bet20To100[ ];
static string hundred;
static string thousand;
Numbers(int);
void print();
};
#endif
Main.cpp
#include <iostream>
#include <string>
#include “Numbers.h”
using namespace std;
int main()
{
int Num;
cout << “This program displays the English description of a number. ”
<< “Enter a number in the range 0 through 9999: “;
cin >> Num;
Numbers Obj(Num);
Obj.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.