实验结论

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

 

C++(实验二)的更多相关文章

  1. 20145215&20145307《信息安全系统设计基础》实验二 固件设计

    20145215&20145307<信息安全系统设计基础>实验二 固件设计 实验目的与要求 了解多线程程序设计的基本原理,学习 pthread 库函数的使用. 了解在 linux ...

  2. FPGA与simulink联合实时环路系列——实验二LED

    实验二LED 实验内容 在实验一的基础上,将simulink产生的测试信号输出到FPGA开发板上的LED灯进行显示,这里要在生成的硬件模型上进行修改,将传送到FPGA的信号输出到8个LED灯上,并且对 ...

  3. 20145204&20145212信息安全系统实验二

    20145204&20145212信息安全系统实验二 链接

  4. 20145204&20145212实验二报告

    实验二固件设计 步骤: 1.开发环境的配置,参考实验一 1.将实验代码拷贝到共享文件夹中. 2.在虚拟机中编译代码.对于多线程相关的代码,编译时需要加-lpthread的库.下载调试在超级终端中运行可 ...

  5. 20145215实验二 Java面向对象程序设计

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...

  6. 实验二 用C语言表示进程的调度

    实验二 一. 实验目的 通过模拟进程的调度,进一步了解进程的调度的具体过程. 二. 实验内容和要求 1.进程PCB的结构体定义 2.定义队列 3.输入进程序列 4.排序(按到位时间) 5.输出进程运行 ...

  7. 20145218&20145240 《信息安全系统设计基础》实验二 固件设计

    20145218&20145240 <信息安全系统设计基础>实验二 固件设计 实验报告链接:http://www.cnblogs.com/20145240lsj/p/6035512 ...

  8. 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...

  9. 实验二 PHP基本语法实验

    实验二 PHP基本语法实验 0 实验准备 0.1实验环境和相关工具软件 具体到的机房环境,请在Windowsxp环境下做本实验: l  操作系统:Windowsxp l  Web服务器:Apache ...

  10. 20145213《Java程序设计》实验二Java面向对象程序设计实验报告

    20145213<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装,继承,多态 初步掌握UML建模 熟悉S.O. ...

随机推荐

  1. jupyter notebook + frp 实现内容穿透

    服务器上找到frps.ini 配置如下 [common] bind_port = 7000 vhost_http_port = 8890 要穿透的笔记本的frpc.ini配置 [common] ser ...

  2. SpringBoot 注解

    @RestController和@RequestMapping注解 我们的Example类上使用的第一个注解是 @RestController .这被称为一个构造型(stereotype)注解.它为阅 ...

  3. Nginx Install 记录

    一.安装编译工具及库文件 yum -y install gcc yum -y install gcc-c++ yum -y install zlib; yum -y install pcre-deve ...

  4. Mysql 版本号、存储引擎、索引查询

    [1]Mysql 版本号.存储引擎.索引查询 # 查看数据库版本号 SELECT VERSION(); # 查看数据库支持的引擎(默认即Support == DEFAULT行) SHOW ENGINE ...

  5. Linux中LAMP构架的实现

    LAMP:Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度共同组 ...

  6. vmware-tools安装——实用

    1.vmware中点击安装或重新安装vmware-tools 2.在root终端解压并移动安装文件:tar -xvf VMwareTools-9.9.0-2304977.tar.gz -C /tmp ...

  7. NOIP 2017 宝藏 - 动态规划

    题目传送门 传送门 题目大意 (家喻户晓的题目不需要题目大意) 设$f_{d, s}$表示当前树的深度为$d$,与第一个打通的点连通的点集为$s$. 每次转移的时候不考虑实际的深度,深度都当做$d$, ...

  8. Client not ready yet.....

    提示Client not ready yet.....程序安装上就提示停止了 Logcat无提示 只有run里边提示  Client not ready yet....... 我尝试了  Clean ...

  9. python - Random常用方法记录

    import random # range [a,b) 不包含b # 获取随机整数 # randrange [a,b) 不包含b a = random.randrange(0, 101, 5) # E ...

  10. JQuery-FullCalendar 多数据源实现日程展示

    背景 本次需求:实现在一个以月为界面的日历上展示每天发生的事件. 1.每天的事件有多个类型,不同类型的事件使用不同背景色标注,展示为某个类型事件的统计,比如: 会议(6) 2.点击某一天可以查询改天所 ...