首页 > 其他分享 >实验2

实验2

时间:2024-10-23 14:33:58浏览次数:1  
标签:std const int Complex 实验 Fraction include

实验任务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.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:不能。

可能是在类中的声明只包含了关于该函数与类T间的关系,需要额外单独声明
问题2:
line9:根据给与的两个参数构造对象,默认值为0,0
line10:复制构造已有的同类对象
line11:将已有的对象移动到新构造的对象中
line12:对象生命周期结束后调用析构函数,清理对象
问题3:不能正常编译

实验任务2

Complex.h代码

点击查看代码
#include<string>

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

Complex.cpp代码

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


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

const 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() const{
	return real;
}

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


void Complex::add(const 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;
	}
	else{
		return false;
	}
}

bool is_not_equal(const Complex &c1,const Complex &c2){
	if(c1.real!=c2.real||c1.imag!=c2.imag){
		return true;
	}
	else{
		return false;
	}
}

void output(const Complex &c){
	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();
}

运行结果:

实验任务3

task3.cpp代码

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

运行结果:

实验任务4

Fraction.h代码

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

class Fraction{
public:
	Fraction(int u,int d=1);
	Fraction(const Fraction &f);
	
	int get_up() const;
	int get_down() const;
	Fraction negative();
	
	static const std::string doc;
	
	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,down;
};

int getgongin(int m1,int m2);

Fraction.cpp代码

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

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

const string Fraction::doc="Fraction类 v 0.01版";

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() const{
	return up;
}

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

Fraction Fraction::negative(){
	Fraction ff(0,0);
	ff.up=up*-1;
	ff.down=down;
	return ff;
}

int getgongin(int m1,int m2){
	int a=abs(m1);
	int b=abs(m2);
	int t;
	if(a<b){
		t=a;
		a=b;
		b=t;
	}
	while(b>0){
		t=a%b;
		a=b;
		b=t;
	}
	return a;
}

void output(const Fraction &f){
	if(f.down==0){
		cout<<" 分母不能为0"<<endl;
	}
	else if(f.up==0){
		cout<<"0"<<endl;
	}
	else{
		int a=getgongin(f.up,f.down);
		cout<<f.up/a<<"/"<<f.down/a<<endl;
	}
	
}

Fraction add(const Fraction &f1,const Fraction &f2){
	Fraction f3(0,0);
	int a=getgongin(f1.down,f2.down);
	int b=f1.down*f2.down/a;
	int c=f1.up*(b/f1.down)+f2.up*(b/f2.down);
	a=getgongin(c,b);
	f3.up=c/a;
	f3.down=b/a;
	if(f3.up<0&&f3.down<0){
		f3.up=abs(f3.up);
		f3.down=abs(f3.down);
	}
	return f3;
}

Fraction sub(const Fraction &f1,const Fraction &f2){
	Fraction f3(0,0);
	int a=getgongin(f1.down,f2.down);
	int b=f1.down*f2.down/a;
	int c=f1.up*(b/f1.down)-f2.up*(b/f2.down);
	a=getgongin(c,b);
	f3.up=c/a;
	f3.down=b/a;
	if(f3.up<0&&f3.down<0){
		f3.up=abs(f3.up);
		f3.down=abs(f3.down);
	}
	return f3;
}

Fraction mul(const Fraction &f1,const Fraction &f2){
	Fraction f3(0,0);
	f3.up=f1.up*f2.up;
	f3.down=f1.down*f2.down;
	int a=getgongin(f3.up,f3.down);
	f3.up/=a;
	f3.down/=a;
	if(f3.up<0&&f3.down<0){
		f3.up=abs(f3.up);
		f3.down=abs(f3.down);
	}
	
	return f3;
}

Fraction div(const Fraction &f1,const Fraction &f2){
	Fraction f3(0,0);
	f3.up=f1.up*f2.down;
	f3.down=f1.down*f2.up;
	int a=getgongin(f3.up,f3.down);
	f3.up/=a;
	f3.down/=a;
	if(f3.up<0&&f3.down<0){
		f3.up=abs(f3.up);
		f3.down=abs(f3.down);
	}
	
	return f3;
}

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

运行结果:

实验任务5

account.h代码

点击查看代码
#include<iostream>

class SavingsAccount{
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:
	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 rate,double amount);
	void withdraw(int rate,double amount);
	void settle(int date);
	void show() const;
	
};

account.cpp代码

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

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

double SavingsAccount::total=0;

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<<"Erroe: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>

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);
	
	sa0.settle(90);
	sa1.settle(90);
	
	sa0.show();cout<<endl;
	sa1.show();cout<<endl;
	
	cout<<"Total:"<<SavingsAccount::getTotal()<<endl;
	
	return 0;
}

运行结果:

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

相关文章

  • 实验二
    任务一:代码: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(......
  • 计算机网络实验——华为eNSP模拟器常用命令总结
    计算机网络实验——华为eNSP模拟器常用命令总结在进行计算机网络实验时,华为eNSP(EnterpriseNetworkSimulationPlatform)模拟器是一个功能强大的工具,它允许用户模拟和管理虚拟网络设备。通过熟悉并掌握eNSP中的常用命令,我们可以更有效地进行网络配置、故障排查和性能测试。......
  • 实验2
    实验任务1t.h#pragmaonce#include<string>classT{public:T(intx=0,inty=0);T(constT&t);T(T&&t);~T();voidadjust(intradio);voiddisplay()const;private:intm1,m2;public:staticintget......
  • 计算智能——基于模糊逻辑的自动控制系统——上机实验二
    一、实验目的        自动控制系统需要根据设备内温度、设备内湿度决定设备的运转时间。其中,温度的论域是[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......