首页 > 其他分享 >实验二

实验二

时间:2022-10-12 18:44:29浏览次数:34  
标签:string double void Complex 实验 User imag

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

    cout << "c1 = ";
    c1.show();
    cout << endl;

    cout << "c2 = ";
    c2.show();
    cout << endl;
    cout << "c2.imag = " << c2.get_imag() << endl;

    cout << "c3 = ";
    c3.show();
    cout << endl;

    cout << "abs(c1) = ";
    cout << abs(c1) << endl;

    cout << boolalpha;
    cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;

    Complex c4;
    c4 = add(c1, c2);
    cout << "c4 = c1 + c2 = ";
    c4.show();
    cout << endl;

    c1.add(c2);
    cout << "c1 += c2, " << "c1 = ";
    c1.show();
    cout << endl;
}

int main() 
{
    test();
}

 



#pragma once
#include<iostream>
#include<string>
using namespace std;
class User
{
public:
    User(string _name);
    User(string _name, string _password, string _email);
    void set_email();
    void change_password();
    void print_info();
    static void print_count();
private:
    string name;
    string password;
    string email;
    static int count;
};
int User::count = 0;
User::User(string _name) :name{ _name }, password{ "111111" }, email{ "" }
{
    count++;
}
User::User(string _name, string _password, string _email) :name{ _name }, password{ _password }, email{ _email }
{
    count++;
}
void User::set_email()
{
    cout << "请设置邮箱地址:";
    string s;
    cin >> s;
    email = s;
    cout << "邮箱设置成功!" << endl;
}
void User::change_password()
{
    string s1, s2;
    int k = 3;
    cout << "请输入原密码:";
    cin >> s1;
    while (k!=1)
    {
        if (s1 == password)
        {
            cout << "请输入新密码:";
            cin >> s2;
            password = s2;
            cout << "密码设置成功!" << endl;
            break;
        }
        else
        {
            cout << "密码错误,请重新输入:";
            cin >> s1;
            k--;
        }
    }
    if (k == 1)
        cout << "密码错误,请稍后再试!" << endl;
    
}
void User::print_info()
{
    int n;
    n = password.length();
    cout << "name:" << name << endl;
    cout << "password:";
    for (int i = 1; i <= n; i++)
    {
        cout << "*";
    }
    cout << endl;
    cout << "email:" << email << endl;
}
void User::print_count()
{
    cout << "总共有" << User::count << "个用户。" << endl;
}
#include "user.h"
#include <iostream>
// 测试User类
void test() 
{
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();
    cout << endl<< "testing 2......\n\n";

    User user2("Leonard");
    user2.change_password();
    cout << endl;
    user2.set_email();
    cout << endl;
    user2.print_info();
    cout << endl;
    User::print_count();
}
int main() 
{
    test();
}

 

 

 

 



标签:string,double,void,Complex,实验,User,imag
From: https://www.cnblogs.com/220011wan/p/16785577.html

相关文章

  • 实验二 类和对象
    #include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublea=0,doubleb=0):real{a},imag{b}{};Complex(......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验二 类与对象
    实验4不使用C++标准库,自行设计并实现一个简化版复数类Complex.1#pragmaonce2#include<bits/stdc++.h>3usingnamespacestd;4classComplex5{6public......
  • 实验5:开源控制器实践——POX
    一、基础要求1、使用tcpdump验证Hub模块h1pingh22、使用tcpdump验证Switch模块L2_learning模块代码流程图h1pingh2h1pingh3二、进阶要求1、重新搭建......
  • 实验2 类与对象(2)
    实验结论:实验任务四:Complex.hpp:#pragmaonce#include<iostream>#include<math.h>usingnamespacestd;classComplex{public:Complex(doubler=0,do......
  • 实验5:开源控制器实践——POX
    一、基本要求1.构建拓扑2.使用tcpdump验证Hub1)h1pingh22)h1pingh33.tcpdump验证Switch模块1)h1pingh22)h1pingh34.L2_learning模块代码流程图二、......
  • 实验5:开源控制器实践——POX
    (一)基本要求1.阅读Hub模块代码,使用tcpdump验证Hub模块h1pingh2的tcpdump抓包结果h1pingh3的tcpdump抓包结果2.阅读L2_learning模块代码,画出程序流程图,使用tcpd......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 实验五:开源控制器实践——POX
    基本要求1.tcpdump验证Hub模块h1pingh2的tcpdump抓包结果截图h1pingh3的tcpdump抓包结果截图2.tcpdump验证Switch模块h1pingh2的tcpdump抓包结果截图h1p......
  • 实验5:开源控制器实践—— POX
    实验5:开源控制器实践——POX一、实验目的1.能够理解POX控制器的工作原理;2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方......