#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的更多相关文章

  1. [原] 利用 OVS 建立 VxLAN 虚拟网络实验

    OVS 配置 VxLAN HOST A ------------------------------------------ | zh-veth0(10.1.1.1) VM A | | ---|--- ...

  2. Android中Activity的四大启动模式实验简述

    作为Android四大组件之一,Activity可以说是最基本也是最常见的组件,它提供了一个显示界面,从而实现与用户的交互,作为初学者,必须熟练掌握.今天我们就来通过实验演示,来帮助大家理解Activ ...

  3. SEED实验系列文章目录

    美国雪城大学SEEDLabs实验列表 SEEDLabs是一套完整的信息安全实验,涵盖本科信息安全教学中的大部分基本原理.项目组2002年由杜文亮教授创建,目前开发了30个实验,几百所大学已采用.实验楼 ...

  4. 物联网实验4 alljoyn物联网实验之手机局域网控制设备

    AllJoyn开源物联网协议框架,官方描述是一个能够使连接设备之间进行互操作的通用软件框架和系统服务核心集,也是一个跨制造商来创建动态近端网络的软件应用.高通已经将该项目捐赠给了一个名为“AllSee ...

  5. (转)linux下和云端通讯的例程, ubuntu和openwrt实验成功(一)

    一.  HTTP请求的数据流总结#上传数据, yeelink的数据流如下POST /v1.0/device/4420/sensor/9089/datapoints HTTP/1.1Host: api. ...

  6. (原创) alljoyn物联网实验之手机局域网控制设备

    AllJoyn开源物联网协议框架,官方描述是一个能够使连接设备之间进行互操作的通用软件框架和系统服务核心集,也是一个跨制造商来创建动态近端网络的软件应用.高通已经将该项目捐赠给了一个名为“AllSee ...

  7. 实验:Oracle直接拷贝物理存储文件迁移

    实验目的:Oracle直接拷贝物理文件迁移,生产库有类似施工需求,故在实验环境简单验证一下. 实验环境: A主机:192.168.1.200 Solaris10 + Oracle 11.2.0.1 B ...

  8. Oracle RAC 更换存储实验

    实验环境准备: RHEL 6.5 + Oracle 11.2.0.4 RAC (2nodes) OCR和Voting Disk使用的是OCR1磁盘组,底层对应3个1G大小的共享LUN,一般冗余: DA ...

  9. Vertica集群扩容实验过程记录

    需求: 将3个节点的Vertica集群扩容,额外增加3个节点,即扩展到6个节点的Vertica集群. 实验环境: RHEL 6.5 + Vertica 7.2.2-2 步骤: 1.三节点Vertica ...

  10. 数据库---实验四 oracle的安全性和完整性控制

    实验内容: (一) 授权 . 以dba用户的身份登陆oracle,创建用户u1+学号后四位,u2+学号后四位. SQL> create user u1_3985 identified by &q ...

随机推荐

  1. Python爬虫实战一之爬取QQ音乐

    一.前言   前段时间尝试爬取了网易云音乐的歌曲,这次打算爬取QQ音乐的歌曲信息.网易云音乐歌曲列表是通过iframe展示的,可以借助Selenium获取到iframe的页面元素, 而QQ音乐采用的是 ...

  2. MySQL问题汇总

    1.中文乱码 连接设置: 数据库设置:

  3. 51nod--1183 编辑距离(动态规划)

    题目: 1183 编辑距离 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指 ...

  4. PHP -- 七牛云 在线视频 获取某一帧作为封面图

    ### 最近碰到视频处理,需要视频封面? 但用的是七牛云存储视频,索性搜了一下,怎么获取视频的某一帧作为视频的封面图... 发现了七牛官网又自身的接口 ### https://developer.qi ...

  5. AngularJS初识

     AngularJS 简介 AngularJS是一个javaScript框架,是一个用JavaScript编写的库,通过指令扩展了HTML,且通过表达式绑定数据到HTML中. AngularJS使开发 ...

  6. Java Web 中使用ffmpeg实现视频转码、视频截图

    Java Web 中使用ffmpeg实现视频转码.视频截图 转载自:[ http://www.cnblogs.com/dennisit/archive/2013/02/16/2913287.html  ...

  7. table切换jquery插件 jQuery插件写法模板 流程

    通过$.extend()来扩展jQuery 通过$.fn 向jQuery添加新的方法 通过$.widget()应用jQuery UI的部件工厂方式创建 通过$.extend()来扩展jQuery $. ...

  8. ansible的lookup

    lookup路径: /usr/lib/python2.7/site-packages/ansible/plugins/lookup 所有的lookup插件列表cartesian.py dnstxt.p ...

  9. MongoDB超级简明入门教程

    1.概念篇 MongoDB和MySQL分别作为非关系型数据库和关系型数据库的代表,通过它们之间的对比可以很快的建立起对MongoDB的认知. MongoDB MySQL 数据库(Database) 数 ...

  10. 【js】正则

    复习字符串操作search 查找substring 获取子字符串charAt 获取某个字符split 分割字符串,获得数组 <script> var str="abcdef&qu ...