实验二

重载函数:

 1 #include<iostream>
 2 using namespace std;
 3 struct Complex {
 4     double real;
 5     double imaginary;
 6 };
 7 int add(int, int);
 8 double add(double,double);
 9 Complex add(Complex, Complex);
10 int main() 
11 {
12     cout<<"1+2="<<add(1,2)<<endl;
13     cout<<"1.5+2.7="<<add(1.5,2.7)<<endl;
14     Complex x={1.1,2.2};
15     Complex y={2.2,3.3};
16     Complex z=add(x,y);
17     cout<<z.real<<","<<z.imaginary<<endl;
18     return 0;
19 }
20 int add(int x,int y)
21 {
22     return(x+y);
23 }
24 double add(double x,double y)
25 {
26     return (x+y);
27 }
28 Complex add(Complex a,Complex b)
29 {
30     Complex sum;
31     sum.real=a.real+b.real;
32     sum.imaginary=a.imaginary+b.imaginary;
33     return sum;
34     
35 }

 

快速排序:

非模板函数:

 1 #include<iostream>
 2 using namespace std;
 3 void kuaisu(int a[],int low,int high);
 4 int main()
 5 {
 6     int i;
 7     int a[]={152,487,63,47,153,987,623,145,333,687};
 8     kuaisu(a,0,9);
 9     for(i=0;i<=9;i++)
10     {
11         cout<<a[i]<<endl;
12     }
13     return 0;
14 }
15 void kuaisu(int a[],int low,int high)
16 {
17     int n=low;
18     int m=high;
19     int t=a[n];
20     if(n>=m)
21     {
22         return;
23     }
24     while(n<m)
25     {
26         while(n<m&&a[m]>=t)
27         {
28             m--;
29         }
30         a[n]=a[m];
31         while(n<m&&a[n]<=t)
32         {
33             n++;
34         }
35         a[m]=a[n];
36         a[n]=t;
37         kuaisu(a,low,m-1);
38         kuaisu(a,n+1,high);
39     }
40     
41 }

 

模板函数

(暂时无法运行,还没有到找问题所在)

排序:

#ifndef QSORT_H
#define QSORT_H
#include<iostream>
using namespace std;
template<class T>
    void qsort(T a[],int l,int h)
    {
        int first=l;
        int last=h;
        int temp=a[first];
        if(first>=last)
        {
            return;
        }
        else
        {
            while(first<last)
            {
                while(first<last&&a[last]>=temp)
                {
                    last--;
                }
                a[first]=a[last];
                while(first<last&&a[first]<temp)
                {
                    first++;
                }
                a[last]=a[first];
                a[first]=temp;
                qsort(a,l,last-1);
                qsort(a,first+1,h);
            }
        }
        
    }
#endif

输出:

#ifndef OUTPUT_H
#define OUTPUT_H
#include<iostream>
using namespace std;
template<class T>
    void output(T s[],int n)
    {
        int i;
        for(i=0;i<n;i++)
        cout<<s[i]<<" ";
        cout<<endl;
    }
#endif

main函数:

#include<iostream>
#include "QSORT.h"
#include "OUTPUT.h" 
using namespace std;
int main()
{
    int i;
    int a[]={152,487,63,47,153,987,623,145,333,687};
    OUTPUT(a,10);
    return 0;
}

类的定义和使用:

(选做还未完成)

#include <iostream>
#include <string>
using namespace std;
class User
{
    public:
        void setInfo(string names,string passwords="111111",string emails=" ");
        void changePasswd();
        void printInfo();
    private:
        string name;
        string password;
        string email;
    
};
void User::setInfo(string names,string passwords,string emails)
{
    name=names;
    password=passwords;
    email=emails;
    
}
void User::changePasswd()
{
    cout<<"Enter the old password:";
    string yuanmima;
    cin>>yuanmima;
    int i=0;
    while(yuanmima!=password)
    {
        cout<<"passwd input error,Please re-Enter again:";
        cin>>yuanmima;
        i++;
        if(i>=2)
        {
            cout<<"Please try after a while";
            break;
        }
        
    }
    if(yuanmima==password)
    {
        cout<<"Enter the new password:";
        cin>>yuanmima;
    }
    password=yuanmima;
}
void User::printInfo()
{
    cout<<"name:"<<name<<endl;
    cout<<"passwd:"<<"******"<<endl;
    cout<<"email:"<<email<<endl;
}
int main() {
cout << "testing 1......" << endl;
// 测试1
User user1;
user1.setInfo("Leonard");
user1.printInfo();
user1.changePasswd();
user1.printInfo();
cout << endl << "testing 2......" << endl << endl;
// 测试2
User user2;
user2.setInfo("Jonny","92197","xyz@hotmail.com");
user2.printInfo();
return 0;
}

总结:

1、重载函数的形参必须不同:个数不同或者类型不同。

2、快速排序的方法需要动手实践才能了解清楚。

3、class和stuct有异曲同工之妙。默认形参值既可以预先声明,也可以在’引用函数时进行赋值。

互评地址:

https://www.cnblogs.com/syf1/p/10561928.html

https://www.cnblogs.com/qiuxiuh/p/10555836.html

https://www.cnblogs.com/lc21/p/10557547.html

 

posted @ 2019-03-24 09:37  唱国歌也跑调  阅读(351)  评论(4)    收藏  举报