首页 > 其他分享 >实验二

实验二

时间:2022-10-18 16:46:06浏览次数:40  
标签:const cout double Complex 实验 User include

实验任务4

.hpp

#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);
}

.cpp

#include "Complex.hpp"
#include <iostream>

void test() {
 using namespace std;
 Complex c1(4, -7);
 const Complex c2(4.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();
}

实验任务5

.hpp

#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 << "Enter email address:" ;
 cin >> email;
 cout << "email is set sucessfully..." << endl;
}
void User::change_passwd()
{
cout << "Enter old passord:";
int cnt = 3;
while(cnt --)
{
string s0;
cin >> s0;
if(s0 != password)
{
    if (cnt != 0)
    cout <<  "password input error.Please re-enter again:" ;
    if (cnt ==0)
    cout << "password input error.Please try after a while." << 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;
}

.cpp

#include "User.hpp"
#include <iostream>

void test() {
 using std::cout;
 using std::endl;
 cout << "testing 1......\n";
 User user1("Hermione", "654123", "[email protected]");
 user1.printf_info();
 cout << endl
 << "testing 2......\n\n";
 User user2("Granger");
 user2.change_passwd();
 user2.set_email();
 user2.printf_info();
 cout << endl;
 User::print_n();
}
int main() {
 test();
}

 

 

标签:const,cout,double,Complex,实验,User,include
From: https://www.cnblogs.com/Gjr202183290137/p/16803087.html

相关文章

  • 实验二 类和对象(2)
    一.实验任务41.源代码:Complex.hpp:#pragmaonce#include<cmath>#include<iostream>usingnamespacestd;classComplex{public:Complex(doublem=0,double......
  • 实验二 类和对象
    实验四.cpp#include"Complex.hpp"#include<iostream>voidtest(){usingnamespacestd;Complexc1(7,-8);constComplexc2(7.8);Complexc......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验6: 开源控制器实践——RYU
    开源控制器实践——RYU一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境U......
  • 实验一
    #include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;} #include<stdio.h>intmain(){printf("......
  • 《MiniPRO H750开发指南》第五十六章 串口IAP实验
    第五十六章串口IAP实验​IAP,即在应用编程,通俗地说法就是“程序升级”。产品阶段设计完成后,在脱离实验室的调试环境下,如果想对产品做功能升级或BUG修复会十分麻烦,如果硬件支......
  • 《MiniPRO H750开发指南》第五十七章 USB读卡器(Slave)实验
    第五十七章USB读卡器(Slave)实验​STM32H750系列芯片都自带了USBOTGFS和USBOTGHS(HS需要外扩高速PHY芯片实现,速度可达480Mbps),支持USBHost和USBDevice,MiniPROSTM32H75......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU(一)基本要求搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑阅读Ryu文档的TheFirstApplica......
  • 实验二
    task4:Complex.hpp#include<iostream>#include<math.h>usingnamespacestd;classComplex{public:Complex(doublec31,doublec32);Complex(constC......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;够运......