c++程序设计基础实验2

news/2025/3/15 4:55:25/文章来源:https://www.cnblogs.com/1218xzw/p/18498426

任务1:

测试代码:

task1.cpp

#pragma once
#include <string>
// 类T: 声明
class T {
// 对象属性、方法
public:
T(int x = 0, int y = 0); // 普通构造函数
T(const T &t); // 复制构造函数
T(T &&t); // 移动构造函数
~T(); // 析构函数
t.cpp
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
#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;
}
test.cpp
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:

  去掉后不能正常编译运行,报错信息截图如下:

 可能的原因是:虽然在类中声明了func()函数是类T的友元函数,但这仅仅是阐述了二者之间的关系,如果在类外不去声明func()函数,则相当于该函数没有被声明,函数的声明和定义部分并不完整,则函数无法被调用。

问题2:

  1. 默认构造函数:不带参数,用于初始化对象的默认状态。对象在栈上或动态分配时自动调用。

  2. 参数化构造函数:接受参数以初始化对象的特定状态。在通过参数创建对象时调用,例如 T obj(42);

  3. 拷贝构造函数:用于通过已有对象创建新对象,通常用于深拷贝。在对象作为参数传递(非引用)、从对象返回或赋值时调用。

  4. 移动构造函数(C++11 及之后):用于将资源从临时对象转移到新对象,提高效率。当对象被移动时调用。

析构函数用于释放对象的资源和执行清理工作。它在对象生命周期结束时自动调用。

问题3:
不能够实现,报错信息截图如下:

 

因为static修饰的静态变量在头文件中只能声明,不能够定义。如果在头文件中定义可能会导致多重定义错误。

任务2:

代码:

头文件Complex.h:

#pragma once 
#include <iostream>
#include <string>
#include <cmath>using namespace std;class Complex {friend Complex add(const Complex &, const Complex &);friend bool is_equal(const Complex &, const Complex &);friend bool is_not_equal(const Complex &, const Complex &);friend double abs(const Complex &);friend void output(const Complex &);
public:Complex(double real = 0.0, double image = 0.0) : real_{real}, image_{image} {};Complex(const Complex &other) : real_{other.real_}, image_{other.image_} {};double get_real() const;double get_image() const;Complex add(Complex &other);static string doc;double real_;double image_;}; 

Complex类定义文件Complex.cpp:

#include <iostream>
#include "Complex.h"double Complex::get_real() const {return real_;
}double Complex::get_image() const {return image_;
}Complex Complex::add(Complex &other) {Complex::real_+=other.real_;Complex::image_+=other.image_;
}Complex add(const Complex &c1, const Complex &c2) {return Complex(c1.real_ + c2.real_, c1.image_ + c2.image_);
}bool is_equal(const Complex &c1, const Complex &c2) {return c1.real_ == c2.real_ && c1.image_ == c2.image_;
}bool is_not_equal(const Complex &c1, const Complex &c2) {return !is_equal(c1, c2);
}double abs(const Complex &c) {return sqrt(c.real_ * c.real_ + c.image_ * c.image_);
}void output(const Complex &c){if(c.image_>=0)cout<<c.real_<<"+"<<c.image_<<"i"<<endl;elsecout<<c.real_<<c.image_<<"i"<<endl;
}
std::string Complex::doc = "a simplified Complex class"; 

测试文件test.cpp:

// 待补足(多文件组织代码时,需要包含的头文件)
#include <iostream>
#include "Complex.h"
using namespace std;
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_image() << 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 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类的哪些接口:

         构造函数(默认、带参数、拷贝构造)

         成员函数(real() 和 imag()

         全局函数(abs()

         运算符重载(+===!=+

         这些接口共同实现了对复数的创建、运算、比较及获取属性的功能。

对比任务2:
  思考1:在标准库的complex类中,复数的输出附带了()括号和逗号,使复数的输出变得更加明了,同时也不需要像自定义Complex类通过cout输出流来访问Complex类中的类成员real_,image_,然后再人为加上+,—号来输出复数,可以直接输出complex类的对象来输出复数。在复数的运算时,利用标准库complex类的函数,省去了赋值等步骤,减少了测试文件的代码数量。

  思考2:标准库的complex类应该重载了构造函数,可以初始化不同数据类型的complex类,然后将complex类的输入和输出都加上了括号和逗号。使得在测试文件中加减和是否相等等验证代码变得更加简洁。

 任务4:

源代码:

Fraction.h:

#include <iostream>
#include <string>using namespace std;class Fraction {friend void output(const Fraction &);friend Fraction sub(const Fraction &f1, const Fraction &f2);friend Fraction mul(const Fraction &f1, const Fraction &f2);friend Fraction add(const Fraction &f1, const Fraction &f2);friend Fraction div(const Fraction &f1, const Fraction &f2);public:Fraction(int up, int down=1) : Up{up}, Down{down} {simplify();}Fraction(const Fraction &other) : Up{other.Up}, Down{other.Down} {}int get_up() const { return Up; }int get_down() const { return Down; }Fraction negative() const { return Fraction(-Up, Down); }static string doc;
private:int Up;int Down;void simplify();
};

Fraction.cpp:

#include <iostream>
#include "Fraction.h"
using namespace std;string Fraction::doc = "Fraction类 v 0.01版. \n目前仅支持分数对象的构造、输出、加/减/乘/除运算。";int gcd(int a, int b) {while (b != 0) {int temp = b;b = a % b;a = temp;}return a;
}void Fraction::simplify() {if (Up == 0) { Down = 1; return;}if (Down < 0) {Down = -Down;Up = -Up;}int divisor = gcd(abs(Up), abs(Down));Up /= divisor;Down /= divisor;
}void output(const Fraction &f) {if(f.Down!=1){std::cout << f.get_up() << "/" << f.get_down() << endl;}else{std::cout << f.get_up() << 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 add(f1, f2.negative());
}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) {if (f2.get_up() == 0) {cout << "除数不能为零" << endl;system("pause");}return mul(f1, Fraction(f2.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();
}

 

运行结果截图:

 

 

任务5:
源代码:

account.h:

#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__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 date,double amount);void withdraw(int date,double amount);void settle(int date);void show()const;
};
#endif//__ACCOUNT_H__

account.cpp:

#include"account.h"
#include<cmath>
#include<iostream>
using namespace std;
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<<"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;
}

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

  

运行结果截图:

 改进建议:

  1. 数据成员的访问控制

    • 缺点:虽然 idbalancerate, 和 lastDate 是私有的,但可能需要更多的保护措施,尤其是 balance 和 accumulation
    • 改进:可以考虑引入友元函数或其他安全机制,以确保对这些字段的访问受到限制。
  2. 日期处理

    • 缺点:使用 int 来表示日期不是最理想的,因为日期的处理可能会引入复杂性,例如跨月和跨年问题。
    • 改进:可以考虑使用标准库中的日期类型,例如 std::chrono 或自定义日期类来更好地管理日期。
  3. 累积利息的计算

    • 缺点:accumulate 函数和 settle 函数的关系不够明确。每次调用 settle 时,可能需要更新 accumulation 和 lastDate,而目前的设计中没有清晰地表现出这一点。
    • 改进:在 settle 函数中明确更新 accumulation 和 lastDate 的逻辑,使其更清晰。
  4. 异常处理

    • 缺点:deposit 和 withdraw 函数没有处理负数存款或取款超出余额的情况。
    • 改进:可以引入异常处理机制,抛出异常或返回错误代码,以增强程序的健壮性。
  5. 静态成员的管理

    • 缺点:static double total 用于跟踪所有账户的总余额,但没有线程安全的机制,可能在多线程环境中引发问题。
    • 改进:可以考虑使用互斥量(mutex)来保护对 total 的访问。
  6. 接口的灵活性

    • 缺点:当前的接口较为固定,扩展性有限。
    • 改进:可以考虑使用策略模式或模板方法模式,以便更灵活地处理不同的存款和取款策略。
  7. 缺少拷贝构造函数和赋值运算符

    • 缺点:如果需要拷贝 SavingsAccount 对象,编译器生成的默认拷贝构造函数和赋值运算符可能不足以满足需求。
    • 改进:可以实现自定义的拷贝构造函数和赋值运算符,以控制对象的拷贝行为。
  8. 缺少接口文档

    • 缺点:缺乏详细的文档和注释,使得类的使用和维护变得困难。
    • 改进:可以添加函数的详细注释和使用示例,提升代码可读性和可维护性。

  

  

  

  

  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/822583.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JAVA第一次博客作业

一、前言总结三次题目集的知识点、题量、难度等情况这三次题目集的最后一道大作业是一系列递进的编程作业。 第一次作业 答题判题程序-1  设计一个简单的答题判题程序,要求输入题目信息和答题信息,根据标准答案判断答题结果。 主要知识点: 基本的类设计和对象创建。输入输…

如何阅读 CPU 电路图(以 6502 为例)

开篇 你是否曾对 CPU 的工作原理充满好奇,以及简单的晶体管又是如何组成逻辑门,进而构建出复杂的逻辑电路实现?本文将以知名的 6502 CPU 的电路图为例,介绍如何阅读 CPU 电路图,并向你演示如何从晶体管电路还原出逻辑门电路。通过这篇文章的引导,你将能够揭开 CPU 复杂设…

RSA公钥文件解密密文

RSA公钥文件解密密文RSA公钥文件解密密文 做题遇到两个从未见过的文件:flag.enc …

2024-2025-1 20241411王思棋《计算机基础与程序设计》第五周学习总结

班级链接 https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP作业要求 https://www.cnblogs.com/rocedu/p/9577842.html#WEEK05教材学习内容总结 《计算机科学概论》第六章 一、低级程序设计语言概述 (一)定义与特点 低级程序设计语言是一种更接近计算机硬件的编程语言,…

《计算机基础与程序设计》第五周学习总结

学期(2024-2025-1) 学号(20241412) 《计算机基础与程序设计》第五周学习总结 作业信息个作业属于哪个课程 2024-2025-1-计算机基础与程序设计 https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP这个作业要求在哪里 2024-2025-1计算机基础与程序设计第五周作业 https:…

20222408 2024-2025-1 《网络与系统攻防技术》实验三实验报告

1.实验内容 1.1回答问题 (1)杀软是如何检测出恶意代码的? ①基于特征码的检测:AV软件厂商搜集最全最新的特征码库,并以此来尝试匹配文件中的一个或几个片段②启发式恶意软件检测:根据片面特征推断,包括行为(如连接恶意网站、开放端口、修改系统文件等),外观(文件签名…

习题6.6

import numpy as np matches = np.array([ [0, 1, 0, 1, 1, 1], # 1队 [0, 0, 0, 1, 1, 1], # 2队 [1, 1, 0, 1, 0, 0], # 3队 [0, 0, 0, 0, 1, 1], # 4队 [0, 0, 1, 0, 0, 1], # 5队 [0, 0, 1, 0, 0, 0] # 6队 ], dtype=int) n = matches.shape[0] closure…

[BUUCTF]喵喵喵

[BUUCTF]喵喵喵 分析 题目是一张图片,打开属性,放入010editor,无果;于是想到LSB隐写 解题 发现一张PNG,但开头不太对劲将其保存txt文件,发现放入010editor并不可以修改开头在某位大佬博客中学到一个方法,如下: 首先,在notepad中打开txt文件Alt按列选择左侧复制,放入Cy…

22207223-王颖对于编写pta答题判题程序的总结

目录 一、前言1.1 知识点1.2 题量1.3 难度 二、答题判题程序12.1 设计与分析2.2 踩坑心得2.3 改进建议 三、答题判题程序23.1 设计与分析3.2 踩坑心得3.3 改进建议 四、答题判题程序34.1 设计与分析4.2 踩坑心得4.3 改进建议 五、总结 一、前言 1.1 知识点 (1)题目集1题目集1中…

Java实现答题判题程序

一、前言 本文介绍了如何使用Java设计实现答题程序,模拟一个小型的测试,要求输入题目信息和答题信息,根据输入题目信息中的标准答案判断答题的结果。本次编程任务通过三次作业实现了从基本的题目管理到完整的测试系统的设计。 第一次作业主要实现了题目信息、试卷信息和答卷…

Hello Web

Hello Web 兄弟们成了!!!我成了!!!在最近一段时间我基本没有发关于Web的博客,但是我一直在学习后端的知识和后端与前端交互的知识!!!从maven到mysql再到jdbc.mybatis,servlet,http,tomcat,终于,终于老子前端的代码可以把数据传到后端的数据库了!!!哈哈哈哈哈哈哈.我一路学习下来…

前三次大作业总结

一、前言 1. 题目一 1.1 知识点 1.1.1 类设计和封装:题目类:设计题号、题目内容和标准答案的基本属性,包含获取和设置方法,支持答案比对方法,用于判断答题是否正确。 试卷类:设计题目列表和题目数量两个属性,包含将题目存储到列表中的方法,能够按题号排序,以便在输出时…