c++实验二
1.函数重载编程练习编写重载函数add(),实现对int型,double型,Complex型数据的加法
#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 x;
double y;
complex z;
int a,b;
cout<<"请输入两个整数:"<<endl;
cin>>a>>b;
x=add(a,b);
cout<<"整数之和为:"<<x<<endl;
double c,d;
cout<<"请输入两个double型数:"<<endl;
cin>>c>>d;
y=add(c,d);
cout<<"double类型之和为:"<<y<<endl;
complex e,f;
cout<<"请输入两个复数的实部虚部"<<endl;
cin>>e.real>>e.imaginary>>f.real>>f.imaginary;
z=add(e,f);
cout<<"复数之和为:"<<z.real<<"+"<<z.imaginary<<"i"<<endl;
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 c;
c.real=a.real+b.real;
c.imaginary=a.imaginary+b.imaginary;
return c;
}

2.编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。
#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-1);
QuickSort(ar, i+1, right);
}
}
int main()
{
int n = 10 ;
double ar[]={5,78,45,67,45,55,24,89,54,9};
int i;
cout<<"排序前的数为:";
for(i=0;i<n;++i)
cout<<ar[i]<<' ';
cout<<endl;
QuickSort(ar,0,n-1);
cout<<"排序后的数为: ";
for(i=0;i<n;++i)
cout<<ar[i]<<' ';
cout<<endl;
return 0;
}

3.写一个处理用户信息的类,可以打印信息,支持修改密码。
#include <iostream>
#include <string>
using namespace std;
class user
{public:
void setInfo(string name0,string password0="",string email0=" ");
void changePassword();
void printInfo();
private:
string name;
string password;
string email;
}; void user::setInfo(string name0,string password0,string email0)
{name=name0;
password=password0;
email=email0;
} void user::changePassword()
{string old;
int i=;
cout<<"请输入原密码:"<<endl;
cin>>old;
while (old!=password && i<)
{i++;
cout<<"错误,请重新输入:"<<endl;
cin>>old;
}
if(old==password)
{cout<<"请输入新密码:"<<endl;
cin>>password;
}
else if(i=)
cout<<"请稍后再试"<<endl;
} void user::printInfo ()
{cout<<"name :"<<name<<endl;
cout<<"passwd : ******"<<endl;
cout<<"email :"<<email<<endl;
} int main()
{user user1;
user1.setInfo("Leonard");
user1.printInfo();
user1.changePassword();
user1.printInfo();
cout<<endl;
user user2;
user2.setInfo("Jonny","","xyz@hotmail.com");
user2.printInfo();
return ;
}

c++实验二的更多相关文章
- 20145215&20145307《信息安全系统设计基础》实验二 固件设计
20145215&20145307<信息安全系统设计基础>实验二 固件设计 实验目的与要求 了解多线程程序设计的基本原理,学习 pthread 库函数的使用. 了解在 linux ...
- FPGA与simulink联合实时环路系列——实验二LED
实验二LED 实验内容 在实验一的基础上,将simulink产生的测试信号输出到FPGA开发板上的LED灯进行显示,这里要在生成的硬件模型上进行修改,将传送到FPGA的信号输出到8个LED灯上,并且对 ...
- 20145204&20145212信息安全系统实验二
20145204&20145212信息安全系统实验二 链接
- 20145204&20145212实验二报告
实验二固件设计 步骤: 1.开发环境的配置,参考实验一 1.将实验代码拷贝到共享文件夹中. 2.在虚拟机中编译代码.对于多线程相关的代码,编译时需要加-lpthread的库.下载调试在超级终端中运行可 ...
- 20145215实验二 Java面向对象程序设计
一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...
- 实验二 用C语言表示进程的调度
实验二 一. 实验目的 通过模拟进程的调度,进一步了解进程的调度的具体过程. 二. 实验内容和要求 1.进程PCB的结构体定义 2.定义队列 3.输入进程序列 4.排序(按到位时间) 5.输出进程运行 ...
- 20145218&20145240 《信息安全系统设计基础》实验二 固件设计
20145218&20145240 <信息安全系统设计基础>实验二 固件设计 实验报告链接:http://www.cnblogs.com/20145240lsj/p/6035512 ...
- 实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...
- 实验二 PHP基本语法实验
实验二 PHP基本语法实验 0 实验准备 0.1实验环境和相关工具软件 具体到的机房环境,请在Windowsxp环境下做本实验: l 操作系统:Windowsxp l Web服务器:Apache ...
- 20145213《Java程序设计》实验二Java面向对象程序设计实验报告
20145213<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装,继承,多态 初步掌握UML建模 熟悉S.O. ...
随机推荐
- .net 操作excel
.net 操作excel的常用组件:EPPlus,NPOI 1.NPOI,即POI的.NET版本(POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office文件, ...
- anacoda报错No module named 'sklearn.cross_validation'
在目前的snacoda里集成的sklearn已经不存在cross_validation模块了 使用以下模块 from sklearn.model_selection import train_te ...
- hdu 1241 Oil Deposits (简单搜索)
题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...
- vertx的ShardData共享数据
数据类型 一共4种 synchronous shared maps (local) asynchronous maps (local or cluster-wide) asynchronous loc ...
- PHP 递归几种方法
一.利用静态变量的方法 <?php function call(){ static $i = 0; echo $i . ''; $i++; if($i<10){ call(); } } c ...
- form中的button默认提交事件
<form action=""> <input autocomplete="off" type="text" name=& ...
- js 如何将dom转换为 图片(base64)
1.引入js <script src="https://cdn.bootcss.com/html2canvas/0.4.1/html2canvas.js"></s ...
- hackerrank杂记
https://www.hackerrank.com/challenges/py-set-discard-remove-pop/forum 知识点: *list:将list中的值取出,取出的数据大小是 ...
- cmake简明使用指南
cmake简明使用指南 Last update 2018/8/8 先执行cmake生成makefile,然后看看里面的内容,(至少在ubuntu16.04上的cmake3.5.1上),有如下内容提供: ...
- React:受控组件与非受控组件混用实战 - 译文
原文链接:React: hybrid controlled components in action 受控组件 非受控组件 混用受控组件和非受控组件 原则一 原则二 原则三 原则四 实施方案 总结 F ...