This commit is contained in:
RafayAhmad7548 2024-06-16 18:53:25 +05:00
parent 37776af5db
commit ab03d5f10c
4045 changed files with 286212 additions and 3 deletions

View file

@ -0,0 +1,167 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,174 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}
int main(){
BigFloat a("5.34");
cout<<a;
return 0;
}

View file

@ -0,0 +1,143 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
sum -= carry;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,162 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
for(int j=0;j<this->precision-val.precision;j++){
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,169 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,172 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,140 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
int i = 0, carry = 0;
while(i<this->fraction.length() || i<val.fraction.length()){
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,144 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,163 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = char(this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,164 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i] - '0') + (val.fraction[val.fraction.length()-i] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i] - '0') + (val.whole[val.whole.length()-i] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,140 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() || i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i];
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,143 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = (sum%10 + '0')
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,172 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i] - '0') + (val.whole[val.whole.length()-i] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i] + result.whole;
i++;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,176 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<static_cast<string>(val)<<endl;
return output;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
return input;
}
int main(){
BigFloat a("5.34"), b("3.73");
cout<<a+b;
return 0;
}

View file

@ -0,0 +1,163 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = char(this->fraction[i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,161 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i] - '0') + (val.fraction[val.fraction.length()-i] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + this->whole[this->whole.length()-i]
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,157 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,142 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,162 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,169 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = char(this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = char(this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,176 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<static_cast<string>(val)<<endl;
return output;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
return input;
}
int main(){
BigFloat a("5.34");
cout<<a;
return 0;
}

View file

@ -0,0 +1,141 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,176 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<static_cast<string>(val)<<endl;
return output;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
return input;
}
int main(){
BigFloat a("5.34"), b("3.7");
cout<<a+b;
return 0;
}

View file

@ -0,0 +1,140 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
int i = 0, carry = 0;
while(i<this->whole.length()+this->fraction.length() || i<val.whole.length()+val.fraction.length()){
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,159 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while()
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1 @@
{"version":1,"resource":"file:///home/rafayahmad/Stuff/OOP%20Assignment%2004/Q3.cpp","entries":[{"id":"k3hB.cpp","timestamp":1713407026481},{"id":"polH.cpp","timestamp":1713407090251},{"id":"ceSK.cpp","timestamp":1713407101354},{"id":"9R3c.cpp","timestamp":1713407128471},{"id":"Gt2Z.cpp","timestamp":1713407172901},{"id":"Yp40.cpp","timestamp":1713407200538},{"id":"fxFZ.cpp","timestamp":1713407214971},{"id":"M8hY.cpp","timestamp":1713407255428},{"id":"vz6Q.cpp","timestamp":1713407268001},{"id":"3wFw.cpp","timestamp":1713407300174},{"id":"Gwbw.cpp","timestamp":1713407328974},{"id":"mF3L.cpp","timestamp":1713407340921},{"id":"ol7d.cpp","timestamp":1713407351211},{"id":"wnVh.cpp","timestamp":1713407392071},{"id":"A6NI.cpp","timestamp":1713407420731},{"id":"pFSM.cpp","timestamp":1713407603848},{"id":"jDim.cpp","timestamp":1713407665725},{"id":"qkz1.cpp","timestamp":1713407862085},{"id":"7QrC.cpp","timestamp":1713407889205},{"id":"QVhV.cpp","timestamp":1713407914442},{"id":"hiYB.cpp","timestamp":1713407930926},{"id":"IqAk.cpp","timestamp":1713407973236},{"id":"AG8i.cpp","timestamp":1713407986702},{"id":"tzMS.cpp","timestamp":1713408004142},{"id":"S5iv.cpp","timestamp":1713408022066},{"id":"7mu6.cpp","timestamp":1713408056593},{"id":"LjBd.cpp","timestamp":1713408094593},{"id":"crGF.cpp","timestamp":1713408117283},{"id":"zVm3.cpp","timestamp":1713408141482},{"id":"JeFd.cpp","timestamp":1713408198758},{"id":"hGsJ.cpp","timestamp":1713408240008},{"id":"GZ2r.cpp","timestamp":1713408275057},{"id":"pOel.cpp","timestamp":1713408349370},{"id":"fU5R.cpp","timestamp":1713408366943},{"id":"nr9K.cpp","timestamp":1713408378653},{"id":"oHZX.cpp","timestamp":1713408412576},{"id":"H1CP.cpp","timestamp":1713408591281},{"id":"ynX1.cpp","timestamp":1713408605294},{"id":"969x.cpp","timestamp":1713408646661},{"id":"iDU1.cpp","timestamp":1713408689617},{"id":"muzx.cpp","timestamp":1713419737167},{"id":"0Hjo.cpp","timestamp":1713419747387},{"id":"0TJ9.cpp","timestamp":1713444358539},{"id":"tLbA.cpp","timestamp":1713444426197},{"id":"uneb.cpp","timestamp":1713444476889},{"id":"YVxK.cpp","timestamp":1713444599760},{"id":"I8iT.cpp","timestamp":1713444634952},{"id":"bIKF.cpp","timestamp":1713444668088},{"id":"uyIY.cpp","timestamp":1713444716425},{"id":"hhME.cpp","timestamp":1713444854782}]}

View file

@ -0,0 +1,172 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i] - '0') + (val.fraction[val.fraction.length()-i] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i] - '0') + (val.whole[val.whole.length()-i] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = this->whole[this->whole.length()-i] + result.whole;
i++;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,142 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,162 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i] - '0') + (val.fraction[val.fraction.length()-i] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i] - '0') + (val.whole[val.whole.length()-i] - '0');
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,176 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() || i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<static_cast<string>(val)<<endl;
return output;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
return input;
}
int main(){
BigFloat a("5.34"), b("3.7", 1);
cout<<a+b;
return 0;
}

View file

@ -0,0 +1,162 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,175 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,162 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
for(int j=0;j<this->precision-val.precision;j++){
val.fraction += "0";
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
/*
8.45
6.79
----
15.24
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,137 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,143 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = (sum%10 + '0') + result.fraction;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,167 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,172 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i] - '0') + (val.fraction[val.fraction.length()-i] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i] - '0') + (val.whole[val.whole.length()-i] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i] + result.whole;
i++;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,172 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i] - '0') + (val.fraction[val.fraction.length()-i] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i] - '0') + (val.whole[val.whole.length()-i] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i] + result.whole;
i++;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,143 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,158 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
/*
8.45
6.79
----
15.24
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,167 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i] - '0') + (val.fraction[val.fraction.length()-i] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i] - '0') + (val.whole[val.whole.length()-i] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i] + result.whole;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,140 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
int i = 0;
while(i<this->whole.length()+this->fraction.length() || i<val.whole.length()+val.fraction.length()){
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,160 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,174 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator std::string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}
int main(){
BigFloat a("5.34");
cout<<a;
return 0;
}

View file

@ -0,0 +1,166 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = char(this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
/*
8.456
6.79
----
15.246
*/
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,174 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<static_cast<string>(val)<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}
int main(){
BigFloat a("5.34");
cout<<a;
return 0;
}

View file

@ -0,0 +1,176 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i-1] - '0') + (val.whole[val.whole.length()-i-1] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i-1] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i-1] + result.whole;
i++;
}
if(carry > 0){
result.whole = char(carry + '0') + result.whole;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<static_cast<string>(val)<<endl;
return output;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
return input;
}
int main(){
BigFloat a("5.34"), b("3.7", 1);
cout<<a+b;
return 0;
}

View file

@ -0,0 +1,142 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,146 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
}
while(i<this->fraction.length()){
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,172 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i-1]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i-1]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + (this->fraction[this->fraction.length()-i-1] - '0') + (val.fraction[val.fraction.length()-i-1] - '0');
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
sum = carry + (this->whole[this->whole.length()-i] - '0') + (val.whole[val.whole.length()-i] - '0');
carry = sum/10;
result.whole = char(sum%10 + '0') + result.whole;
i++;
}
while(i<this->whole.length()){
result.whole = this->whole[this->whole.length()-i] + result.whole;
i++;
}
while(i<val.whole.length()){
result.whole = val.whole[val.whole.length()-i] + result.whole;
i++;
}
return result;
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}

View file

@ -0,0 +1,161 @@
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int to_int(string s){
int result = 0;
for(int i=s.length()-1;i>=0;i++){
if(s[i] != '-') result += (s[i] - '0')*pow(10, s.length()-1-i);
}
return result;
}
class BigFloat{
// think about the private data members
// You will need two string data members: one for the integer part and one for the fractional part.
// You will also need a boolean data member to store the sign of the number.
// Additionally, you will need an integer data member to store the precision of the number.
string whole;
string fraction;
int precision;
bool sign; // true = +, false = -
public:
BigFloat(double val = 0.0, int precision = 2) : precision(precision), sign(val>=0){
this->whole = to_string(int(val));
this->fraction = to_string((val - int(val)) * pow(10, precision));
if(this->fraction.length()<this->precision){
for(int i=0;i<this->fraction.length()-this->precision;i++) this->fraction += '0';
}
else if(this->fraction.length()>this->precision){
this->fraction = this->fraction.substr(0, this->fraction.find_first_of('.'));
}
}
BigFloat(const string& text, int precision = 2) : precision(precision), sign(text[0] != '-'){
whole = text.substr(0, text.find_first_of('.'));
fraction = text.substr(text.find_first_of('.')+1, text.length());
fraction = fraction.substr(0, precision);
}
BigFloat(const BigFloat& copy){
*this = copy;
}
// Binary Operators
// Arithmetic Operators
BigFloat operator+(const BigFloat& val) const{
BigFloat result("");
int i = 0, carry = 0, sum = 0;
if(this->precision>val.precision){
while(i<this->precision-val.precision){
result.fraction = (this->fraction[this->fraction.length()-i]) + result.fraction;
i++;
}
}
else if(this->precision<val.precision){
while(i<val.precision-this->precision){
result.fraction = (val.fraction[val.fraction.length()-i]) + result.fraction;
i++;
}
}
while(i<this->fraction.length() && i<val.fraction.length()){
sum = carry + this->fraction[this->fraction.length()-i] + val.fraction[val.fraction.length()-i];
carry = sum/10;
result.fraction = char(sum%10 + '0') + result.fraction;
i++;
}
i = 0;
while(i<this->whole.length() && i<val.whole.length()){
}
}
BigFloat operator-(const BigFloat& val) const{
BigFloat result;
result.whole = to_string(to_int(this->whole) - to_int(val.whole));
result.fraction = to_string(to_int(this->fraction) - to_int(val.fraction));
if(to_int(result.fraction) < 0){
// 2.5-1.7 0.8
result.fraction = to_string(pow(10, result.precision) + to_int(result.fraction));
result.whole = to_string(to_int(result.whole) - 1);
}
return result;
}
BigFloat operator*(const BigFloat& val) const{
}
BigFloat operator/(const BigFloat& val) const;
BigFloat operator%(const BigFloat& val) const; // Modulus Operator
// Compound Assignment Operators
BigFloat operator+=(const BigFloat& rhs){
return *this + rhs;
}
BigFloat operator-=(const BigFloat& rhs){
return *this - rhs;
}
BigFloat operator*=(const BigFloat& rhs){
return *this * rhs;
}
BigFloat operator/=(const BigFloat& rhs){
return *this / rhs;
}
BigFloat operator%=(const BigFloat& rhs){
return *this % rhs;
}
// Logical Operators
bool operator==(const BigFloat& val) const{
return this->whole == val.whole && this->fraction == val.fraction && this->sign == val.sign && this->precision == val.precision;
}
bool operator!=(const BigFloat& val) const{
return !(*this == val);
}
bool operator<(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) < to_int(val.fraction);
}
bool operator<=(const BigFloat& val) const{
if(to_int(this->whole) < to_int(val.whole)) return true;
else if(to_int(this->whole) > to_int(val.whole)) return false;
else return to_int(this->fraction) <= to_int(val.fraction);
}
bool operator>(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) > to_int(val.fraction);
}
bool operator>=(const BigFloat& val) const{
if(to_int(this->whole) > to_int(val.whole)) return true;
else if(to_int(this->whole) < to_int(val.whole)) return false;
else return to_int(this->fraction) >= to_int(val.fraction);
}
// Unary Operators
BigFloat operator+() const{
return *this;
}
BigFloat operator-() const{
BigFloat result(*this);
if(result.sign) result.sign = false;
else result.sign = true;
return result;
}
// Conversion Operator
operator std::string() const{
return this->whole + "." + this->fraction;
}
~BigFloat(){} // destructor
friend std::ostream& operator<<(std::ostream& output, const BigFloat& val);
// outputs the BigFloat
friend std::istream& operator>>(std::istream& input, BigFloat& val); // inputs the BigFloat
};
ostream& operator<<(ostream& output, const BigFloat& val){
output<<val.operator string()<<endl;
}
istream& operator>>(istream& input, BigFloat& val){
string text;
input>>text;
val = BigFloat(text);
}