(1)

#include<iostream>
using namespace std;
void InsertSort(int a[], int n)
{
for (int j = 1; j < n; j++)
{
int key = a[j];
int i = j - 1;
while (i >= 0 && key < a[i])
{
a[i + 1] = a[i];
i--;
}
a[i + 1] = key;
}
}
int main() {
int d[] = { 23, 15, 9, 20, 69, 31, 45 };
cout << "输入数组 { 23, 15, 9, 20, 69, 31, 45 } " << endl;
InsertSort(d,7);
cout << "排序后结果:";
for (int i = 0; i < 7; i++)
{
cout << d[i]<<" ";
}
}

(2)

#include<iostream>using namespace std;
#include"InsertSort.h"
#include"output.h"int main()
{
int a[5]={3,2,6,8,7};
double b[5]={4.4,8.8,7.7,1.1,9.9};

InsertSort(a,0,4);
output(a,5);
cout<<endl;
InsertSort(b,0,4);
output(b,5);
return 0;
}
#ifndef INSERTSORT_H#define INSERTSORT_Husing namespace std;
template<class T>
void InsertSort(T s[],int l,int r)
{
if(l<r)
{
int i,j;
T x;
i=l,j=r,x=s[l];
while(i<j)
{
while(i<j&&s[j]>=x)
j--;
if(i<j)
s[i++]=s[j];
while(i<j&&s[i]<=x)
i++;
if(i<j)
s[j--]=s[i];
}
s[i]=x;
InsertSort(s,l,i-1);
InsertSort(s,i+1,r);
}
}

(3)

#include<iostream>
#include<string>
using namespace std;
class user
{
public:
void setInfo(string name0,string password0="111111",string email0=" ");
void changePassword();
void printInfo();
private:
string name;
string password;
string email;
};int main()
{
cout<<"testing 1...."<<endl;
user user1;
user1.setInfo("Leonard");
user1.printInfo();
user1.changePassword();
user1.printInfo();
cout<<"testing 2...."<<endl;
user user2;
user2.setInfo("jonny","92197","xyz@hotmail.com");
user2.printInfo();
}void user::setInfo(string name0,string password0,string email0)
{
name=name0;
password=password0;
email=email0;
}void user::printInfo()
{
cout<<"name is"<<":"<<name<<endl;
cout<<"password is"<<":"<<"******"<<endl;
cout<<"email is"<<":"<<email<<endl;
}void user::changePassword()
{
string oldpassword;
cout<<"Please enter your old password"<<":";
cin>>oldpassword;
int i=1;
while(oldpassword!=password&&i<3)
{
cout<<"wrong!!please continue cin"<<":";
cin>>oldpassword;
i++;
}
if(i>=3)
{
cout<<"Please try latter"<<endl;
}
if(oldpassword==password)
{
string newpassword;
cout<<"Please enter your new password"<<":";
cin>>newpassword;
password=newpassword;
}
}

 

posted on 2019-03-25 22:39  转身陌路  阅读(105)  评论(1编辑  收藏  举报