首页 > 编程语言 > 详细

C++ 实验2

时间:2019-03-24 16:04:28      阅读:168      评论:0      收藏:0      [点我收藏+]

#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

原文:https://www.cnblogs.com/suifeng823/p/10588507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!