实验二

实验二

实验任务1

#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 << "c2 == c3 :" << (c2 == c3) << endl;
    
    complex<double>c4 = 2;
    cout << "c4 = " << c4 << endl;
    c4 += c1;
    cout << "c4 =" << c4 << endl;
    
    return 0;
}

实验任务2

#include <iostream>
#include <array>

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()
{
    using namespace std;
    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;
}

实验任务2

//hpp

#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::left;
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;
    
public:
    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 << 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" << endl;
}


// cpp

#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;
    
    Employee employee2{"Tony",20000,2020,3,16};
    employee2.raise_salary(15);
    employee2.display_info();
    cout << endl<< endl;
    
    Employee::display_count();
    
}

int main()
{
    test();
}

//

实验任务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(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();
}

实验任务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 << "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;
}

// test.cpp

// 类测试
#include "User.hpp"
#include <iostream>

// 测试User类
void test() {
   using std::cout;
   using std::endl;

   cout << "testing 1......\n";
   User user1("Jonny", "92197", "xyz@hotmail.com");
   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();
}

posted @ 2022-10-13 22:00  MRchenyuheng  阅读(25)  评论(0编辑  收藏  举报