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","[email protected]");
user2.printInfo();
return 0;
}
Summary:本次實驗主要是對函數模板的熟悉,編寫以及調試。編寫模板時要註意對應問題和對各個部分的調諧。
C++ 實驗2