用C++实现复数类以及大整数类,主要是练习C++的运算符重载等知识。
复数类:
主要实现构造,重载+-运算符。
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 class Complex { 5 public: 6 double real; 7 double image; 8 Complex(double r=2.0,double i=-2.0); 9 Complex(const Complex& c); 10 11 //一元运算符推荐不用友元 12 Complex& operator += (const Complex& c); 13 Complex& operator -= (const Complex& c); 14 15 //二元运算符推荐用友元 16 friend bool operator == (const Complex& c1,const Complex& c2); 17 friend Complex operator + (const Complex& c1,const Complex& c2); 18 friend ostream& operator << (ostream& o,const Complex& c); 19 }; 20 21 Complex::Complex(double r,double i) : real(r),image(i) { } 22 Complex::Complex(const Complex& c) { 23 this->real=c.real; 24 this->image=c.image; 25 } 26 27 Complex& Complex::operator +=(const Complex& c) { 28 this->real+=c.real; 29 this->image+=c.image; 30 return *this; 31 } 32 33 Complex& Complex::operator -=(const Complex& c) { 34 this->real-=c.real; 35 this->image-=c.image; 36 return *this; 37 } 38 39 bool operator == (const Complex& c1,const Complex& c2) { 40 return (c1.real==c2.real && c1.image==c2.image); 41 } 42 43 Complex operator + (const Complex& c1,const Complex &c2) { 44 Complex ret(c1.real+c2.real,c1.image+c2.image); 45 return ret; 46 } 47 48 ostream& operator << (ostream& o,const Complex& c) { 49 o<<c.real<<" "<<c.image<<endl; 50 return o; 51 } 52 53 int main() 54 { 55 Complex c1; 56 Complex c2(3.0,-3.0); 57 Complex c3=c1; 58 cout<<"c1:"<<c1<<"c2:"<<c2<<"c3:"<<c3; 59 cout<<"c1==c2 ? "<<(c1==c2)<<endl; 60 cout<<"c1==c3 ? "<<(c1==c3)<<endl; 61 cout<<"c1+c2 : "<<(c1+c2); 62 63 c1+=c2; 64 cout<<"c1:"<<c1; 65 c1-=c3; 66 cout<<"c1:"<<c1; 67 return 0; 68 }
大整数类:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 class BigInteger { 5 private: 6 vector<char> digits; 7 bool sign; 8 void trim(); 9 10 public: 11 BigInteger(); 12 BigInteger(int b); 13 BigInteger(const string& s); 14 BigInteger(const BigInteger& b); 15 16 friend int abscompare(const BigInteger& b1,const BigInteger& b2); 17 friend bool operator == (const BigInteger& b1,const BigInteger& b2); 18 friend bool operator < (const BigInteger& b1,const BigInteger& b2); 19 friend bool operator > (const BigInteger& b1,const BigInteger& b2); 20 21 BigInteger operator - (); //负号 22 BigInteger& operator += (const BigInteger& b); 23 BigInteger& operator -= (const BigInteger& b); 24 BigInteger& operator *= (const BigInteger& b); 25 BigInteger& operator /= (const BigInteger& b); 26 27 friend void carry(BigInteger& b); 28 friend BigInteger operator + (const BigInteger& b1,const BigInteger& b2); 29 friend BigInteger operator - (const BigInteger& b1,const BigInteger& b2); 30 friend BigInteger operator * (const BigInteger& b1,const BigInteger& b2); 31 friend BigInteger operator / (const BigInteger& b1,const BigInteger& b2); 32 33 void print(); 34 }; 35 36 BigInteger::BigInteger() { 37 sign=true; 38 } 39 BigInteger::BigInteger(int b) { 40 if (b<0) { sign=false; b=-b; } 41 else sign=true; 42 while (b) { 43 digits.push_back(char('0'+b%10)); 44 b/=10; 45 } 46 } 47 BigInteger::BigInteger(const string& s) { 48 int start=0,len=s.length(); 49 if (s[0]=='-') { start=1; sign=false; } 50 else { start=0; sign=true; } 51 52 for (int i=len-1;i>=start;i--) 53 digits.push_back(s[i]); 54 } 55 BigInteger::BigInteger(const BigInteger& b) { 56 sign=b.sign; 57 digits=b.digits; 58 } 59 60 void BigInteger::print() { 61 if (!sign) cout<<"-"; 62 for (int i=digits.size()-1;i>=0;i--) 63 cout<<digits[i]; 64 //cout<<endl; 65 } 66 67 BigInteger BigInteger::operator - () { 68 BigInteger tmp=*this; 69 tmp.sign=1-tmp.sign; 70 return tmp; 71 } 72 73 //比较b1和b2的绝对值大小,b1>b2返回1,b1==b2返回0,b1<b2返回-1 74 int abscompare(const BigInteger& b1,const BigInteger& b2) { 75 if (b1.digits.size()>b2.digits.size()) return 1; 76 if (b1.digits.size()<b2.digits.size()) return -1; 77 bool bigger=0,smaller=0; 78 int len=b1.digits.size(); 79 for (int i=len-1;i>=0;i--) { 80 if (b1.digits[i]>b2.digits[i]) { bigger=true; break; } 81 if (b1.digits[i]<b2.digits[i]) { smaller=true; break; } 82 } 83 if (bigger) return 1; 84 else if (smaller) return -1; 85 else return 0; 86 } 87 bool operator == (const BigInteger& b1,const BigInteger& b2) { 88 if (b1.sign!=b2.sign) return false; 89 return (abscompare(b1,b2)==0); 90 } 91 bool operator < (const BigInteger& b1,const BigInteger& b2) { 92 if (b1.sign==true && b2.sign==false) return false; 93 if (b1.sign==false && b2.sign==true) return true; 94 int cmp=abscompare(b1,b2); 95 if (b1.sign==true) 96 if (cmp>=0) return false; 97 else return true; 98 if (b1.sign==false) 99 if (cmp<=0) return true; 100 else return false; 101 } 102 bool operator > (const BigInteger& b1,const BigInteger& b2) { 103 if (b1.sign==true && b2.sign==false) return true; 104 if (b1.sign==false && b2.sign==true) return false; 105 int cmp=abscompare(b1,b2); 106 if (b1.sign==true) 107 if (cmp<=0) return false; 108 else return true; 109 if (b1.sign==false) 110 if (cmp>=0) return true; 111 else return false; 112 } 113 114 void carry(BigInteger &b) { 115 int len=b.digits.size(); 116 for (int i=0;i<len-1;i++) { 117 b.digits[i+1]+=(b.digits[i]-'0')/10; 118 b.digits[i]=(b.digits[i]-'0')%10+'0'; 119 } 120 while (b.digits[b.digits.size()-1]>='0'+10) { 121 int t=b.digits.size()-1; 122 b.digits.push_back((b.digits[t]-'0')/10+'0'); 123 b.digits[t]=(b.digits[t]-'0')%10+'0'; 124 } 125 } 126 BigInteger operator + (const BigInteger& b1,const BigInteger& b2) { 127 if (b1.sign!=b2.sign) { 128 BigInteger newb1(b1),newb2(b2); 129 if (b1.sign==true && b2.sign==false) return (newb1-(-newb2)); 130 else return newb2-(-newb1); 131 } 132 int maxlen=max(b1.digits.size(),b2.digits.size()); 133 BigInteger ret; 134 for (int i=0;i<maxlen;i++) 135 if (b1.digits.size()-1>=i && b2.digits.size()-1>=i) ret.digits.push_back(b1.digits[i]+b2.digits[i]-'0'); 136 else if (b1.digits.size()-1>=i) ret.digits.push_back(b1.digits[i]); 137 else ret.digits.push_back(b2.digits[i]); 138 carry(ret); 139 ret.sign=b1.sign; 140 return ret; 141 } 142 BigInteger operator - (const BigInteger& b1,const BigInteger& b2) { //待完善 143 if (b1.sign!=b2.sign) { 144 BigInteger newb1(b1),newb2(b2); 145 return (newb1+(-newb2)); 146 } 147 if (b1.sign==false && b2.sign==false) { 148 BigInteger newb1(b1),newb2(b2); 149 return (-newb2)-(-newb1); 150 } 151 if (b1<b2) { 152 BigInteger newb1(b1),newb2(b2); 153 return -(newb2-newb1); 154 } 155 156 BigInteger ret; 157 int maxlen=max(b1.digits.size(),b2.digits.size()); 158 for (int i=0;i<maxlen;i++) 159 if (b1.digits.size()-1>=i && b2.digits.size()-1>=i) ret.digits.push_back(b1.digits[i]-b2.digits[i]); 160 else ret.digits.push_back(b1.digits[i]-'0'); 161 for (int i=0;i<maxlen;i++) 162 if (ret.digits[i]<0) { 163 ret.digits[i+1]--; 164 ret.digits[i]+=10; 165 } 166 while (ret.digits.size()>1 && ret.digits[ret.digits.size()-1]==0) ret.digits.pop_back(); 167 for (int i=0;i<ret.digits.size();i++) ret.digits[i]+='0'; 168 return ret; 169 } 170 BigInteger operator * (const BigInteger& b1,const BigInteger& b2) { 171 BigInteger ret; 172 ret.sign=(b1.sign==b2.sign); 173 int len1=b1.digits.size(),len2=b2.digits.size(); 174 vector<int> tmp(len1+len2,0); 175 for (int i=0;i<len1;i++) 176 for (int j=0;j<len2;j++) 177 tmp[i+j]+=(b1.digits[i]-'0')*(b2.digits[j]-'0'); 178 for (int i=0;i<(len1+len2-1);i++) { 179 tmp[i+1]+=tmp[i]/10; 180 tmp[i]%=10; 181 } 182 while (tmp[tmp.size()-1]>=10) { 183 tmp.push_back(tmp[tmp.size()-1]/10); 184 tmp[tmp.size()-2]%=10; 185 } 186 for (int i=0;i<tmp.size();i++) 187 ret.digits.push_back(tmp[i]+'0'); 188 while (ret.digits.size()>1 && ret.digits[ret.digits.size()-1]=='0') ret.digits.pop_back(); 189 return ret; 190 } 191 192 BigInteger& BigInteger::operator +=(const BigInteger& b) { 193 *this=*this+b; 194 return *this; 195 } 196 BigInteger& BigInteger::operator -=(const BigInteger& b) { 197 *this=*this-b; 198 return *this; 199 } 200 BigInteger& BigInteger::operator *=(const BigInteger& b) { 201 *this=*this*b; 202 return *this; 203 } 204 205 int main() 206 { 207 BigInteger b1(81532); 208 BigInteger b2(-156156131); 209 BigInteger b3(string("-1561565")); 210 cout<<"b1: "; b1.print(); cout<<endl; 211 cout<<"b2: "; b2.print(); cout<<endl; 212 cout<<"b3: "; b3.print(); cout<<endl; 213 BigInteger b4(b2); 214 cout<<"b4: "; b4.print(); cout<<endl; 215 216 BigInteger b5(81533); 217 cout<<"-b5 : "; (-b5).print(); cout<<endl; 218 cout<<"b2==b1 ? "<<(b2==b1)<<endl; 219 cout<<"b2==b4 ? "<<(b2==b4)<<endl; 220 cout<<"b1>b5 ? "<<(b1>b5)<<endl; 221 cout<<"b1<b5 ? "<<(b1<b5)<<endl; 222 cout<<"b5<-b5 ? "<<(-b5>b5)<<endl; 223 224 //开始测试加减乘除 225 BigInteger b6=b1+b5; 226 cout<<"b1+b5 : "; b6.print(); cout<<endl; 227 cout<<"b1-b5 : "; (b1-b5).print(); cout<<endl; 228 cout<<"b4-b2 : "; (b4-b2).print(); cout<<endl; 229 cout<<"b2-b1 : "; (b2-b1).print(); cout<<endl; 230 cout<<"b2+b1 : "; (b2+b1).print(); cout<<endl; 231 cout<<"b1*b5 : "; (b1*b5).print(); cout<<endl; 232 cout<<"b1*b2 : "; (b1*b2).print(); cout<<endl; 233 234 //测试+=,-=,*=,/=; 235 b5*=b1; 236 cout<<"b1*b5 : "; b5.print(); cout<<endl; 237 b1*=b2; 238 cout<<"b1*b2 : "; b1.print(); cout<<endl; 239 return 0; 240 }