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. ...
随机推荐
- encode与decode
import torch from torch import nn import numpy as np import matplotlib.pyplot as plt import torch.ut ...
- notepad++安装nppFTP
官网下载的最新版notepad++,结果pluginadmin里面installnppftp总是安不上,点击install之后然后点是就退出,再进去也没有安装好. 网上找了半天也没找到什么有用信息,最 ...
- ubuntu系统的teamviewer的安装及使用
参考链接: 安装: https://blog.csdn.net/weixin_34613450/article/details/80541799 使用: https://jingyan.baidu.c ...
- 十五丶IO model
事件驱动模型 上节的问题: 协程:遇到IO操作就切换. 但什么时候切回去呢?怎么确定IO操作完了? 很多程序员可能会考虑使用“线程池”或“连接池”.“线程池”旨在减少创建和销毁线程的频率,其维持一定合 ...
- Typescript---02 变量声明
声明变量: let和const是JavaScript里相对较新的变量声明方式.let在很多方面与var是相似的,但是可以避免在JavaScript里常见一些问题. const是对let的一个增强,它能 ...
- KVM 热迁移
最终我们迁移的目的就是: ·简化系统维护管理 ·高系统负载均衡 ·增强系统错误容忍度 ·优化系统电源管理 热迁移 又叫动态迁移,实时迁移,即虚拟机保存( save )/恢复( restore ):将整 ...
- 项目实战-使用PySpark处理文本多分类问题
原文链接:https://cloud.tencent.com/developer/article/1096712 在大神创作的基础上,学习了一些新知识,并加以注释. TARGET:将旧金山犯罪记录(S ...
- Redis实战 - 5事务:multi、exec和watch
介绍 redis的目标的是: 简洁,高效,由于事务本身就是一个很复杂的东西,所有我们不能把事务做的太复杂. DISCARD 取消事务,放弃执行事务块内的所有命令. EXEC 执行所有事务块内的命令. ...
- ***CodeIgniter框架集成支付宝即时到账支付SDK
本文为CI集成支付宝即时到账支付接口 1.下载支付宝官方demo ;即时到账交易接口(create_direct_pay_by_user)(DEMO下载) 原文地址:https://doc.open. ...
- Debian+Django+uWsgi+nginx+mysql+celery
下载系统各种依赖 nano /etc/apt/sources.list 在Debian中使用apt-get安装软件包时经常会提示让你插入netinst的光盘: Media change: please ...