Define constructors-extract and insert operators, C/C++ Programming

Assignment Help:

For your class to work properly, you'll need to define appropriate constructors, extract and insert operators, and of course arithmetic operators. (If you wanted to use it as a general-purpose numeric type, you would also need to add features that are not needed here, such as assignment and comparison operators, and type conversions to and from other numeric types.)

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 implement (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 and can be defined in the class header file. For example, a suitable class skeleton would look like this:

class Wordnum {
public:
Wordnum(int n) { value_ = n; }
friend Wordnum operator+ (const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ + n2.value_);
}
... // other arithmetic operators are similar
friend std::ostream& operator<< (std::ostream&, const Wordnum& n);
friend std::istream& operator>> (std::istream&, Wordnum& n);
private:
int value_;
};

Hints

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 ditits, 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.


Related Discussions:- Define constructors-extract and insert operators

Program to calculate average of marks, Program for calculate average of tot...

Program for calculate average of total marks: #include using namespace std; void print(int marks_arr[],int cnt) { int ind[cnt]; int i=0; int j=0; int k=0;

KRPano, I am seeking an expert to create a 360 degree virtual tour in KRPan...

I am seeking an expert to create a 360 degree virtual tour in KRPano for use on PCs, tablets and smartphones. You will be given with panoramas in order to create an advanced KRPano

Program for implement vectors and iterators, - Write a program using vector...

- Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles. The user should be able to view all DVD titles in his library, add and d

Algorithme, algorithm to find whether a given number is prime number or not...

algorithm to find whether a given number is prime number or not? using pseudo code and flowchart

Fibonacci series, draw the flow chart to print the fibonacci series upto n ...

draw the flow chart to print the fibonacci series upto n th terms

Explain the working of strcpy function, Explain the Working of strcpy Funct...

Explain the Working of strcpy Function? strcpy() : This function is meant to use to copy one string to another. The strcpy function as well accepts two strings as arguments.

Define a function in c program, Define a function in c program: int f1...

Define a function in c program: int f1() { static int x=10; return x; } int f2() { int y=5; return y; } extern int z; void f()     {

PROGRAM, TO CONVERT A NUMBER OR A DIGIT INTO ALPHABET

TO CONVERT A NUMBER OR A DIGIT INTO ALPHABET

Expression, i need expression and its types with example programs in c++

i need expression and its types with example programs in c++

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd