实验2:函数重载、函数模板、简单类的定义和实现

函数重载编程练习

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

#include <iostream>
using namespace std;
struct complex{
     double real;
     double imaginary;
};
int add(int x2,int y2 );
double add(double x1,double y1 );
complex add(complex x,complex y);
int main()
{ int a=4,b=3,s1;
  double m=2.1,n=3.5,s2;
  complex complex1,complex2,complex3;
  complex1.real=2,complex1.imaginary=3;
  complex2.real=6,complex2.imaginary=4;
  s1=add(a,b);
  s2=add(m,n);
  complex3=add(complex1,complex2);
  cout<<s1<<endl;
  cout<<s2<<endl;
  cout<<complex3.real <<"+"<<complex3.imaginary <<"i"<<endl;
  return 0;
}
int add(int x2,int y2)
 {return x2+y2;} 
double add(double x1,double y1)
 {return x1+y1;} 
complex add(complex x,complex y)
{ complex z;
  z.imaginary =x.imaginary +y.imaginary ;
  z.real =x.real +y.real ;
  return z; 
}

函数模板编程练习

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

#include <iostream>
#include <iomanip>
#include "QS.h"
using namespace std;
int main ()
{int i;
 int a[5]={12,4,34,1,5 };
 double b[5]={14.2,9.5,55.8,25.7,3.1};
 QS(a,0,5);
 QS(b,0,5);
 for (i=0;i<=4;i++)
   cout<<setw(5)<<a[i];
   cout<<endl;
 for (i=0;i<=4;i++)
   cout<<setw(6)<<b[i];
   cout<<endl;
 return 0;
}
#ifndef Quicksort_H
#define Quicksort_H
template <class T>
void QS(T s[], int low, int high) {
    int a, b, c = 0;
    T f, ex;
    a = low; b = high - 1; f = s[(low+high)/2];
    if (a < b) {
    while (a < b) {
        while (a < b&&f < s[b])
            b--;
        while (a < b&&f > s[a])
            a++;
        if (a >= b) c = b;
        else { ex = s[a]; s[a] = s[b]; s[b] = ex; }
    }
        QS(s, low, c);
        QS(s, c + 1, high);
    }
}
#endif
//此程序源自我优秀的同学,非我原创!

快速排序实在是不会,挣扎了好久还是做不出来,就借来了同学的程序参考(已注明),我这菜鸟花了好多时间才写出了快速排序的第一遍排序(下方),第一遍排序的结果是对的,但不知为什么low和high的值出现了问题。难道不是当low和high相同时就不运行了吗?可为什么又做了一次运算呢?请大佬们指点。

#include <iostream> 
#include <iomanip>
using namespace std;
int main()
{int low,high,mid,m,n,f=1,t,i;
 int a[5]={12,4,34,1,5};
 low=0;high=4;
 t=a[low];
 while(low<high)
  {if(f==0) 
      n=a[low];
   else
      m=a[high];
   while(f==1)
    { if(t<m)
        {--high;
         f=1;cout<<"1low: "<<low<<endl;cout<<"1high: "<<high<<endl;
         break;
        }
      else
        {a[low]=a[high];
         ++low;cout<<"2low: "<<low<<endl;cout<<"2high: "<<high<<endl;
         f=0;
         break;
        }
    }
   while(f==0)
    { if(t<n) 
        {a[high]=a[low];
         --high;
         f=1;cout<<"3low: "<<low<<endl;cout<<"3high: "<<high<<endl;
         break;
        }
      else
        {++low;
         f=0;cout<<"4low: "<<low<<endl;cout<<"4high: "<<high<<endl;
         break;
        }
     
    }

  } 
   a[low]=t;
   mid=low;
   for(i=0;i<=4;i++)
   cout<<a[i]<<endl;
}

 

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

设计并实现一个用户类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 name0,string password0="111111",string email0="");
      void changePasswd();
      void printInfo();
    private:
      string name;
      string password;
      string email;        
}; 
void User::setInfo(string name0,string password0,string email0):name(name0),password(password0),mail(email0){
}
void User::changePasswd(){
    string oldpassword;
    int i=1;
    cout<<"please input your old password:";cin>>oldpassword;
    while(oldpassword!=password&&i<3)
    {cout<<"wrong,please input it again:";cin>>oldpassword;
     i++;
    }
    if(oldpassword!=password&&i==3)
     cout<<"please try later"<<endl;
    if(oldpassword==password)
     {cout<<"please input your new password:";cin>>password;}    
}
void User::printInfo(){
    cout<<"name:  "<<name<<endl;
    cout<<"password:****** "<<endl;;
    cout<<"email:  "<<email<<endl;
    cout<<endl;
}
int main()
{ cout<<"testing 1......"<<endl;
  User user1;
  user1.setInfo("Leonard");
  user1.printInfo();
  user1.changePasswd();
  user1.printInfo();
  cout<<"testing2......"<<endl;
  User user2;
  user2.setInfo("Jonny","92197","xyz@hotmail.com");
  user2.printInfo(); 
  return 0;  
}

 

实验总结与体会

  • 个人认为三道编程中快速排序最难,思考并验证了很久还是不会,过程很痛苦,挫败感十足。想着可以借鉴同学们程序,结果还是看不懂,自己摸索来摸索去也就会第一遍排序,过程还是很繁琐,加上low和high的值似乎出现了问题,觉得自己与其他的优秀的同学相差的太多,还是要勤加锻炼,不断探索。
  • 在重载编程练习和类的定义使用编程上还是要深入思考,在编程的过程中花了大量的时间来理解和记忆,很明显这是我对于已学知识不熟悉的表现。学习的积累在于平时,我还是要压缩时间,把精力放在更多有意义的事情上。
  • 以上的程序必有改进之处,请大佬们不吝赐教。

互评链接

https://www.cnblogs.com/csc13813017371/p/10584971.html

https://www.cnblogs.com/libing-072921/p/10587072.html

https://www.cnblogs.com/nnn13579/p/10561474.html

 

转载于:https://www.cnblogs.com/Ann-88/p/10587891.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值