首页 > 其他分享 >实验二

实验二

时间:2022-10-17 17:00:19浏览次数:31  
标签:const cout Complex 实验 User c2 c1

实验四

 

 

#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using std::cout;
using std::endl;
class Complex
{
public:
Complex();
Complex(double a0,double b0);
Complex(double a0);
Complex(Complex& other);
double get_real() const {return a;};
double get_imag() const;
void show() const;
Complex add(const Complex &c2);
friend Complex add(const Complex &c1,const Complex &c2);
friend bool is_equal(const Complex &c1,const Complex &c2);
friend double abs(const Complex &c);
private:
double a,b;
};
Complex::Complex(double a0,double b0)
{
a = a0;b = b0;
}
Complex::Complex()
{
a = 0;b = 0;
}
Complex::Complex(double a0)
{
a = a0;b = 0;
}
double Complex::get_imag()const
{
return b;
}
void Complex::show() const
{
if(b) cout << a << (b > 0 ? "+" : "-") << (b > 0 ? b :(-b) ) << "i" << endl;
else cout << a << endl;
}
Complex::Complex(Complex &other)
{
a = other.a;
b = other.b;
}
Complex Complex::add(const Complex &c2)
{
Complex c;
c.a += a + c2.a;
c.b += b + c2.b;
return c;

}
Complex add(const Complex &c1,const Complex &c2)
{
Complex c3;
c3.a = c1.a + c2.a;
c3.b = c1.b + c2.b;
return c3;
}
bool is_equal(const Complex &c1,const Complex &c2)
{
return (c1.a == c2.a) && (c1.b == c2.b);
}
double abs(const Complex &c)
{
return sqrt(c.a * c.a + c.b * c.b);
}

#include "Complex.hpp"
#include <iostream>
// 类测试
void test() {
using namespace std;
Complex c1(3, -4);
const Complex c2(4.5);
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();
}

 

 

 

 

 

 #include "User.hpp"

#include <iostream>
// 测试User类
void test() {
using std::cout;
using std::endl;
cout << "testing 1......\n";
User user1("Jonny", "92197", "[email protected]");
user1.printf_info();
cout << endl
<< "testing 2......\n\n";
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.printf_info();
cout << endl;
User::print_n();
}
int main() {
test();
}

 

 #pragma once

#include<iostream>
#include<string>
using namespace std;
class User
{
public:
User(string name0);
User(string name0,string password0,string email0);
void set_email();
void change_passwd();
void printf_info();
static int print_n();
private:
string name,password,email;
};
User::User(string name0)
{
name = name0;
password = "111111";
email = ' ';
}
User::User(string name0,string password0,string email0)
{
name = name0;
password = password0;
email = email0;
}
void User::set_email()
{
cout << "please enter your email address:" << endl;
cin >> email;
cout << "email is set sucessfully..." << endl;
}
void User::change_passwd()
{
cout << "Please Enter your old passord:";
int cnt = 3;
while(cnt --)
{
string s0;
cin >> s0;
if(s0 != password)
{
cout << (cnt == 1 ? "password input error.Please try again later" : "password input error.Please re-enter again!" ) << endl;
}
else
{
cout << "Enter new password:";
cin >> password;
cout << "new password is set successfully..." << endl;
break;
}}}
void User::printf_info()
{
string s1(password.length(),'*');
cout << "name:" << name << endl;
cout <<"password:" << s1 << endl;
cout << "Email:" << email << endl;
}

int User::print_n()
{
static int x = 1;
x ++;
cout << "there are " << x << " users." << endl;
return x;
}



 

标签:const,cout,Complex,实验,User,c2,c1
From: https://www.cnblogs.com/lxyyyds/p/16799798.html

相关文章

  • 完成基础实验【硬件课程设计】
    完成基础实验【硬件课程设计】​​前言​​​​推荐​​​​完成基础实验​​​​最后​​前言第一周周二:学生小组完成基础实验2022/8/30仅供学习交流使用​请您阅读文章......
  • 学生小组设计自选实验【硬件课设】
    学生分组和选题【硬件课程设计】​​前言​​​​推荐​​​​学生小组设计自选实验​​​​1方案设计​​​​2系统原理图设计​​​​3系统仿真与仿真结果​​​​4PCB......
  • 实验2 类和对象(2)
    实验4complex.hpp#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublex0,doubley);Complex(c......
  • 实验二 类和对象
    #pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classcomplex{public:complex(doubler=0.0,doublei=0.0):real(r),imag(i){}......
  • 实验5:开源控制器实践——POX
    一.基础要求只需要提交h1pingh2、h2和h3的tcpdump抓包结果截图,外加L2_learning模块代码流程图,其余文字请勿赘述;1.使用命令创建拓扑:sudomn--topo=single,3--mac--co......
  • 软件设计实验12
    实验12:外观模式[实验任务一]:计算机开启在计算机主机(Mainframe)中,只需要按下主机的开机按钮(on()),即可调用其他硬件设备和软件的启动方法,如内存(Memory)的自检(check())......
  • 实验室深度学习服务器崩溃——Oops: 0000 [#1] SMP NOPTI
    这两天实验室的服务器总是崩溃,重启已经不能解决问题了,由于是跑深度学习的服务器,而且还是承接国家级项目的运行服务器,可以说是实验室的主要生产力了,给出报错的日志:  Oc......
  • 实验二 类与对象(2)
    任务4:Complex.hpp1#pragmaonce2#include<bits/stdc++.h>3usingnamespacestd;4classComplex{5public:6Complex(doubler=0,doubl......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......