C++实验二

 

一、函数重载编程练习

编写重载函数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;
    cin>>a>>b;
    cout<<"sum="<<add(a,b)<<endl;
    double x,y;
    cin>>x>>y;
    cout<<"sum="<<add(x,y)<<endl;
    complex h,j,s1;
    cin>>h.real>>h.imaginary>>j.real>>j.imaginary;
    s1=add(h,j);
    cout<<"sum="<<s1.real<<"+"<<s1.imaginary<<"i"<<endl;
    return 0;

}
int add(int a1,int b1){
    return a1+b1;
}
double add(double x1,double y1){
    return x1+y1;
}
complex add(complex h,complex j){
    complex s1;
    s1.real=h.real+j.real;
    s1.imaginary=h.imaginary+j.imaginary;
    return s1;
}
View Code

二、函数模板编程练习

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

#include <iostream>
using namespace std;
template<class T>
void Quick_sort(T a[],int l,int h)
{
  int i=l;
  int j=h;
  int t=a[i];
  if(i<j)
  {
   while(i<j)
   { 
     while(i<j&&a[j]>=t)
     j--;
     if(i<j)
     {
      a[i]=a[j];
      i++;
 }
while(i<j&&t>a[i])
i++;
if(i<j)
{
a[j]=a[i];
j--;}
}
a[i]=t;
Quick_sort(a,l,i-1);
Quick_sort(a,i+1,h);
}
} 
int main()
{ 
 int i;
 int m[10]={56,12,77,10,6,8,64,32,14,79};
 Quick_sort(m,0,9);
 cout<<"从小到大:"; 
 for(i=0;i<10;i++)
 cout<<m[i]<<"  "; 
 return 0;
} 
View Code

 

 

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

设计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下:
每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。

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

#include<iostream>
#include<string>
using namespace std;
class User{
    public:
        void setInfo(string name1,string passwd1="111111",string email1=" ");
        void changePasswd();
        void printInfo();
    private:
        string name;
        string passwd;
        string email;    
};
void User::setInfo(string name1,string passwd1,string email1)
    {
    name=name1;
    passwd=passwd1;
    email=email1;
}
void User::changePasswd()
{ string passwd;
  int i=1;
  cout<<"请输入旧密码:"<<endl;
  cin>>passwd;
  while(passwd!="111111"&&i<=2) 
  {i++;
  cout<<"密码错误,请重新输入:"<<endl;
  cin>>passwd;
  }
  if(passwd=="111111")
  {cout<<"请输入新的密码:"<<endl;
  cin>>passwd;
  cout<<"输入成功"<<endl; 
  }
  else 
  cout<<"三次输入错误,请稍后再试"<<endl; 
}
void User::printInfo (){
    cout<<"name: "<<name<<endl;
    cout<<"password: "<<"******"<<endl;
    cout<<"email: "<<email<<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;
} 
View Code

 

 

 总结

虽然老师已经给了大概的框架但是这三道题目还是让我研究了很久,尤其是第二题,我在网上搜索了一下但还是不是很理解。

实验一评论:

https://www.cnblogs.com/bzwy/p/10533991.html#4213194

 

https://www.cnblogs.com/ggwdcs/p/10543901.html#4211995

 

https://www.cnblogs.com/qsxsc/p/10541028.html#4211998

 

posted @ 2019-03-26 13:11  景行ai  阅读(205)  评论(2编辑  收藏  举报