实验二

task1:
t.h:

点击查看代码
#pragma once
#include<string>

class T{
public:
    T(int x=0,int y=0);
    T(const T &t);
    T(T &&t);
    ~T();
    
    void adjust(int radio);
    void display() const;

private:
    int m1,m2;

public:
    static int get_cnt();

public:
    static const std::string doc;
    static const int max_cnt;

private:
    static int cnt;
    
    friend void func();
};

void func();

t.cpp:

点击查看代码
#include "t.h"
#include <string>
#include <iostream>
using namespace std;

const string T::doc{"a simple calss 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;
}

task.cpp:

点击查看代码
#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();
}

问题1

task2:
complex.h:

点击查看代码
#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
#include <cmath>
#include <string>

class Complex {
public:
    // 类属性
    static const std::string doc;

    // 构造函数
    Complex();                 // 默认构造函数,0+0i
    Complex(double r);         // 构造实部的复数,虚部为0
    Complex(double r, double i); // 构造具有指定实部和虚部的复数
    Complex(const Complex &c); // 拷贝构造函数

    // 接口方法
    double get_real() const;  // 返回实部
    double get_imag() const;  // 返回虚部
    void add(const Complex &c); // 将复数c加到当前复数上
    double abs() const;       // 返回复数的模

    // 友元函数
    friend Complex add(const Complex &c1, const Complex &c2); // 两个复数相加
    friend bool is_equal(const Complex &c1, const Complex &c2); // 判断相等
    friend bool is_not_equal(const Complex &c1, const Complex &c2); // 判断不等
    friend void output(const Complex &c); // 以a+bi的形式输出复数

private:
    double real;  // 实部
    double imag;  // 虚部
};

#endif // COMPLEX_H

Complex.cpp:

点击查看代码
#include "Complex.h"

// 初始化类属性
const std::string Complex::doc = "a simplified complex class";

// 构造函数实现
Complex::Complex() : real(0.0), imag(0.0) {}

Complex::Complex(double r) : real(r), imag(0.0) {}

Complex::Complex(double r, double i) : real(r), imag(i) {}

Complex::Complex(const Complex &c) : real(c.real), imag(c.imag) {}

// 接口方法实现
double Complex::get_real() const {
    return real;
}

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

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

double Complex::abs() const {
    return std::sqrt(real * real + imag * imag);
}

// 友元函数实现
Complex add(const Complex &c1, const Complex &c2) {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

bool is_equal(const Complex &c1, const Complex &c2) {
    return c1.real == c2.real && c1.imag == c2.imag;
}

bool is_not_equal(const Complex &c1, const Complex &c2) {
    return !is_equal(c1, c2);
}

void output(const Complex &c) {
    std::cout << c.real;
    if (c.imag >= 0) std::cout << " + " << c.imag << "i";
    else std::cout << " - " << -c.imag << "i";
    std::cout << std::endl;
}

task2.cpp:

点击查看代码
#include "Complex.h"
#include <iostream>

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

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

task3:
task3.cpp:

点击查看代码
#include <complex>
#include <iostream>

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

void test() {
  cout << "标准库模板类complex测试: " << 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(); }

task4
fraction.h

点击查看代码
#ifndef FRACTION_H
#define FRACTION_H

#include <string>

class Fraction {
public:
    // 类属性
    static const std::string doc;

    // 构造函数
    Fraction(int numerator = 0, int denominator = 1); // 默认构造,支持分子、分母设置
    Fraction(const Fraction &other); // 拷贝构造函数

    // 接口方法
    int get_up() const;         // 返回分子
    int get_down() const;       // 返回分母
    Fraction negative() const;  // 求负运算,返回负数的分数对象

    // 友元函数
    friend void output(const Fraction &f);           // 输出分数
    friend Fraction add(const Fraction &f1, const Fraction &f2); // 加法
    friend Fraction sub(const Fraction &f1, const Fraction &f2); // 减法
    friend Fraction mul(const Fraction &f1, const Fraction &f2); // 乘法
    friend Fraction div(const Fraction &f1, const Fraction &f2); // 除法

private:
    int up;   // 分子
    int down; // 分母
    void reduce(); // 化简分数

    static int gcd(int a, int b); // 辅助函数,用于计算最大公约数
};

#endif // FRACTION_H

fraction.cpp
点击查看代码
#include "Fraction.h"
#include <iostream>
#include <cmath>

// 初始化类属性
const std::string Fraction::doc = "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算.";

// 构造函数实现
Fraction::Fraction(int numerator, int denominator) : up(numerator), down(denominator) {
    if (down == 0) {
        std::cerr << "Error: Denominator cannot be zero." << std::endl;
        exit(1);
    }
    if (down < 0) {  // 保证分母为正
        up = -up;
        down = -down;
    }
    reduce();
}

Fraction::Fraction(const Fraction &other) : up(other.up), down(other.down) {}

// 接口方法实现
int Fraction::get_up() const {
    return up;
}

int Fraction::get_down() const {
    return down;
}

Fraction Fraction::negative() const {
    return Fraction(-up, down);
}

// 友元函数实现
void output(const Fraction &f) {
    if (f.down == 1) {
        std::cout << f.up << std::endl;
    } else {
        std::cout << f.up << "/" << f.down << std::endl;
    }
}

Fraction add(const Fraction &f1, const Fraction &f2) {
    int new_up = f1.up * f2.down + f2.up * f1.down;
    int new_down = f1.down * f2.down;
    return Fraction(new_up, new_down);
}

Fraction sub(const Fraction &f1, const Fraction &f2) {
    int new_up = f1.up * f2.down - f2.up * f1.down;
    int new_down = f1.down * f2.down;
    return Fraction(new_up, new_down);
}

Fraction mul(const Fraction &f1, const Fraction &f2) {
    int new_up = f1.up * f2.up;
    int new_down = f1.down * f2.down;
    return Fraction(new_up, new_down);
}

Fraction div(const Fraction &f1, const Fraction &f2) {
    if (f2.up == 0) {
        std::cerr << "Error: Division by zero." << std::endl;
        exit(1);
    }
    int new_up = f1.up * f2.down;
    int new_down = f1.down * f2.up;
    return Fraction(new_up, new_down);
}

// 私有方法实现
void Fraction::reduce() {
    int divisor = gcd(std::abs(up), down);
    up /= divisor;
    down /= divisor;
}

int Fraction::gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

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

posted @ 2024-10-29 20:51  灬灬zhang  阅读(6)  评论(0编辑  收藏  举报