C++ 实验2
#include <iostream>
using namespace std;
template<class T>
void insertionSort(T a[],int n){
int i,j;
T temp;
for(int i=1;i<n;i++)
{
int j=i;
T temp=a[i];
while (j>0&&temp<a[j-1]){
a[j]=a[j-1];
j--;
}
a[j]=temp;
}
}
int main()
{
int i ,a[10] = {5,2,6,0,3,9,1,7,4,8};
insertionSort(a,10);
for( i=0; i < 10 ;i++ )
{
cout << a[i]<<” “;
}
cout << endl;
return 0;
}

#include <iostream>
using namespace std;
template<class T>
void mySwap(T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
template<class T>
void selectionSort(T a[],int n)
{
for(int i=0;i<n-1;i++){
int leastIndex=i;
for(int j=i+1;j<n;j++)
if(a[j]<a[leastIndex])
leastIndex=j;
mySwap(a[i],a[leastIndex]);
}
}
int main()
{
int i;
int a[5]={0,2,3,1,5};
selectionSort(a,5);
for(int i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}

#include <iostream>
using namespace std;
template<class T>
void mySwap(T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
template<class T>
void bubbleSort(T a[],int n){
int i=n-1;
while (i>0){
int lastExchangeIndex=0;
for(int j=0;j<i;j++)
if(a[j+1]<a[j]){
mySwap(a[j],a[j+1]);
lastExchangeIndex=j;
}
i=lastExchangeIndex;
}
}
int main()
{
int i;
int a[5]={0,2,3,1,5};
bubbleSort(a,5);
for(int i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}

#include <iostream>
using namespace std;
template<class T>
int seqSearch(const T list[],int n,const T &key){
for(int i=0;i<n;i++)
if(list[i]==key)
return i;
return -1;
}
int main()
{
int i;
int a[5]={0,2,3,1,5};
cout<<seqSearch(a,5,1);
cout<<endl;
return 0;
}

#include<iostream>
using namespace std;
template<class T>
int binSearch(const T list[],int n,const T &key)
{
int low=0;
int high=n-1;
while (low<=high){
int mid=(low+high)/2;
if(key==list[mid])
return mid;
else if(key<list[mid])
high=mid-1;
else low=mid+1;
}
return -1;
}
int main()
{
int a[5]={1,2,3,4,5};
cout<<binSearch(a,5,2);
cout<<endl;
return 0;
}

#include<iostream>
using namespace std;
struct Complex {
double real;
double imaginary;
};
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 i;
i.real=a.real+b.real;
i.imaginary=a.imaginary+b.imaginary;
return i;
};
int main() {
int m,n;
cout<<"Enter two integer:";
cin>>m>>n;
cout<<"Their sum:"<<add(m,n)<<endl;
double x,y;
cout<<"Enter two real number:";
cin>>x>>y;
cout<<"Their sum:"<<add(x,y)<<endl;
Complex a,b,c;
cout<<"Enter two complex unmber:";
cin>>a.real>>a.imaginary;
cin>>b.real>>b.imaginary;
c=add(a,b);
cout<<"Their sum:"<<c.real<<"+"<<c.imaginary<<"i"<<endl;
return 0;
}

#include <iostream>
using namespace std;
template <class T>
void InsertSort(T a[],int i,int j)
{
int x,y;
T s;
x=i,y=j,s=a[i];
while(x<y)
{
while(x<y&&a[y]>=s)
y--;
if(x<y)
a[x++]=a[y];
while(x<y&&a[x]<=s)
x++;
if(x<y)
a[y--]=a[x];
}
a[x]=s;
int k=0;
for(k=0;k<j+1;k++)
{
cout<<a[k]<<" ";
}
}
int main()
{
int a[5]={1,2,3,4,5};
InsertSort(a,0,4);
return 0;
}

#include <iostream>
#include <string>
using namespace std;
class User {
public:
void setInfo(string name,string passwd_="111111",string email_=" ");
void changePasswd();
void printInfo();
private:
string name;
string passwd;
string email;
};
void User::setInfo(string name_,string passwd_,string email_){
name=name_;
email=email_;
passwd=passwd_;
}
void User::printInfo(){
cout << "name:\t" <<name<<endl;
cout << "passwd:\t" <<"******"<<endl;
cout << "email:\t" <<email<<endl;
}
void User::changePasswd(){
int i=1;
string oldpasswd;
while(i<3)
{
cout<<"Enter the old passwd:";
cin>>oldpasswd;
if(oldpasswd==passwd)
{
string newpasswd;
cout<<"Enter the new passwd:";
cin>>newpasswd;
passwd=newpasswd;
break;
}
else if(oldpasswd!=passwd)
{
cout<<"passwd input error,Please re-Enter again:";
cin>>oldpasswd;
i++;
if(i==3)
{
cout<<endl<<" Please try after a while"<<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","92197","xyz@hotmail.com");
user2.printInfo();
return 0;
}

Summary:本次实验主要是对函数模板的熟悉,编写以及调试。编写模板时要注意对应问题和对各个部分的调谐。
C++ 实验2的更多相关文章
- [原] 利用 OVS 建立 VxLAN 虚拟网络实验
OVS 配置 VxLAN HOST A ------------------------------------------ | zh-veth0(10.1.1.1) VM A | | ---|--- ...
- Android中Activity的四大启动模式实验简述
作为Android四大组件之一,Activity可以说是最基本也是最常见的组件,它提供了一个显示界面,从而实现与用户的交互,作为初学者,必须熟练掌握.今天我们就来通过实验演示,来帮助大家理解Activ ...
- SEED实验系列文章目录
美国雪城大学SEEDLabs实验列表 SEEDLabs是一套完整的信息安全实验,涵盖本科信息安全教学中的大部分基本原理.项目组2002年由杜文亮教授创建,目前开发了30个实验,几百所大学已采用.实验楼 ...
- 物联网实验4 alljoyn物联网实验之手机局域网控制设备
AllJoyn开源物联网协议框架,官方描述是一个能够使连接设备之间进行互操作的通用软件框架和系统服务核心集,也是一个跨制造商来创建动态近端网络的软件应用.高通已经将该项目捐赠给了一个名为“AllSee ...
- (转)linux下和云端通讯的例程, ubuntu和openwrt实验成功(一)
一. HTTP请求的数据流总结#上传数据, yeelink的数据流如下POST /v1.0/device/4420/sensor/9089/datapoints HTTP/1.1Host: api. ...
- (原创) alljoyn物联网实验之手机局域网控制设备
AllJoyn开源物联网协议框架,官方描述是一个能够使连接设备之间进行互操作的通用软件框架和系统服务核心集,也是一个跨制造商来创建动态近端网络的软件应用.高通已经将该项目捐赠给了一个名为“AllSee ...
- 实验:Oracle直接拷贝物理存储文件迁移
实验目的:Oracle直接拷贝物理文件迁移,生产库有类似施工需求,故在实验环境简单验证一下. 实验环境: A主机:192.168.1.200 Solaris10 + Oracle 11.2.0.1 B ...
- Oracle RAC 更换存储实验
实验环境准备: RHEL 6.5 + Oracle 11.2.0.4 RAC (2nodes) OCR和Voting Disk使用的是OCR1磁盘组,底层对应3个1G大小的共享LUN,一般冗余: DA ...
- Vertica集群扩容实验过程记录
需求: 将3个节点的Vertica集群扩容,额外增加3个节点,即扩展到6个节点的Vertica集群. 实验环境: RHEL 6.5 + Vertica 7.2.2-2 步骤: 1.三节点Vertica ...
- 数据库---实验四 oracle的安全性和完整性控制
实验内容: (一) 授权 . 以dba用户的身份登陆oracle,创建用户u1+学号后四位,u2+学号后四位. SQL> create user u1_3985 identified by &q ...
随机推荐
- .Net Core---- 自带Json返回日期带T格式 解决
前段时间再做core的列表显示中(前台代码是在.net core bootstrap集成框架上的(这是效果浏览地址:http://core.jucheap.com[效果地址来自:http://blog ...
- apache负载调优
Apache负载调优 watch -n 1 -d "pgrep httpd|wc -l" #apache动态查看连接数 ps aux | grep httpd | wc ...
- Ubuntu下 安装MiniGUI
1. 需要下载的组件 首先需要这些安装包,这些安装包可以在MiniGUI官网上下载. libminigui-gpl-3_0_12.tar.gzmg-samples-3_0_12.tar.gzfreet ...
- 如何解决 kubernetes 重启后,启来不来的问题
参考了 https://blog.csdn.net/nklinsirui/article/details/80855415 最近在调研 kubeneter ,准备把线上的服务器架构再调整下,然后模拟各 ...
- mq_receive
NAME mq_receive - 从消息队列中获取消息 (REALTIME) SYNOPSIS #include <mqueue.h> ssize_t mq_receive(mqd_t ...
- 【ABP】工作单元——不进行事物独立执行功能
1.注入 private readonly IUnitOfWorkManager unitOfWorkManager; 2.构造 3.开启新事物 using (var unitOfWork = uni ...
- Beta(7/7)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- iframe ios中h5页面 样式变大
实际项目开发中,iframe在移动设备中使用问题还是很大的,说一说我的那些iframe坑 做过的这个后台管理框架,最开始的需求是PC,但随着业务需要,需要将项目兼容到ipad,后台的框架也是使用的开源 ...
- Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题
(转载)Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题 这几天在用spring boot开发项目, 在开发的过程中遇到一个问题hibernate在执 ...
- JavaScript浏览器解析原理
首先,JavaScript的特点是: 1. 跨平台 可以再不同的操作系统上运行. 2. 弱类型 与之相对的是强类型 强类型:在定义变量的时候,需要将变量的数据类型表明.例如:Java 弱类型:定义变量 ...