U E D R , A S I H C RSS

미시Cpp/0901

  • fixed point real number code

6자리의 고정된 소수점의 연산자 오버로딩
나눗셈을 제외한 8개의 이항연산자(+, -, *, +=, -=, *=, =, ==)와 2개의 단항연산자(++, --)를 오버로딩

홍성현

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class FixedRealNum {
private:
	long long mInt;
	long long fraction;
	const int INDICATOR = 1000000;

public:
	FixedRealNum() {
		mInt = 0;
		fraction = 0;
	}

	FixedRealNum(float factor) {
		factor *= INDICATOR;
		this->mInt = (int)factor / INDICATOR;
		this->fraction = (int)factor % INDICATOR;
	}

	FixedRealNum operator+(FixedRealNum rhs) {
		FixedRealNum temp(0.0);
		temp.mInt = this->mInt + rhs.mInt;
		temp.fraction = this->fraction + rhs.fraction;

		if (temp.fraction >= INDICATOR) {
			temp.fraction -= INDICATOR;
			temp.mInt++;
		}
		return temp;
	}

	FixedRealNum operator-(FixedRealNum rhs) {
		FixedRealNum temp(0.0);
		temp.mInt = this->mInt - rhs.mInt;
		temp.fraction = this->fraction - rhs.fraction;
		if (temp.fraction < 0) {
			temp.mInt--;
			temp.fraction *= -1;
		}

		return temp;
	}

	FixedRealNum operator*(FixedRealNum rhs) {
		FixedRealNum temp(0.0);
		temp.mInt = this->mInt * rhs.mInt + (this->mInt * rhs.fraction) / INDICATOR + (this->fraction * rhs.mInt) / INDICATOR;
		temp.fraction = (this->mInt * rhs.fraction) % INDICATOR + (this->fraction * rhs.mInt) % INDICATOR + (this->fraction * rhs.fraction) / INDICATOR;
		return temp;
	}

	FixedRealNum& operator+=(FixedRealNum rhs) {
		this->mInt = this->mInt + rhs.mInt;
		this->fraction = this->fraction + rhs.fraction;

		if (this->fraction >= INDICATOR) {
			this->fraction -= INDICATOR;
			this->mInt++;
		}
		return *this;
	}

	FixedRealNum& operator-=(FixedRealNum rhs) {
		this->mInt = this->mInt - rhs.mInt;
		this->fraction = this->fraction - rhs.fraction;
		if (this->fraction < 0) {
			this->mInt--;
			this->fraction *= -1;
		}
		return *this;
	}

	FixedRealNum& operator*=(FixedRealNum rhs) {
		this->mInt = this->mInt * rhs.mInt + (this->mInt * rhs.fraction) / INDICATOR + (this->fraction * rhs.mInt) / INDICATOR;
		this->fraction = (this->mInt * rhs.fraction) % INDICATOR + (this->fraction * rhs.mInt) % INDICATOR + (this->fraction * rhs.fraction) / INDICATOR;
		return *this;

	}

	bool operator==(FixedRealNum rhs) {
		return ((this->mInt == rhs.mInt) && (this->fraction == rhs.fraction));
	}

	bool operator!=(FixedRealNum rhs) {
		return !(*this == rhs);
	}

	string toString() {
		ostringstream buff;

		buff << this->mInt << "." << this->fraction;
		return buff.str();
	}
};


김상헌

//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);
}

피드백

  • 피드백은 아니지만 체스를 던진 후 cpp 복습을 게을리 했더니 벌써 날라가기 시작했습니다...시무룩

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2015-09-01 13:32:32
Processing time 0.0095 sec