首页 > 其他分享 >实验二

实验二

时间:2023-10-16 23:11:33浏览次数:25  
标签:const string int double void 实验 include

任务一

t.h

 1 #ifndef T_H
 2 #define T_H
 3 #include <iostream>
 4 #include <string>
 5 using namespace std;
 6 class T
 7 {
 8 public:
 9     T(int x=0,int y=0);
10     T(const T&t);
11     T(T &&t);
12     ~T();
13     void set_m1(int x);
14     int get_m1() const;
15     int get_m2() const;
16     void display() const;
17     friend void func();
18 private:
19     int m1,m2;
20 public:
21     static void disply_count();
22     static const string doc;
23     static const int max_count;
24 private:
25     static int count;
26 };
27 void func();
28 #endif
View Code

t.cpp

 1 #include "t1.h"
 2 const string T::doc{"a simple class"};
 3 const int T::max_count=99;
 4 int T::count=0;
 5 T::T(int x,int y):m1{x},m2{y}
 6 {
 7     ++count;
 8     cout<<"constructor called \n";
 9 }
10 T::T(const T &t):m1{t.m1},m2{t.m2}
11 {
12     ++count;
13     cout<<"copy construcyor called \n";
14 }
15 T::T(T &&t):m1{t.m1},m2{t.m2}
16 {
17     ++count;
18     cout<<"move constructor called \n";
19 }
20 T::~T()
21 {
22     --count;
23     cout<<"destructor called \n";
24 }
25 void T::set_m1(int x)
26 {
27     m1=x;
28 }
29 int T::get_m1() const
30 {
31     return m1;
32 
33 }
34 int T::get_m2() const
35 {
36     return m2; 
37 }
38 void T::display() const
39 {
40     cout<<m1<<","<<m2<<endl;
41 }
42 void T::disply_count()
43 {
44     cout<<"T objects:"<<count<<endl;
45 }
46 void func()
47 {
48     T t1;
49     t1.set_m1(55);
50     t1.m2=77;
51     t1.display();
52 }
View Code

main.cpp

 1 #include "t1.h"
 2 void test() 
 3 {
 4     cout << "T class info: " << T::doc << endl;
 5     cout << "T objects max_count: " << T::max_count << endl;
 6     T::disply_count();
 7     T t1;
 8     t1.display();
 9     t1.set_m1(42);
10     T t2{t1};
11     t2.display();
12     T t3{std::move(t1)};
13     t3.display();
14     t1.display();
15     T::disply_count();
16 }
17 int main()
18 {
19     cout<<"===========T=================="<<endl;
20     test();
21     cout<<endl;
22     cout<<"===================func()==============="<<endl;
23     func();
24 }
View Code

 

 

任务二

t.hpp
  1 #pragma once
  2 #include <iostream>
  3 #include <string>
  4 #include <iomanip>
  5 using std::string;
  6 using std::cout;
  7 using std::endl;
  8 using std::setfill;
  9 using std::setw;
 10 using std::left;
 11 using std::right;
 12 using std::to_string;
 13 struct Date 
 14 {
 15     int year;
 16     int month;
 17     int day;
 18 };
 19 // Employee类的声明
 20 class Employee
 21 {
 22 public:
 23     Employee();
 24     Employee(string name0, double salary0, int y, int m, int d = 1);
 25     void set_info(string name0, double salary0, int y, int m, int d = 1);
 26     // 设置雇员信息
 27     string get_name() const; // 获取雇员姓名
 28     double get_salary() const; // 获取雇员薪水
 29     void display_info() const; // 显示雇员信息
 30     void update_salary(double s); // 更新雇员薪水
 31     void update_hire_date(int y, int m, int d); // 更新雇佣日期
 32     void raise_salary(double by_percent); // 计算提薪加成
 33 public:
 34     static void display_count(); // 类方法,显示雇员总数
 35     private:
 36     string id; // 雇员工号
 37     string name; // 雇员姓名
 38     double salary; // 雇员薪水
 39     Date hire_date; // 雇员雇佣日期
 40 public:
 41     static const string doc; // 类属性,用于描述类
 42 private:
 43     static int count; // 类属性,用于记录雇员总人数
 44 };
 45 const string Employee::doc {"a simple Employee class"};
 46 int Employee::count = 0;
 47 // 默认构造函数
 48 Employee::Employee(): id{ to_string(count+1) } 
 49 {
 50     ++count;
 51 }
 52 // 带参数的构造函数
 53 Employee::Employee(string name0, double salary0, int y, int m, int d):id{to_string(count+1)}, name{name0}, salary{salary0},
 54 hire_date{y, m, d} 
 55 {
 56     ++count;
 57 }
 58 // 设置员工信息
 59 void Employee::set_info(string name0, double salary0, int y, int m, int d)
 60 {
 61     name = name0;
 62     salary = salary0;
 63     hire_date.year = y;
 64     hire_date.month = m;
 65     hire_date.day = d;
 66 }
 67 // 获取员工姓名
 68 string Employee::get_name() const 
 69 {
 70     return name;
 71 }
 72 // 获取员工薪水
 73 double Employee::get_salary() const 
 74 {
 75     return salary;
 76 }
 77 // 显示雇员信息
 78     void Employee::display_info() const {
 79     cout << left << setw(15) << "id: " << id << endl;
 80     cout << setw(15) << "name: " << name << endl;
 81     cout << setw(15) << "salary: " << salary << endl;
 82     cout << setw(15) << "hire_date: " << hire_date.year << "-";
 83     cout << std::right << setfill('0') << setw(2) << hire_date.month << "-"<< setw(2) << hire_date.day;
 84     cout << setfill(' '); // 恢复到默认空格填充
 85 }
 86 // 更新薪水
 87 void Employee::update_salary(double s) 
 88 {
 89     salary = s;
 90 }
 91 // 更新雇佣日期
 92 void Employee::update_hire_date(int y, int m, int d) 
 93 {
 94     hire_date.year = y;
 95     hire_date.month = m;
 96     hire_date.day = d;
 97 }
 98 // 雇员提薪加成
 99 // by_percent是提升比例
100 void Employee::raise_salary(double by_percent) 
101 {
102     double raise = salary * by_percent / 100;
103     salary += raise;
104 }
105 // 类方法
106 // 显示雇员总数
107 void Employee::display_count() 
108 {
109     cout << "there are " << count << " employees\n";
110 }
View Code main.cpp
 1 #include "t2.h"
 2 #include <iostream>
 3 // 测试:Employee类
 4     void test() 
 5 {
 6     using std::cout;
 7     using std::endl;
 8     cout << Employee::doc << endl << endl;
 9     Employee employee1;
10     employee1.set_info("Sam", 30000, 2015, 1, 6);
11     employee1.update_hire_date(2019, 6, 30);
12     employee1.update_salary(35000);    
13     employee1.display_info();
14     cout << endl << endl;
15     Employee employee2{"Tony", 20000, 2023, 3, 16};
16     employee2.raise_salary(15); // 提成15%
17     employee2.display_info();
18     cout << endl << endl;
19     Employee::display_count();
20 }
21 int main() 
22 {
23     test();
24 }
View Code

 

任务三

t3.h

 1 #pragma once
 2 #include <iostream>
 3 #include <cmath>
 4 using namespace std;
 5 class Complex
 6 {
 7 public:
 8     Complex(double a = 0, double b = 0) :a(a), b(b) {}
 9     Complex(const Complex& x) ;
10     double get_real() { return a; }
11     double get_imag() const { return b; }
12     void show() const;
13     void add(const Complex &z);
14     ~Complex(){};
15     friend Complex add(const Complex &k,const Complex &l);
16     friend bool is_equal(const Complex &asd,const Complex &zxc);
17     friend double abs(const Complex &x);
18 
19 private:
20     double a, b;
21 };
View Code

t3.cpp

 1 #include "t3.h"
 2 Complex::Complex(const Complex& x)
 3 {
 4     a = x.a;
 5     b = x.b;
 6 }
 7 void Complex::show() const
 8 {
 9     if (b > 0)
10         cout << a << "+" << b << "i" ;
11     else if (b < 0)
12         cout << a << b << "i" ;
13     else
14         cout << a ;
15 }
16 void Complex::add(const Complex &z)
17 {
18     a = a + z.a;
19     b = b + z.b;
20 }
21 Complex add(const Complex &k,const Complex &l)
22 {
23     Complex s;
24     s.a=k.a+l.a;s.b=k.b+l.b;
25     return (s);
26 }
27 bool is_equal(const Complex &asd,const Complex &zxc)
28 {
29     if (asd.a == zxc.a && asd.b == zxc.b)
30         return (true);
31     else
32         return (false);
33 }
34 double abs(const Complex &x)
35 {
36     return(sqrt(x.a * x.a + x.b * x.b));
37 }
View Code

main3.cpp

 1 #include "t3.h"
 2 void test()
 3 {
 4     using namespace std;
 5     Complex c1(3, -4);
 6     const Complex c2(4.5);
 7     Complex c3(c1);
 8     cout << "c1 = ";
 9     c1.show();
10     cout << endl;
11     cout << "c2 = ";
12     c2.show();
13     cout << endl;
14     cout << "c2.imag = " << c2.get_imag() << endl;
15     cout << "c3 = ";
16     c3.show();
17     cout << endl;
18     cout << "abs(c1) = ";
19     cout << abs(c1) << endl;
20     cout << boolalpha;
21     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
22     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
23     Complex c4;
24     c4 = add(c1, c2);
25     cout << "c4 = c1 + c2 = ";
26     c4.show();
27     cout << endl;
28     c1.add(c2);
29     cout << "c1 += c2, " << "c1 = ";
30     c1.show();
31     cout << endl;
32 }
33 int main()
34 {
35     test();
36 }
View Code

 

任务四

task4.cpp
 1 using namespace std;
 2 class User
 3 {
 4 public:
 5     User(string name, string passwd = "111111", string email = ""):name{name},passwd{passwd},email{email}{n++;}
 6     User(User& x);
 7     string set_email();
 8     void change_passwd();
 9     void print_info();
10     static int print_n();
11     ~User(){--n;}
12 private:
13     string name;
14     string passwd;
15     string email;
16     static int n;
17 };
18 User::User(User& x)
19 {
20     name = x.name; passwd = x.passwd; email = x.email;
21 }
22 string User::set_email()
23 {
24     cout << "Enter email address:";
25     cin >> email;
26     cout<< "email is set successfully..." << endl;
27 }
28 int User::n=0;
29 void User::change_passwd()//修改密码 
30 {
31     for (int i = 0; i < 3; i++)
32 {
33     if(i==0)
34         cout << "Enter old password:";
35         cin >> passwd;
36     if (passwd == "111111")
37     {
38         cout<<"Enter new passwd:";
39         cin >> passwd;
40         cout << "new passwd is set successfilly" << endl;
41         break;
42     }
43     else if(i<2)
44         cout<<"Password input error.Plase re-enter it again:";
45     else 
46         cout<<"assword input error.Plase try after while";
47 }
48     cout<<endl;    
49 }
50 int User::print_n()
51 {
52     cout<<"there are "<<n<<"users"<<endl;
53 }
54 void User::print_info()
55 {
56     string s(passwd.size(),'*');
57     cout<<"name:"<<name<<endl;
58     cout<<"passwd:"<< s<<endl;
59     cout<<"email:"<<email<<endl;
60 }
View Code

main4.cpp

 1 #include "t4.h"
 2 #include <iostream>
 3 void test()
 4 {
 5     using std::cout;
 6     using std::endl;
 7     cout << "testing 1......\n";
 8     User user1("Jonny", "92197", "[email protected]");
 9     user1.print_info();
10     cout << endl<< "testing 2......\n\n";
11     User user2("殷润哲");user2.print_info();
12     user2.change_passwd();
13     user2.set_email();
14     user2.print_info();
15     cout << endl;
16     User::print_n();
17 }
18 int main()
19 {
20     test();
21 }
View Code

 

任务五

account.h

 1 //account.h
 2 class SavingsAccount
 3 {
 4 private:
 5     int id;
 6     double balance;
 7     double rate;
 8     int lastDate;
 9     double accumulation;
10     static double total;
11     void record(int data,double amount);
12     double accumulate(int date)const
13     {
14         return accumulation+balance*(date-lastDate);
15     }
16 public:
17     SavingsAccount(int data,int id,double rate);
18     int getId()const{return id;}
19     double getBalance()const{return balance;}
20     double getRate()const{return rate;}
21     static double getTotal(){return total;}
22     void deposit(int date,double amount);
23     void withdraw(int data,double amount);
24     void settle(int date);
25     void show() const;    
26 };
View Code

account.cpp

 1 //account.cpp
 2 #include"account.h"
 3 #include<cmath>
 4 #include<iostream>
 5 using namespace std;
 6 double SavingsAccount::total=0;
 7 SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0)
 8 {
 9     cout<<date<<"\t#"<<id<<"is created"<<endl;
10 }
11 void SavingsAccount::record(int date,double amount)
12 {
13     accumulation=accumulate(date);
14     lastDate=date;
15     amount=floor(amount*100+0.5)/100;
16     balance+=amount;
17     total+=amount;
18     cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
19 }
20 void SavingsAccount::deposit(int date,double amount)
21 {
22     record(date,amount);
23 }
24 void SavingsAccount::withdraw(int date,double amount)
25 {
26     if(amount>getBalance())
27         cout<<"Error:not enough money"<<endl;
28     else
29         record(date,-amount);
30 }
31 void SavingsAccount::settle(int data)
32 {
33     double interest=accumulate(data)*rate/365;
34     if(interest!=0)
35         record(data,interest);
36     accumulation=0;
37 }
38 void SavingsAccount::show() const
39 {
40     cout<<"#"<<id<<"\tBalance:"<<balance;
41 }
View Code

5_11.cpp

 1 //account.cpp
 2 #include"account.h"
 3 #include<cmath>
 4 #include<iostream>
 5 using namespace std;
 6 double SavingsAccount::total=0;
 7 SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0)
 8 {
 9     cout<<date<<"\t#"<<id<<"is created"<<endl;
10 }
11 void SavingsAccount::record(int date,double amount)
12 {
13     accumulation=accumulate(date);
14     lastDate=date;
15     amount=floor(amount*100+0.5)/100;
16     balance+=amount;
17     total+=amount;
18     cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
19 }
20 void SavingsAccount::deposit(int date,double amount)
21 {
22     record(date,amount);
23 }
24 void SavingsAccount::withdraw(int date,double amount)
25 {
26     if(amount>getBalance())
27         cout<<"Error:not enough money"<<endl;
28     else
29         record(date,-amount);
30 }
31 void SavingsAccount::settle(int data)
32 {
33     double interest=accumulate(data)*rate/365;
34     if(interest!=0)
35         record(data,interest);
36     accumulation=0;
37 }
38 void SavingsAccount::show() const
39 {
40     cout<<"#"<<id<<"\tBalance:"<<balance;
41 }
View Code

 

标签:const,string,int,double,void,实验,include
From: https://www.cnblogs.com/dmsx/p/17768629.html

相关文章

  • 实验二
    1,源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));//以当前系统时间作为随机种子for(i=0;i<N;++i){number=rand()%(N2-N1+1)+N1;......
  • 实验2
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));//以当前系统时间作为随机种子for(i=0;i<N;++i){number=rand()%(N2-N1+1)+N1;printf(......
  • 实验2
    task1问题一:line17作用:限制随机数范围在374~465之间问题二:程序功能:随时间变化,随机生成在202383290376374~202383290376465之间的数task21#include<stdio.h>2intmain(){3chara;4while(1){5a=getchar();6getchar();7if(a==EOF){8......
  • 实验1 类和对象_基础编程
    实验任务11.代码:1#include<iostream>2#include<string>3#include<vector>4#include<array>567//函数模板8//对满足特定条件的序列类型T对象,使用范围for输出9template<typenameT>10voidoutput1(constT&obj){11for(autoi:obj)......
  • 实验2
    实验2.1实验2.1代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=1;i<N;++i){number=rand()%(N2-N1+1)+......
  • 实验二
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1+1)+N1;//计算num的值printf("......
  • 实验2
    task1代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain(){10intnumber,i;1112srand(time(0));1314for(i=0;i<N;++i......
  • 实验二 c语言分支与循环基础应用编程
    实验一源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){ intnumber; inti; srand(time(0)); for(i=0;i<N;i++) { number=rand()%(N2-N1+1)+N1; printf("20238329%04......
  • 实验二
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1+......
  • 实验2
    实验任务1 #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){ system("colorE5");intnumber;inti;srand(time(0));//以当前系统时间作为随机种子for(i=0;i&......