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 ...
随机推荐
- 18 常用模块 random shutil shevle logging sys.stdin/out/err
random:随机数 (0, 1) 小数:random.random() ***[1, 10] 整数:random.randint(1, 10) *****[1, 10) 整数:random.rand ...
- 将DataTable转换为List<T>对象遇到问题:类型“System.Int64”的对象无法转换为类型“System.Int32”。
可以利用反射将DataTable转换为List<T>对象:原始链接http://www.jb51.net/article/67386.htm 但是该方法在DataTable里某个字段类型是 ...
- this:当前调用的对象
- 金蝶k/3 K3密码对照破解源码
金蝶k/3 K3密码对照破解源码 通过密码对照表进行密码破解 以下是源码: VERSION 5.00 Object = "{0ECD9B60-23AA-11D0-B351-00A0C9055 ...
- WPF 杂记
1,跨屏最大化 单屏幕的时候我们可以设置 WindowState 来使应用最大化 当接多个屏幕的时候,就需要下面这个设置: private void FullScreen() { this.Windo ...
- ajax 函数回调
var initTaxPriod = function (taxNo) { intitSearch(); $("#taxPeriod").html(""); t ...
- python面试题之什么是PEP8规范
1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格. 2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车. 3 类和top ...
- JSONObject类的引用必须jar包
JSONObject所必需的6个jar包: commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar c ...
- C#-导入Excel 内容到 DataTable中
C#-导入Excel 内容到 DataTable中 直接传入文件路径,支持所有Excel格式. 缺点:如果数据量庞大会很占内存. public static DataTable ImportExcel ...
- XVII Open Cup named after E.V. Pankratiev. XXI Ural Championship
A. Apple 按题意模拟即可. #include<stdio.h> #include<iostream> #include<string.h> #include ...