Question

In: Computer Science

IN C++ Create a class called TextInt. The purpose of a TextInt is to store an...

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

Solutions

Expert Solution

#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;
}

Related Solutions

Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables-a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class this class should contain the following private data types: - name (string) - id number (string) - email (s) - phone number (string) - number of courses (int) - a dynamic array of course objects. the user will specify how many courses are there in the array. the following are public members of the student class: - default constructor (in this constructor, prompt the...
with PHP Create a class called Invoice that a hardware store might use to represent an...
with PHP Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables — a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method...
2. Create a class called Invoice that a store might use to represent an invoice for...
2. Create a class called Invoice that a store might use to represent an invoice for an item sold at the store. An Invoice should include four data members—the ID for the item sold (type string), name of item (type string), item description (type string) and the price of the item (type int). Your class should have a constructor that initializes the four data members. A constructor that receives multiple arguments. Example: ClassName( TypeName1 parameterName1, TypeName2 parameterName2, ... ) Provide...
Create a class called RandomGuess. In this game, generate and store a random number between 1...
Create a class called RandomGuess. In this game, generate and store a random number between 1 and 100. Then, continuously (in a loop): Ask the user to enter a number between 1 and 100 Let the user know if the guess is high or low, until the user enters the correct value If the user enters the correct value, then display a count of the number of attempts it took and exit
In C++ Create a class called Rational (separate the files as shown in the chapter) for...
In C++ Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example,...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT