1.
函数重载编程练习
编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型 数据,调用测试。
编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型 数据,调用测试。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#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; double x,y,z; Complex m,n,p; cin>>a>>b; c=add(a,b); cout<<"和为:"<<c<<endl; cin>>x>>y; z=add(x,y); cout<<"和为:"<<z<<endl; cin>>m.real>>m.imaginary>>n.real>>n.imaginary; p=add(m,n); cout<<"和为:"<<m.real<<"+"<<m.imaginary<<"i"<<endl; return 0; } int add(int a,int b) {return a+b; } double add(double x,double y) {return x+y; } Complex add(Complex m,Complex n) {Complex d; d.real=m.real+n.real; d.imaginary=m.imaginary+n.imaginary; return d; }
2.
函数模板编程练习
编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#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]={28,11,98,10,03}; cout<<"m[5]={28,11,98,10,03}"<<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类的各项操作(设置用户信息,修改密码,打印用户信 息)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include<iostream> #include<string> using namespace std; class User{ public: void setInfo(string name0,string passwd0="111111",string email0=" "); void changePasswd(); void printInfo(); private: string name; string passwd; string email; }; void User::setInfo(string name0,string passwd0,string email0) { name=name0; passwd=passwd0; email=email0; } void User::changePasswd() { string passwd; int i=0; cout<<"请输入旧密码:"<<endl; cin>>passwd; while(passwd!="111111"&&i<3) {i++; cout<<"输入错误,请重新输一遍:"<<endl; cin>>passwd; } if(passwd=="111111") {cout<<"请输入你的新密码:"<<endl; cin>>passwd; cout<<"修改密码成功"<<endl; } else if(i==3) cout<<"请稍后再试"<<endl; } void User::printInfo (){ cout<<"name: "<<name<<endl; cout<<"password: "<<"******"<<endl; cout<<"email: "<<email<<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; }
实验总结与体会:
第二题的逻辑我是实在想不明白,询问同学,同学也不是很清楚。本想按着上学期C语言老师讲的方法试一试,可存在着很多小问题,运行不出结果。参考了一下同学的程序,还是有很多看不明白的地方。我一下子就感受到了与其他同学的差距,别人写的程序又快又好,可自己磨蹭了好久好久还是想不明白,希望通过以后的练习我能有进步吧。