In: Computer Science
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:
Member Function Description
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.
== 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.
<< operator Displays the numeric value of BigDecimal to the
standard output.
>> operator Reads a string value from the standard input and
stores it in the BigDecimal object.
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;
// 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: