In: Computer Science
IN C++
Create a class called TextInt. The purpose of a TextInt is to store an integer and convert it to the English text form of the integer when needed, such as ‘zero’ for 0, ‘one’ for 1, and so on, up to ‘nine thousand nine hundred ninety nine’ for 9999. You do NOT need punctuation (commas, hyphens, ‘and’, etc.) The TextInt class should have its own .h and .cpp files. At least the translate function should be implemented in the .cpp file. The rest can be inline functions. A TextInt should have a private member variable number of type int. The TextInt class should have static string members to help translate integers to text. For example, to represent the unique words for translating, you would use:
static string lessThan20[] = {“zero”, “one”, …, “eighteen”, “nineteen”};
static string tens[] = {“twenty”, “thirty”, … static string hundred = “Hundred”;
Any number can be translated using a combination of these, such as 1234 = 1000 + 200 + 30 + 4 = one thousand two hundred thirty four.
The class should have a default constructor with one parameter that initializes the value of the member variable number. Note that the parameter must have a default value to make it a default constructor. It should have public methods (member functions) to return the integer and text versions of number, and to change the value of number.
Overload the following operators so that TextInt can work just like a normal int in code: +, -, /, *, %, and <<.
Note that the first 5 will be almost identical.
For example, this code should work when you are done:
TextInt textInt1(2458), textInt2(1278);
TextInt textInt3 = textInt1 + textInt2;
cout << textInt3 << endl;
Should output: 3736: three thousand seven hundred thirty six
#include<iostream>
#include<vector>
#include<stdexcept>
using namespace std;
class TextInt{
private:
// Private number
int num;
// Gets the translation for three digit number
string threeDigit(int x)
{
string ans;
ans += (x/100 != 0) ? lessThan20[x/100] + "hundred " : "";
x %= 100;
if(x < 20)
ans += lessThan20[x];
else
ans += tens[x/10] + lessThan20[x%10];
return ans;
}
vector<string> lessThan20 = { "", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ",
"thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen " };
vector<string> tens = { "", "", "twenty ", "thirty ", "forty ",
"fifty ", "sixty ", "seventy ", "eighty ",
"ninety " };
public:
// Constructor with default argument as 0
TextInt(int num = 0) {
this->num = num;
}
// Translates the number to it's string representation
string translate()
{
if(num == 0)
return "zero ";
string ans;
int x = this->num;
if(x/1000000)
ans += threeDigit(x/1000000) + "million ";
x %= 1000000;
if(x/1000)
ans += threeDigit(x/1000) + "thousand ";
x %= 1000;
ans += threeDigit(x);
return ans;
}
// Returns the number
int get_number() {
return num;
}
// Setter for number
void set_number(int num) {
this->num = num;
}
//Using friend function
friend TextInt operator + (TextInt num1, TextInt num2);
friend TextInt operator * (TextInt num1, TextInt num2);
friend TextInt operator / (TextInt num1, TextInt num2);
friend TextInt operator % (TextInt num1, TextInt num2);
friend ostream& operator<<(ostream& os, TextInt num);
};
// Overloading + operator
TextInt operator + (TextInt num1, TextInt num2) {
return TextInt(num1.get_number() + num2.get_number());
}
// Overloading * operator
TextInt operator * (TextInt num1, TextInt num2) {
return TextInt(num1.get_number() * num2.get_number());
}
// Overloading / operator
TextInt operator / (TextInt num1, TextInt num2) {
if(!num2.get_number())
throw runtime_error("Division by zero");
return TextInt(num1.get_number() / num2.get_number());
}
// Overloading % operator
TextInt operator % (TextInt num1, TextInt num2) {
if(!num2.get_number())
throw runtime_error("Modulus by zero");
return TextInt(num1.get_number() % num2.get_number());
}
// Overloading << operator
ostream& operator << (ostream &os, TextInt num)
{
os << num.get_number() << ": " << num.translate();
return os;
}
//Driving code
int main()
{
TextInt obj = TextInt(2458);
cout<<obj.translate()<<"\n";
TextInt c = TextInt(2458) + TextInt(1278);
cout<<c;
}