函數重載編程練習 html
編寫重載函數add(),實現對int型,double型,Complex型數據的加法。在main()函數中定義不一樣類型 數據,調用測試。ios
#include <iostream> using namespace std; struct complex{ double real; double imaginary; }; int add(int x2,int y2 ); double add(double x1,double y1 ); complex add(complex x,complex y); int main() { int a=4,b=3,s1; double m=2.1,n=3.5,s2; complex complex1,complex2,complex3; complex1.real=2,complex1.imaginary=3; complex2.real=6,complex2.imaginary=4; s1=add(a,b); s2=add(m,n); complex3=add(complex1,complex2); cout<<s1<<endl; cout<<s2<<endl; cout<<complex3.real <<"+"<<complex3.imaginary <<"i"<<endl; return 0; } int add(int x2,int y2) {return x2+y2;} double add(double x1,double y1) {return x1+y1;} complex add(complex x,complex y) { complex z; z.imaginary =x.imaginary +y.imaginary ; z.real =x.real +y.real ; return z; }
函數模板編程練習編程
編寫實現快速排序函數模板,並在main()函數中,定義不一樣類型數據,調用測試。函數
#include <iostream> #include <iomanip> #include "QS.h" using namespace std; int main () {int i; int a[5]={12,4,34,1,5 }; double b[5]={14.2,9.5,55.8,25.7,3.1}; QS(a,0,5); QS(b,0,5); for (i=0;i<=4;i++) cout<<setw(5)<<a[i]; cout<<endl; for (i=0;i<=4;i++) cout<<setw(6)<<b[i]; cout<<endl; return 0; }
#ifndef Quicksort_H #define Quicksort_H template <class T> void QS(T s[], int low, int high) { int a, b, c = 0; T f, ex; a = low; b = high - 1; f = s[(low+high)/2]; if (a < b) { while (a < b) { while (a < b&&f < s[b]) b--; while (a < b&&f > s[a]) a++; if (a >= b) c = b; else { ex = s[a]; s[a] = s[b]; s[b] = ex; } } QS(s, low, c); QS(s, c + 1, high); } } #endif //此程序源自我優秀的同窗,非我原創!
快速排序實在是不會,掙扎了很久仍是作不出來,就借來了同窗的程序參考(已註明),我這菜鳥花了好多時間才寫出了快速排序的第一遍排序(下方),第一遍排序的結果是對的,但不知爲何low和high的值出現了問題。難道不是當low和high相同時就不運行了嗎?可爲何又作了一次運算呢?請大佬們指點。學習
#include <iostream> #include <iomanip> using namespace std; int main() {int low,high,mid,m,n,f=1,t,i; int a[5]={12,4,34,1,5}; low=0;high=4; t=a[low]; while(low<high) {if(f==0) n=a[low]; else m=a[high]; while(f==1) { if(t<m) {--high; f=1;cout<<"1low: "<<low<<endl;cout<<"1high: "<<high<<endl; break; } else {a[low]=a[high]; ++low;cout<<"2low: "<<low<<endl;cout<<"2high: "<<high<<endl; f=0; break; } } while(f==0) { if(t<n) {a[high]=a[low]; --high; f=1;cout<<"3low: "<<low<<endl;cout<<"3high: "<<high<<endl; break; } else {++low; f=0;cout<<"4low: "<<low<<endl;cout<<"4high: "<<high<<endl; break; } } } a[low]=t; mid=low; for(i=0;i<=4;i++) cout<<a[i]<<endl; }
類的定義、實現和使用編程練習測試
設計並實現一個用戶類User,並在主函數中使用和測試這個類。具體要求以下: 每個用戶有用戶名(name), 密碼(passwd),聯繫郵箱(email)三個屬性。 支持設置用戶信息setInfo()。容許設置信息時密碼默認爲6個1,聯繫郵箱默認爲空串。 支持打印用戶信息printInfo()。打印用戶名、密碼、聯繫郵箱。其中,密碼以6個*方式顯示。 支持修改密碼changePasswd(),。在修改密碼前,要求先輸入舊密碼,驗證無誤後,才容許修改。 若是輸入舊密碼時,連續三次輸入錯誤,則提示用戶稍後再試,暫時退出修改密碼程序。 在main()函數中建立User類實例,測試User類的各項操做(設置用戶信息,修改密碼,打印用戶信息)ui
#include <iostream> #include <string> using namespace std; class User{ public: void setInfo(string name0,string password0="111111",string email0=""); void changePasswd(); void printInfo(); private: string name; string password; string email; }; void User::setInfo(string name0,string password0,string email0):name(name0),password(password0),mail(email0){ } void User::changePasswd(){ string oldpassword; int i=1; cout<<"please input your old password:";cin>>oldpassword; while(oldpassword!=password&&i<3) {cout<<"wrong,please input it again:";cin>>oldpassword; i++; } if(oldpassword!=password&&i==3) cout<<"please try later"<<endl; if(oldpassword==password) {cout<<"please input your new password:";cin>>password;} } void User::printInfo(){ cout<<"name: "<<name<<endl; cout<<"password:****** "<<endl;; cout<<"email: "<<email<<endl; cout<<endl; } int main() { cout<<"testing 1......"<<endl; User user1; user1.setInfo("Leonard"); user1.printInfo(); user1.changePasswd(); user1.printInfo(); cout<<"testing2......"<<endl; User user2; user2.setInfo("Jonny","92197","xyz@hotmail.com"); user2.printInfo(); return 0; }
實驗總結與體會spa
互評連接設計
https://www.cnblogs.com/csc13813017371/p/10584971.htmlcode
https://www.cnblogs.com/libing-072921/p/10587072.html
https://www.cnblogs.com/nnn13579/p/10561474.html