首页 技术 正文
技术 2022年11月19日
0 收藏 648 点赞 3,573 浏览 4417 个字

一、实验过程

函数重载编程练习

实验要求:编写重载函数add(),实现对int型,double型,complex型数据的加法。在main函数中定义不同类型的数据,调用测试。

代码实现:

先是简单的体验函数重载:

#include<iostream>
using namespace std;
struct Complex {
double real;
double imag;
};
int add(int, int);
double add(double,double);
Complex add(Complex, Complex);
int main()
{
cout<<add(,)<<endl;
cout<<add(5.7,12.7) <<endl;
cout<<add(,)<<"+"<<add(,)<<"i"<<endl;
}
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 r;
r.real=a.real+b.real;
r.imag=a.imag+b.imag;
return r;
}

C++实验二——函数重载、函数模板、简单类的定义和实现

在前面内容的基础上,对代码和功能做了完善,使它更符合这个实验对代码的运行要求。

#include<iostream>
using namespace std;
struct Complex {
double real;
double imag;
};
int add(int, int);
double add(double,double);
Complex add(Complex, Complex);
int main()
{
char c;
int tries=;
while(true)
{
tries++;
cout<<"This is your "<<tries<<"th calculation"<<endl;
cout<<"please choose your data type(p(复数) or r(实数)):";
cin>>c;
if(c=='r')
{
cout<<"please enter two numbers:" ;
double a,b;
cin>>a>>b;
cout<<"The sum of the two numbers is:" <<add(a,b)<<endl;
}
else if(c=='p')
{
cout<<"please enter two plural:";
Complex p,q;
cin>>p.real>>p.imag;
cin>>q.real>>q.imag;
cout<<"The sum of the two numbers is:"<<(add(p,q)).real<<"+"<<(add(p,q)).imag<<"i"<<endl;
}
else
{
cout<<"Your input is incorrect" <<endl;
break;
}
}
return ;
}
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 r;
r.real=a.real+b.real;
r.imag=a.imag+b.imag;
return r;
}

运行结果如下:

C++实验二——函数重载、函数模板、简单类的定义和实现

函数模板编程练习

实验要求:编写实现快速排序函数模板,并在main函数中,定义不同类型数据,调用测试。

算法设计:快速排序用了二分的思想,先通过一个数把一组数分成两部分,其中一部分均比另一部分要小。之后分别再对这两个部分继续分割排序,最后使整个数组有序排列。排序分成了三个步骤:选择一个数(pivot)作为分割标准、分割、递归。这个排序函数模板的核心就是最后的递归。

代码实现

#include<iostream>
using namespace std;
void QuickSort(double ar[], int left ,int right) {
if(left<right)
{
int i = left;
int j = right;
double x = ar[i];
while(i<j)
{
while(i<j&&ar[j]>x)
j--;
if(i<j)
{
ar[i] = ar[j];
i++;
}
while(i<j&&ar[i]<x)
i++;
if(i<j)
{
ar[j] = ar[i];
j--;
}
}
ar[i] = x;
QuickSort(ar, left, i-);
QuickSort(ar, i+, right);
}
}
int main()
{
int n = ;
double ar[]={,,,,,,,,};
int i;
cout<<"The remote arry is:";
for(i=;i<n;++i)
cout<<ar[i]<<' ';
cout<<endl;
QuickSort(ar,,n-);
cout<<"The arranged arry is: ";
for(i=;i<n;++i)
cout<<ar[i]<<' ';
cout<<endl;
return ;
}

C++实验二——函数重载、函数模板、简单类的定义和实现

类的定义、实现和使用编程练习
实验要求:设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下:

  • 每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。
  • 支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。
  • 支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。
  • 支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。
  • 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。

在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信
息)
User类功能的完善及拓展丰富(===选做===)
自行设计。在前述内容的基础上,对代码和功能做完善,使其更符合实际应用场景。比如:

  • 支持修改邮箱;用户设置邮箱时对邮箱地址合法性的检测提示(比如是否包含@)
  • 用户更改密码时,当用户输入旧密码和新密码时,屏幕上均以星号代替,而不会显示用户实际
  • 输入的密码;设置密码时对密码的长度、合法性进行有效性校验,等等

代码实现

#include <iostream>
#include <string>
#include<conio.h>
using namespace std;
class User
{
public:
User();
~User();public:
//设置用户信息
void setInfo(); //打印用户信息
void printInfo(); //修改密码
void changePassword(); //修改邮箱
void changeEmail(); //设置*号
string getXinghao();private:
string user_name_; //用户名
string user_password_; //密码
string user_email_; //邮箱
};User::User()
{
user_password_ = ""; //密码默认为6个1
user_email_ = "";
}User::~User()
{}void User::setInfo()
{
cout << "请输入用户名:";
cin >> user_name_; cout << "请输入密码:";
cin >> user_password_; cout << "邮箱:";
cin >> user_email_;
}void User::printInfo()
{
cout << "用户名:" << user_name_ <<endl;
cout << "密码:" << user_password_ <<endl;
cout << "邮箱:" << user_email_ <<endl;
}void User::changePassword()
{
string strOld, strNew;
int nCount = ; do
{
cout << "请输入旧密码:" <<endl;
strOld = getXinghao();
if (strOld.compare(user_password_) != )
{
int tries=;
tries++;
cout << "旧密码不正确!" << "这是您第"<<tries<<"输错密码"<<endl;
if(tries>=)
{
break;
}
}
else
{
cout << "请输入新密码:" << endl;
strNew = getXinghao();
if (strNew.size() != ) //密码6位数
{
cout << "密码长度不正确!" <<endl;
}
else
{
user_password_ = strNew;
cout << "修改密码成功!" <<endl;
break;
}
}
nCount++;
} while (nCount < );
}void User::changeEmail()
{
cout << "请输入新邮箱:";
string strEmail;
cin >> strEmail; if (strEmail.find("@") != string::npos)
{
user_email_ = strEmail;
}
else
{
cout << "邮箱地址不正确!" << endl;
}
}string User::getXinghao()
{
string password;
int i = ;
char ch;
while ((ch = _getch()) != )
{
i++;
if (ch != '\0')
{
password += ch;
cout << "*";
}
} cout <<endl; return password;
}int main()
{
User user; //设置信息
user.setInfo(); //显示信息
user.printInfo(); //修改密码
user.changePassword(); //显示信息
user.printInfo(); //修改邮箱
user.changeEmail(); //显示信息
user.printInfo();
}

C++实验二——函数重载、函数模板、简单类的定义和实现C++实验二——函数重载、函数模板、简单类的定义和实现

二、实验反思

1、实验一中关于复数的运算,可以写一个复数类。实验二中快排的优点就是速度极快,数据移动少,但是当快排到后期,当待排数组割到一定大小后,快排不如其他一些排序方法好,此时可以用插排等,后期查了些资料,三数取中+插排效率要更高一点。总而言之,各排序方法都有各自的优缺点。

2、函数重载减少了函数名的数量,让代码更清晰,代码可读性提高。类有很多优点,我认为很大一部分体现在模块化编程上,将具有特定功能的一个组件封装到一个类中。将各个模块独立,能够使得程序结构更加清晰,可以减少程序的bug。

互评地址:https://www.cnblogs.com/yidaoyigexiaopenyou/p/10556646.html

https://www.cnblogs.com/sjn1/p/10556014.html

https://www.cnblogs.com/elise00/p/10555817.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905