一、函数重载编程练习 编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型 数据,调用测试。
实验代码
#include<iostream> using namespace std; struct Complex{ double real; double imagnary; }; int add(int A1,int B1); double add(double A2,double B2); Complex add(Complex A3,Complex B3); int main() { int a1,b1; cout<<"please enter the int number"<<":"; cin>>a1>>b1; double a2,b2; cout<<"please enter the double number"<<":"; cin>>a2>>b2; Complex a3,b3; cout<<"please enter the complex number"<<":"; cin>>a3.real>>a3.imagnary>>b3.real>>b3.imagnary; add(a1,b1); add(a2,b2); add(a3,b3); } int add(int A1,int B1) { cout<<A1+B1<<endl; } double add(double A2,double B2) { cout<<A2+B2<<endl; } Complex add(Complex A3,Complex B3) { cout<<A3.real+B3.real<<'+'<<A3.imagnary+B3.imagnary<<'i'<<endl; }
运行截图
二、函数模板编程练习 编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。
实验代码
mian函数
#include<iostream> using namespace std; #include"InsertSort.h" #include"output.h" int main() { int a[5]={3,2,6,8,7}; double b[5]={4.4,8.8,7.7,1.1,9.9}; InsertSort(a,0,4); output(a,5); cout<<endl; InsertSort(b,0,4); output(b,5); return 0; }
排序函数
#ifndef INSERTSORT_H #define INSERTSORT_H using namespace std; template<class T> void InsertSort(T s[],int l,int r) { if(l<r) { int i,j; T x; i=l,j=r,x=s[l]; while(i<j) { while(i<j&&s[j]>=x) j--; if(i<j) s[i++]=s[j]; while(i<j&&s[i]<=x) i++; if(i<j) s[j--]=s[i]; } s[i]=x; InsertSort(s,l,i-1); InsertSort(s,i+1,r); } } #endif
输出函数
#ifndef OUTPUT_H #define OUTPUT_H #include<iostream> using namespace std; template<class T> void output(T s[],int n) { cout<<"number after sorting is"<<":"; int i; for(i=0;i<n;i++) cout<<s[i]<<" "; cout<<endl; } #endif
运行截图
类的定义、实现和使用编程练习 设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下: 每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。 支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。 支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。 支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。 在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信息)
实验代码
#include<iostream> #include<string> using namespace std; class user{ public: void setInfo(string name0,string password0="111111",string email0=" "); void changePassword(); void printInfo(); private: string name; string password; string email; }; int main() { cout<<"testing 1...."<<endl; user user1; user1.setInfo("Leonard"); user1.printInfo(); user1.changePassword(); user1.printInfo(); cout<<"testing 2...."<<endl; user user2; user2.setInfo("jonny","92197","[email protected]"); user2.printInfo(); } void user::setInfo(string name0,string password0,string email0) { name=name0; password=password0; email=email0; } void user::printInfo() { cout<<"name is"<<":"<<name<<endl; cout<<"password is"<<":"<<"******"<<endl; cout<<"email is"<<":"<<email<<endl; } void user::changePassword() { string oldpassword; cout<<"Please enter your old password"<<":"; cin>>oldpassword; int i=1; while(oldpassword!=password&&i<3) { cout<<"wrong!!please continue cin"<<":"; cin>>oldpassword; i++; } if(i>=3) { cout<<"Please try latter"<<endl; } if(oldpassword==password) { string newpassword; cout<<"Please enter your new password"<<":"; cin>>newpassword; password=newpassword; } }
运行截图
实验总结
1.c语言中对不同类型的数据进行相同的操作需要定义名称完全不同的函数,函数重载的出现大大提高了效率,更加的人性化。类、函数模板有着异曲同工之妙。
2.编写诸如快速排序之类的函数关键在于理解算法,其次再转化成语言进行实现。
3.在带默认形参值的函数中,要注意避免在同一个函数的多个声明中对同一个参数的默认值重复定义。