首页 > 其他分享 >实验二

实验二

时间:2024-10-27 19:19:58浏览次数:3  
标签:const int double Complex 实验 Fraction include

实验任务一:

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 ratio); 
		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<iostream>
#include<string>


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



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";} 
const std::string T::doc{"a simple class sample"};
const int T::max_cnt=999;
int T::cnt= 0;

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.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:

可以去掉line36。

问题2:

普通构造函数:
功能:允许在创建对象时候提供初始化参数。
调用时机:当创建了对象提供参数的时候。如T obj{10,20};。

拷贝构造函数:
功能:用于创建一个对象作为另一个同类对象的副本。
调用时机:当一个对象被另一个对象初始化时候,如T obj2= obj;或 T obj2{obj};

移动构造函数:
功能:用于创建一个对象,该对象通过获取另一个同类型对象的资源来初始化,通常用于移动语义,以避免不必要的拷贝。
调用时机:当一个对象被一个临时对象或右值引用初始化时,如 T obj2(std::move(obj));。

问题3:

可以编译运行。

实验任务二:

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <string>
#include <iostream>
using namespace std;

class Complex {
public:
    static const string doc; 

    
    Complex(); 
    Complex(double real); 
    Complex(double real, double imag); 
    Complex(const Complex& other); 

   
    double get_real() const; 
    double get_imag() const; 
    void add(const Complex& other); 


   
    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);
    friend double abs(const Complex& c);
    private:
        double real;
        double imag;
};

#endif 

Complex.cpp

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

using namespace std;

const string Complex::doc="a simplified complex class";

Complex::Complex():real{0},imag{0}{}

Complex::Complex(double real):real{real},imag{0}{}

Complex::Complex(double real, double imag):real{real},imag{imag}{};

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

double Complex::get_real() const{
return real;}
double Complex::get_imag() const{
return imag;}
void Complex::add(const Complex& other){
    real+=other.real;
    imag+=other.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){
    if(c1.real==c2.real&&c1.imag==c2.imag)
    return true;
    return false;
}
bool is_not_equal(const Complex& c1, const Complex& c2){
    if(c1.real==c2.real&&c1.imag==c2.imag)
    return false;
    return true;
}
void output(const Complex& c){
    if(c.imag>=0)
    cout<<c.real<<"+"<<c.imag<<"i"<<endl;
    else
    cout<<c.real<<c.imag<<"i"<<endl;
}
double abs(const Complex& c){
    return (sqrt(c.real*c.real+c.imag*c.imag));
}

task2.cpp

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

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

运行结果:

实验任务三:

 

 #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<double> c1;:默认构造函数,创建了一个实部和虚部都为0的复数。
  • complex<double> c2(3, -4);:参数化构造函数,创建了一个实部为3,虚部为-4的复数。
  • const complex<double> c3(3.5);:参数化构造函数,创建了一个实部为3.5,虚部为0的复数。
  • complex<double> c4(c3);:拷贝构造函数,创建了一个与c3相同的复数

c4.real和c4.imag返回了复数的实部和虚部。

abs(c2)是用来计算复数的模。

c1+c2是复数的加法运算。

boolalpha:用于控制bool值的输出格式,使得在输出比较结果时,直接显示truefalse,而不是10

实习任务四:

Fraction.h

#pragma once
#include<iostream>
#include<string>
using namespace std;


class Fraction{
    public:
        static const string doc;
        Fraction(int up);
        Fraction(int up, int down);
        Fraction(const Fraction &other);
        int get_up() const;
        int get_down() const;
        Fraction negative() const;
        
        friend void output(const Fraction& f1);
        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 simplify();
        int gcd(int a,int b);
};

Fraction.cpp

#include"Fraction.h"
#include<string>
#include<iostream>
using namespace std;

const string Fraction::doc{"Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算."};
Fraction::Fraction(int up):up{up},down{1}{simplify();}
Fraction::Fraction(int up,int down):up{up},down{down}{
    if (down == 0) {
        cout<<"分母不能为0"<<endl;
    }
    simplify();}
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);
}
int Fraction::gcd(int a,int b){
    while(b!=0){
        int r=a%b;
        a=b;
        b=r;
    }
    return a;
}
void Fraction::simplify(){
    if(down<0)
    {
        up=-up;
        down=-down;
    }
    int k=gcd(up,down);
    up=up/k;
    down=down/k;
}
void output(const Fraction& f1){
    if(f1.get_down()==0){
        return;
    }
    if(f1.get_up()==0)
    cout<<0<<endl;
    else if(f1.get_down()==1)
    cout<<f1.get_up()<<endl;
    else
    cout<<f1.get_up()<<"/"<<f1.get_down()<<endl;
}
Fraction add(const Fraction& f1,const Fraction& f2){
    return Fraction(f1.get_up() * f2.get_down() + f2.get_up() * f1.get_down(), f1.get_down() * f2.get_down());
}
Fraction sub(const Fraction& f1,const Fraction& f2){
    return Fraction(f1.get_up() * f2.get_down() - f2.get_up() * f1.get_down(), f1.get_down() * f2.get_down());
}
Fraction mul(const Fraction& f1,const Fraction& f2){
    return Fraction(f1.get_up() * f2.get_up(), f1.get_down() * f2.get_down());
}
Fraction div(const Fraction& f1,const Fraction& f2){
    return Fraction(f1.get_up() * f2.get_down(), f1.get_down() * f2.get_up());
}

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

运行结果:

实验任务五:

account.h

// account.cpp : Defines the entry point for the console application.



#ifndef _ACCOUNT_H_

#define _ACCOUNT_H_

class SavingsAccount //储蓄账户类

{

private:

    int id; //账号

    double balance; //余额

    double rate; //存款的年利率

    int lastDate; //上次变更余额的日期

    double accumulation; //余额按日累加之和

    static double total; //所有账户的总金额

                         //记录一笔账,date为日期,desc为说明

    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); //取出现金

                                            //结算利息,每年1月1日调用一次该函数

    void settle(int date);

    //显示账户信息

    void show() const;

};

#endif//_ACCOUNT_H_

account.cpp

//account.cpp

#include"account.h"

#include<cmath>

#include<iostream>

using namespace std;

double SavingsAccount::total = 0;

//SavingsAccount类相关成员函数的实现

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:not 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;

}

5.11.cpp

#include"account.h"
 
#include<iostream>
#include"account.cpp"
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);
 
    //开户后第90天到了银行的计息日,结算所有账户的年息
 
    sa0.settle(90);
 
    sa1.settle(90);
 
    //输出各个账户信息
 
    sa0.show(); cout << endl;
 
    sa1.show(); cout << endl;
 
    cout << "Total:" << SavingsAccount::getTotal() << endl;
 
    return 0;
 
}

 

 

标签:const,int,double,Complex,实验,Fraction,include
From: https://www.cnblogs.com/huashu1/p/18504351

相关文章

  • 20222425 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    202224252024-2025-1《网络与系统攻防技术》实验三实验报告目录1.实验内容2.实验问题3.实验过程3.1正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧3.2通过组合应用各种技术实现恶意代码免杀3.3用另一电脑实测,在杀软开启的情况下,可运行并回连成功,......
  • HNU-操作系统实验Lab5-2022级
    实验目的深刻理解中断的原理和机制,掌握CPU访问中断控制器的方法,掌握Arm体系结构的中断机制和规范,实现时钟中断服务和部分异常处理等。实验过程前言中断是一种硬件机制。借助于中断,CPU可以不必再采用轮询这种低效的方式访问外部设备。将所有的外部设备与CPU直接相连是不......
  • 实验2 类和对象_基础编程
    1.实验任务一:t.h:#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数vo......
  • 实验三
    实验一代码#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:......
  • 实验3
    1#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11printf("分......
  • 实验3
    任务一源代码1#include<stdio.h>2charscore_to_grade(intscore);3intmain(){4intscore;5chargrade;6while(scanf("%d",&score)!=EOF){7grade=score_to_grade(score);8printf("分数:%d,等级:%c\n\n",score,grade......
  • 实验1
    Task1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>usin......
  • 实验3
    #include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:%c\n\......
  • 实验2(2)
    Task1://t.hpragmaonceinclude//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);~T();//移动构造函数//析构函数voidadjust(intratio);voiddisplay()const;private:int......
  • 实验干货|电流型霍尔传感器采样设计03-信号调理
    在前两篇博客中,将霍尔输出的电流信号转换成了有正有负的电压信号,但是DSP需要采集0~3V的电压信号,因此需要对信号缩放并抬升至全部为正的信号。常见的方法是,通过比例放大(缩小)电路对信号进行放缩,通过加法电路抬升基准电平。这里分为两步,首先设计基准电平。设计基准电平DSP的A......