首页 > 其他分享 >实验2

实验2

时间:2023-10-22 23:44:20浏览次数:31  
标签:std const int double void Complex 实验

任务1

#ifndef T_H 
#define T_H 
#include <iostream> 
#include <string> 
using namespace std; 
class T { 
public: 
T(int x = 0, int y = 0);  
T(const T &t); 
T(T &&t); 
~T(); 
void set_m1(int x); 
int get_m1() const; 
int get_m2() const; 
void display() const;
friend void func(); 
private: 
int m1, m2; 
public: 
static void disply_count();
public: 
static const string doc; 
static const int max_count;
private: 
static int count; 
};
void func(); 
#endif

 

#include "t.h"  
const string T::doc{"a simple class"}; 
const int T::max_count = 99; 
int T::count = 0; 
T::T(int x, int y): m1{x}, m2{y} { 
++count; 
cout << "constructor called.\n"; 
}
T::T(const T &t): m1{t.m1}, m2{t.m2} { 
++count; 
cout << "copy constructor called.\n"; 
}
T::T(T &&t): m1{t.m1}, m2{t.m2} { 
++count; 
cout << "move constructor called.\n"; 
}
T::~T() { 
--count; 
cout << "destructor called.\n"; 
}
void T::set_m1(int x) { 
m1 = x; 
}
int T::get_m1() const { 
return m1; 
}
int T::get_m2() const { 
return m2; 
}
void T::display() const { 
cout << m1 << ", " << m2 << endl; 
}

void T::disply_count() { 
cout << "T objects: " << count << endl;  
}
 
void func() { 
T t1; 
t1.set_m1(55); 
t1.m2 = 77; 
t1.display(); 
}
#include "t.h" 
void test() { 
cout << "T class info: " << T::doc << endl; 
cout << "T objects max_count: " << T::max_count << endl; 
T::disply_count(); 
T t1; 
t1.display(); 
t1.set_m1(42); 
T t2{t1}; 
t2.display(); 
T t3{std::move(t1)}; 
t3.display(); 
t1.display(); 
T::disply_count(); 
}
int main() { 
cout << "============测试类T============" << endl; 
test(); 
cout << endl; 
cout << "============测试友元函数func()============" << endl; 
func(); 
}

任务2

#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(2019, 6, 30);
employee1.update_salary(35000);
employee1.display_info();
cout << endl << endl;
Employee employee2{"Tony", 20000, 2023, 3, 16};
employee2.raise_salary(15);
employee2.display_info();
cout << endl << endl;
Employee::display_count();
}
int main() {
test();
}

任务3

#pragma once

#include <iostream> 
#include <cmath>


using namespace std;


class Complex{
public:
    Complex();
    Complex(double real0);
    Complex(double real0,double imag0);
    Complex(const Complex &x);
    
    double get_real()const {return real;}
    double get_imag()const {return imag;}
    void show()const;
    void add(const Complex &x);
    
    friend Complex add(Complex x1, Complex x2);
    friend bool is_equal(const Complex &x1,const Complex &x2
 


    friend double abs(const Complex &x);
    
private:
    double real,imag;
};

Complex::Complex(){
    real = 0;
    imag = 0;
}

Complex::Complex(double real0){
    real = real0;
    imag = 0;
}

Complex::Complex(double real0,double imag0) {
    real = real0;
    imag = imag0;
}

Complex::Complex(const Complex &x){
    real = x.real;
    imag = x.imag;
}


void Complex::show() const {
    
    using namespace std;
    if(real == 0 && imag == 0) {cout << 0;} 
    else if(real == 0){cout << imag << "i"; } 
    else if(imag == 0){cout << real;}
    else {
        if (imag > 0){
            cout << real << " + " << imag << "i" ;
    }
        else if(imag < 0) {
            cout << real << " - " << -imag << "i" ;
}
}
}

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

Complex add(Complex x1, Complex x2) {
    double real,imag;
    x1.real = x1.real + x2.real;
    x2.imag = x1.imag + x2.imag;
    return x1;
}

bool is_equal(const Complex &x1, const Complex &x2) {
    bool real,imag;
    real = x1.real == x2.real;
    imag = x1.imag == x2.imag;
    return real && imag;
}
double abs(const Complex &x) {
    return sqrt(pow(x.real, 2) + pow(x.imag, 2));
}
#include "t.hpp"


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

任务4

#include "源1.cpp"
#include <iostream>
void test() {
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();
    cout << endl
        << "testing 2......\n\n";

    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    cout << endl;
    User::print_n();
}
int main() {
    test();
}
#include <iostream>
#include <string>
#include <limits>
class User {
private:
    static int n;
public:
    static void print_n();
private:
    std::string name, password, email;
public:
    User(std::string nm,
        std::string p = "111111",
        std::string e = "") : name{ nm }, password{ p }, email{ e }
    {
        n += 1;
    };
    void set_email();
    void change_passwd();
    void print_info();
};
int User::n = 0;

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

void User::set_email() {
    using namespace std;
    cout << "Enter email address: ";
    string e;
    cin >> e;
    cin.ignore((std::numeric_limits<streamsize>::max)(), '\n');
    cout << "email is set successfully...\n";
    email = e;
}

void User::change_passwd() {
    using namespace std;
    string o, n;
    for (int i = 0; i <= 3; i++) {
        if (i == 3) {
            cout << "Please try after a while. \n";
            break;
        }
        if (i == 0)
            cout << "Enter old password: ";
        else
            cout << "Please re-enter again: ";
        cin >> o;
        cin.ignore((std::numeric_limits<streamsize>::max)(), '\n');
        if (o != password) {
            cout << "password input error. ";
            continue;
        }
        else {
            cout << "Enter new passwd: ";
            cin >> n;
            cin.ignore((std::numeric_limits<streamsize>::max)(), '\n');
            password = n;
            cout << "new passwd is set successfully...\n";
            break;
        }
    }
}

void User::print_info() {
    using namespace std;
    cout << "name: \t" << name << endl;
    string s(password.size(), '*'); cout << "password: \t" << s << endl;
    cout << "email: \t" << email << endl;
}
void User::print_n() {
    cout << "there are " << n << "users" << endl;
}

代码报错,修改中

任务5

# ifndef ACCOUNT_H__
# define ACCOUNT_H__
class SavingsAccount{
private:
    int id;
    double balance;
    double rate;
    int lastDate;
    double accumulation;
    static double total;
    void record(int date, double amount);
    double accumulate(int date) const{
    return accumulation + balance * (date - lastDate);
    }
    
public:
    SavingsAccount(int date, int id, double rate);
    int getId() const {return id;}
    double getBalance() const {return balance;}
    double getRate() const {return rate;}
    static double getTotal(){return total;}
    void deposit(int date, double amount);
    void withdraw(int date, double amount);
    void settle(int date);
    void show() const;
};
# endif
# include"account.h"
# include<cmath>
# include<iostream>
using namespace std;

double SavingsAccount::total = 0;
SavingsAccount::SavingsAccount(int date, int id, double rate):
    id(id), balance(0), rate(rate), lastDate(date), accumulation(0){
        cout << date << "\t#" << id << " is created" << endl;
    }
    
void SavingsAccount::record(int date, double amount){
    accumulation = accumulate(date);
    lastDate = date;
    amount = floor(amount *100 + 0.5)/ 100;
    balance += amount;
    total += amount;
    cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}

void SavingsAccount::deposit(int date, double amount){
    record(date, amount);
}

void SavingsAccount::withdraw(int date, double amount){
    if(amount>getBalance())
    cout << "Error:nit enough money" << endl;
    else record(date, -amount);
}

void SavingsAccount::settle(int date){
    double interest = accumulate(date)*rate/365;
    if(interest !=0)
       record(date, interest);
    accumulation = 0;
}

void SavingsAccount::show() const {
     cout << "#" << id << "\tBalance: " << balance;
}
# include"account.h"
# include<iostream>
using namespace std;
int main(){
    SavingsAccount sa0(1, 21325302, 0.015);
    SavingsAccount sa1(1, 58320212, 0.015);
    
    sa0.deposit(5, 5000);
    sa1.deposit(25, 10000);
    sa0.deposit(45, 5500);
    sa1.withdraw(60, 4000);
    
    sa0.settle(90);
    sa1.settle(90);
    
    sa0.show(); cout << endl;
    sa1.show(); cout << endl;
    cout << "Total: " << SavingsAccount::getTotal() << endl;
    return 0;
}

报错,修改中

标签:std,const,int,double,void,Complex,实验
From: https://www.cnblogs.com/wtx2004/p/17781398.html

相关文章

  • 实验2
    3.c.hpp#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0);Complex(constComplex&c);doubleget_real()const;doubleget_imag()const;voidshow()const;voidadd(const......
  • 实验二 类和对象
    实验任务三1#include<iostream>2#include<cmath>34classComplex{56private:7doublereal;8doubleimag;910public:11Complex(doubler=0,doublei=0){12real=r;13imag=i;14}15......
  • 实验二
    任务三:complex.hpp代码:#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=i;};Complex(constComplex&c){real=c.real;imag=c.imag;};~Complex(){};doubleget_real()const{returnreal......
  • 实验2 类和对象
    实验任务3t.hpp#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex();Complex(doublereal0);Complex(doublereal0,doubleimag0);Complex(constComplex&x);doub......
  • 实验二 类与对象
    实验任务3complex.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classUser{public:User(std::stringname,std::stringpasswd="111111",std::stringemail=""):name{name},p......
  • 实验2 类和对象_基础编程2
    //第一个任务简单Complex类#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doublex0=0,doubley0=0);//构造函数Complex(constComplex&c);//拷贝构造函数~Complex(){};//析构函数doubleget_real()con......
  • 北京电子科技学院(BESTI)实验报告
    实验报告课程:计算机基础与程序设计班级:2314姓名:高伟光学号:20231404成绩:指导教师:娄佳鹏实验日期:2022.9.26实验密级:预习程度:部分预习实验时间:34节仪器组次:无必修/选修:必修实验序号:实验一实验名称:Li......
  • 实验二
    task.3.hpp#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=i;}Complex(constComplex&x){real=x.real;imag=x.imag;......
  • 实验二
    试验任务三complex.hpp点击查看代码#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=i;}Complex(constComplex&x){real=x.real;imag=x.i......
  • 实验2 类和对象_基础编程2
    1.实验任务3程序代码组织如下:•Complex.h类Complex的声明#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0);Complex(constComplex&c);~Complex(){};doubleget_real()cons......