zoukankan      html  css  js  c++  java
  • 第二次C++作业(函数重载与函数模板,以及简单类的定义与实现)

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

    实验代码

     1 #include<iostream>
     2 using namespace std;
     3 struct Complex 
     4         {
     5             double real;
     6             double imaginary;
     7         };
     8 
     9 int add(int, int);
    10 double add(double,double);
    11 Complex add(Complex, Complex);
    12 int main() {
    13     int x, y;
    14     cout << "Please input two numbers:";
    15     cin >> x >> y;
    16     cout << "The sum is:" << add(x, y) << endl;
    17     double m, n;
    18     cout << "Please input two double numbers:";
    19     cin >> m >> n;
    20      cout << "The sum is:" << add(m, n) << endl;
    21         struct Complex e,f;
    22         cout << "Please input the first r and i numbers:";
    23         cin >> e.real >> e.imaginary;
    24       cout << "Please input the second r and i numbers:";
    25          cin >> f.real >> f.imaginary;
    26     cout<<"The sum is:";
    27     add(e,f);
    28 
    29 
    30         return 0;
    31 }
    32 int add(int a,int b)
    33 {
    34     return a + b;
    35 }
    36 double add(double a,double b)
    37 {
    38     return a + b;
    39 }
    40 Complex add(Complex e,Complex f)
    41 {
    42     cout<<e.real+f.real<<"+"<<e.imaginary+f.imaginary<<"i";
    43 
    44 }

    运行截图

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

    这是一开始挣扎的错误代码

     1 #include<iostream>
     2 using namespace std;
     3 template<class T>
     4 void myswap(T &x,T &y)
     5 {
     6  T temp=x;
     7  x = y;
     8  y = temp;
     9 }
    10 template<class T>
    11 void quicksort(T a[],int n)
    12 {
    13  while(true)
    14  {
    15  int i = 0, j = n - 1;
    16  T key = a[0];
    17  for (i = 1; i < n;i++)
    18  {
    19  if(a[i]<key)
    20  {
    21  i = 0;
    22  break;
    23  }
    24  else
    25  key = a[i + 1];
    26  }
    27  for (j = n - 1; j>i; j--)
    28  {
    29  if(a[j]<key)
    30  {
    31  myswap(a[i], a[j]);
    32  break;
    33  }
    34  }
    35  for (i = 0; i<j; i++)
    36  if (a[i]>key)
    37  {
    38  myswap(a[i], a[j]);
    39  break;
    40  }
    41  quicksort(a, n);
    42  if (i == j)
    43  break;
    44  }
    45 }
    46 template<class T>
    47 void output(T q[],int k)
    48 {
    49  for (int i = 0; i < k;i++)
    50  {
    51  cout << q[i]<<" ";
    52  }
    53  cout << endl;
    54 }
    55 int main()
    56 {
    57  int m[7] = {6, 3, 7, 4, 2, 1,9};
    58  quicksort(m, 7);
    59  output(m, 7);
    60  return 0;
    61 }

    想利用一个辅助的交换数值的函数来做,还想不用递归

    呃,查资料发现不用递归也可以做,但是大量涉及栈等等我不清楚的东西,等我以后再学学然后回头想想吧(确信)


    下面是学习了之后,一个简洁而标准的快速排序算法(利用递归)
     1 #include<iostream>
     2 using namespace std;
     3 template<class T>
     4 void quicksort(T a[], int l, int r)
     5  {
     6  if (l < r)
     7  {
     8  int i, j;
     9  T key;
    10 
    11          i = l;      //l为数组最左端
    12          j = r;      //r为数组最右端
    13          key = a[i]; //key为基准值
    14          while (i < j)
    15          {
    16              while (i < j && a[j] > key)
    17                  j--; // 从右向左找第一个小于x的数
    18              if (i < j)
    19                  a[i++] = a[j];
    20              while(i < j && a[i] < key)
    21                  i++; // 从左向右找第一个大于x的数
    22              if(i < j)
    23                  a[j--] = a[i];
    24          }
    25          a[i] = key;//基准值重置
    26          quicksort(a, l, i-1); //递归使用
    27          quicksort(a, i+1, r); 
    28      }
    29 }
    30 template<class T>
    31 void output(T q[],int k)
    32 {
    33     for (int i = 0; i < k;i++)
    34     {
    35         cout << q[i]<<" ";
    36     }
    37     cout << endl;
    38 }
    39 int main()
    40 {
    41     int m[7] = {6, 3, 7, 4, 2, 1,9};
    42     double q[7] = {22.3, 4.5, 56.1, 26.3, 33.2, 891.2, 1.5};
    43     quicksort(m,0, 6);
    44     output(m, 7);
    45     quicksort(q, 0, 6);
    46     output(q, 7);
    47     return 0;
    48 }

    成功运行的截图

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

    支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。 支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。

    支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。

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

    代码

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 class User {
     5 public:
     6  void setinfo(string n,string p="111111",string e="");
     7  void changePassword();
     8  void printInfo();
     9 private:
    10 string name;
    11 string password;
    12 string email;
    13 };
    14 void User::setinfo(string n,string p,string e)
    15 {
    16  name = n;
    17  password = p;
    18  email = e;
    19 }
    20 void User::changePassword()
    21 {
    22  string oldpassword;
    23  int i=1;
    24  cout<<"Please input your old password:";
    25  cin>>oldpassword;
    26  while(oldpassword!=password&&i<3)
    27  {
    28  cout<<"The password is wrong,please input it again:";
    29  cin>>oldpassword;
    30  i++;
    31  }
    32  if(oldpassword!=password&&i==3)
    33  {
    34  cout<<"please try later"<<endl;
    35  changePassword();
    36  return;
    37  }
    38  if (oldpassword == password)
    39  {
    40 
    41 
    42  cout<<"please input your new password:";
    43  cin>>password;
    44  } 
    45 }
    46 void User::printInfo(){
    47  cout<<"name: "<<name<<endl;
    48  cout<<"password: "<<password<<endl;;
    49  cout<<"email: "<<email<<endl;
    50  cout<<endl;
    51 }
    52 int main() {
    53 cout << "testing 1......" << endl;
    54 User user1;
    55 user1.setinfo("Leonard");
    56 user1.printInfo();
    57 user1.changePassword();
    58 user1.printInfo();
    59 cout << endl << "testing 2......" << endl << endl;
    60 User user2;
    61 user2.setinfo("Jonny","92197","xyz@hotmail.com");
    62 user2.printInfo();
    63 return 0;
    64 }

    运行截图
     

    总结:

    1.关于return使函数退出和exit(0)、exit(1)使程序正常异常退出的方法。

    利用return来使输入三次错误密码后退出函数,再用递归实现re-input(但是想实现一下暂时无法输入的功能的,发现和时间相关的东西不会写Σ(っ°Д°;)っ)

    详见https://www.cnblogs.com/cxchanpin/p/6927025.html

    2.灵活利用函数重载和函数模板可以有效简化代码,提高效率。

    3.快速排序的相关递归调用,有点绕但是需要深入研究。

    评论地址

    https://www.cnblogs.com/wmy0621/p/10590291.html

    https://www.cnblogs.com/zcq1224/p/10589618.html

    https://www.cnblogs.com/charlotte00/p/10585906.html

                                                                                   ----X.Raven

  • 相关阅读:
    Angular9 cdk-virtual-scroll-viewport' is not a known element 报错解决方案
    angular8打包时提示ERROR in Child compilation failed:解决方案
    angular8配置proxy本地跨域代理
    Vue-cli 本地跨域配置方法
    axios中url参数变量配置
    在vue-cli中使用axios时报错TypeError: Cannot set property 'lists' of undefined at eval
    nuxt.js element-ui踩坑记录(已解决)
    nuxt 关闭ESlint 语法检测
    javaScript学习之正则表达式初探
    直接在低版本IE6/7/8浏览器中使用HTML5的audio和video标签播放视频音频的办法
  • 原文地址:https://www.cnblogs.com/laboratory-X/p/10590562.html
Copyright © 2011-2022 走看看