首页 > 其他分享 >实验2

实验2

时间:2024-10-23 08:48:05浏览次数:7  
标签:const int double Complex 实验 Fraction include

实验任务1

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

main.cpp

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

using namespace std;

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 object`max count:"<<T::max_cnt<<endl;
    cout<<"T object`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(move(t2));
    cout<<"t3=";t4.display();cout<<endl;
    
    cout<<"T objects`current count:"<<T::get_cnt()<<endl;
    
    func();
}

实验任务2

Complex.h

class Complex{

public:
    static string doc;
    Complex(double r=0,double i=0);
    Complex(const Complex &c);
    double get_real();
    double get_imag();
    void add(Complex &c);
    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 &c1);
    friend double abs(const Complex &c1);
private:
    double real;
    double imag;
    
};

#endif

Complex.cpp

#include <bits/stdc++.h>
#include "Complex.h"
using namespace std;

string Complex::doc="a simplified complex class";
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(){return real;}
double Complex::get_imag(){return imag;}
void Complex::add(Complex &c){
    real+=c.real;
    imag+=c.imag;
}
Complex add(const Complex &c1,const Complex &c2)
{
    Complex c3;
    c3.real=c1.real+c2.real;
    c3.imag=c1.imag+c2.imag;
    return c3;
}

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 &c1)
{
    cout<<c1.real<<" + "<<c1.imag<<"i"<<endl;
}

double abs(const Complex &c1)
{
    return sqrt(c1.real*c1.real+c1.imag*c1.imag);
}

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 namespace std;

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

实验任务4

Fraction.h

#pragma once
#include<string>

class Fraction{
private:
    int up;
    int down;

public:
    Fraction(int u,int d=1);
    Fraction(const Fraction &f);
    
    static const std::string doc;
    int get_up();
    int get_down();
    Fraction negative();
    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);
};
void output(const Fraction f);
Fraction add(const Fraction f1,const Fraction f2);
Fraction sub(const Fraction f1,const Fraction f2);
Fraction mul(const Fraction f1,const Fraction f2);
Fraction div(const Fraction f1,const Fraction f2);

Fraction.cpp

#include <bits/stdc++.h>
#include "Fraction.h"
using namespace std;

const string Fraction::doc="Fraction类 v 0.01版.\n目前仅支持分数对象的构造,输出,加/减/乘/除运算.\n";
Fraction::Fraction(int u,int d):up{u},down{d}{}
Fraction::Fraction(const Fraction &f):up{f.up},down{f.down}{}
int Fraction::get_up()
{
    int a,b;
    a=abs(up);b=abs(down);
    
    while (b)
    {
        int tmp=a;
        a=b;
        b=tmp%b;
    }
    return up/a;
}
int Fraction::get_down()
{
    int a,b;
    a=abs(up);b=abs(down);
    
    while (b)
    {
        int tmp=a;
        a=b;
        b=tmp%b;
    }
    return down/a;
}
Fraction Fraction::negative()
{
    int new_up=-up;
    Fraction f(new_up,down);
    return f;
}

void output(const Fraction f)
{
    if (f.down==0)
    {
        cout<<"分母不能为0";
        return ;
    }
    
    int a,b;
    a=abs(f.up);b=abs(f.down);
    
    while (b)
    {
        int tmp=a;
        a=b;
        b=tmp%b;
    }
    int new_down=f.down/a;
    if (new_down==1)
        cout<<f.up/a;
    else if (f.up*f.down>0)
        cout<<abs(f.up)/a<<"/"<<abs(f.down)/a;
    else 
        cout<<"-"<<abs(f.up)/a<<"/"<<abs(f.down)/a;
}

Fraction add(const Fraction f1,const Fraction f2)
{
    int new_down=f1.down*f2.down;
    int new_up=f1.up*f2.down+f2.up*f1.down;
    int a,b;
    a=abs(new_up);b=abs(new_down);
    while (b)
    {
        int tmp=a;
        a=b;
        b=tmp%b;
    }
    new_up/=a;new_down/=a;
    Fraction f(new_up,new_down);
    return f;
}

Fraction sub(Fraction f1,Fraction f2)
{
    int new_down=f1.down*f2.down;
    int new_up=f1.up*f2.down-f2.up*f1.down;
    int a,b;
    a=abs(new_up);b=abs(new_down);
    while (b)
    {
        int tmp=a;
        a=b;
        b=tmp%b;
    }
    new_up/=a;new_down/=a;
    Fraction f(new_up,new_down);
    return f;
}

Fraction mul(Fraction f1,Fraction f2)
{
    int new_up=f1.up*f2.up;
    int new_down=f1.down*f2.down;
    int a,b;
    a=abs(new_up);b=abs(new_down);
    while (b)
    {
        int tmp=a;
        a=b;
        b=tmp%b;
    }
    new_up/=a;new_down/=a;
    Fraction f(new_up,new_down);
    return f;
}

Fraction div(Fraction f1,Fraction f2)
{
    
    int new_up=f1.up*f2.down;
    int new_down=f1.down*f2.up;
    int a,b;
    a=abs(new_up);b=abs(new_down);
    while (b)
    {
        int tmp=a;
        a=b;
        b=tmp%b;
    }
    new_up/=a;new_down/=a;
    Fraction f(new_up,new_down);
    return f;
}

main.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__

class SavingAccount{
private:
    int id;
    double balance;
    double rate;
    double lastData;
    double accumulation;
    
    static double total;
    
    void record(int data,double amount);
    double accumulate(int data) const
    {
        return accumulation+balance*(data-lastData);
    }
public:
    SavingAccount(int data,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 data,double amount);
    void withdraw(int data,double amount);
    void settle(int data);
    void show() const;
}; 
#endif

account.cpp

#include <bits/stdc++.h>
#include "account.h"
using namespace std;

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

void SavingAccount::deposit(int data,double amount)
{
    record(data,amount);
}

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

void SavingAccount::settle(int data)
{
    double interest=accumulate(data)*rate/365;
    if (interest!=0)
        record(data,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.deposit(60,4000);
    
    sa0.settle(90);
    sa1.settle(90);
    
    sa0.show();cout<<endl;
    sa1.show();cout<<endl;
    cout<<"Total:"<<SavingAccount::getTotal()<<endl;
    return 0;
}

 

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

相关文章

  • 计算智能——基于模糊逻辑的自动控制系统——上机实验二
    一、实验目的        自动控制系统需要根据设备内温度、设备内湿度决定设备的运转时间。其中,温度的论域是[0℃,100℃],有3个模糊标记:低、中、高;湿度的论域是[0%,60%],有3个模糊标记:小、中、大;运转时间论域是[0s,1000s],有3个模糊标记:短、中、长。这些模糊标记在模糊......
  • 实验三 C语言函数应用编程
    任务一#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:%c\n......
  • 20222306 2024-2025-1《网络与系统攻防技术》实验三实验报告
    1.实验内容1.1实践任务(1)正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧(2)通过组合应用各种技术实现恶意代码免杀(3)用另一电脑实测,在杀软开启的情况下,可运行并回连成功,注明电脑的杀软名称与版本1.2问题回答(1)杀软是如何检测出恶意代码的?杀软检测......
  • 20222304 2024-2025-1 《网络与系统攻防技术》实验二实验报告
    一、实验内容1.1知识回顾堆栈结构和堆栈变化EIP:存储下一条指令;EBP:栈底指针;ESP:栈顶指针。栈溢出的三种方法:修改栈中邻接变量;修改函数返回地址;代码植入。shellcode构建RET返回地址;NOP空(0X90);shellcode调用shell;NSR模式;RNS模式;RS模式缓冲区溢出的防范技术源程序检查:静态检......
  • 生成对抗网络模拟缺失数据,辅助PAMAP2数据集仿真实验
    PAMAP2数据集是一个包含丰富身体活动信息的数据集,它为我们提供了一个理想的平台来开发和测试HAR模型。本文将从数据集的基本介绍开始,逐步引导大家通过数据分割、预处理、模型训练,到最终的性能评估,在接下来的章节中,我们将详细介绍PAMAP2数据集的特点、数据预处理的关键步骤......
  • 操作系统实验之Windows中的线程与线程同步现象
    一、实验目的1.掌握Windows中线程的操作。2.熟悉线程不同步时的现象及环境因素。3.掌握一种同步对象的使用。二、实验理论基础及教材对应关系1.线程和线程不同步的认识。2.线程间的同步和通信。3.本实验内容主要对应于操作系统教材第2章中关于线程的各节。三、实验内容与步......
  • 20222426 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    1.实验内容·免杀原理免杀技术的核心原理是通过修改病毒、木马的内容,改变其特征码,从而躲避杀毒软件的查杀。杀毒软件通常使用特征码识别技术来检测和清除恶意软件,因此,通过修改恶意软件的特征码,可以使其绕过杀毒软件的检测。·免杀技术1.修改特征码。·直接修改:将特征码所对应......
  • 20222321 2024-2025-1 《网络与系统攻防技术》实验二实验报告
    一.实验内容(1)使用netcat获取主机操作Shell,cron启动某项任务(任务自定)(2)使用socat获取主机操作Shell,任务计划启动(3)使用MSFmeterpreter(或其他软件)生成可执行文件,利用ncat或socat传送到主机并运行获取主机Shell(4)使用MSFmeterpreter(或其他软件)生成获取目标主机音频、摄......
  • 20222406 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    202224062024-2025-1《网络与系统攻防技术》实验三实验报告1.实验内容1.1实践内容正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧通过组合应用各种技术实现恶意代码免杀用另一电脑实测,在杀软开启的情况下,可运行并回连成功,注明电脑的杀软名称与版本......
  • 20222427 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    1.实践内容1.1本周知识总结深入学习关于缓冲区溢出的基础知识。学习了关于后门的一些基础知识。1.2回答问题(1)杀软是如何检测出恶意代码的?病毒特征码检测加密文件分析基于行为检测的主动防御病毒云查杀(2)免杀是做什么?免杀,即Anti-AntiVirus(简写VirusAV......