This assignment calls for doing a partial implementation of

This assignment calls for doing a partial implementation of the abstract data type \"MyFloat\". This ADT is to serve a specialized need for small floats between 0 and 1 that can have up to 20 digits of accuracy. The creation of the MyFloat class will allow calculations with accuracy not possible with floats. Recall that to implement an abstract data type, you must:

1) Choose a concrete data representation for the abstract data type. When programming in C++, this usually requires coming up with a suitable class definition. If possible, this class should be chosen in such a way that a) memory is conserved ( for example using 16-bit int to store a single digit is wasteful.)

b) the operations in step 2 will be efficient

2) Implement the operations on the data values, i.e. write the member functions of the class MyFloat. For this assignment you\'ll need to implement the following member functions: Digits Returns the number of digits in a MyFloat. Important: if Digits is 0 then your code has previously detected an illegally formatted float. MaxDigits Returns the maximum number of possible digits in a MyFloat. Read Reads a MyFloat from standard input. If an error is detected in reading the MyFloat X, X.Digits() should return a 0. Write Writes a leading 0, a decimal point, and the digits in a MyFloat to standard output. If the MyFloat has 0 digits (error flag), output should be \'0.?\' + Adds two MyFloat variables X and Y together and stores results in MyFloat Z. Standard addition rules apply. Carry over into ones field is ignored for this assignment. == returns a 1 if two MyFloats are equal, 0 otherwise. Constructor Need to write a default constructor, and make sure set NumberofDigits to 0. Hints, Suggestions and Warnings:

1) Their is a driver program named \"TestMyFl.cpp\" on my Web site, copy this to use. This will facilitate the testing of your class. The test driver assumes that the specification and implementation file are combined into the same file called \"MyFloat.cpp\" and is in the current directory. Make sure to read this driver program first to understand what to do.

2) Recall good programmers write and debug functions one at a time! Again read \"TestMyFl.cpp\" first to help understand which one to write first. Note that function stubs will be needed in your class to get TestMyFl.cpp to compile and run. Also remember when coding your file MyFloat.cpp to just compile it and not MAKE it. Compile will find the syntax errors.

3) Do not underestimate the amount of help you can get be studying the examples that we looked at during class. They and the book(s) are very helpful.

4) When writing Read, be sure to follow standard floating point read conventions. Leading white space and zeros are ignored, a decimal point is required, and input stops when a non-numeric char is detected.

5) In the Read function, you will be storing the digits in an array of char. Be sure to convert chars such as \'0\', \'1\', ect. to corresponding ints 0, 1 , etc. before storing in your array of digits. Otherwise you will pay a server penalty when doing next assignment! Also need to use \"cin.putback(Ch);\" in the Read function.

6) Remember if Ch is a char with value 1 (not \'1\'), \"cout << Ch\"; will display a happy face!

7) Do not turn in a listing of TestMyFl.cpp, I already have it.

8) Warning: In your comments, be sure to carefully describe how you store a typical MyFloat! If you don\'t you will loose points. Remember comments are very important.

using this:

Solution

/*********************************************************************************

Class Definition for MyFloat

*********************************************************************************/

class MyFloat

{

// Private Member Variables

private:

                enum { MAX_DIGITS = 20 };

                char Number[MAX_DIGITS + 1]; // Index 0 contains decimal. The number starts from index 1

                char NoOfDigits;

// Member Functions

public:

                MyFloat(); // default constructor

                int Num_Digits(); // counts no. of digits

                int MaxDigits(); // maximum digits which is always 20

                void Read(); // reads a MyFloat data

                void Write(); // displays a MyFloat data

                MyFloat operator+ (MyFloat X); // add 2 MyFloats

                int operator== (MyFloat X); // compare 2 MyFloats

}; // End of MyFloat class definition

/*********************************************************************************

MyFloat constructor that initializes Number[] and NoOfDigits to 0.

*********************************************************************************/

MyFloat::MyFloat()

{

                for (int i = 0; i < MAX_DIGITS; i++)

                                Number[i] = 0;

                NoOfDigits = 0;

}

/*********************************************************************************

Returns the total number of digits in MyFloat.

*********************************************************************************/

int MyFloat::Num_Digits()

{

                return NoOfDigits;

}

/*********************************************************************************

Returns the maximum number of digits in a MyFloat.

*********************************************************************************/

int MyFloat::MaxDigits()

{

                return MAX_DIGITS;

}

/*********************************************************************************

Read - Reads a MyFloat from standard input and stores numbers into character array of max size 21. If an error is detected in reading the MyFloat X, X.Digits() should return a 0.

Index 0 is a decimal point, and remaining elements are digits (20 max numbers).

*********************************************************************************/

void MyFloat::Read()

{

                int y = 0;

                char inp;

                do

                {

                                cin.get(inp); // get user input

                } while (inp != \'.\'); // look for decimal

                Number[0] = inp; // index 0 is now a decimal

                cin.get(inp);

                while ((isdigit(inp)) && (y < MAX_DIGITS))

                {

                                Number[y + 1] = inp - \'0\'; // convert to int

                                y++;

                                cin.get(inp);

                }

                cin.putback(inp); // put \"\ \" back in buffer

                NoOfDigits = y;

                for (++y; y <= MAX_DIGITS; y++) // convert remaining elements to \"\\0\"

                                Number[y] = \'0\' - \'0\';

}

/*********************************************************************************

Write - Writes a leading 0, a decimal point, and the digits in a MyFloat to standard output. If the MyFloat has 0 digits (error flag), output should be \'0.?\'

Converts to int for output.

*********************************************************************************/

void MyFloat::Write()

{

                cout << \"0.\";

                if (NoOfDigits == 0) // if no digits - an input error

                                cout << \"?\";

                for (int i = 1; i <= NoOfDigits; i++) // display user defined instance

                                cout << int(Number[i]);

}

/*********************************************************************************

+ Operator – Adds two MyFloat variables X and Y together and stores results in MyFloat Z. Standard addition rules apply. Carry over into ones field is ignored for this assignment.

*********************************************************************************/

MyFloat MyFloat::operator+ (MyFloat Y)

{

                MyFloat Z; // store sum of \"X + Y\"

                int NumCarryDigits = 0; // carry count for addition

                if (NoOfDigits >= Y.NoOfDigits) // determine if left-hand number has more digits

                {

                                for (int i = NoOfDigits; i > 0; i--) // use the number that has more digits

                                {

                                                Z.Number[i] = Number[i] + Y.Number[i];

                                                Z.NoOfDigits++;

                                                if (NumCarryDigits == 1) // add the carry if appropriate

                                                                ++Z.Number[i];

                                                if (Z.Number[i] > 9) // determine if carrying needed during addition

                                                {

                                                                Z.Number[i] = Z.Number[i] % 10; // only use rightmost digit if sum is

                                                                                                                                // more than 9

                                                                NumCarryDigits = 1;

                                                }

                                                else

                                                                NumCarryDigits = 0;

                                }

                }

                if (NoOfDigits < Y.NoOfDigits) // if right-hand number is bigger

                {

                                for (int j = Y.NoOfDigits; j > 0; j--)

                                {

                                                Z.Number[j] = Number[j] + Y.Number[j];

                                                Z.NoOfDigits++;

                                                if (NumCarryDigits == 1)

                                                                ++Z.Number[j];

                                                if (Z.Number[j] > 9)

                                                {

                                                                Z.Number[j] = Z.Number[j] % 10;

                                                                NumCarryDigits = 1;

                                                }

                                                else

                                                                NumCarryDigits = 0;

                                }

                }

                return Z; // return the sum of X + Y

}

/*********************************************************************************

== Operator - Returns a 1 if two MyFloats are equal, 0 otherwise. Accounts for the fact that \".2\" is the same as \".20\".

*********************************************************************************/

int MyFloat::operator== (MyFloat Y) // Compares X == Y

{

                for (int k = 1; k < MAX_DIGITS; k++)

                {

                                if (Number[k] == Y.Number[k])

                                                continue;

                                else

                                                return 0; // if index doesn\'t match- they don\'t match

                }

                return 1; // they do match- return true

}

/*********************************************************************************

End of Class MyFloat.

*********************************************************************************/

This assignment calls for doing a partial implementation of the abstract data type \
This assignment calls for doing a partial implementation of the abstract data type \
This assignment calls for doing a partial implementation of the abstract data type \
This assignment calls for doing a partial implementation of the abstract data type \
This assignment calls for doing a partial implementation of the abstract data type \
This assignment calls for doing a partial implementation of the abstract data type \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site