1、编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型 数据,调用测试。
#include <iostream> using namespace std; struct Complex{ double real; double imaginary; }; int add(int a,int b){ return a+b;} double add(double a,double b){ return a+b;} Complex add(Complex a,Complex b){ Complex c; c.real=a.real+b.real; c.imaginary=a.imaginary+b.imaginary; return c;} int main() {int x1,y1; cout<<"输入两个整数:"; cin>>x1>>y1; cout<<x1<<" "<<y1<<"的和是"<<add(x1,y1)<<endl; double x2,y2; cout<<"输入两个双精度型的数:"; cin>>x2>>y2; cout<<x2<<" "<<y2<<"的和是"<<add(x2,y2)<<endl; cout<<"输入两个复数型的数:"; Complex c,a,b; cin>>a.real>>a.imaginary>>b.real>>b.imaginary; c=add(a,b); cout<<c.real<<"+"<<c.imaginary<<"i"<<endl; return 0; }
运行结果:
2、编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。(算法可参考这里, 内有排序示意图及算法逻辑)
#include <iostream> using namespace std; template<class T> void quick_sort(T a[],int before,int after) { int i=before; int j=after; int t=a[i]; if(i<j) {while(i<j) { while(i<j&&a[j]>=t) j--; if(i<j) {a[i]=a[j]; i++; } while(i<j&&t>a[i]) i++; if(i<j) {a[j]=a[i]; j--;} } a[i]=t; quick_sort(a,before,i-1); quick_sort(a,i+1,after); } } int main() { int i; int m[5]={2,5,7,9,1}; cout<<"m[5]={2,5,7,9,1}"<<endl; quick_sort(m,0,4); cout<<"数组升序为:"; for(i=0;i<5;i++) cout<<m[i]<<" "; return 0; }
运行结果:
3、设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下: 每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。 支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。 支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。 支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。 在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信 息)
#include <iostream> #include <string> using namespace std; class User { public: User(string name1, string passwd1, string email1); User() { name = ""; passwd = "111111"; email = ""; } 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) {if (name == " ") cin >> name1; name=name1; if (passwd == " ") cin >> passwd1; passwd=passwd1; if (email == " ") cin >> email1; email=email1; } void User::changePasswd(){ string oldpasswd; int n=1; cout<<"input oldpasswd:"; cin>>oldpasswd; while(oldpasswd!=passwd&&n<3) {cout<<"wrong,try it again:"; cin>>oldpasswd; n++;} if(oldpasswd!=passwd&&n==3) cout<<"busy,try it later:"; if(oldpasswd==passwd) {cout<<"input your newpasswd:"; cin>>passwd; } } void User::printInfo(){ cout<<"name: "<<name<<endl; cout<<"passwd:******"<<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 << endl << "testing 2......" << endl << endl; User user2; user2.setInfo("Jonny","92197","xyz@hotmail.com"); user2.printInfo(); return 0; }
运行结果:
https://www.cnblogs.com/joey-yan/p/10588054.html
https://www.cnblogs.com/DADABu-21/p/10583707.html#4211820
https://www.cnblogs.com/shenqidetao/p/10579872.html#4211809
原文:https://www.cnblogs.com/charlotte00/p/10585906.html