题目一
【问题描述】
下列主函数涉及分数类Fraction对象的运算,Fraction对象由分子和分母构成,分析给出的代码并设计Fraction类,使主函数程序代码正确执行。提示:需要对相关运算符进行重载并使该分数约分成最简形式。
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 锘?/鍒嗘暟棰樼洰锛屽鏋滃垎姣嶈礋鍒嗗瓙姝g殑璇濓紝鏄彉鎴愬垎瀛愯礋锛屽垎姣嶆锛屽墠缂€鑷锛屽厛绾﹀垎锛屽啀鍒嗗瓙鍑忎竴 #include <iostream> #include <cmath> using namespace std;int maxyue (int a, int b) ; class Fraction { private : int fz; int fm; public : Fraction (); Fraction (int a,int b); Fraction operator =(Fraction ob); void input () ; void print () ; Fraction operator +(Fraction& ob); Fraction operator --(); Fraction operator --(int ); Fraction operator /(Fraction& ob); Fraction operator *(Fraction& ob); Fraction operator -(Fraction& ob); }; Fraction::Fraction () { fz = 0 ; fm = 0 ; } Fraction::Fraction (int a, int b) { int c = maxyue (a, b); a /= c; b /= c; if (a < 0 && b < 0 ) { a = abs (a); b = abs (b); } else if (a * b < 0 ) { a = -abs (a); b = abs (b); } fz = a; fm = b; } Fraction Fraction::operator =(Fraction ob) { int a = ob.fz; int b = ob.fm; if (a < 0 && b < 0 ) { a = abs (a); b = abs (b); } else if (a * b < 0 ) { a = -abs (a); b = abs (b); } this ->fz = a; this ->fm = b; return *this ; } void Fraction::input () { int a = 0 , b = 0 ; char d='\0' ; cin >> a >> d >> b; int c = maxyue (a, b); a /= c; b /= c; if (a < 0 && b < 0 ) { a = abs (a); b = abs (b); } else if (a * b < 0 ) { a = -abs (a); b = abs (b); } fz = a; fm = b; } void Fraction::print () { if (fz == 0 ) cout << "0" << endl; else cout << fz << "/" << fm << endl; } Fraction Fraction::operator +(Fraction& ob) { int a = this ->fz * ob.fm + this ->fm * ob.fz; int b = this ->fm * ob.fm; int c = maxyue (a, b); a /= c; b /= c; if (a < 0 && b < 0 ) { a = abs (a); b = abs (b); } else if (a * b < 0 ) { a = -abs (a); b = abs (b); } Fraction p (a, b) ; return p; } Fraction Fraction::operator --() { this ->fz--; return *this ; } Fraction Fraction::operator --(int ) { Fraction tmp = *this ; this ->fz--; int c = maxyue (fz, fm); fz /= c; fm /= c; return tmp; } Fraction Fraction::operator /(Fraction& ob) { int a = this ->fz * ob.fm; int b = this ->fm * ob.fz; int c = maxyue (a, b); a /= c; b /= c; if (a < 0 && b < 0 ) { a = abs (a); b = abs (b); } else if (a * b < 0 ) { a = -abs (a); b = abs (b); } Fraction p (a, b) ; return p; } Fraction Fraction::operator *(Fraction& ob) { Fraction tmp; int a = this ->fz * ob.fz; int b = this ->fm * ob.fm; int c = maxyue (a, b); a /= c; b /= c; if (a < 0 && b < 0 ) { a = abs (a); b = abs (b); } else if (a * b < 0 ) { a = -abs (a); b = abs (b); } Fraction p (a, b) ; return p; } Fraction Fraction::operator -(Fraction& ob) { int a = this ->fz * ob.fm - ob.fz * this ->fm; int b = this ->fm * ob.fm; int c = maxyue (a, b); a /= c; b /= c; if (a < 0 && b < 0 ) { a = abs (a); b = abs (b); } else if (a * b < 0 ) { a = -abs (a); b = abs (b); } Fraction p (a, b) ; return p; } int maxyue (int a, int b) { int x = a, y = b; x = abs (x); y = abs (y); if (x > y) { int tmp = x; x = y; y = tmp; } int m = 1 ; for (int i = 2 ;i <= x;i++) { if (a % i == 0 && b % i == 0 ) { m = i; } } return m; } int main () { Fraction f1, f2, f3 (4 , -8 ), f4; f1.input (); f2.input (); f4 = f1 + f3; f4.print (); f4 = ----f3; f4.print (); f3 = f1 / f2; f3.print (); f3 = f2--; f3.print (); (f1 * f2).print (); (f1 - f2).print (); return 0 ; }
题目二
【问题描述】
向量运算符重载
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 #include <iostream> using namespace std;class myCArray { private : int * p_arr; int size; public : myCArray (); myCArray (int * p_a,int s); myCArray (const myCArray &r_other); ~myCArray (); int operator [](int pos) const ; int & operator [](int pos); myCArray operator +(myCArray m1) { myCArray m4; for (int i = 0 ; i < m4.size; i++) { m4.p_arr[i] = this ->p_arr[i]; } for (int i = 0 ; i < this ->size ; i++) { m4[i] += m1[i]; } return m4; } myCArray operator -(myCArray m1) { myCArray m3; for (int i = 0 ; i < m3.size; i++) { m3.p_arr[i] = this ->p_arr[i]; } for (int i = 0 ; i < this ->size ; i++) { m3[i] -= m1[i]; } return m3; } void print () const ; }; myCArray::myCArray () { this ->p_arr = new int [5 ]; this ->size = 5 ; } myCArray::myCArray (int * p_a, int s) { this ->p_arr = p_a; this ->size = s; } myCArray::myCArray (const myCArray& r_other) { this ->size = r_other.size; this ->p_arr = r_other.p_arr; } myCArray::~myCArray () { } int myCArray::operator [](int pos) const { int s= *(this ->p_arr + pos); return s; } int & myCArray::operator [](int pos){ return *(this ->p_arr + pos); } void myCArray::print () const { for (int i=0 ;i<this ->size ;i++) cout<<this ->p_arr[i]<<" " ; cout<<endl; } int main () { int a[5 ]={1 ,2 ,3 ,4 ,5 },b[5 ]={1 ,1 ,1 ,1 ,1 }; myCArray t1 (a,5 ) ,t2 (b,5 ) ,t3 ; t3=t1+t2; t3.print (); t3=t1-t2; t3[4 ]=100 ; t3.print (); return 0 ; }
【问题描述】
链表重载
【样例输入】
1 2 3 4 3 1001 AAA1002 BBBB1003 CCC
【样例输出】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ID:1003 Name:CCC ID:1002 Name:BBBB ID:1001 Name:AAA ID:1003 Name:CCC ID:1002 Name:BBBB ID:1001 Name:AAA ID:1003 Name:CCC ID:1002 Name:BBBB ID:1001 Name:AAA ID:1005 Name:Test ID:1003 Name:CCC ID:1002 Name:BBBB ID:1001 Name:AAA ID:1005 Name:Test ID:1003 Name:CCC ID:1002 Name:BBBB ID:1001 Name:AAA Over!
题目三
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 #include <iostream> using namespace std;class Student { public : Student (string id = "" , string name = "" ); virtual ~Student (); string Getid () { return mId; } void Setid (string val) { mId = val; } string GetName () { return mName; } void SetName (string val) { mName = val; } void Show () { cout << "ID:" << this ->mId << " Name:" << this ->mName << endl; } protected :private : string mId; string mName; }; Student::Student (string id, string name) { this ->mId = id; this ->mName = name; } Student::~Student () { } class LinkList ;class Node { public : Node (const Student& t); Node (string id, string name); virtual ~Node (); Student GetData () { return mData; } void SetData (Student val) { mData = val; } Node* GetNext () { return mNext; } void SetNext (Node* val) { mNext = val; } friend class LinkList ; protected :private : Student mData; Node* mNext; }; Node::Node (const Student& t) :mData (t) { this ->mNext = NULL ; } Node::Node (string id="" , string name="" ) : mData (id, name) { this ->mNext = NULL ; } Node::~Node () { } class LinkList { public : LinkList (); LinkList (const LinkList& list1); virtual ~LinkList (); void InsertNodeAtHead (const Student& t) ; void Show () ; void DeleteLinkList () ; LinkList operator =(const LinkList& list1); protected : Node* mHead; void copylist (const LinkList& list1) ; int mLen; }; LinkList::LinkList () { this ->mHead = NULL ; this ->mLen = 0 ; } LinkList::~LinkList () { this ->DeleteLinkList (); } void LinkList::InsertNodeAtHead (const Student& t) { Node* temp; temp = new Node (t); if (this ->mHead == NULL ) { this ->mHead = temp; this ->mLen++; } else { temp->mNext = this ->mHead; this ->mHead = temp; this ->mLen++; } } void LinkList::Show () { Node* temp; temp = this ->mHead; while (temp != NULL ) { temp->mData.Show (); temp = temp->mNext; } } void LinkList::DeleteLinkList () { if (mHead != NULL ) { Node* pTmp; while (mHead != NULL ) { pTmp = mHead->mNext; delete mHead; mHead = pTmp; } } } LinkList::LinkList (const LinkList& list1) { Node* p,*q; q = list1.mHead; p = this ->mHead=new Node (*q); this ->mLen = list1.mLen; if (q != NULL ) { q = q->mNext; while (q != NULL ) { p->mNext = new Node (*q); p = p->mNext; q = q->mNext; } } } void LinkList::copylist (const LinkList& list1) { Node* p; this ->mLen = list1.mLen; p = list1.mHead; if (p != NULL ) { Node* q; q = this ->mHead = new Node (*p); p = p->mNext; while (p != NULL ) { q->mNext= new Node (*p); p = p->mNext; q = q->mNext; } } } LinkList LinkList::operator =(const LinkList& list1) { copylist (list1); return *this ; } int main () { LinkList list1, list2; int n; string id, name; cin >> n; Student st; for (int i = 0 ; i < n; i++) { cin >> id >> name; st.Setid (id); st.SetName (name); list1.InsertNodeAtHead (st); } list1.Show (); list2 = list1; list2.Show (); list2.InsertNodeAtHead (Student ("1005" , "Test" )); LinkList list3 (list2) ; list1.Show (); list2.Show (); list3.Show (); cout << "Over!" << endl; return 0 ; }
题目四
【问题描述】
对时钟类进行重载
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 #include <iostream> using namespace std;class Clock { public : Clock (int NewH=0 , int NewM=0 , int NewS=0 ); void ShowTime () ; Clock operator ++(); Clock operator ++(int ); Clock operator +(const Clock& other) { int newHour = (Hour + other.Hour) % 24 ; int newMinute = Minute + other.Minute; int newSecond = Second + other.Second; if (newSecond >= 60 ) { newSecond -= 60 ; newMinute++; } if (newMinute >= 60 ) { newMinute -= 60 ; newHour = (newHour + 1 ) % 24 ; } return Clock (newHour, newMinute, newSecond); } private : int Hour, Minute, Second; }; Clock Clock::operator ++() { Second++; if (Second >= 60 ) { Second -= 60 ; Minute++; if (Minute >= 60 ) { Minute -= 60 ; Hour = (Hour + 1 ) % 24 ; } } return *this ; } Clock Clock::operator ++(int ) { Clock temp (*this ) ; ++(*this ); return temp; } Clock::Clock (int NewH, int NewM, int NewS) { Hour = NewH; Minute = NewM; Second = NewS; } void Clock::ShowTime () { cout<<Hour<<":" <<Minute<<":" <<Second<<endl; } int main () { Clock c1 (23 ,59 ,59 ) ,c2 (5 ,12 ,10 ) ,c3 ; c1.ShowTime (); c1++; c1.ShowTime (); c3=++c2; c3.ShowTime (); c3=c2+c1; c3.ShowTime (); return 0 ; }