Question

In: Computer Science

Decimal value data types such as float and double represent the decimal number as an approximation....

Decimal value data types such as float and double represent the decimal number as an approximation. In other words, float or double arithmetic do not give exact answer but near approximation to the answer. As an example, run the following program and check its result:

            #include <iostream>
            using namespace std;
            int  main()
            {

float x= 0.1 * 7; if (x == 0.7)

cout<< "TRUE. \n"; else

cout<< "FALSE. \n"; return 0;

}

In some situations, we need our programs to give exact solutions instead of near to exact answers. Some programming languages provide a special data type called Decimal that represents decimal numbers without the use of approximation. Write a program in C++ that implements the Decimal data type using a class called BigDecimal. This class will allow users to create decimal values and perform several operations on these values. The class should use two member variables:

  • - the integer part saved as a string

  • - the decimal part saved as a string
    For example, the following instance BigDecimal d("6.45678"); will store "6" in the first variable and "45678" in the second variable
    The class should have the following functionalities:

    BigDecimal() The default constructor. Creates the number 0.0 BigDecimal(string) A constructor that accepts a string representing the numeric

    value with the decimal point.

Member Function

Description

== operator

Accepts two BigDecimal values and compares their values. If the two decimal values are equal then the method should return true otherwise it should return false.

!= operator

Accepts two BigDecimal values and compares their values. If the two decimal values are equal then the method should return false otherwise it should return true.

++ operator (prefix)

This operator should increment the integer part of BigDecimal object by one. The overloaded ++ operator should return the contents of the object (using this pointer) after it is incremented.

++ operator (postfix)

This operator should increment the integer part of BigDecimal object by one. The overloaded ++ operator should return the contents of the object before it is incremented.

Displays the numeric value of BigDecimal to the standard output. BigDecimal object.

<< operator
>> operator Reads a string value from the standard input and stores it in the

  

double toDouble() Converts the BigDecimal value to a double.
You can assume that all the values of BigDecimal are nonnegative. Here is an example of how

BigDecimal can be used: BigDecimal x("45.67");

BigDecimal y("2.5"); //Should print 2.5

       cout << x++ << endl;

//Should print 4.5

       cout << ++x << endl;
       //Should print false

cout << (x==y) << endl;

What to hand in

Submit your project electronically through D2L. Please hand in the following:

  • C++ source file (for example .cpp file) that you implemented for this assignment.

  • Please follow the documentation rules (programming guidelines) that are attached under

    Week 1.

Solutions

Expert Solution

  • // C++ program to create and test BigDecimal class
    #include <iostream>
    #include <sstream>
    #include <string>

    using namespace std;

    class BigDecimal
    {
    private:
    string intPart;
    string decimalPart;
    void setDecimal(string);
    public:
    BigDecimal() ;
    BigDecimal(string);
    friend bool operator==(const BigDecimal&, const BigDecimal& );
    friend bool operator!=(const BigDecimal&, const BigDecimal& );
    BigDecimal& operator++();
    BigDecimal operator++(int);
    friend ostream& operator<<(ostream& , const BigDecimal&);
    friend istream& operator>>(istream& , BigDecimal&);
    double toDouble();

    };

    // default constructor to create decimal 0.0
    BigDecimal::BigDecimal() : intPart("0"), decimalPart("0")
    {}

    // parameterized constructor that accepts a string representing the numeric value with the decimal point.
    BigDecimal::BigDecimal(string str)
    {
    setDecimal(str);
    }

    // private helper function to set the decimal to the input string representing the numeric value
    void BigDecimal:: setDecimal(string str)
    {
    size_t idx = str.find("."); // get the index of decimal point in string
    // decimal point is present
    if(idx != string::npos)
    {
    if(idx != 0) // string contains integer part
    {
    // get the integer part of the string
    intPart = str.substr(0,idx);
    }else // no integer part
    {
    intPart = "0"; // set integer part to 0
    }
    // extract and set the decimal part
    decimalPart = str.substr(idx+1);
    }
    else // no decimal part
    {
    intPart = str; // set string to integer part
    decimalPart = "0"; // set decimal part to 0
    }
    }

    // function to compare 2 BigDecimal and return true if both are equal else return false
    bool operator==(const BigDecimal& bd1, const BigDecimal& bd2)
    {
    // 2 BigDecimal objects are equal if they have same integer and decimal parts
    return (bd1.intPart == bd2.intPart && bd1.decimalPart == bd2.decimalPart);
    }

    // function to compare 2 BigDecimal and return true if both are not equal else return false
    bool operator!=(const BigDecimal& bd1, const BigDecimal& bd2)
    {
    return (!(bd1 == bd2));
    }

    // overloaded prefix increment operator that increment the integer part of BigDecimal object by one
    BigDecimal& BigDecimal:: operator++()
    {
    // convert integer part from string to int
    stringstream intStr(intPart);
    int num;
    intStr >> num;
    num++; // increment integer part by 1
    // clear the stream
    intStr.clear();
    // convert int to string
    intStr << num;
    intPart = intStr.str();

    return *this;
    }

    // overloaded postfix increment operator that increment the integer part of BigDecimal object by one
    BigDecimal BigDecimal:: operator++(int i)
    {
    BigDecimal temp = *this; // set temp to this object value
    ++*this; // increment this
    return temp; // return the contents of the object before it is incremented
    }

    // overloaded insertion operator that displays the numeric value of BigDecimal to the standard output.
    ostream& operator<<(ostream& out, const BigDecimal& d)
    {
    out<<d.intPart<<"."<<d.decimalPart;
    return out;
    }

    // overloaded extraction operator that reads a string value from the standard input and stores it in the BigDecimal object.
    istream& operator>>(istream& in, BigDecimal& d)
    {
    string s;
    in>>s;
    d.setDecimal(s);
    return in;
    }

    // function that converts the BigDecimal value to a double.
    double BigDecimal:: toDouble()
    {
    // concatenate intPart and decimalPart containing the decimal point
    // convert from string to double
    stringstream ss(intPart+"."+decimalPart);

    double value;
    ss >> value;
    return value;
    }


    int main()
    {
    // test the class
    BigDecimal x("2.5");
    BigDecimal y("45.67");
    cout<<boolalpha;
    //Should print 2.5
    cout << x++ << endl;
    //Should print 4.5
    cout << ++x << endl;
    //Should print false
    cout << (x==y) << endl;

    return 0;
    }

    //end of program

    Output:


Related Solutions

i keep getting return value ignored and and conversion from double to float , possible data...
i keep getting return value ignored and and conversion from double to float , possible data loss dont know how to fix the mistakes. if you could please fix it. #define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE #include #include void O_Read_age_1() {    int sum = 0;    int i;    for (i = 1; i <= 2; i++)    {        int temp;        printf("please enter the age of employee %d:", i);        scanf("%d", &temp);       ...
2. a) Represent the decimal value 47.375 as a single precision IEEE floating point number. Give...
2. a) Represent the decimal value 47.375 as a single precision IEEE floating point number. Give your answer in hexadecimal and show your work. b) Represent the decimal value 47.375 as a double precision IEEE floating point number. Give your answer in hexadecimal and show your work.
if hexadecimal number FF16 represent a signed number in one's compliments, what the decimal representation of...
if hexadecimal number FF16 represent a signed number in one's compliments, what the decimal representation of that number? include the sign for that number with no spaces between the number and sign. for example -3 or +3
if hexadecimal number FA16 represent a signed number in signed magnitude, what the decimal representation of...
if hexadecimal number FA16 represent a signed number in signed magnitude, what the decimal representation of that number? include the sign for that number with no spaces between the number and sign. for example -3 or +3
Use a numerical solver and Euler's method to obtain a four-decimal approximation of the indicated value....
Use a numerical solver and Euler's method to obtain a four-decimal approximation of the indicated value. First use h = 0.1 and then use h = 0.05 y' = y − y2, y(0) = 0.8;    y(0.5) y(0.5) ≈   ? (h = 0.1) y(0.5) ≈ ? (h = 0.05)
Represent the decimal number +105.25 as a sign (S), mantissa (M), and exponent (E)
Represent the decimal number +105.25 as a sign (S), mantissa (M), and exponent (E)
Use the improved Euler's method to obtain a four-decimal approximation of the indicated value. First use...
Use the improved Euler's method to obtain a four-decimal approximation of the indicated value. First use h = 0.1 and then use h = 0.05. y' = y − y^2, y(0) = 0.8; y(0.5) y(0.5) ≈________ (h = 0.1) y(0.5) ≈ _________(h = 0.05)
Use Euler's method to obtain a four-decimal approximation of the indicated value. Carry out the recursion...
Use Euler's method to obtain a four-decimal approximation of the indicated value. Carry out the recursion of (3) in Section 2.6 yn + 1 = yn + hf(xn, yn)        (3) by hand, first using h = 0.1 and then using h = 0.05. y' = 2x − 3y + 1, y(1) = 8;   y(1.2) y(1.2) ≈    (h = 0.1) y(1.2) ≈    (h = 0.05)
1) Use Euler's method to obtain a four-decimal approximation of the indicated value. Carry out the...
1) Use Euler's method to obtain a four-decimal approximation of the indicated value. Carry out the recursion of (3) in Section 2.6 yn + 1 = yn + hf(xn, yn)        (3) by hand, first using h = 0.1 and then using h = 0.05. y' = 2x − 3y + 1, y(1) = 4;   y(1.2) y(1.2) ≈ (h = 0.1) y(1.2) ≈ (h = 0.05) 2)Use Euler's method to obtain a four-decimal approximation of the indicated value. Carry out the recursion of...
Represent the value -65.75 in single and double precision. Please show all work.
Represent the value -65.75 in single and double precision. Please show all work.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT