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

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 {
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);
int value_;


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.

Posted Date: 3/4/2013 6:29:31 AM | Location : United States

Related Discussions:- Define constructors-extract and insert operators, Assignment Help, Ask Question on Define constructors-extract and insert operators, Get Answer, Expert's Help, Define constructors-extract and insert operators Discussions

Write discussion on Define constructors-extract and insert operators
Your posts are moderated
Related Questions
Write a recursive function recursiveMin that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element in the array. The

Classify computer system according to capacity. How they are different from computers according to the classification of technology. Provide comparative study also.

Purchasing Code Signing Certificate I want to purchase a Code Signing Certificate in good standing from someone in order to sign my .EXE programs with. Please be aware I do n

Project Description: collect / modify person records / photo images/ lookup / facebook iphone and ipad App basic function : 1 user will configure facebook connection

payment system, in c++ only..just use the printf or scanf.please

This task involves creating a new class(es) that will allow the system to playback pattern sequences using the Neo  Freerunner  hardware. The first thing to note is that the vibrat

Hello. I need a solution for this assignment. It is for C++ Language: Create a BusinessPartner class that contains a first name, company name, and a telephone number. Create a Con

Expected output of the program: 1. Consider the following programs. For each, indicate whether the program is correct. If yes, what is the expected output? If not, what is the

C Program for DIVISER   void main() {           int result,number,min;           clrscr();           printf("ENTER THE NUMBER=");           flushall();