第二次c++实验

1.函数重载编程练习编写重载函数add(),实现对int型,double型,Complex型数据的加法:

程序:

 1 #include<iostream>
 2 using namespace std;
 3 struct complex{
 4     double real;
 5     double imaginary;
 6 }; 
 7 int add(int,int);
 8 double add(double, double);
 9 complex add(complex, complex);
10 int main(){
11     int x1;
12     double x2;
13     complex x3;
14     int a,b;
15     cin>>a>>b;
16     x1=add(a,b);
17     cout<<"整数之和为:"<<x1<<endl;
18     double c,d;
19     cin>>c>>d;
20     x2=add(c,d);
21     cout<<"double类型之和为:"<<x2<<endl;//不知为何,输入所有的数,在浮点数的结果上都变成了整数
22     complex e,f;
23     cin>>e.real>>e.imaginary>>f.real>>f.imaginary;
24     x3=add(e,f);
25     cout<<"复数之和为:"<<x3.real<<"+"<<x3.imaginary<<"i"<<endl; 
26     return 0;
27 }
28 int add(int a,int b)
29 {
30     return (a+b);
31 }
32 double add(double a,double b)
33 {
34     return (a+b);
35 }
36 complex add(complex a,complex b)
37 {
38     complex c;
39     c.real=a.real+b.real;
40     c.imaginary=a.imaginary+b.imaginary;
41     return c;
42 }

图片:

2.实验:

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

代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 6
 4 void swap(int*a,int*b){
 5     int temp;
 6     temp=*a;
 7     *a=*b;
 8     *b=temp;
 9 }
10 void quicksort(int array[],int low,int high){
11     int i,j;
12         if(low<high){
13     i=low+1;
14     j=high;
15     while(i<j)
16     {
17         if(array[low]<array[i])
18         {
19             swap(&array[i],&array[j]);j--;
20         }
21         else i++;
22         
23     }
24     if(array[i]>=array[low])//这个地方切不可以忘记等于号,一开始多次运行出现错误结果,都是因为少了等号。
25     {
26         i--;
27     }
28     swap(&array[i],&array[low]);
29 quicksort(array,low,i);
30 quicksort(array,j,high);
31 }
32 }
33 int main(){
34     int array[N]={4,2,4,7,6,8};
35     int i=0;
36     quicksort(array,0,N-1);
37     for(int i=0;i<N;i++)
38     {
39         printf("%d ",array[i]);
40     }
41     return 0;
42 }

图片:

3.实验:

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

代码:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 class User {
 6 public:
 7 void setInfo(string name0,string password0="111111",string email0="");
 8 void changePasswd();
 9 void printInfo();
10 void findword(); 
11 private:
12 string name;
13 string password;
14 string email;
15 };
16 void User::setInfo(string name0,string password0,string email0){
17     name=name0;
18     password=password0;
19     email=email0;
20     
21 }
22 void User::printInfo(){
23     cout<<"name: "<<name<<endl;
24     cout<<"password:****** "<<endl;
25     cout<<"email: "<<email<<endl;
26 }
27 void User::findword(){
28     string a="@";
29     string::size_type idx;
30     idx=email.find(a);
31     while(idx==string::npos){
32     cout<<"email address is wrong,please enter the right one:"<<endl;
33     cin>>email;
34     findword();
35 }
36 }
37 void User::changePasswd()
38 {
39     string word;
40     string newword;
41     cout<<"Enter the old password:";
42     int j=0;
43     for(int i=0;i<3;i++)
44     {
45     cin>>word;
46     
47     if(word==password)
48     {
49         cout<<"please enter the new password:";
50         cin>>newword;
51         int n=newword.size();
52         while(n!=6)
53         {
54            cout<<"the length of your newword is wrong,please enter again:"<<endl;
55            cin>>newword;
56            n=newword.size();           
57         }
58         password=newword;
59         break;
60     }
61     else{
62     j=j+1;
63     if(j<3)
64     cout<<"password input error,please re-enter again:"<<endl;
65     if(j==3)
66     cout<<"password is missing."<<endl;
67 }
68 }
69 }
70     
71 int main() {
72 cout << "testing 1......" << endl;
73 User user1;
74 user1.setInfo("Leonard");
75 user1.printInfo();
76 user1.changePasswd();
77 user1.printInfo();
78 cout << endl << "testing 2......" << endl << endl;
79 User user2;
80 user2.setInfo("Ben","12306","[email protected]");
81 user2.findword();
82 user2.printInfo();
83 return 0;
84 }

图片:

实验总结:

1. 算法的搞懂一定是第一位,写代码之前应首先搞懂算法,正所谓基础不牢地动山摇,算法只要有一小处不懂,编码便会难以实现。

2.在函数声明时,默认函数的值一定要从后往前写,一开始做的时候老是通过不了皆是这个原因。

3.类极大的将各种功能整合在一起,方便而又高效,更是我了解到c++较之c的优越性。

猜你喜欢

转载自www.cnblogs.com/lszz/p/10563906.html