实验结论
1.函数重载编程练习:
编写重载函数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 a1,a2,asum;
double b1,b2,bsum;
Complex c1,c2,csum;
cout<<"please enter two integers:";
cin>>a1>>a2;
asum=add(a1,a2);
cout<<a1<<"+"<<a2<<"="<<asum<<endl;
cout<<"please enter two real numbers:";
cin>>b1>>b2;
bsum=add(b1,b2);
cout<<b1<<"+"<<b2<<"="<<bsum<<endl;
cout<<"please enter two plurals:";
cin>>c1.real>>c1.imaginary>>c2.real>>c2.imaginary;
cout<<"c1"<<"="<<c1.real<<"+"<<c1.imaginary<<"i"<<endl;
cout<<"c2"<<"="<<c2.real<<"+"<<c2.imaginary<<"i"<<endl;
csum=add(c1,c2);
cout<<"c1+c2"<<"="<<csum.real<<"+"<<csum.imaginary<<"i"<<endl;
return ;
}
int add(int a1,int a2){
int asum;
asum=a1+a2;
return asum;
}
double add(double b1,double b2){
double bsum;
bsum=b1+b2;
return bsum;
}
Complex add(Complex c1,Complex c2){
Complex csum;
csum.real=c1.real+c2.real;
csum.imaginary=c1.imaginary+c2.imaginary;
return csum;
}
运行截图
2.函数模板编程练习
编写实现快速排序函数模板,并在main( )函数中,定义不同类型数据,调用测试。(算法可参考这里, 内有排序示意图及算法逻辑)
参考了网上许多快速排序法的程序,此程序参考(https://www.cnblogs.com/miracleswgm/p/9199124.html)
(1)整数排序
#include <iostream>
#include <iomanip>
using namespace std;
void Quicksort(int num[],int left,int right);
int partition(int num[], int left, int right); int main(){
int i;
int num[]={,,,,,,,,,};
Quicksort(num,,);
for(i=;i<=;i++)
cout<<num[i]<<setw();
cout<<endl;
return ;
} int partition(int num[], int left, int right)
{
int i = left + ;
int j = right;
int temp = num[left]; while(i <= j)
{
while (num[i] < temp)
{
i++;
}
while (num[j] > temp )
{
j--;
}
if (i < j)
swap(num[i++], num[j--]);
else i++;
}
swap(num[j], num[left]);
return j; } void Quicksort(int num[],int left,int right)
{
int j;
if (left>right)
return;
j = partition(num, left, right);
Quicksort(num, left, j - );
Quicksort(num, j + , right);
}
运行截图
(2)实数排序
#include <iostream>
#include <iomanip>
using namespace std;
void Quicksort(float num[],int left,int right);
float partition(float num[], int left, int right); int main(){
int i;
float num[]={9.1,8.9,7.2,6.4,5.3,4.2,3.1,2.6,1.8,78.5};
Quicksort(num,,);
for(i=;i<=;i++)
cout<<num[i]<<setw();
cout<<endl;
return ;
} float partition(float num[], int left, int right)
{
int i = left + ;
int j = right;
float temp = num[left]; while(i <= j)
{
while (num[i] < temp)
{
i++;
}
while (num[j] > temp )
{
j--;
}
if (i < j)
swap(num[i++], num[j--]);
else i++;
}
swap(num[j], num[left]);
return j; } void Quicksort(float num[],int left,int right)
{
float j;
if (left>right)
return;
j = partition(num, left, right);
Quicksort(num, left, j - );
Quicksort(num, j + , right);
}
运行截图
3.类的定义、实现和使用编程练习
设计并实现一个用户类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 name0,string passwd0="",string email0="");
string getname();
string getpasswd();
string getemail();
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;
}
string User::getname(){
return name;
}
string User::getpasswd(){
return passwd;
}
string User::getemail(){
return email;
}
void User::changePasswd(){
int i=;
string oldpasswd,newpasswd;
cout<<"please enter the old passwd:";
cin>>oldpasswd;
while(i){
if(passwd == oldpasswd)
{ cout<<"please enter a new pass word:";
cin>>newpasswd;
passwd=newpasswd;
cout<<"change pass word successfully"<<endl;
break;}
if(i>){
cout<<"passwdb input error,please re-enter again:";
cin>>oldpasswd;
}
else{
cout<<"you are wrong,please try after a while";
cout<<endl;
}
i--;
}
} void User::printInfo(){
cout << "name: " << name << endl;
cout << "passwd: " << "******" << 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","","xyz@hotmail.com");
user2.printInfo();
return ; }
运行截图
实验总结与体会
1.结构体相关的知识还需强化
2.类相关的知识还是很薄弱的
实验一评论链接:
https://www.cnblogs.com/joey-yan/p/10543990.html#4213143
https://www.cnblogs.com/lovecpp/p/10520456.html#4211900
https://www.cnblogs.com/qiuqiuwr/p/10525885.html#4211895