From 9a6e68a1374f9bed5119c7bc62a97028670f614f Mon Sep 17 00:00:00 2001 From: IoTcat Date: Wed, 10 Apr 2019 21:31:14 +0800 Subject: [PATCH] find change --- as2/ex1/Fraction.h | 62 +++++++++++++++++++++++++++++++++++++++ as2/ex1/ex1.cpp | 67 ++++++++++++++++--------------------------- as2/ex2/archer.cpp | 2 +- as2/ex2/mage.cpp | 2 +- as2/ex2/swordsman.cpp | 2 +- 5 files changed, 90 insertions(+), 45 deletions(-) diff --git a/as2/ex1/Fraction.h b/as2/ex1/Fraction.h index d4df07b..38d1480 100644 --- a/as2/ex1/Fraction.h +++ b/as2/ex1/Fraction.h @@ -658,6 +658,52 @@ class iFraction: public Fraction { /* 析构 */ ~iFraction(){}; + + /* 加法重载 */ + iFraction operator +(const iFraction& obj) const{ + return iFraction(this->top() * obj.bottom() + this->bottom() * obj.top(), this->bottom() * obj.bottom()); + }; + + /* 加法重载(后) */ + friend iFraction operator +(const double d, const iFraction& obj){ + return obj + d; + }; + + /* 减法重载 */ + iFraction operator -(const iFraction& obj) const{ + return iFraction(this->top() * obj.bottom() - this->bottom() * obj.top(), this->bottom() * obj.bottom()); + }; + + /* 乘法重载 */ + iFraction operator *(const iFraction& obj) const{ + return iFraction(this->top() * obj.top(), this->bottom() * obj.bottom()); + }; + + /* 乘法重载(后) */ + friend iFraction operator *(const double d, const iFraction& obj){ + return obj * d; + }; + + /* 除法重载 */ + iFraction operator /(const iFraction& obj) const{ + return iFraction(this->top() * obj.bottom(), this->bottom() * obj.top()); + }; + + /* 求余重载 */ + iFraction operator %(const iFraction& obj) const{ + return iFraction(fmod(this->val(), obj.val())); + }; + + /* 求余重载 */ + iFraction operator %(const int t) const{ + return iFraction(fmod(this->val(), t)); + }; + + /* 求余重载 */ + iFraction operator %(const double t) const{ + return iFraction(fmod(this->val(), t)); + }; + /* 前++重载 */ iFraction operator ++(){ if(this->_isNegative) this->_top -= this->_bottom; @@ -672,6 +718,22 @@ class iFraction: public Fraction { return *this; }; + /* 后++重载 */ + iFraction operator ++(int){ + iFraction f = *this; + if(_isNegative) _top -= _bottom; + else _top += _bottom; + return f; + }; + + /* 后--重载 */ + iFraction operator --(int){ + iFraction f = *this; + if(_isNegative) _top += _bottom; + else _top -= _bottom; + return f; + }; + /* 负号重载 */ iFraction operator -() const{ return iFraction(-this->top(), this->bottom()); diff --git a/as2/ex1/ex1.cpp b/as2/ex1/ex1.cpp index c45d205..a706b09 100644 --- a/as2/ex1/ex1.cpp +++ b/as2/ex1/ex1.cpp @@ -1,34 +1,21 @@ #include +#include #include "Fraction.h" using namespace std; int main() { - Fraction a; - Fraction b(3,-4); - Fraction c(5); - Fraction d(-3.14); - Fraction e = 1.333333; - Fraction f = -b; + iFraction a; + iFraction b(1,3,-4); + iFraction c = convertF(Fraction(3,-4)); + iFraction d(-3.14); + iFraction e = 1.333333; + iFraction f = -b; - Fraction q(-3.2); - iFraction p(-3.2); - - //p += q + p; - - //q = p; - - iFraction o; - - - cout << p.integer(); - - -/* - cout << "Output in Fraction Form: " << endl; + cout << "Output in iFraction Form: " << endl; cout << "a: " << a << endl; cout << "b: " << b << endl; cout << "c: " << c << endl; @@ -46,13 +33,22 @@ int main() cout << "f: " << f.val() << endl << endl; + cout << "Output integer: " << endl; + cout << "a: " << a.integer() << endl; + cout << "b: " << b.integer() << endl; + cout << "c: " << c.integer() << endl; + cout << "d: " << d.integer() << endl; + cout << "e: " << e.integer() << endl; + cout << "f: " << f.integer() << endl << endl; + + cout << "Output Numerator: " << endl; - cout << "a: " << a.top() << endl; - cout << "b: " << b.top() << endl; - cout << "c: " << c.top() << endl; - cout << "d: " << d.top() << endl; - cout << "e: " << e.top() << endl; - cout << "f: " << f.top() << endl << endl; + cout << "a: " << a.itop() << endl; + cout << "b: " << b.itop() << endl; + cout << "c: " << c.itop() << endl; + cout << "d: " << d.itop() << endl; + cout << "e: " << e.itop() << endl; + cout << "f: " << f.itop() << endl << endl; cout << "Output Denominator: " << endl; @@ -110,26 +106,13 @@ int main() cout << "Devide 0 test: "; try{ - Fraction g(1,0); + iFraction g(2,1,0); }catch(const char* msg){ cout << msg << endl; } cout << endl << "************ This is the END of TEST section !! ************" << endl << endl; - cout << "Have a try by YOURSELF!! (Press Ctrl+C to quit)" << endl; - - while(1){ + system("pause"); - try{ - cout << endl << "Please Input a Fraction(-3/5) or a Decimals(e.g. 3.14): "; - cin >> a; - cout << a << endl; - }catch(const char* msg){ - cout << endl << msg << endl; - } - - - } - */ return 0; } \ No newline at end of file diff --git a/as2/ex2/archer.cpp b/as2/ex2/archer.cpp index c1b860a..43aef35 100644 --- a/as2/ex2/archer.cpp +++ b/as2/ex2/archer.cpp @@ -56,7 +56,7 @@ bool archer::attack(player &p) double EXPtemp=0; // player obtained exp double hit=1; // attach factor, probably give critical attack srand((unsigned)time(NULL)); // generating random seed based on system time - int attack = (speed*1.8)/p.DP; //depand on player's speed and p.DP + float attack = ((float)speed*1.8/(float)p.DP); //depand on player's speed and p.DP // If speed greater than opponent, you have some possibility to do double attack if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 diff --git a/as2/ex2/mage.cpp b/as2/ex2/mage.cpp index 637b30e..d74a0f7 100644 --- a/as2/ex2/mage.cpp +++ b/as2/ex2/mage.cpp @@ -56,7 +56,7 @@ bool mage::attack(player &p) double EXPtemp=0; // player obtained exp double hit=1; // attach factor, probably give critical attack srand((unsigned)time(NULL)); // generating random seed based on system time - int attack = (EXP/75 +25)/p.DP; + float attack = ((float)EXP/((float)75 +25)/(float)p.DP)+1; // If speed greater than opponent, you have some possibility to do double attack if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 diff --git a/as2/ex2/swordsman.cpp b/as2/ex2/swordsman.cpp index d75536b..f9e444b 100644 --- a/as2/ex2/swordsman.cpp +++ b/as2/ex2/swordsman.cpp @@ -56,7 +56,7 @@ bool swordsman::attack(player &p) double EXPtemp=0; // player obtained exp double hit=1; // attach factor, probably give critical attack srand((unsigned)time(NULL)); // generating random seed based on system time - int attack = AP/p.DP; //depand on player's speed and p.DP + float attack = ((float)AP/(float)p.DP); //depand on player's speed and p.DP // If speed greater than opponent, you have some possibility to do double attack if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100