實驗二
函數重載編程練習
編寫重載函數add(),實現對int型,double型,Complex型數據的加法。在main()函數中定義不同類型
數據,調用測試。
#include <iostream> using namespace std; struct Complex{ double real; double imaginary; }; int add(int,int); double add(double,double); Complex add(Complex,Complex); int main(){ int a,b,c; cin>>a>>b; c=add(a,b); cout<<"the sum is"<<c<<endl; double m,n,s; cin>>m>>n; s=add(m,n); cout<<"the sum is"<<s<<endl; Complex j,k,l; cin>>j.real>>j.imaginary>>k.real>>k.imaginary; l=add(j,k); cout<<"the sum is"<<l.real<<"+"<<l.imaginary<<"i"<<endl; return 0; }//兩個以上的函數,具有相同的函數名,但是形參的個數或者類型不同,編譯器根據實參和形參的類型及個數的最佳匹配,自動確定調用哪一個函數,這就是函數的重載。 int add(int x,int y) { return x+y; } double add(double x,double y) { return x+y; } Complex add(Complex a,Complex b) { Complex c; c.real=a.real+b.real; c.imaginary=a.imaginary+b.imaginary; return c; }
結果

函數模板編程練習
編寫實現快速排序函數模板,并在main()函數中,定義不同類型數據,調用測試。
#include<iostream> using namespace std; void swap(int *a,int *b){ int temp=*a; *a=*b; *b=temp; } void swap(double *a,double *b){ double temp=*a; *a=*b; *b=temp; } int part(int a[],int start,int end,int mid){ while(start!=end){ while(a[end]>=mid&&start!=end) end--; if(start!=end){ swap(&a[start],&a[end]); start++; while(a[start]<=mid&&start!=end) start++; if(start!=end) { swap(&a[start],&a[end]); end--; } } } return start; } int part(double b[],int start,int end,double mid){ while(start!=end){ while(b[end]>=mid&&start!=end) end--; if(start!=end){ swap(&b[start],&b[end]); start++; while(b[start]<=mid&&start!=end) start++; if(start!=end) { swap(&b[start],&b[end]); end--; } } } return start; } int quicksort(int a[],int start,int end){ int i; if(start<end){ i=part(a,start,end,a[start]); quicksort(a,start,i-1); quicksort(a,i+1,end); } } int quicksort(double b[],int start,int end){ int i; if(start<end){ i=part(b,start,end,b[start]); quicksort(b,start,i-1); quicksort(b,i+1,end); } } int main(){ int array[7]={1,4,2,5,7,8,9}; int j=7,i; cout<<"Befor: "<<endl; for(i=0;i<j;i++) cout<<array[i]<<" "; quicksort(array,0,j-1); cout<<"\n"<<"After: "<<endl; for(i=0;i<j;i++){ cout<<array[i]<<" "; } cout<<"\n"; double b[7]={2.5,8.1,9.3,4.9,5.2,3.2,7.7}; int k=7,l; cout<<"Befor: "<<endl; for(l=0;l<k;l++) cout<<b[l]<<" "; quicksort(b,0,k-1); cout<<"\n"<<"After: "<<endl; for(l=0;l<k;l++) cout<<b[l]<<" "; return 0; }
結果

類的定義、實現和使用編程練習
設計并實現一個用戶類User,并在主函數中使用和測試這個類。具體要求如下:
每一個用戶有用戶名(name), 密碼(passwd),聯系郵箱(email)三個屬性。
支持設置用戶信息setInfo()。允許設置信息時密碼默認為6個1,聯系郵箱默認為空串。
支持打印用戶信息printInfo()。打印用戶名、密碼、聯系郵箱。其中,密碼以6個*方式顯示。
支持修改密碼changePasswd(),。在修改密碼前,要求先輸入舊密碼,驗證無誤后,才允許修改。
如果輸入舊密碼時,連續三次輸入錯誤,則提示用戶稍后再試,暫時退出修改密碼程序。
在main()函數中創建User類實例,測試User類的各項操作(設置用戶信息,修改密碼,打印用戶信
息)
#include <iostream> #include <string> using namespace std; // User類的聲明 class User { public: void setInfo(string name1,string passwd1="111111",string email1=""); void changePasswd(); void printInfo(); private: string name; string passwd; string email; }; void User::setInfo(string name1,string passwd1,string email1){ name=name1; passwd=passwd1; email=email1; } void User::changePasswd(){ string oldpasswd; cout<<"Plesae enter your old password:"; cin>>oldpasswd; int i=1; while(oldpasswd!=passwd&&i<3){ cout<<"Wrong! Please enter your oldpassword again:"; cin>>oldpasswd; i++; } if(i>=3) cout<<"Please try later!"<<endl; if(oldpasswd==passwd){ string newpasswd; cout<<"Please enter your new password:"; cin>>newpasswd; passwd=newpasswd; } } void User::printInfo(){ cout<<"name: "<<name<<endl; cout<<"password: "<<"******"<<endl; cout<<"email: "<<email<<endl; } //在主函數中測試User類 //用User類創建對象,測試類的功能 int main(){ cout<<"testing 1......"<<endl; //測試1 User user1; user1.setInfo("Leonard"); user1.printInfo(); user1.changePasswd(); user1.printInfo(); cout<<endl<<"testing 2......"<<endl<<endl; //測試2 User user2; user2.setInfo("Jonny","92197","Fairy@hotmain.com"); user2.printInfo(); return 0; }
結果
實驗總結與體會
總體來說,第一題和第三題給了模塊和提示,感覺比較簡單,做的也比較快。第二題,我琢磨了一個晚上,對快速排序算是有了一個大概的了解。還是覺得練的程序不夠多,需要多多的練習。
互評
http://www.rzrgm.cn/sqcmxg/p/10574927.html
http://www.rzrgm.cn/DADABu-21/p/10583707.html
http://www.rzrgm.cn/csc13813017371/p/10584971.html
浙公網安備 33010602011771號