c++实验2

函数重载编程练习

 1 #include<iostream>
 2 using namespace std;
 3 struct complex
 4 {
 5     double real;
 6     double imaginary;
 7 };
 8 int add(int ,int );
 9 double add(double ,double );
10 complex add(complex ,complex );
11 int main()
12 {
13         int a=1,b=2;
14         double c=1.32,d=5.98;
15         complex s1,s2;
16         s1.real=1;
17         s1.imaginary=1.3;
18         s2.real=3.32;
19         s2.imaginary=2.31; 
20         cout<<add(a,b)<<endl;
21         cout<<add(c,d)<<endl;
22         cout<<add(s1,s2).real<<"+"<<add(s1,s2).imaginary<<"i"<<endl;
23         return 0;        
24 }
25 int add(int x,int y)
26 {
27     return x+y;
28 }
29 double add(double x,double y)
30 {
31     return x+y;
32 }
33 complex add(complex x,complex y)
34 {
35     complex s3;
36     s3.real=x.real+y.real;
37     s3.imaginary=(x.imaginary+y.imaginary);
38     return s3;
39 }

 

函数模板编程练习

头文件quicksort.h

 1 #ifndef QUICKSORT
 2 #define QUICKSORT
 3 #include<iostream>
 4 #include<iomanip>
 5 using namespace std;
 6 template<class T>
 7 void quicksort(T a[],int left,int right){
 8     if(left<right){
 9         int i,j;
10         T norm;
11         norm=a[left];//作为基准的数
12         i=left;
13         j=right;
14     //找到两个要交换的数 
15         while(i!=j){
16             while(a[j]>=norm&&i<j)
17                 j--;
18             while(a[i]<=norm&&i<j)
19                 i++;
20     //交换这两个数
21             if(i<j) {
22                 swap(a[i],a[j]);
23             }
24         }
25     //把基准数放到中间
26         a[left] =a[i];
27         a[i]=norm;
28     //递归
29         quicksort(a,left,i-1);
30         quicksort(a,i+1,right); 
31     }
32 }
33 template<class T>
34 void output(T a[],int len){
35     int i;
36     for(i=0;i<len;i++)
37         cout<<setw(5)<<a[i]; 
38     
39 }
40 #endif

主函数

#include<iostream>
#include"quicksort.h"
using namespace std;
int main(){
    int a[10]={3,4,2,1,6,10,5,9,7,8};
    quicksort(a,0,9);
    output(a,10);
    cout<<endl;
    double b[10] ={2.1,3.2,4.5,9.8,7.8,6.5,10.1,3.9,1.2,0.3};
    quicksort(b,0,9);
    output(b,10);
    return 0;
}

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

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 // User类的声明
 5 class User {
 6 public:
 7     void setInfo(string Nname, string Npasswd = "111111", string Nemail = "");
 8     // 声明带有默认形参值的成员函数setInfo()
 9     // 密码默认值为6个1
10     // 邮箱默认值为空串
11     void changePasswd();
12     void printInfo();
13 private:
14     string name;
15     string passwd;
16     string email;
17 };
18 // User类的实现
19 // 成员函数setInfo()的实现
20 // 功能:设置用户名(name), 密码(passwd), 联系邮箱(email)
21 void User::setInfo(string Nname, string Npasswd, string Nemail ) {
22     name = Nname;
23     passwd = Npasswd;
24     email = Nemail;
25 }
26 // 成员函数changePasswd()的实现
27 // 功能:修改密码
28 // 要求:在修改密码前,要求先输入旧密码,验证无误后,才允许修改。
29 // 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。
30 void User::changePasswd() {
31     int count=0;
32     string oldpasswd;
33     cout << "please enter your old password" << endl;
34     while (true) {
35         cin >> oldpasswd;
36         if (oldpasswd == passwd) {
37             cout << "please enter new password" << endl;
38             cin >> passwd;
39             break;
40         }
41         else {
42             cout << "sorry,wrong password,pls try again" << endl;
43             count++;
44         }
45         if (count == 3) {
46             cout << "pls try later" << endl;
47             break;
48         }
49     }
50 }
51 // 成员函数printInfo()的实现
52 // 功能:打印用户信息
53 // 要求: 密码以6个*显示
54 void User::printInfo() {
55     cout << name << endl;
56     cout << "******" << endl;
57     cout << email << endl;
58 }
59 
60 int main() {
61     cout << "testing 1......" << endl;    
62     User user1;
63     user1.setInfo("Leonard");
64     user1.printInfo();
65     user1.changePasswd();
66     user1.printInfo();
67     cout << endl << "testing 2......" << endl;
68     User user2;
69     user2.setInfo("Jonny", "92197", "xyz@hotmail.com");
70     user2.printInfo();
71     return 0;
72 }

 

 实验总结体会:
1. 快速排序,上课的时候毫无头绪,课后查阅了一些资料后搞懂了。

2. 快速排序在数据量较多时能有效减少计算机的工作负担。

3. 对于类的定义和使用现在只能刻板的根据书本模仿,还不能随心所欲地应用,还有很大的提升空间。

转载于:https://www.cnblogs.com/Yyaoyyy/p/10594051.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 、结构体与类的编写: (A)利用struct关键字定义一个学生结构体(包括学号、姓名、性别):类名:student, num、name、sex,在主函数定义两个对象stud1,stud2,对stud1对象赋值并输出,对第2个对象stud2赋值输出; (B)利用class关键字将1改成类的编写,其它不变 (C)将输出封装成display,输入封装成setdata函数,分别在类里面定义2函数,在主函数中输入输出; (D)将上面两成员函数移至类外定义并输出 (E)将setdata函数利用对象的引用做函数参数,在主函数中输入输出 2、(1)定义一个时间类,属性包括小时、分、秒,定义两成员函数:settime,showtime,分别以两种方式、类内定义成员函数和内外定义成员函数 (2)对1两成员函数分别利用对象的引用做参数、默认参数做参数进行编写与调用并输出。属性 3、编写一个程序,模拟电梯的功能。功能接口包括电梯上行按钮、下行按钮、楼层选择和电梯在行驶过程中的楼层显示。 要求: (1)、由用户选择按上行按钮还是下行按钮,选择操作后再由用户输入要进入的楼层,进而电梯开始运行,显示所到的每一楼层层数。 (2).如果是上行,则选择输入的楼层号不能比当前楼层号小,否则应给出不合法提示。 (3). 如果是下行,则选择输入的楼层号不能比当前楼层号大,否则应给出不合法提示。 (4).电梯一旦开始运作就会始终运行,直到窗口关闭。 (5).电梯在经过不同楼层时,最好每个楼层的显示之间能有延迟,最终停靠的楼层的输出形式能更加醒目。如果可以,在电梯最初开始运行时,能在电梯由内部显示当前日期(提示:实现这些功能时,需要调用系统api,实现时间显示功能可以使用CDate类)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值