首页 > 其他分享 >第二次实验

第二次实验

时间:2022-10-14 14:57:57浏览次数:44  
标签:const string double Complex 实验 User 第二次 c1

实验任务四

Complex.hpp 源代码

 1 #pragma once
 2 
 3 
 4 #include<iostream>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 class Complex
 9 {
10 public:
11     Complex();
12     Complex(double x);
13     Complex(double x, double y);
14     Complex(Complex& other);
15     friend Complex add(const Complex &c1, const Complex &c2);
16     Complex add(const Complex& other);
17      friend bool is_equal(const Complex &c1,const Complex &c2);
18     friend double abs(const Complex &c1);
19     double get_real() const
20     {
21         return x;
22     }
23     double get_imag() const
24     {
25         return y;
26     }
27     void show() const
28     {
29         if (y > 0)
30         {
31             cout << x << "+" << y << "i";
32         }
33         if (y == 0)
34         {
35             cout << x;
36         }
37         if (y < 0)
38         {
39             cout << x << y << "i";
40         }
41     }
42 private:
43     double x, y;
44 };
45 Complex::Complex()
46 {
47     x = 0;
48     y = 0;
49 }
50 Complex::Complex(double x)
51 {
52     this->x = x;
53     y = 0;
54 }
55 Complex::Complex(double x,double y)
56 {
57     this->x = x;
58     this->y = y;
59 
60 }
61 Complex::Complex(Complex& other)
62 {
63     this->x = other.x;
64     this->y = other.y;
65 }
66 Complex add(const Complex &c1, const Complex &c2) 
67 {
68     Complex c3;
69     c3.x = c1.x + c2.x;
70     c3.y = c2.y + c1.y;
71     return c3;
72 }
73 bool is_equal(const Complex &c1,const Complex &c2)
74 {
75     if (c1.x == c2.x && c2.y == c1.y)
76     {
77         return true;
78     }
79     return false;
80 }
81 double abs(const Complex &c1)
82 {
83     double s;
84     s = sqrt(c1.x * c1.x + c1.y * c1.y);
85     return s;
86 }
87 Complex Complex::add(const Complex& other)
88 {
89     this->x += other.x;
90     this->y += other.y;
91     return *this;
92 }

Complex.cpp源代码

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 // 类测试
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(2, 4);
 9    const   Complex c2(4.5);
10     Complex c3(c1);
11 
12     cout << "c1 = ";
13     c1.show();
14     cout << endl;
15 
16     cout << "c2 = ";
17     c2.show();
18     cout << endl;
19     cout << "c2.imag = " << c2.get_imag() << endl;
20 
21     cout << "c3 = ";
22     c3.show();
23     cout << endl;
24 
25     cout << "abs(c1) = ";
26     cout << abs(c1) << endl;
27 
28     cout << boolalpha;
29     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
30     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
31 
32     Complex c4;
33     c4 = add(c1, c2);
34     cout << "c4 = c1 + c2 = ";
35     c4.show();
36     cout << endl;
37 
38     c1.add(c2);
39     cout << "c1 += c2, " << "c1 = ";
40     c1.show();
41     cout << endl;
42 }
43 
44 int main() {
45     test();
46 }

 

 

 实验任务五

User.hpp源代码

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 
 7 class User
 8 {
 9 public:
10     User(string name);
11     User(string name, string password, string email);
12     void set_email();
13     void change_passwd();
14     void print_info();
15      static int print_n();
16 private:
17     string name, password, email;
18 };
19 User::User(string name)
20 {
21     this->name = name;
22     this->password = "111111";
23     this->email = ' ';
24     
25 }
26 
27 User::User(string name, string password, string email)
28 {
29     this->name = name;
30     this->password = password;
31     this->email = email;
32 }
33 
34 void User::set_email()
35 {
36     cout << "enter email address:";
37     cin >> email;
38     cout << "email is set successfully..." << endl;
39 
40 }
41 
42 void User::change_passwd()
43 {
44     cout << "Enter old password:";
45     string s1;
46     int count = 3;
47     while (count)
48     {
49         cin >> s1;
50         if (s1 != password)
51         {
52             if (count != 1)
53                 cout << "password input error.Please re-enter again:" << endl;
54             else
55                 cout << "password input error.Please try after a while." << endl;
56         }
57         else
58         {
59             cout << "Enter new password:";
60             cin >> password;
61             cout << "new password id set successfully..." << endl;
62             break;
63         }
64         count--;
65     }
66     
67 }
68 void User::print_info()
69 {
70     string s1(password.length(), '*');
71     cout << "name:" << name << endl;
72     cout << "password:" << s1 << endl;
73     cout << "email:" << email << endl;
74 
75 }
76 int User::print_n()
77 {
78     static int x = 1;79     x++;
80     cout << "there are  "<<x <<"  users." << endl;81     return x;
82 }

User.cpp源代码

 1 #include "User.hpp"
 2 #include <iostream>
 3 
 4 // 测试User类
 5 void test() {
 6     using std::cout;
 7     using std::endl;
 8 
 9     cout << "testing 1......\n";
10     User user1("Jonny", "92197", "[email protected]");
11     user1.print_info();
12 
13     cout << endl
14         << "testing 2......\n\n";
15 
16     User user2("Leonard");
17     user2.change_passwd();
18     user2.set_email();
19     user2.print_info();
20 
21     cout << endl;
22     User::print_n();
23 }
24 
25 int main() {
26     test();
27 }

 

 

 

 

标签:const,string,double,Complex,实验,User,第二次,c1
From: https://www.cnblogs.com/pdywsf/p/16791570.html

相关文章

  • 实验1
    实验任务1 实验任务2-1 #include<stdio.h>#include<math.h>intmain(){doublex,ans;while(scanf("%lf",&x)!=EOF){ans=pow(x,365);printf("%.2......
  • 实验2 类和对象(2)
    实验任务1程序源码#include<iostream>#include<complex>intmain(){usingnamespacestd;complex<double>c1{3,4},c2{4.5};constcomplex<doub......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验......
  • 《MiniPRO H750开发指南》第五十二章 FPU测试(Julia分形)实验
    第五十二章FPU测试(Julia分形)实验​本章我们将学习如何开启STM32H750的硬件FPU,并对比使用硬件FPU和不使用硬件FPU的速度差别,以体现硬件FPU的优势。​本章分为如下几个小节......
  • 《MiniPRO H750开发指南》第五十三章 DSP测试实验
    第五十三章DSP测试实验​上一章,我们测试了STM32H750的硬件FPU。STM32H750除了集成硬件FPU外,还支持多种DSP指令集。同时ST还提供了一整套DSP库方便我们工程中开发应用。​本......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......
  • 超级课表实验感想
    1.超级课程表查询:(1)按照图中样式完成web界面设计;    (2)将上课课表信息导入数据库。(可以在数据库管理端手动添加);(3)输入课程名称、任课教师、上课地点任一一项内容,点......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......