首页 > 其他分享 >实验二

实验二

时间:2022-10-17 22:44:47浏览次数:37  
标签:const cout int void 实验 Employee string

task1:

#include <iostream>
#include <complex>

int main(){
using namespace std;

complex<double> c1={3, 4}, c2={4.5};
const complex<double> c3{c2};

cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "c3 = " << c3 << endl;
cout << "c3.real = " << c3.real() << endl;
cout << "c3.imag = " << c3.imag() << endl;

cout << "c1 + c2 = " << c1 + c2 << endl;
cout << "c1 - c2 = " << c1 - c2 << endl;
cout << "abs(c1) = " << abs(c1) << endl;

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

complex<double> c4 = 2;
cout << "c4 = " << c4 << endl;
c4 += c1;
cout << "c4 = " << c4 << endl;

}

 

task2:

#include <iostream>
#include <array>
#include <string>
using namespace std;
template<typename T>
void output1(const T &obj) {
for(auto i: obj)
std::cout << i << ", ";
std::cout << "\b\b \n";
}
template<typename T>
void output2(const T &obj) {
for(auto p = obj.cbegin(); p != obj.cend(); ++p)
std::cout << *p << ", ";
std::cout << "\b\b \n";
}

int main() {
array<int, 5> x1;
cout << "x1.size() = " << x1.size() << endl;
x1.fill(42);
x1.at(0) = 999;
x1.at(4) = -999;
x1[1] = 777;
cout << "x1: ";
output1(x1);
cout << "x1: ";
output2(x1);


array<double, 10> x2{1.5, 2.2};
cout << "x2.size() = " << x2.size() << endl;
cout << "x2: ";
output1(x2);

array<int, 5> x3{x1};
cout << boolalpha << (x1 == x3) << endl;
x3.fill(22);
cout << "x3: ";
output1(x3);

swap(x1, x3);
cout << "x1: ";
output1(x1);
cout << "x3: ";
output1(x3);
return 0;
}

 

 task3:

#pragma once
#include <iostream>
#include <string>
#include <iomanip>
using std::string;
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
using std::left;
using std::right;
using std::to_string;

struct Date {
int year;
int month;
int day;
};
class Employee
{
public:
Employee();
Employee(string name0, double salary0, int y, int m, int d = 1);
void set_info(string name0, double salary0, int y, int m, int d = 1);
string get_name() const;
double get_salary() const;
void display_info() const;
void update_salary(double s);
void update_hire_date(int y, int m, int d);
void raise_salary(double by_percent);

public:
static void display_count();

private:
string id;
string name;
double salary;
Date hire_date;

public:
static const string doc;

private:
static int count;

};

const string Employee::doc {"a simple Employee class"};
int Employee::count = 0;

Employee::Employee(): id{ to_string(count+1) } {
++count;
}

Employee::Employee(string name0, double salary0, int y, int m, int d):
id{to_string(count+1)}, name{name0}, salary{salary0}, hire_date{y, m, d} {
++count;
}

void Employee::set_info(string name0, double salary0, int y, int m, int d) {
name = name0;
salary = salary0;
hire_date.year = y;
hire_date.month = m;
hire_date.day = d;
}

string Employee::get_name() const {
return name;
}

double Employee::get_salary() const {
return salary;
}

void Employee::display_info() const {
cout << left << setw(15) << "id: " << id << endl;
cout << setw(15) << "name: " << name << endl;
cout << setw(15) << "salary: " << salary << endl;
cout << setw(15) << "hire_date: " << hire_date.year << "-";
cout << std::right << setfill('0') << setw(2) << hire_date.month << "-" << setw(2) << hire_date.day;

cout << setfill(' ');
}

void Employee::update_salary(double s) {
salary = s;
}

void Employee::update_hire_date(int y, int m, int d) {
hire_date.year = y;
hire_date.month = m;
hire_date.day = d;
}
void Employee::raise_salary(double by_percent) {
double raise = salary * by_percent / 100;
salary += raise;
}
void Employee::display_count() {
cout << "there are " << count << " employees\n";
}

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

void test() {
using std::cout;
using std::endl;

cout << Employee::doc << endl << endl;

Employee employee1;
employee1.set_info("Sam", 30000, 2015, 1, 6);
employee1.update_hire_date(2017, 6, 30);
employee1.update_salary(35000);
employee1.display_info();
cout << endl << endl;

Employee employee2{"Tony", 20000, 2020, 3, 16};
employee2.raise_salary(15);
employee2.display_info();
cout << endl << endl;

Employee::display_count();
}

int main() {
test();
}

 comlex.cpp

#pragma once
#include<iostream>
#include<cmath>

using std::cout;
using std::endl;

class Complex{
    public:
        Complex(double x=0.0,double y=0.0):real{x},imag{y}{};
        Complex(const Complex&obj):real{obj.real},imag{obj.imag}{};
        double get_real() const;
        double get_imag() const;
        void show() const;
        void add(const Complex&obj);
        friend Complex add(const Complex&x,const Complex&y);
        friend bool is_equal(const Complex&x,const Complex&y);
        friend double abs(const Complex&obj);
    private:
        double real;
        double imag; 
};

double Complex::get_real() const{
return real;
}

double Complex::get_imag() const{
return imag;
}

void Complex::show() const{
if(imag==0)
cout<<real;
if(imag>0)
cout<<real<<" + "<<imag<<"i";
if(imag<0)
cout<<real<<imag<<"i";
}

void Complex::add(const Complex&obj){
real+=obj.real;
imag+=obj.imag;
}

Complex add(const Complex&x,const Complex&y){
    Complex obj;
    obj.real=x.real+y.real;
    obj.imag=x.imag+y.imag;
    return obj;
}

bool is_equal(const Complex&x,const Complex&y){
    if(x.real==y.real&&x.imag==y.imag)
    return true;
    else
    return false;
}

double abs(const Complex&obj){
    return sqrt(obj.real*obj.real+obj.imag*obj.imag);
}

 task4:

#include"Complex.h"
#include<iostream>
void test(){
    using namespace std;
    Complex c1(10,-8);
    const Complex c2(9.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();
}

 

 

User.hpp

#pragma once
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

class User{
public:
User(string x,string p="111111",string e="");
void set_email();
void change_password();
void print_info();
static void print_n();
private:
string name;
string password;
string email;
private:
static int n;
};
int User::n=0;

User::User(string x,string p,string e):name{x},password{p},email{e}{
++n;
}

void User::print_info(){
cout<<left<<setw(10)<<"name: "<<name<<endl;
cout<<left<<setw(10)<<"password: ";
for(int i=0;i<password.length();i++)
cout<<"*";
cout<<endl;
cout<<left<<setw(10)<<"email: "<<email<<endl;
}

static void User::print_n(){
cout<<"there are "<<User::n<<" users.";
}

void User::set_email(){
cout<<"Enter email address: ";
string x;
cin>>x;
email=x;
cout<<"email is set successfully..."<<endl;
}

void User::change_password(){
string y;
cout<<"Enter old password: ";
cin>>y;
int count=1;
while(y!=password&&count<3)
{
cout<<"password input error. Please re-enter again: ";
cin>>y;
count++;
if(count==3)
cout<<"password input error. Please try after a while."<<endl;
}
if(count!=3)
{
cout<<"Enter new passwd: ";
cin>>y;
password=y;
cout<<"new passwd is set successfully..."<<endl;
}
}

task5

#include"User.h"
#include<iostream>
void test(){
    using std::cout;
    using std::endl;
    
    cout<<"testing 1......\n";
    User user1("Chaihao","520666","[email protected]");
    user1.print_info();
    
    cout<<endl
    <<"testing 2......\n\n";
    
    User user2("liyuan");
    user2.change_password();
    user2.set_email();
    user2.print_info();
    
    cout<<endl;
    User::print_n();
} 
int main(){
    test();
}

 

 

 



 

标签:const,cout,int,void,实验,Employee,string
From: https://www.cnblogs.com/CYYyyds030711/p/16785058.html

相关文章

  • 实验6:开源控制器实践——RYU
    一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Deskto......
  • 【计算机网络实验】NAT配置实验
    【实训目的】掌握内部网络设计过程和私有IP地址使用。验证端口地址转换工作机制。掌握路由器地址转换配置过程。验证私有地址与公有地址之间的转换过程。验证IP分组和TCP报......
  • 【计算机网络实验】单区域OSPF配置实验
    【实训目的】掌握路由器OSPF配置过程验证OSPF创建动态路由项过程验证OSPF聚合网络地址过程【实训环境】eNSP模拟软件【实验原理】配置过程分为两部分:完成所有路由器接口IP地......
  • 【计算机网络实验】虚拟局域网组建
    【实训目的】(1)掌握基于端口的vlan划分方法(2)熟悉端口的基本参数应用(3)掌握Vlan成员的添加和删除方法【实训环境】eNSP模拟软件【实验原理】虚拟局域......
  • 【个人实验报告】博客网站
    目录​​项目名称​​​​个人实验目标​​​​完成情况​​​​项目主要内容​​​​1.任务完成情况:​​​​2.项目目标实现情况:​​​​3.项目经验总结:​​​​4.存在的不......
  • 【计算机网络实验】多区域OSPF配置实验
    【实训目的】掌握划分网络区域的方法和布置掌握路由器多区域OSPF配置过程进一步验证OSPF工作机制验证OSPF聚合网络地址过程【实训环境】eNSP模拟软件【实验原理】配置过程分......
  • 【计算机网络实验】BGP配置实验
    【实训目的】验证分层路由机制掌握互联网自治系统划分方法进一步验证BGP工作机制验证自治系统之间的连通性。【实训环境】eNSP模拟软件【实验原理】BGP路由协议关键命令如下......
  • 实验二
    实验任务4Complex.hpp#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doubler=0,doublei=0);......
  • 实验二 类与对象(2)
    实验任务三:Employee.hpp:#pragmaonce#include<iostream>#include<string>#include<iomanip>usingstd::string;usingstd::cout;usingstd::endl;usingstd::s......
  • 【数据库】期末必知必会-----第六章 实验部分
    第六章实验部分(这一部分是考试重点)1、SQL语言的组成、特点?组成:1)DDL(数据库定义语言:CREAT、DROP、ALTER)2)DML(数据库操纵语言:INSERT、UPDATE、DELETE、SELECT)3)DCL(数据库控制语......