首页 > 其他分享 >实验2

实验2

时间:2024-10-23 15:48:47浏览次数:1  
标签:std const int Complex 实验 Fraction include

x.h

#include <iostream>
#include <math.h>
#include <string>

using namespace std;
using std::string;

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

        static int get_cnt();   // 显示当前T类对象总数
        static const std::string doc; // 类T的描述信息
        static const int max_cnt; // 类T对象上限
        void adjust(int ratio); // 按系数成倍调整数据
        void display() const;   // 以(m1, m2)形式显示T类对象信息
        
    private:
        int m1, m2;
        static int cnt; // 当前T类对象数目

        friend void func();
};

void func();

x.cpp

#include <iostream>
#include <math.h>
#include <string>
#include "x.h"

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

main.cpp

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

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

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

析构函数在文件最后被调用

不能

 

 

x.h

#include <iostream>
#include <math.h>
#include <string>

using namespace std;
using std::string;

class Complex
{
    private:
        double r;
        double i;
        
    public:
        static const string doc;
        
        Complex(double A=0,double B=0);
        Complex(const Complex &t);
        ~Complex();
        
        double get_real();
        double get_imag();
        void add(const Complex &t);
        
        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 &a);
        friend double abs(const Complex &a);
};

x.cpp

#include <iostream>
#include <math.h>
#include <string>
#include "x.h"

using namespace std;
using std::string;

const string Complex::doc = "a simplified Complex class";
//----------------------------------------------------------------
Complex::Complex(double A,double B): r{A},i{B} {}
Complex::Complex(const Complex &t): r{t.r},i{t.i} {}
Complex::~Complex() {}

double Complex::get_real()  {  return r;  }
double Complex::get_imag()  {  return i;  }
void Complex::add(const Complex &t) {  r+=t.r;  i+=t.i;  }
//------------------------------------------------------------------ 
Complex add(const Complex &a,const Complex &b)
{
    double dr = a.r + b.r;
    double di = a.i + b.i;
    
    return Complex(dr,di);
}

bool is_equal(const Complex &a,const Complex &b)
{
    if( a.r==b.r && a.i==b.i)
        return true;
    else
        return false;
}

bool is_not_equal(const Complex &a,const Complex &b)
{
    if( a.r==b.r && a.i==b.i)
        return false;
    else
        return true;
}

void output(const Complex &a)
{
    if(a.i>=0)
        cout << a.r << " + " << a.i << "i";
    else
        cout << a.r << " - " << -a.i << "i";
}

double abs(const Complex &a)
{
    return sqrt(a.r*a.r+a.i*a.i);
}

main.cpp

#include <iostream>
#include "x.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();
}

#include <iostream>
#include <complex> //引用标准库模板类complex 

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

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

 x.h

#include <iostream>
#include <math.h>
#include <string>

using namespace std;
using std::string;

class Fraction
{
    private:
        int up;
        int down;
        
    public:
        static const string doc;
        
        Fraction(int A,int B=1);
        Fraction(const Fraction &t);
        ~Fraction();
        
        int get_up();
        int get_down();
        Fraction negative();
        
        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);
        
};

x.cpp

#include <iostream>
#include <math.h>
#include <string>
#include "x.h"

using namespace std;
using std::string;

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

Fraction::Fraction(int A,int B): up{A},down{B} 
{
    if(down<0 && up>0)
    {
        down = -down;
        up = -up;
        int n = (-up)<down? (-up):down;
        for(n-=1; n>1; n--)
            if(up%n==0 && down%n==0)
            {    up/=n;    down/=n;   }
    }
    else if(down<0 && up<0)
    {
        down = -down;
        up = -up;
        int n = up<down? up:down;
        for(n-=1; n>1; n--)
            if(up%n==0 && down%n==0)
            {    up/=n;    down/=n;   }
    }
    else
    {
        int n = (-up)<down? (-up):down;
        for(n-=1; n>1; n--)
            if(up%n==0 && down%n==0)
            {    up/=n;    down/=n;   }
    }
}
Fraction::Fraction(const Fraction &t): up{t.up},down{t.down} {}
Fraction::~Fraction() {}

int Fraction::get_up()  {  return up;  }
int Fraction::get_down()  {  return down;  }
Fraction Fraction::negative()
{
    int Nup,Ndown;
    Nup = -up;
    Ndown = down;
    
    return Fraction(Nup,Ndown);
}

void output(const Fraction &a)
{
    if(a.down==0)
        cout << "分母不能为0";
    else if(a.up==0)
        cout << "0";
    else if(a.down==1)
        cout << a.up;
    else 
        cout << a.up << "/" << a.down;
}
Fraction add(const Fraction &a,const Fraction &b)
{
    int dup = a.up*b.down + b.up*a.down;
    int ddown = a.down*b.down;
    
    return Fraction(dup,ddown);
}
Fraction sub(const Fraction &a,const Fraction &b)
{
    int dup = a.up*b.down - b.up*a.down;
    int ddown = a.down*b.down;
    
    return Fraction(dup,ddown);
}
Fraction mul(const Fraction &a,const Fraction &b)
{
    int dup = a.up*b.up;
    int ddown = a.down*b.down;
    
    return Fraction(dup,ddown);
}
Fraction div(const Fraction &a,const Fraction &b)
{
    int dup = a.up*b.down;
    int ddown = a.down*b.up;
    
    return Fraction(dup,ddown);
}
/*
*/

main.cpp

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

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

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

x.h

#include <iostream>
#include <math.h>
#include <string>

using namespace std;
using std::string;

class SavingsAccount {
    private:
        int id;//账号
        double balance;//余额
        double rate;//存款的年利率
        int lastDate;//上次变更余额的时期
        double accumulation;//余额按日累加之和
        static double total;//所有账户的总金额
        
        //记录一笔账,date为日期,amount为金额,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;
};

x.cpp

#include <iostream>
#include <cmath>
#include <string>
#include "x.h"

using namespace std;
using std::string;

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

main.cpp

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

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

int main() { 
    //建立几个账户
    SavingsAccount sa0(1, 21325302, 0.015);
    SavingsAccount sal(1, 58320212, 0.015);
    //几笔账目
    sa0.deposit(5, 5000);
    sal.deposit(25, 10000);
    sa0.deposit(45, 5500);
    sal.withdraw(60, 4000);
    //开户后第90天到了银行的计息日,结算所有账户的年息
    sa0.settle(90);
    sal.settle(90);
    //输出各个账户信息
    sa0.show(); cout<<endl;
    sal.show(); cout<<endl;
    cout<<"Total: "<<SavingsAccount::getTotal()<<endl;
    return 0;
}

 

标签:std,const,int,Complex,实验,Fraction,include
From: https://www.cnblogs.com/5464665465L/p/18494470

相关文章

  • 实验2
    任务1:t.h代码:#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数voida......
  • 实验2 类和对象 基础编程1
    1,实验任务1t.cpp1//类T:实现2//普通函数实现34#include"t.h"5#include<iostream>6#include<string>78usingstd::cout;9usingstd::endl;10usingstd::string;1112//static成员数据类外初始化13conststd::stringT::doc{"......
  • 20222310 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    一、实验内容1.正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧(1)正确使用msf编码器,使用msfvenom生成如jar之类的其他文件(2)学会使用veil,加壳工具(3)能够使用C+shellcode编程2.通过组合应用各种技术实现恶意代码免杀成功实现了免杀的,简单语言描述原理,不......
  • 实验2
    实验任务1t.h代码点击查看代码#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构......
  • 实验二
    任务一:代码:t.h:1#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数12......
  • 实验二
    1.实验任务11#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数12~T(......
  • 实验十八 电子和场
            ......
  • 实验十七 铁磁材料的磁滞回线和基本磁化曲线
            ......
  • 模拟 DDoS 攻击与防御实验
            模拟DDoS攻击与防御实验可以帮助理解攻击原理和防御策略。在进行这种实验时,必须确保在受控、合法的环境中进行,避免对真实网络造成损害。以下是具体步骤:环境要求硬件:至少两台计算机(或虚拟机),一台作为目标服务器,一台或多台作为攻击源。软件:Web服务器(如A......
  • opp实验二
    任务一1#include<iostream>2#include<math.h>3#include"Complex.h"4usingnamespacestd;5Complex::stringdoc={"asimpleclass"};67Complex::Complex(doubler=0,doublei=0):real(r),imag(i){}8Complex::Complex(......