Question: PLEASE ANSWER EACH POINT SEPRETELY Most first graders know that nine hundred and ninety nine plus one is one thousand, but C++ doesn't! Sure, a
PLEASE ANSWER EACH POINT SEPRETELY
Most first graders know that nine hundred and ninety nine plus one is one thousand, but C++ doesn't! Sure, a computer can easily compute 999 + 1 and get 1000. But reading and writing the numbers in words is not so easy.
Consider the following code, which implements a very simple 4-function calculator:
float n1, n2;
char op;
while (cin >> n1 >> op >> n2) {
switch (op) {
case '+': cout << n1 + n2 << endl; break;
case '-': cout << n1 - n2 << endl; break;
case '*': cout << n1 * n2 << endl; break;
case '/': cout << n1 / n2 << endl; break;
}
}
The code will read input consisting of simple arithmetic expressions, and for each it will display the result. For example, it could compute
2.1 + 3.7 // answer: 5.8
1.732906E-9 / 4.5870021E-25 // answer: 3.77786e+15
By simply changing the type of the numbers n1 and n2 from float to some other type, the calculator can compute the answers for various kinds of number. For example, changing the type to int will mean that numbers are computed and displayed according to the rules of integer arithmetic, so you could compute that 7 / 2 is 3.
Task
Your task for this assignment is to define a class, named Wordnum, that will read and write numbers expressed in word form. To use the class in the calculator, simply include the class header file and change the type of the numbers n1 and n2 to Wordnum. Then the program can read input consisting of expressions such as
five + six
ninety_nine - ninety_nine one_
thousand_two_hundred_thirty_four * five_hundred_forty_three
one million / negative_one hundred
and for each line it will write the result in words:
eleven
zero
six_hundred_seventy_thousand_sixty_two
negative_ten_thousand
Download the file wordnum_0.zip, which contains the calculator code and a skeleton version of the Wordnum class. The class defines (but doesn't fully implement) appropriate constructors to initialise instances of Wordnum, extract and insert operators for input and output of Wordnum values, and of course arithmetic operators to compute the results. Note that for a numeric type such as this, the arithmetic operators and IO operators are best be defined as friend functions. The extract and insert operators are the most difficult to implent (indeed, they are the sole reason for defining this class), but if the number's value is represented using an int, the arithmetic operators are trival to implement.
Background
For this assignment, numbers should be written as English words separated by underscore characters, with the default wording following the US conventions (because they are simpler to program). Thus 1001 is written "one_ thousand_ one" and 123000009 is "one_ hundred_ twenty_ three_ million_ nine". If British format is in effect (from level 6), tens and units are separated by a hyphen and preceded by the word "and". For example in British format 123 would be written one_ hundred_ and_ twenty-three.
Because of the way numbers are written as words in English, you'll probably find it easiest if you break a number up into groups of 3 digits, called triads, and deal with each triad separately. For example, 12345678 is best though of as 12_ million_ 345_ thousand_ 678. Because the code for converting a triad to and from words is the same for each group, you can convert a multitriad number like this:
work out how many millions (m), thousands (t) and units (u)
write triad for m followed by "million"
write triad for t followed by "thousand"
write triad for u
and to input a number you would reverse the process:
try to read a triad followed by "million" and set m to the result
try to read a triad followed by "thousand" and set t to the result
try to read a triad and set u to the result the number is m * 1000000 + t * 1000 + u
To input or output a triad, the simplest strategy is to use arrays of strings for the various words, with the array indexes carefully arranged to correspond to the numeric values of the words. Then you can convert a number to words by indexing the arrays with the appropriate value, and you can convert words to a number by looping though the arrays looking for a match. Of course, you'll need to deal with the case where the hundreds, tens, or units digits in the triad are zero.
Rather than trying to read input and write output directly, you'll probably find it easier to convert numbers to and from an array of strings, with each element in the array representing one word. Then the extract and insert operators can simply read and write the string arrays. For example, the insert operator would output each word separated by an underscore, and the input operator would tokenise the input using the underscore as a delimiter, storing the tokens into successive array elements.
Function Points
In the skeleton program, the extract and insert operators are implemented using helper functions write_number and read_number, which currently contain placeholder code. Implement a simple version of the helpers so that the program can handle input and output of numbers from 1 to 9.
Extend the class so that it is insensitive to the case of the input. Output should always be in lower case.
Extend the class so that the program can handle input and output of numbers from 1 to 100. Don't forget that forty is spelt without a 'u'.
Extend the class so that the program can handle input and output of negative numbers, such as negative_twenty_three, and zero.
Extend the class so that the program can handle output of all numbers with magnitude less than 1000. For this level, input numbers will always be less than 100.
Extend the class so that the program can also handle input of all numbers with magnitude less than 1000.
Extend the program so that if the command-line option -b is specified, input and output are in British format.
Extend the class so that the program can handle output of all numbers with magnitude less than 1,000,000,000. For this level, input numbers will always be less than 1000.
. Extend the class so that the program can also handle input of all numbers with magnitude less than 1,000,000,000.
Extend the class to deal sensibly with invalid input. Specifically, it should process the longest valid prefix of the input and ignore the remainder. As a special case, if none of the input is valid, the number should be interpreted as zero. For example, the input
six_squillion + more
should produce the output
six
file zip includes:
main.cpp
#include
#include "Wordnum.h"
using namespace std;
int main() {
Wordnum n1, n2;
char op;
while (cin >> n1 >> op >> n2) {
switch (op) {
case '+': cout << n1 + n2 << endl;
break;
case '-': cout << n1 - n2 << endl;
break;
case '*': cout << n1 * n2 << endl;
break;
case '/': cout << n1 / n2 << endl;
break;
}
}
return 0;
}
wordnum.cpp:
#include
#include
#include
#include "Wordnum.h"
using namespace std;
/**
* Creates a Wordnum for a given value
*
* @param n the value of the number
*/
Wordnum::Wordnum(int n) {
value_ = n;
}
/**
* Creates a Wordnum equivalent to other
*
* @param other the other value
*/
Wordnum::Wordnum(const Wordnum& other) {
value_ = other.value_;
}
/**
* Inserts a Wordnum into an output stream as a word string
*
* @param os the stream to insert into
* @param n the value to insert
* @return the modified stream
*/
ostream& operator<<(ostream& os, const Wordnum& n) {
return os << Wordnum::write_number(n.value_);
}
/**
* Extracts a Wordnum in word string form from an input stream
*
* @param is the stream to extract from
* @param n the value to assign
* @return the modified stream
*/
istream& operator>>(istream& is, Wordnum& n) {
string text;
if (is >> text) {
n.value_ = Wordnum::read_number(text);
}
return is;
}
/**
* Returns sum, difference, product, or quotion of n1 and n2
*/
Wordnum operator+(const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ + n2.value_);
}
Wordnum operator-(const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ - n2.value_);
}
Wordnum operator*(const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ * n2.value_);
}
Wordnum operator/(const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ / n2.value_);
}
/**
* Writes a number word string
*
* @param n the number to write
* @return the word string
*/
string Wordnum::write_number(int n) {
return ""; // replace with your conversion code
}
/**
* Reads a number word string
*
* @param n the string to read
* @return the value read
*/
int Wordnum::read_number(string n) {
return 0; // replace with your conversion code
}
Wordnum.h
#ifndef WORDNUM_H_
#define WORDNUM_H_
#include
/**
* A class to read and write numbers in word form
*
* Written for the topic ...
* by ...
* on ...
*/
class Wordnum {
public:
/**
* Creates a Wordnum for a given value
*
* @param n the value of the number
*/
Wordnum(int n = 0);
/**
* Creates a Wordnum equivalent to other
*
* @param other the other value
*/
Wordnum(const Wordnum& other);
/**
* Inserts a Wordnum into an output stream as a word string
*
* @param out the stream to insert into
* @param n the value to insert
* @return the modified stream
*/
friend std::ostream& operator <<(std::ostream &out, const Wordnum& n);
/**
* Extracts a Wordnum in word string form from an input stream
*
* @param in the stream to extract from
* @param n the value to assign
* @return the modified stream
*/
friend std::istream& operator >>(std::istream &in, Wordnum& n);
/**
* Returns sum, difference, product, or quotion of n1 and n2
*/
friend Wordnum operator +(const Wordnum& n1, const Wordnum& n2);
friend Wordnum operator -(const Wordnum& n1, const Wordnum& n2);
friend Wordnum operator *(const Wordnum& n1, const Wordnum& n2);
friend Wordnum operator /(const Wordnum& n1, const Wordnum& n2);
private:
/**
* Writes a number word string
*
* @param n the number to write
* @return the word string
*/
static std::string write_number(int n);
/**
* Reads a number word string
*
* @param n the string to read
* @return the value read
*/
static int read_number(std::string n);
int value_;
};
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
