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

一、实验内容

1、函数重载编程练习

编写重载函数add(),实现对int型,double型,complex型数据的加法。

 

 1 #include<iostream>
 2 using namespace std;
 3 struct Complex{
 4     double real;
 5     double imaginary;
 6 };
 7 
 8 int add(int x,int y){
 9     return x+y;
10 }
11 double add(double x,double y){
12     return x+y;
13 }
14 Complex add(Complex x,Complex y){
15     Complex z;
16     z.imaginary=x.imaginary+y.imaginary;
17     z.real=x.real+y.real; 
18     return z;
19 }
20 
21 int main(){
22     int a,b;
23     cout<<"Enter two integer:";
24     cin>>a>>b;
25     cout<<"The answer is:"<<add(a,b)<<endl;
26     
27     double m,n;
28     cout<<"Enter two real number:";
29     cin>>m>>n;
30     cout<<"The answer is:"<<add(m,n)<<endl;
31     
32     struct Complex p,q;
33     cout<<"Enter two struct:";
34     cin>>p.real>>p.imaginary>>q.real>>q.imaginary;
35     cout<<"The answer is:"<<add(p,q).real<<"+"<<add(p,q).imaginary<<"i"<<endl;
36     
37     return 0;
38 }

 

结果如下:

 

2、函数模板编程练习

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

 1 //quicksort.h
 2 # ifndef QS_H 
 3 # define QS_H
 4 
 5 template<class T>
 6 void QuickSort(T a[], int low, int high) {
 7     int i = low, j = high - 1, p;
 8     T k = a[i];
 9     if (i < j) {
10         while (i < j) {
11             while ((a[j] >= k) && i < j) j--;
12             a[i] = a[j];
13             while ((a[i] <= k) && i < j) i++;
14             a[j] = a[i];
15         }
16         a[i] = k;
17     }
18     else  return;
19     QuickSort(a, low, i);
20     QuickSort(a, i + 1, high);
21 } 
22 # endif
 1 //output.h
 2 # ifndef OP_H
 3 # define OP_H
 4 # include<iostream>
 5 using namespace std;
 6 
 7 template<class T>
 8 void output(T a[], int n) {
 9     for (int i = 0;i < n;i++)
10         cout << a[i] << " ";
11     cout << endl;
12 }
13 
14 # endif
 1 //MAIN.cpp
 2 #include<iostream>
 3 #include<string>
 4 #include"output.h"
 5 #include"quicksort.h"
 6 #define M 8
 7 #define N 10
 8 using namespace std;
 9 
10 int main() {
11     int a[N] = { 8,4,2,34,76,36,13,89,32,64 };
12     double b[M] = { 7.1,3.4,2.3,7.8,3.2,3.4,8.7,5.6 };
13     cout << "the array is:" << endl;
14     output(a, N);
15     output(b, M);
16 
17     cout << "after sorting:" << endl;
18     int m = 0;
19     QuickSort(a, m, N);
20     QuickSort(b, m, M);
21     output(a, N);
22     output(b, M);
23  
24     system("pause");
25     return 0;
26 }

效果如下:

 

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

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 //User类声明
 6 class User {
 7 public:
 8     void setInfo(string name0,string passwd0 = "111111", string email0 = " ");//支持设置用户信息
 9     void changePasswd();                                       //支持修改密码
10     void printInfo();                                          //支持打印用户信息
11     void changemail();                                         //支持修改邮箱
12 private:
13     string name;
14     string passwd;
15     string email;
16 };
17 void User::setInfo(string name0, string passwd0, string email0) {
18     name = name0;
19     passwd = passwd0;
20     email = email0;
21 }
22 
23 void User::changePasswd() {
24     string passwd1, passwdN;
25     int n=1;
26     cout << "Enter the old passwd:";
27     cin >> passwd1;
28     while (n <= 3) {
29         if (User::passwd == passwd1) {
30             cout << "Enter the new passwd:";
31             cin >> passwdN;
32             break;
33         }
34         else if(n<3&&(User::passwd != passwd1)) {
35             cout << "passwd input error,Please re-Enter again:";
36             cin >> passwd1;
37             n++;
38         }
39         else if (n == 3 && (User::passwd != passwd1)) {
40             cout << "Please try after a while." << endl;
41             break;
42         }
43     }
44 }
45 
46 void User::printInfo() {
47     cout << "name:\t\t" << name << endl;
48     cout << "passwd:\t\t" <<"******"<< endl;
49     cout << "email:\t\t" << email << endl;
50 }
51 
52 void User::changemail() {
53     string emailN;
54     char a = '@';
55     string::size_type idx;        //string类提供字符串中查找另一字符串的函数find
56     cout << "Enter the new email:";
57     cin >> emailN;
58     idx = emailN.find(a);         //在emailN中查找a
59     while (idx == string::npos) { //不存在
60         cout << "Add @ to your email:";
61         cin >> emailN;
62         idx = emailN.find(a);
63     }
64 
65 }
66 
67 int main() {
68     cout << "testing 1......" << endl << endl;
69 
70     User user1;
71     user1.setInfo("Leonard");
72     user1.printInfo();
73     user1.changePasswd();
74     user1.printInfo();
75 
76     cout << endl << "testing 2......" << endl << endl;
77     User user2;
78     user2.setInfo("Jonny", "92197", "xyz@hotmail.com");
79     user2.printInfo();
80     user2.changemail();
81 
82     system("pause");
83     return 0;
84 }

结果如下:

 

二、实验反思

1、快速排序实验写了很久,也拿不同数据测了多次,有时一串数可以换些有特点的数就不行了。上网查了下,语句位置不一样,条件内容不一样,就会出现问题。写代码还是需要严谨,不可以拿自己思维与计算机比较。

2、类的实验看似复杂冗长,其实静下心来就可以顺利写下来,一直困在实验二又不敢着手于实验三。后来学到c++中的查找字符串还是比较兴奋的。

3、花了很多时间在这次实验上,不仅仅因为快速排序,更多是小细节上出错,以后会多多练习。

 

三、实验评论

https://www.cnblogs.com/Kun-520/p/10589170.html#4212874

https://www.cnblogs.com/xtn-0326/p/10587619.html#4212940

https://www.cnblogs.com/jessi-wu1005/p/10588322.html#4212849

转载于:https://www.cnblogs.com/zuiyankh/p/10587674.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值