1.RMB類
要求:
定義一個RMB類 Money,包含元、角、分三個數據成員,友元函數重載運算符‘+’(加)
和 ‘-’(減),實現貨幣的加減運算
例如:
請輸入元、角 分:
2 3 4
請輸入元、角 分:
3 7 3
和:6元0角7分
差:-1元3角9分
又例如:
請輸入元、角 分:
2 3 4
請輸入元、角 分:
3 0 0
和:4元6角0分
差:-0元6角6分
代碼實現:
1 #include <iostream> 2 using namespace std; 3 4 class Money 5 { 6 private: 7 int yuan, jiao, fen; 8 bool flag; //標志位,0表示錢數為正,1表示錢數為負 -> 默認結果的錢數為正 9 public: 10 Money(); 11 Money(int fg, int y=0, int j=0, int f=0): yuan(y), jiao(j), fen(f), flag(fg){} 12 friend Money operator+(Money a, Money b); 13 friend Money operator-(Money a, Money b); 14 void display(); 15 16 }; 17 18 Money::Money() 19 { 20 flag = 0; 21 cout << "請輸入元、角 分:" <<endl; 22 cin >> yuan >> jiao >> fen; 23 } 24 25 void Money::display() 26 { 27 if(flag==1) cout << '-'; 28 cout << yuan << "元" << jiao << "角" << fen << "分" << endl; 29 } 30 31 //請用友元函數重載加/減,並要在類Money中聲明為友元 32 //TODO重載加 類外定義Money類的+重載運算 33 Money operator+(Money a, Money b) 34 { 35 Money temp(0); 36 // 將錢全部轉化成分來計算 37 int money_a = a.yuan*100 + a.jiao*10 + a.fen*1; 38 int money_b = b.yuan*100 + b.jiao*10 + b.fen*1; 39 int res = money_a + money_b; 40 // 因為是相加 故應該結果是正的 所以不用判斷符號 但是相減時 結果可能為負 就要判斷符號 41 temp.yuan = res/100; 42 res = res % 100; 43 temp.jiao = res/10; 44 temp.fen = res % 10; 45 46 // 返回結果對象 47 return temp; 48 } 49 50 //TODO重載減 類外定義Money類的-重載運算 51 Money operator-(Money a, Money b) 52 { 53 Money temp(0); 54 // 將錢全部轉化成分來計算 55 int money_a = a.yuan*100 + a.jiao*10 + a.fen*1; 56 int money_b = b.yuan*100 + b.jiao*10 + b.fen*1; 57 int res = money_a - money_b; 58 // 因為是相減時 結果可能為負 就要判斷符號 59 if(res < 0) 60 { 61 temp.flag = 1; 62 res = -res; 63 } 64 temp.yuan = res/100; 65 res = res % 100; 66 temp.jiao = res/10; 67 temp.fen = res % 10; 68 69 // 返回結果對象 70 return temp; 71 } 72 73 int main() 74 { 75 Money m1, m2, m3(0), m4(0); //m3用來記錄和,m4用來記錄差 76 77 // + 78 m3=m1+m2; 79 cout<<"和:"; 80 m3.display(); 81 82 // - 83 m4=m1-m2; 84 cout<<"差:"; 85 m4.display(); 86 87 return 0; 88 }
2.日期類運算符重載
要求:
定義一個日期類Date,包含年、月、日三個數據成員
友元函數重載自增運算符(++),實現日期的自增。運行結果如下
例如:
請輸入年、月、日:
2016 11 11
今天是:2016-11-11
明天是:2016-11-12
請完成以下程序 ,並注意特殊情況,例
輸入的是2016-12-31日
輸出的是2017-1- 1日
代碼實現:
1 #include <iostream> 2 using namespace std; 3 4 class Date{ 5 private: 6 int year; 7 int month; 8 int day; 9 public: 10 Date(); 11 Date operator++(int); // 申明重載的操作符,這種方法是后綴++ 12 /* 13 ++和--重載注意: ++和--有前后綴之分,另外下面的int只是一個占位符用來提示編譯器 14 類名 operator++(); ++前綴 -> 調用方式: ++對象名 15 類名 operator--(); --前綴 -> 調用方式: --對象名 16 類名 operator++(int); ++后綴 -> 調用方式: 對象名++ 17 類名 operator--(int); --后綴 -> 調用方式: 對象名-- 18 */ 19 void display(); 20 }; 21 22 // 判斷是否是閏年 是閏年返回1 不是返回0 23 int judge(int year) 24 { 25 if(year%4==0&&year%100!=0||year%400==0) 26 return 1; //閏年 27 return 0; //非閏年 28 } 29 30 // 構造函數 31 Date::Date() 32 { 33 cout<<"請輸入年、月、日:\n"; 34 cin>>year>>month>>day; 35 } 36 37 // 運算符重載 38 Date Date::operator++(int) 39 { 40 // 數組存儲一年12個月的天數,如果對象中的年份為閏年就將2月份的天數+1 41 int month_days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; 42 if(judge(year)) 43 { 44 month_days[1]++; 45 } 46 47 // 天數剛好是這個月的最后一天 就要判斷月份是否是一年的最后一個月(12月) 48 // 如果是12月 那么說明這一天是這一年的最后一天 那么年份+1 月和天均置1 49 // 如果不是12月 那么就把月份+1,天置1 50 if(day==month_days[month-1]) 51 { 52 if(month==12) 53 { 54 year++; 55 month=1; 56 day=1; 57 } 58 else 59 { 60 month++; 61 day=1; 62 } 63 } 64 // 天數不是這個月的最后一天 就直接把天數+1即可 65 else 66 { 67 day++; 68 } 69 70 // 返回計算后的對象 就是套路 凡是++和--運算符重載 都可以這樣做 71 // 當然可以提前用一個temp保存*temp的值然后返回temp 也是同理 書上這兩種方法都有 72 return *this; 73 } 74 75 void Date::display() 76 { 77 cout<<year<<"-"<<month<<"-"<<day<<endl; 78 } 79 80 int main() 81 { 82 Date d1; 83 cout<<"今天是:"; 84 d1.display(); 85 d1++; 86 cout<<"明天是:"; 87 d1.display(); 88 }