Design a class Numbers that can be used to translate whole dollar amounts in the
ID: 3641221 • Letter: D
Question
Design a class Numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into a English description of the numberAlso, demonstrate the class by writing a main program that loops asking the user to enter a number in the proper range and then prints its English description
this class should have a single integer member variable
int num;
and a collection of static string members that specify how to translate key dollar amounts into the desired format. per example :
static string lessThan20[] = {"zero", "one", ..., "nineteen" };
static string hundred = "hundred";
static string thousand = "thousand";
the class should have a constructor that accepts a non-negative integer and uses it to initialize the Numbers object. It should have a a member function print() that prints the English description of the Numbers object;
Explanation / Answer
//Numbers.h
#include <iostream>
#include <string>
using namespace std;
#ifndef NUMBERS_H
#define NUMBERS_H
class Numbers
{
public:
Numbers(int = 0);
~Numbers();
void print() const;
private:
static const string LESS_THAN_20[];
static const string TENS[];
static const string HUNDRED;
static const string THOUSAND;
int num;
};
#endif
//Numbers.cpp
#include "Numbers.h"
const string Numbers::LESS_THAN_20[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
const string Numbers::TENS[] = {
"twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"
};
const string Numbers::HUNDRED = "hundred";
const string Numbers::THOUSAND = "thousand";
//---------------------------------------
Numbers::Numbers(int num)
:num(num)
{
if (this->num > 9999 || this->num < 0)
this->num = 0;
}
//---------------------------------------
Numbers::~Numbers()
{
}
//---------------------------------------
void Numbers::print() const
{
if (!num)
cout << LESS_THAN_20[num];
else
{
int firstDigit = num / 1000;
int secondDigit = num / 100 % 10;
int thirdDigit = num / 10 % 10;
int fourthDigit = num % 10;
if (firstDigit)
cout << LESS_THAN_20[firstDigit] << " " << THOUSAND;
if (secondDigit)
cout << (firstDigit ? " " : "")
<< LESS_THAN_20[secondDigit] << " " << HUNDRED;
if (thirdDigit || fourthDigit)
{
cout << (secondDigit ? " " : (firstDigit ? " " : ""));
if (firstDigit || secondDigit)
{
if (!thirdDigit
|| (num % 100 == 10 || num % 100 == 12)
|| (!secondDigit && firstDigit))
cout << "and ";
}
if (thirdDigit < 2)
cout << LESS_THAN_20[thirdDigit * 10 + fourthDigit];
else if (fourthDigit)
cout << TENS[thirdDigit - 2] << " " << LESS_THAN_20[fourthDigit];
else
cout << TENS[thirdDigit - 2];
}
}
}
//TestDriver.cpp
#include <iostream>
#include "Numbers.h"
using namespace std;
int main()
{
Numbers test0;
cout << "0 = ";
test0.print();
cout << endl;
Numbers test1(8);
cout << "8 = ";
test1.print();
cout << endl;
Numbers test2(18);
cout << "18 = ";
test2.print();
cout << endl;
Numbers test3(30);
cout << "30 = ";
test3.print();
cout << endl;
Numbers test4(45);
cout << "45 = ";
test4.print();
cout << endl;
Numbers test5(100);
cout << "100 = ";
test5.print();
cout << endl;
Numbers test6(304);
cout << "304 = ";
test6.print();
cout << endl;
Numbers test7(289);
cout << "289 = ";
test7.print();
cout << endl;
Numbers test8(8000);
cout << "8000 = ";
test8.print();
cout << endl;
Numbers test9(9563);
cout << "9563 = ";
test9.print();
cout << endl;
Numbers test10(7050);
cout << "7050 = ";
test10.print();
cout << endl;
Numbers test11(3480);
cout << "3480 = ";
test11.print();
cout << endl;
Numbers test12(12);
cout << "12 = ";
test12.print();
cout << endl;
Numbers test13(6007);
cout << "6007 = ";
test13.print();
cout << endl;
Numbers test14(-7);
cout << "-7 = ";
test14.print(); //should print zero (negative value)
cout << endl;
Numbers test15(62007);
cout << "62007 = ";
test15.print(); //should print zero (>9999)
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.