1.函数重载编程练习

编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型数据,调用测试。

#include <iostream>
using namespace std;
struct complex {
    double real;
    double imaginary;
};
int add(int,int);
double add(double,double);
complex add(complex,complex);
int main(){
    int a1,b1,S1;
    cin>>a1>>b1;
    S1=add(a1,b1);
    cout<<"S1="<<S1<<endl;
    
    double a2,b2,S2;
    cin>>a2>>b2;
    S2=add(a2,b2);
    cout<<"S2="<<S2<<endl;
    
    complex a3,b3,S3;
    cin>>a3.real>>a3.imaginary>>b3.real>>b3.imaginary;
    S3=add(a3,b3);
    cout<<"S3="<<S3.real<<"+"<<S3.imaginary<<"i"<<endl;
    
    return 0;
}

int add(int E1,int E2)
{  return E1+E2; }

double add(double X1,double X2)
{  return X1+X2; }

complex add(complex O1,complex O2)
{  complex C;
   C.real=O1.real+O2.real;
   C.imaginary=O1.imaginary+O2.imaginary;
   return C;
}

2.函数模板编程练习

编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试

#include<iostream>
using namespace std;

void swap(int *a,int *b)
{    int temp=*a;
    *a=*b;
    *b=temp;
}

void swap(double *a,double *b)
{    double temp=*a;
    *a=*b;
    *b=temp;
}

int part(int a[],int start,int end,int mid)
{
    while(start!=end)
    {
        while(a[end]>=mid&&start!=end) end--;
        if(start!=end)
        {
            swap(&a[start],&a[end]);
            start++;
            while(a[start]<=mid&&start!=end) start++;
            if(start!=end) 
            {
                swap(&a[start],&a[end]);
                end--;
            }
        }
    }
    return start;
}

int part(double b[],int start,int  end,double mid){
    while(start!=end)
    {
        while(b[end]>=mid&&start!=end) end--;
        if(start!=end)
        {
            swap(&b[start],&b[end]);
            start++;
            while(b[start]<=mid&&start!=end) start++;
            if(start!=end) 
            {
                swap(&b[start],&b[end]);
                end--;
            }
        }
    }
    return start;
}

int quicksort(int a[],int start,int end)
{
    int i;
    if(start<end)
    {
        i=part(a,start,end,a[start]);
        quicksort(a,start,i-1);
        quicksort(a,i+1,end);
    }
}

int quicksort(double b[],int start,int end)
{
    int i;
    if(start<end)
    {
        i=part(b,start,end,b[start]);
        quicksort(b,start,i-1);
        quicksort(b,i+1,end);
    }
}

int main()
{
    int array[7]={1,9,9,4,4,1,2};
       int j=7,i;
       cout<<"First: "<<endl;
       for(i=0;i<j;i++)
         cout<<array[i]<<"  ";

    quicksort(array,0,j-1);
    cout<<"\n"<<"Second: "<<endl;
    for(i=0;i<j;i++)
    {  cout<<array[i]<<"  ";  }
    cout<<"\n";
    double b[7]={9.4,5.4,11.28,2.4,4.8,7.38,3.24};
    int k=7,l;
    cout<<"First: "<<endl;
    for(l=0;l<k;l++)
         cout<<b[l]<<"  ";
    quicksort(b,0,k-1);
    cout<<"\n"<<"Second: "<<endl;
    for(l=0;l<k;l++)
         cout<<b[l]<<"  ";

    return 0;
}
//抱歉,本题此代码非本人所写

3.类的定义、实现和使用编程练习

设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下:

每一个用户有用户名(name)密码(passwd)联系邮箱(email)三个属性

支持设置用户信息setlnfo()允许设置信息时密码默认为6个1,联系邮箱默认为空串

支持打印用户信息printlnfo()打印用户、密码、联系邮箱。其中,密码以6个*方式显示

支持修改密码ChangePasswd()在修改密码前,要求先输入旧密码,验证无误后,才允许修改

如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序

在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信息)

#include <iostream>
#include <string>
using namespace std;
class User {
    public:
        void setInfo(string n1,string p1="111111",string e1="\0");        
        void ChangePasswd();
        void printInfo();
    private:
        string name;
        string passwd;
        string email; 
}; 

void User::setInfo(string n1,string p1,string e1){
    name=n1;
    passwd=p1;
    email=e1;
}

void User::ChangePasswd(){
    string oldpasswd;
    cout<<"Oldpasswd"<<endl;
    cin>>oldpasswd; 
    int n=1;
    do
    { cout<<"Sorry,that's wrong.Please enter again:";
      cin>>oldpasswd;
      n++;
    } while(oldpasswd!=passwd&&n<3);
    
    if(oldpasswd!=passwd&&n==3)
     cout<<"Please wait"<<endl;
    if(oldpasswd==passwd)
     { cout<<"Please input your new password:";
       cin>>passwd; }    
}

void User::printInfo(){
    cout<<"name:  "<<name<<endl;
    cout<<"password:****** "<<endl;;
    cout<<"email:  "<<email<<endl;
    cout<<endl;
}

int main(){
    cout<< " teating 1... " <<endl;
    User user1;
    user1.setInfo("Leonard");
    user1.printInfo();
    user1.ChangePasswd();
    user1.printInfo();
    
    cout<< " testing 2 " <<endl<<endl;
    User user2;
    user2.setInfo("Jonny","92197","xyz@hotmail.com");
    user2.printInfo();
    
    return 0;
}

评论链接:https://www.cnblogs.com/hi-ypy/p/10604787.html

                  https://www.cnblogs.com/0906mxyd/p/10604795.html

                 https://www.cnblogs.com/0122Frank/p/10587023.html