实验二

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

#include<iostream>
using namespace std;

int add(int a,int b)
{
    return a+b;
}
double add(double a,double b)
{
    return a+b;
}
Complex add(Complex a,Complex b)
{
    struct Complex c;
    c.real=a.real+b.real;
    c.imaginary=a.imaginary+b.imaginary;
    return c;
}

struct Complex
{
    double real;
    double imaginary;
}e,f;

int add(int  x, int y);

double add(double x, double y);

Complex add(Complex x,Complex y);

int main()
{
    int a,b;
    double c,d;
    
    cin>>a>>b;
    cout<<add(a,b)<<endl;
    cin>>c>>d;
    cout<<add(c,d)<<endl;
    cin>>e.real>>e.imaginary>>f.real>>f.imaginary; 
    cout<<add(e,f).real<<"+"<<add(e,f).imaginary<<"i"<<endl;
    return 0;
}

 

 

 

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

#include <iostream>
using namespace std;
template<typename T>
int kspx(T s[], int left, int right)
{
 int i = left + 1, j = right;
 T temp = s[left], t;
 while (i <= j)
 {
  while (s[i] < temp)
   i++;
  while (s[j] > temp)
   j--;
  if (i < j)
  {
   t = s[i];
   s[i] = s[j];
   s[j] = t;
  }
  else i++;
 }
 t = s[j];
 s[j] = s[left];
 s[left] = t;
 return j;
}

template<typename T>
void px(T s[], int left, int right)
{
 if (left > right)
  return;
 int j = kspx(s, left, right);
 px(s, left, j - 1);
 px(s, j + 1, right);
}

int main()
{
 int a[5];
 double b[5];
 int i, j;
 cout << "请输入五个整型数字" << endl;
 for (i = 0;i <= 4;i++)
  cin >> a[i];
 cout << "请输入五个double型数字" << endl;
 for (i = 0;i <= 4;i++)
  cin >> b[i];
 cout << endl;
 px(a, 0, 4);
 px(b, 0, 4);
 for (i = 0;i <= 4;i++)
  cout << a[i] << endl;
 for (i = 0;i <= 4;i++)
  cout << endl << b[i];
 return 0;
}

 

 

 

 

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

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 //user类的声明 
 5 class user{
 6     public:
 7         void setinfo(string name ,string password="111111" ,string email="");//带有默认形参值 
 8         void changepassword();
 9         void printfinfo();
10     private:
11         string name;
12         string password;
13         string email;
14 };
15 //user类的实现 
16 void user::setinfo(string name0, string password0, string email0) {
17     name = name0;
18     password = password0;
19     email = email0;
20 }
21 //成员函数changepassword的实现 
22 void user::changepassword()
23 {
24     string password1;
25     int c = 3;
26     cout << "please enter the old password firstly,if it is right ,you can change password then" << endl;
27     cin >> password1;
28     while (password1 != "111111"&&c > 1)
29     {
30         c--;
31         cout << "old password is not corrent,you have " << c << " time(s) to try it again" << endl;
32         cin >> password1;
33     }
34     if (c <= 1)
35         cout << "you have no time to try, please try it next day!" << endl;
36     else
37     {
38         cout << "you changed your password successfully! please remember it" << endl;
39         password = password1;
40     }
41 }
42 //成员函数printinfo的实现 
43 void user::printfinfo()
44 {
45     cout << "name:\t"<< name << endl;
46     cout << "password:\t" << "******" << endl;
47     cout << "email:\t" << email << endl;
48 }
49 //主函数中对user类进行测试 
50 int main()
51 {
52     cout << "...testing 1..." << endl;
53     user user1;
54     user1.setinfo("George");
55     user1.printfinfo();
56     user1.changepassword();
57     user1.printfinfo();
58     cout << endl;
59     cout << "...testing 2..." << endl;
60     user user2;
61     user2.setinfo("Mary", "520520", "xyzabc@qqcom");
62     user2.printfinfo();
63     return 0;
64 }

 

 

posted @ 2019-03-23 15:48  GeorgeWan  阅读(165)  评论(0编辑  收藏  举报