C++练习 | 运算符重载练习
#include <iostream> #include <cmath> #include <cstring> #include <iomanip> using namespace std; int gcd(int x,int y) { x=abs(x); y=abs(y); if(x<y) { int t=x; x=y; y=t; } if(x%y==0) return y; else return gcd(x%y,y); } class Rational { private: int z; //分子 int m; //分母 public: Rational(int a=0, int b=1)//构造有理数分数,分子默认为0,分母默认为1 { z=a; m=b; } friend Rational& yuefen(Rational& r)//约分函数对分数化简 { int c; c=gcd(r.z,r.m); r.z=r.z/c; r.m=r.m/c; if(r.z>0&&r.m<0) { r.z=0-r.z; r.m=0-r.m; } if(r.z<0&&r.m<0) { r.z=abs(r.z); r.m=abs(r.m); } return r; } friend Rational operator+(const Rational &r1, const Rational &r2) { Rational t; t.m=r1.m*r2.m; t.z=r1.z*r2.m+r2.z*r1.m; return t; } friend Rational operator-(const Rational &r1, const Rational &r2) { Rational t; t.m=r1.m*r2.m; t.z=r1.z*r2.m-r2.z*r1.m; return t; } friend Rational operator*(const Rational &r1, const Rational &r2) { Rational t; t.m=r1.m*r2.m; t.z=r1.z*r2.z; return t; } friend Rational operator/(const Rational &r1, const Rational &r2) { Rational t; t.m=r1.m*r2.z; t.z=r1.z*r2.m; return t; } Rational & operator+=(const Rational &r) { Rational t; t.m=this->m*r.m; t.z=this->z*r.m+r.z*this->m; this->z=t.z; this->m=t.m; return *this; } Rational & operator-=(const Rational &r) { Rational t; t.m=this->m*r.m; t.z=this->z*r.m-r.z*this->m; this->m=t.m; this->z=t.z; return *this; } Rational & operator*=(const Rational &r) { this->m=this->m*r.m; this->z=this->z*r.z; return *this; } Rational & operator/=(const Rational &r) { Rational t; t.m=this->m*r.z; t.z=this->z*r.m; this->m=t.m; this->z=t.z; return *this; } friend bool operator==(const Rational &s1, const Rational &s2)//判断两个有理数是否相等 { int m1,m2,z1,z2,t1,t2; t1=gcd(s1.z,s1.m); t2=gcd(s2.z,s2.m); m1=s1.m/t1; m2=s2.m/t2; z1=s1.z/t1; z2=s1.z/t2; if(m1==m2&&z1==z2) return 1; else return 0; } friend bool operator!=(const Rational &s1, const Rational &s2)//判断两个有理数是否不等 { int m1,m2,z1,z2,t1,t2; t1=gcd(s1.z,s1.m); t2=gcd(s2.z,s2.m); m1=s1.m/t1; m2=s2.m/t2; z1=s1.z/t1; z2=s1.z/t2; if(m1==m2&&z1==z2) return 0; else return 1; } friend ostream & operator<<(ostream &t1, const Rational &t2) { t1<<t2.z<<"/"<<t2.m; return t1; } friend istream & operator>>(istream &t1, Rational &t2) { t1>>t2.z>>t2.m; return t1; } }; int main() { Rational r1,r2,r3; while(cin>>r1>>r2) { cout << "r1 = " << yuefen(r1) << "\n" << "r2 = " << yuefen(r2) << endl; r3 = r1 + r2; cout << "r1+r2 = " << yuefen(r3) << endl; r3 = r1 - r2; cout << "r1-r2 = " << yuefen(r3) << endl; r3 = r1 * r2; cout << "r1*r2 = " << yuefen(r3) << endl; r3 = r1 / r2; cout << "r1/r2 = " << yuefen(r3) << endl; cout << (r1 == r2) << " " << (r1 != r2) << endl; cout << yuefen(r1 += r2) << endl; cout << yuefen(r1 -= r2) << endl; cout << yuefen(r1 *= r2) << endl; cout << yuefen(r1 /= r2) << endl; } return 0; }
#include <iostream> #include <cmath> #include <cstring> #include <iomanip> using namespace std; class CheckedPtr { public: CheckedPtr(int * b, int * e):beg(b), end(e), curr(b){ } CheckedPtr & operator ++()// prefix ++ { this->curr+=1; return *this; } CheckedPtr & operator --() // prefix -- { this->curr-=1; return *this; } CheckedPtr operator ++(int)// postfix ++,()内int用于c区分前置后置 { CheckedPtr t=*this; this->curr+=1; return t; } CheckedPtr operator --(int)// postfix -- { CheckedPtr t=*this; this->curr-=1; return t; } int * GetBeg() { return beg; } int * GetEnd() { return end; } int * GetCurr() { return curr; } private: int * beg; // pointer to beginning of the array int * end; // one past the end of the array int * curr; // current position within the array }; int main() { int array[10] = {1,2,3,4,5,6,7,8,9,10}; CheckedPtr cp(array, array+10); for(;cp.GetCurr()<cp.GetEnd();cp++) cout<<*cp.GetCurr()<<" "; cout<<endl; for(--cp;cp.GetCurr()>cp.GetBeg();cp--) cout<<*cp.GetCurr()<<" "; cout<<*cp.GetCurr()<<endl; return 0; }