实验2

任务1:

t.h代码:

#pragma once

#include <string>

// 类T: 声明
class T {
// 对象属性、方法
public:
    T(int x = 0, int y = 0);   // 普通构造函数
    T(const T &t);  // 复制构造函数
    T(T &&t);       // 移动构造函数
    ~T();           // 析构函数

    void adjust(int ratio);      // 按系数成倍调整数据
    void display() const;           // 以(m1, m2)形式显示T类对象信息

private:
    int m1, m2;

// 类属性、方法
public:
    static int get_cnt();          // 显示当前T类对象总数

public:
    static const std::string doc;       // 类T的描述信息
    static const int max_cnt;           // 类T对象上限

private:
    static int cnt;         // 当前T类对象数目

// 类T友元函数声明
    friend void func();
};

// 普通函数声明
void func();

t.cpp代码:

// 类T: 实现
// 普通函数实现

#include "t.h"
#include <iostream>
#include <string>

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

// static成员数据类外初始化
const std::string T::doc{"a simple class sample"};
const int T::max_cnt = 999;
int T::cnt = 0;


// 对象方法
T::T(int x, int y): m1{x}, m2{y} { 
    ++cnt; 
    cout << "T constructor called.\n";
} 

T::T(const T &t): m1{t.m1}, m2{t.m2} {
    ++cnt;
    cout << "T copy constructor called.\n";
}

T::T(T &&t): m1{t.m1}, m2{t.m2} {
    ++cnt;
    cout << "T move constructor called.\n";
}    

T::~T() {
    --cnt;
    cout << "T destructor called.\n";
}           

void T::adjust(int ratio) {
    m1 *= ratio;
    m2 *= ratio;
}    

void T::display() const {
    cout << "(" << m1 << ", " << m2 << ")" ;
}     

// 类方法
int T::get_cnt() {
   return cnt;
}

// 友元
void func() {
    T t5(42);
    t5.m2 = 2049;
    cout << "t5 = "; t5.display(); cout << endl;
}

task1代码:

#include "t.h"
#include <iostream>

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

void test();

int main() {
    test();
    cout << "\nmain: \n";
    cout << "T objects'current count: " << T::get_cnt() << endl;
}

void test() {
    cout << "test class T: \n";
    cout << "T info: " << T::doc << endl;
    cout << "T objects'max count: " << T::max_cnt << endl;
    cout << "T objects'current count: " << T::get_cnt() << endl << endl;


    T t1;
    cout << "t1 = "; t1.display(); cout << endl;

    T t2(3, 4);
    cout << "t2 = "; t2.display(); cout << endl;

    T t3(t2);
    t3.adjust(2);
    cout << "t3 = "; t3.display(); cout << endl;

    T t4(std::move(t2));
    cout << "t3 = "; t4.display(); cout << endl;

    cout << "T objects'current count: " << T::get_cnt() << endl;

    func();
}

运行结果:

 问题一:

不能。

 原因可能是没有声明函数。

问题二:

普通构造函数实现了对类的初始化,复制构造函数主要用于初始化一个新的对象,使其成为另一个同类型对象的副本,它会将引用的源对象的值复制到新对象中。移动构造函数的作用是提高程序的效率,其通过右值引用创建新对象,它避免了深拷贝,直接转移原始对象的资源到新对象中,从而减少了复制的时间。析构函数作用是在对象生命周期结束时进行资源清理。当前三种构造函数全部结束时析构函数会自动调用多遍。

问题3:

不能正确编译。

任务2:

Complex.h代码:

#ifndef COMPLEX_H
#define COMPLEX_H
#include<string>
using namespace std;

class Complex{
    private:
         double real;
         double imag;
    public:
        Complex();
        Complex(double x);
        Complex(double x1,double x2);
        Complex(const Complex &y);
        ~Complex();
    public:
        double get_real()const;
        double get_imag()const;
        void add(const Complex &y);
        
        friend Complex add(const Complex &a,const Complex &b);
        friend bool is_equal(const Complex &a,const Complex &b);
        friend bool is_not_equal(const Complex &a,const Complex &b);
        friend void output(const Complex &x);
        friend double abs(const Complex &x);
        static const string doc;
};
#endif

Complex.cpp代码:

#include<iostream>
#include<string>
#include<cmath>
#include"Complex.h"
using namespace std;
const string Complex::doc{"a simplified Complex class"};
Complex::Complex():real(0),imag(0){}
Complex::Complex(double x):real(x){}
Complex::Complex(double x1,double x2):real(x1),imag(x2){}
Complex::Complex(const Complex &y){
    real=y.get_real();
    imag=y.get_imag();
}
Complex::~Complex(){}
double Complex::get_real()const{
    return real;
}
double Complex::get_imag()const{
    return imag;
}
void Complex::add(const Complex &y){
    real+=y.get_real();
    imag+=y.get_imag();
}
Complex add(const Complex &a,const Complex &b){
    Complex c;
    c.real=a.get_real()+b.get_real();
    c.imag=a.get_imag()+b.get_imag();
    return c;
}
bool is_equal(const Complex &a,const Complex &b){
    if(a.get_real()==b.get_real()&&a.get_imag()==b.get_imag())
    return true;
    else
    return false;
}
bool is_not_equal(const Complex &a,const Complex &b){
    if(a.get_real()==b.get_real()&&a.get_imag()==b.get_imag())
    return false;
    else
    return true;
}
void output(const Complex &x){
    cout<<x.get_real();
    if(x.get_imag()<0)
    {
        cout<<" - ";
        double d;
        d=(-1.0)*x.get_imag();
        cout<<d<<"i";
    }
    else
    cout<<" + "<<x.get_imag() <<"i";
}
double abs(const Complex &x){
    double m,n;
    m=pow(x.get_real(),2)+pow(x.get_imag(),2);
    n=sqrt(m);
    return n;
}

main.cpp代码:

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

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

void test() {
    cout << "类成员测试: " << endl;
    cout << Complex::doc << endl;

    cout << endl;

    cout << "Complex对象测试: " << endl;
    Complex c1;
    Complex c2(3, -4);
    const Complex c3(3.5);
    Complex c4(c3);

    cout << "c1 = "; output(c1); cout << endl;
    cout << "c2 = "; output(c2); cout << endl;
    cout << "c3 = "; output(c3); cout << endl;
    cout << "c4 = "; output(c4); cout << endl;
    cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;

    cout << endl;

    cout << "复数运算测试: " << endl;
    cout << "abs(c2) = " << abs(c2) << endl;
    c1.add(c2);
    cout << "c1 += c2, c1 = "; output(c1); cout << endl;
    cout << boolalpha;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
    cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
    c4 = add(c2, c3);
    cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
}

int main() {
    test();
}

运行结果:

 任务3:

#include <iostream>
#include <complex>

using std::cout;
using std::endl;
using std::boolalpha;
using std::complex;

void test() {
    cout << "标准库模板类comple测试: " << endl;
    complex<double> c1;
    complex<double> c2(3, -4);
    const complex<double> c3(3.5);
    complex<double> c4(c3);

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

    cout << "复数运算测试: " << endl;
    cout << "abs(c2) = " << abs(c2) << endl;
    c1 += c2;
    cout << "c1 += c2, c1 = " << c1 << endl;
    cout << boolalpha;
    cout << "c1 == c2 : " << (c1 == c2) << endl;
    cout << "c1 != c3 : " << (c1 != c3) << endl;
    c4 = c2 + c3;
    cout << "c4 = c2 + c3, c4 = " << c4 << endl;
}

int main() {
    test();
}

运行结果:

 complex库可以大大简化程序,我认为complex就是一个现成的类,使用了之后就省去了我们构造类的过程。

任务4:

Fraction.h代码:

#ifndef FRACTION_H
#define FRACTION_H
#include<iostream> 
#include<string>
using namespace std;
class Fraction{
    private:
        int up;
        int down;
    public:
        Fraction();
        Fraction(int val);
        Fraction(int val1,int val2);
        Fraction(const Fraction &a);
        ~Fraction();
    public:
        int get_up()const ;
        int get_down()const ;
        Fraction negative();
    public:
        friend void output(const Fraction &a);
        friend Fraction add( const Fraction &a,const Fraction &b);
        friend Fraction sub(const Fraction &a,const Fraction &b);
        friend Fraction mul(const Fraction &a, const Fraction &b);
        friend Fraction div(const Fraction &a,const Fraction &b);
        static const string doc;
};
#endif

Fraction.cpp代码:

#include<iostream>
#include<string>
#include<cmath>
#include"Fraction.h"
using namespace std;
const string Fraction::doc{"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."};
Fraction::Fraction(){}
Fraction::Fraction(int val):up(val),down(1){}
Fraction::Fraction(int val1, int val2):up(val1),down(val2){}
Fraction::Fraction(const Fraction &a){up=a.up;down=a.down;}
Fraction::~Fraction(){}
int Fraction::get_up()const{
    return up;
}
int Fraction::get_down()const{
    return down;
}
Fraction Fraction::negative(){
    Fraction c;
    c.up=-up;
    c.down=down;
    int p,q;
    p=max(c.get_up(),c.get_down());
    q=min(c.get_up(),c.get_down());
    if(c.get_up()==c.get_down())
    {
        c.up=1;
        c.down=1;
    }
    else if(c.get_down()==0)
    cout<<"分母不能为0"; 
    else{
        while(true)
        {
            if(p%q==0)
            {
                c.down=c.get_down()/q;
                c.up=c.get_up()/q;
                break;
            }
            else{
                int t=p;
                p=q;
                q=t%q;
            }
        }
    }
    return c;
}
void output(const Fraction &a)
{
    Fraction c(a);
    int p,q;
    p=max(c.get_up(),c.get_down());
    q=min(c.get_up(),c.get_down());
    if(c.get_up()==c.get_down())
    {
        c.up=1;
        c.down=1;
        cout<<"1";
    }
    else if(c.get_up()==0&&c.get_down()!=0)
    {
        cout<<"0";        
    }
    else if(c.get_down()==0)
    cout<<"分母不能为0"; 
    else if(c.get_down()==1)
    cout<<c.get_up();
    else{
        while(true)
        {
            if(p%q==0)
            {
                c.down=c.get_down()/q;
                c.up=c.get_up()/q;
                break;
            }
            else{
                int t=p;
                p=q;
                q=t%q;
            }
        }
        cout<<c.get_up()<<"/"<<c.get_down();
    }
    
}
Fraction add(const Fraction &a,const Fraction &b){
    Fraction c;
     c.down=a.get_down()*b.get_down();
     c.up=a.get_up()*b.get_down()+b.get_up()*a.get_down();
     return c;
}
Fraction sub(const Fraction &a,const Fraction &b){
    Fraction c;
     c.down=a.get_down()*b.get_down();
     c.up=a.get_up()*b.get_down()-b.get_up()*a.get_down();
     return c;
}
Fraction mul(const Fraction &a,const Fraction &b){
    Fraction c;
     c.down=a.get_down()*b.get_down();
     c.up=a.get_up()*b.get_up();
     return c;
}
Fraction div(const Fraction &a,const Fraction &b){
    Fraction c;
     c.down=a.get_down()*b.get_up();
     c.up=a.get_up()*b.get_down();
     return c;
}

main1.cpp代码:

#include"Fraction.h"
#include <iostream>

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


void test1() {
    cout << "Fraction类测试: " << endl;
    cout << Fraction::doc << endl << endl;

    Fraction f1(5);
    Fraction f2(3, -4), f3(-18, 12);
    Fraction f4(f3);
    cout << "f1 = "; output(f1); cout << endl;
    cout << "f2 = "; output(f2); cout << endl;
    cout << "f3 = "; output(f3); cout << endl;
    cout << "f4 = "; output(f4); cout << endl;

    Fraction f5(f4.negative());
    cout << "f5 = "; output(f5); cout << endl;
    cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;

    cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
    cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
    cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
    cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
    cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
}

void test2() {
    Fraction f6(42, 55), f7(0, 3);
    cout << "f6 = "; output(f6); cout << endl;
    cout << "f7 = "; output(f7); cout << endl;
    cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
}

int main() {
    cout << "测试1: Fraction类基础功能测试\n";
    test1();

    cout << "\n测试2: 分母为0测试: \n";
    test2();
}

运行截图:

 任务5:

account.h:

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include<iostream>
class SavingAccount{
    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:
        SavingAccount(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);//取出现金
        //结算利息,每年1月1日调用一次下函数
        void settle(int date);
        void show()const; 
};
#endif

account.cpp:

#include"account.h"
#include<iostream>
#include<cmath>
using namespace std;
double SavingAccount::total=0;
SavingAccount::SavingAccount(int date,int id,double rate):id{id},balance(0),rate(rate),lastDate(date),accumulation(0){
   cout<<date<<"\t#"<<id<<"is created"<<endl;    
}
void SavingAccount::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 SavingAccount::deposit(int date,double amount)
{
    record(date,amount);
}
void SavingAccount::withdraw(int date,double amount)
{
    if(amount>getBalance())
    cout<<"Error:not enough money"<<endl;
    else
    record(date,-amount);
}
void SavingAccount::settle(int date)
{
    double interest=accumulate(date)*rate/365;
    if(interest!=0)
       record(date,interest);
    accumulation=0;
}
void SavingAccount::show()const
{
    cout<<"#"<<id<<"\tBalance:"<<balance;
}

main.cpp:

#include"account.h"
#include<iostream>
using namespace std;
int main()
{
    SavingAccount sa0(1,21325302,0.015);
    SavingAccount 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:"<<SavingAccount::getTotal()<<endl;
    return 0;
}

运行截图:

 

posted @ 2024-10-23 15:27  忘忧熊  阅读(13)  评论(0编辑  收藏  举报