C++(实验二)

实验结论

1.函数重载编程练习:

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

 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct Complex{
 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     int a1,a2,asum;
13     double b1,b2,bsum;
14     Complex c1,c2,csum;
15     cout<<"please enter two integers:";
16     cin>>a1>>a2;
17     asum=add(a1,a2);
18     cout<<a1<<"+"<<a2<<"="<<asum<<endl;
19     cout<<"please enter two real numbers:";
20     cin>>b1>>b2;
21     bsum=add(b1,b2);
22     cout<<b1<<"+"<<b2<<"="<<bsum<<endl;
23     cout<<"please enter two plurals:";
24     cin>>c1.real>>c1.imaginary>>c2.real>>c2.imaginary;
25     cout<<"c1"<<"="<<c1.real<<"+"<<c1.imaginary<<"i"<<endl;
26     cout<<"c2"<<"="<<c2.real<<"+"<<c2.imaginary<<"i"<<endl;
27     csum=add(c1,c2);
28     cout<<"c1+c2"<<"="<<csum.real<<"+"<<csum.imaginary<<"i"<<endl;
29     return 0;
30 }
31 int add(int a1,int a2){
32     int asum;
33     asum=a1+a2;
34     return asum;
35 }
36 double add(double b1,double b2){
37     double bsum;
38     bsum=b1+b2;
39     return bsum;
40 }
41 Complex add(Complex c1,Complex c2){
42     Complex csum;
43     csum.real=c1.real+c2.real;
44     csum.imaginary=c1.imaginary+c2.imaginary;
45     return csum;
46 }

运行截图

 

2.函数模板编程练习

编写实现快速排序函数模板,并在main( )函数中,定义不同类型数据,调用测试。(算法可参考这里, 内有排序示意图及算法逻辑)

参考了网上许多快速排序法的程序,此程序参考(https://www.cnblogs.com/miracleswgm/p/9199124.html)

(1)整数排序

 1 #include <iostream>
 2 #include <iomanip>
 3 using namespace std;
 4 void Quicksort(int num[],int left,int right);
 5 int partition(int num[], int left, int right);
 6 
 7 int main(){
 8     int i;
 9     int num[10]={9,8,7,6,5,4,3,2,1,78};
10     Quicksort(num,0,9);
11     for(i=0;i<=9;i++)
12         cout<<num[i]<<setw(3);
13     cout<<endl;
14     return 0;
15 }
16 
17 int partition(int num[], int left, int right)
18 {
19     int i = left + 1 ;
20     int j = right;
21     int temp = num[left];
22     
23     while(i <= j)
24     {
25         while (num[i] < temp)
26         {
27             i++;
28         }
29         while (num[j] > temp )
30         {
31             j--;
32         }
33         if (i < j)
34             swap(num[i++], num[j--]);
35         else i++;
36     }
37     swap(num[j], num[left]);
38     return j;
39     
40 }
41 
42 void Quicksort(int num[],int left,int right)
43 {
44     int j;
45     if (left>right)
46            return;
47     j = partition(num, left, right);
48     Quicksort(num, left, j - 1);
49     Quicksort(num, j + 1, right);
50 }

 

运行截图

(2)实数排序

 1 #include <iostream>
 2 #include <iomanip>
 3 using namespace std;
 4 void Quicksort(float num[],int left,int right);
 5 float partition(float num[], int left, int right);
 6 
 7 int main(){
 8     int i;
 9     float num[10]={9.1,8.9,7.2,6.4,5.3,4.2,3.1,2.6,1.8,78.5};
10     Quicksort(num,0,9);
11     for(i=0;i<=9;i++)
12         cout<<num[i]<<setw(6);
13     cout<<endl;
14     return 0;
15 }
16 
17 float partition(float num[], int left, int right)
18 {
19     int i = left + 1 ;
20     int j = right;
21     float temp = num[left];
22     
23     while(i <= j)
24     {
25         while (num[i] < temp)
26         {
27             i++;
28         }
29         while (num[j] > temp )
30         {
31             j--;
32         }
33         if (i < j)
34             swap(num[i++], num[j--]);
35         else i++;
36     }
37     swap(num[j], num[left]);
38     return j;
39     
40 }
41 
42 void Quicksort(float num[],int left,int right)
43 {
44     float j;
45     if (left>right)
46         return;
47     j = partition(num, left, right);
48     Quicksort(num, left, j - 1);
49     Quicksort(num, j + 1, right);
50 }

运行截图

 

 

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

设计并实现一个用户类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 {
 7   public:
 8         void setInfo(string name0,string passwd0="111111",string email0="");
 9         string getname();
10         string getpasswd();
11         string getemail();
12         void changePasswd();
13         void printInfo();
14   private:
15         string name;
16         string passwd;
17         string email;
18 };
19 void User::setInfo(string name0,string passwd0,string email0){
20     name = name0;
21     passwd = passwd0;
22     email = email0;
23 }
24 string User::getname(){
25     return name;
26 }
27 string User::getpasswd(){
28     return passwd;
29 }
30 string User::getemail(){
31     return email;
32 }
33 void User::changePasswd(){
34     int i=3;
35     string oldpasswd,newpasswd;
36     cout<<"please enter the old passwd:";
37     cin>>oldpasswd;
38     while(i){
39         if(passwd == oldpasswd)
40         {   cout<<"please enter a new pass word:";
41             cin>>newpasswd;
42             passwd=newpasswd;
43             cout<<"change pass word successfully"<<endl;
44             break;}
45         if(i>1){
46                  cout<<"passwdb input error,please re-enter again:";
47                  cin>>oldpasswd;
48                 }
49         else{
50                  cout<<"you are wrong,please try after a while";
51                  cout<<endl;
52                }
53             i--;
54           }
55        }
56 
57 void User::printInfo(){
58     cout << "name:  " << name << endl;
59     cout << "passwd:  " << "******" << endl;
60     cout << "email:  " << email<<endl;
61 }
62 
63 
64 int main() {
65     cout << "testing 1......" << endl;
66     User user1;
67     user1.setInfo("Leonard");
68     user1.printInfo();
69     user1.changePasswd();
70     user1.printInfo();
71     cout << endl << "testing 2......" << endl << endl;
72     User user2;
73     user2.setInfo("Jonny","92197","xyz@hotmail.com");
74     user2.printInfo();
75     return 0; }

 

 

 

 

运行截图

 

实验总结与体会

1.结构体相关的知识还需强化

2.类相关的知识还是很薄弱的

 

实验一评论链接:

https://www.cnblogs.com/joey-yan/p/10543990.html#4213143

https://www.cnblogs.com/lovecpp/p/10520456.html#4211900

https://www.cnblogs.com/qiuqiuwr/p/10525885.html#4211895 

 

转载于:https://www.cnblogs.com/dadadacy/p/10597778.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、付费专栏及课程。

余额充值