U E D R , A S I H C RSS

미시Cpp/0901 (rev. 1.1)

미시Cpp/0901

  • fixed point real number code

김상헌

//Fixed Point real number
//소수점 밑 6자리 까지
//사칙연산, toString구현

#include <iostream>
#include <string>

using namespace std;
using std::string;

class FixedRealNum{
private:
	int x, y;
public:
	FixedRealNum(){
		x = 0;
		y = 0;
	}
	FixedRealNum(float num){
		x = (int)num;
		y = (num - (int)num) * 1000000;
		while (y > 10000000){
			y /= 10;
		}
	}

	FixedRealNum operator+ (FixedRealNum &rhs){
		FixedRealNum temp;
		temp.x = this->x + rhs.x;
		temp.y= this->y + rhs.y;
		if (temp.y > 10000000){
			temp.y -= 10000000;
			temp.x++;
		}
		return temp;
	}
	FixedRealNum operator+= (FixedRealNum &rhs){
		this->x += rhs.x;
		this->y += rhs.y;
		if (this->y > 10000000){
			this->y -= 10000000;
			this->x++;
		}
		return *this;
	}

	FixedRealNum operator- (FixedRealNum &rhs){
		FixedRealNum temp;
		temp.x = this->x - rhs.x;
		temp.y = this->y - rhs.y;
		if (temp.y < 0){
			temp.y = (temp.y + 1000000) % 100000;
			temp.x--;
		}
		return temp;
	}
	FixedRealNum operator-= (FixedRealNum &rhs){
		this->x -= rhs.x;
		this->y -= rhs.y;
		if (this->y < 0){
			this->y = (this->y + 1000000) % 100000;
			this->x--;
		}
		return *this;
	}

	FixedRealNum operator* (FixedRealNum &rhs){
		FixedRealNum temp;
		int mul_one = this->x * rhs.x;
		float mul_two = this->x * (rhs.y / 1000000.0);
		float mul_three = rhs.x * (this->y / 1000000.0);
		float mul_four = (this->y / 1000000.0) * (rhs.y / 1000000.0);
		float mul_float = mul_two + mul_three + mul_four;
		mul_one += (int)mul_float;
		mul_float = mul_float - (int)mul_float;
		temp.x = mul_one;
		temp.y = mul_float * 1000000;
		return temp;
	}
	FixedRealNum operator*= (FixedRealNum &rhs){
		int mul_one = this->x * rhs.x;
		float mul_two = this->x * (rhs.y / 1000000.0);
		float mul_three = rhs.x * (this->y / 1000000.0);
		float mul_four = (this->y / 1000000.0) * (rhs.y / 1000000.0);
		float mul_float = mul_two + mul_three + mul_four;
		mul_one += (int)mul_float;
		mul_float = mul_float - (int)mul_float;
		this->x = mul_one;
		this->y = mul_float * 1000000;
		return *this;
	}
	
	bool operator== (FixedRealNum &rhs){
		if (this->x == rhs.x && this->y == rhs.y) return true;
		else return false;
	}
	bool operator!= (FixedRealNum &rhs){
		if (this->x == rhs.x && this->y == rhs.y) return false;
		else return true;
	}

	string toString(){
		string myString("");
		myString += to_string(x);
		myString += ".";
		myString += to_string(y);
		return myString;
	}
};

int main(){
	FixedRealNum p1(456.10000);
	FixedRealNum p2(123.45670);
	FixedRealNum p3 = p1 + p2;
	FixedRealNum q3 = p1;
	q3 += p2;
	FixedRealNum p4 = p1 - p2;
	FixedRealNum q4 = p1;
	q4 -= p2;
	FixedRealNum p5 = p1 * p2;
	FixedRealNum q5 = p1;
	q5 *= p2;
	cout << p3.toString() << endl << p4.toString() << endl<< p5.toString() <<endl;
	cout << q3.toString() << endl << q4.toString() << endl << q5.toString() << endl;
	if (p1 == p1) cout << "true" << endl;
	if (p1 != p2) cout << "true" << endl;
	while (1);
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:31
Processing time 0.0159 sec