实验二

实验结论:

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 a,b;
   cout<<"enter two integer:";
   cin>>a>>b;
   cout<<"their sum:"<<add(a,b)<<endl;    
      
   double c,d;
   cout<<"enter two real number:";
   cin>>c>>d;
   cout<<"their sum:"<<add(c,d)<<endl;
   
   Complex e,f;
   char m,n,y,g;
   cout<<"enter two complex:";
   cin>>e.real>>m>>e.imaginary>>n>>f.real>>y>>f.imaginary>>g;
   add(e,f);
   
    return 0;
}
int add(int a,int b){
    return a+b;
}
double add(double a,double b){
    return a+b;
}
Complex add(Complex a,Complex b){
    cout<<"their sum:"<<a.real+b.real<<'+'<<a.imaginary+b.imaginary<<'i';
}

2.函数模板编程练习

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

quicksort.h

#ifndef QUICKSORT_H
#define QUICKSORT_H

template<class T>
void Qsort(T a[],int low,int high)
{if(low>=high)
   return ;
 int fleft=low,fright=high;
 T flagn=a[low];
 while(fleft<fright)
      {while(fleft<fright&&a[fright]>=flagn) fright--;
       a[fleft]=a[fright];
       while(fleft<fright&&a[fleft]<=flagn) fleft++;
       a[fright]=a[fleft];
      }//结束时左右指针指在同一空位 
 a[fleft]=flagn;
 Qsort(a,low,fleft-1);
 Qsort(a,fleft+1,high);//递归直到子序列的Low和high相等 
}

template<class T>
Quicksort(T a[],int n)
{Qsort(a,0,n-1);
}

#endif

output.h

#ifndef OUTPUT_H
#define OUTPUT_H
#include<iostream>
using namespace std;

template<class T>
Output(T a,int n)
{int i;
 for(i=0;i<=n-1;i++)
   cout<<a[i]<<"  ";
} 

#endif 

main.cpp

#include<iostream>
#include"quicksort.h"
#include"output.h"
using namespace std;

int main()
{int a[5]={3,1,4,8,2};
 double b[10]={1.1,3.5,2.2,3.1,6.4,5.9,5.1,6.9,7.1,8.8};
 
 Quicksort(a,5);
 Output(a,5);
 cout<<endl;
 
 Quicksort(b,10);
 Output(b,10);
 cout<<endl;
 
 return 0;
}

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

设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下: 每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。 支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。 支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。 支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。 在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信 息)

#include <iostream>
#include <string>
using namespace std;
// User类的声明
class User {
     public:
            void setInfo(string name0="",string passwd0="111111",string email0=" ");
            // 声明带有默认形参值的成员函数setInfo()
            //密码默认值为6个1
            // 邮箱默认值为空串
            void changePasswd();
            void printInfo();
     private:
            string name;
            string passwd;
            string email;
};
// User类的实现
// 成员函数setInfo()的实现
// 功能:设置用户名(name), 密码(passwd), 联系邮箱(email)
void User::setInfo(string name0,string passwd0,string email0)
{name=name0;
 passwd=passwd0;
 email=email0;
}
// 成员函数changePasswd()的实现
// 功能:修改密码
// 要求:在修改密码前,要求先输入旧密码,验证无误后,才允许修改。
// 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。
void User::changePasswd()
{string oldpasswd;
 int n=0;
 cout<<"please enter old passwword:";
 cin>>oldpasswd;
 n++;
 while(oldpasswd!=passwd&&n<=2)
      {cout<<"passwd input error,Please re-Enter again:";
       cin>>oldpasswd;
       n++;
      }
 if(oldpasswd==passwd)
   {cout<<"please enter new passward: ";
    cin>>passwd;
   }
  else if(n=3)
        cout<<"Please try after a while."<<endl;
}
// 成员函数printInfo()的实现
// 功能:打印用户信息
// 要求: 密码以6个*显示
void User::printInfo()
{cout<<"name:"<<name<<endl;
 cout<<"paassward:******"<<endl;
 cout<<"email:"<<email<<endl;
}
// 在主函数中测试User类
// 用User类创建对象,测试类的功能
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.快速排序程序,让我学会了使用自定义头文件,也让我了解到了快速排序在编程时的思维与平时使用快速排序时非常不一样,甚至用到了递归。平时应该多多练习和了解计算机的各种算法。

 

 

 

互评:

http://www.cnblogs.com/laboratory-X/

http://www.cnblogs.com/lovecpp/

http://www.cnblogs.com/ggwdcs/

posted @ 2019-03-23 21:05  shona小暖  阅读(190)  评论(2编辑  收藏  举报