首页 > 编程语言 >程序设计实验2

程序设计实验2

时间:2024-10-26 21:58:43浏览次数:1  
标签:std const int double Complex 实验 Fraction 程序设计

运行结果1

问题1:

不能运行,因为func函数未声明

问题2:

普通构造函数可以初始化,复制构造函数是对同类对象的引用,移动构造函数是对参数右值的引用,编译周期结束时,自动调用析构函数

问题3:

不能运行

实验任务2

 1 //Complex.h
 2 #pragma once
 3 #include <string>
 4 #include<iostream>
 5 #include<cmath>
 6 // 类Complex: 声明
 7 class Complex {
 8 public:
 9     Complex();
10     Complex(double r);
11     Complex(double r, double i);
12     Complex(const Complex& c);
13     double get_real()const;
14     double get_imag()const;
15     void add(const Complex& c);
16     //~Complex();           // 析构函数
17     //     void output(const Complex& c);
18     void output()const;
19     //void Complex add(const Complex& c);
20     static const std::string doc;
21     friend Complex add(const Complex& c1, const Complex& c2);
22     friend bool is_equal(const Complex& c1, const Complex& c2);
23     friend bool is_not_equal(const Complex& c1, const Complex& c2);
24     friend double abs(const Complex& c);
25     //friend Complex output(const Complex& c);
26     friend std::ostream& operator<<(std::ostream& os, const Complex& c);
27 
28 private:
29     double real;
30     double imag;
31 
32 };
 1 //Complex.cpp
 2 #include "Complex.h"
 3 // 普通函数实现
 4 #include <iostream>
 5 #include <string>
 6 using std::cout;
 7 using std::endl;
 8 using std::string;
 9 // static成员数据类外初始化
10 const std::string Complex::doc = " a simplified complex class";
11 
12 
13 // 对象方法
14 Complex::Complex() :real(0), imag(0) {}
15 Complex::Complex(double r) :real(r), imag(0) {}
16 Complex::Complex(double r, double i) :real(r), imag(i) {}
17 Complex::Complex(const Complex& c) : real(c.real), imag(c.imag) {}
18 
19 
20 // 类方法
21 double Complex::get_real()const {
22     return real;
23 }
24 double Complex::get_imag() const {
25     return imag;
26 }
27 void Complex::add(const Complex& c) {
28     real += c.real;
29     imag += c.imag;
30 }//将复数加到自身
31 std::ostream& operator<<(std::ostream& os, const Complex& c) {
32     os << c.real << (c.imag >= 0 ? "+" : "") << c.imag << "i";
33     return os;
34 }// 输出流重载
35 Complex add(const Complex& c1, const Complex& c2) {
36     return Complex(c1.real + c2.real, c1.imag + c2.imag);
37 }//加法友元
38 bool is_equal(const Complex& c1, const Complex& c2) {
39     if ((c1.real == c2.real) && (c1.imag == c2.imag))
40         return true;
41     return false;
42 }//判等友元
43 bool is_not_equal(const Complex& c1, const Complex& c2) {
44     return !is_equal(c1, c2);
45 }//判不等友元
46 double abs(const Complex& c) {
47     return std::sqrt(c.real * c.real + c.imag * c.imag);
48 }//取模友元
49 void Complex::output() const {
50     if (imag >= 0)
51         std::cout << real << " + " << imag << "i" << std::endl;
52     else
53         std::cout << real << " - " << imag * (-1) << "i" << std::endl;
54 }//输出
 1 //task2.cpp
 2 #include "Complex.h"
 3 #include <iostream>
 4 
 5 using std::cout;
 6 using std::endl;
 7 using std::boolalpha;
 8 
 9 void test() {
10     cout << "类成员测试: " << endl;
11     cout << Complex::doc << endl;
12 
13     cout << endl;
14 
15     cout << "Complex对象测试: " << endl;
16     Complex c1;
17     Complex c2(3, -4);
18     const Complex c3(3.5);
19     Complex c4(c3);
20 
21     cout << "c1 = "; c1.output(); cout << endl;
22     cout << "c2 = "; c2.output(); cout << endl;
23     cout << "c3 = "; c3.output(); cout << endl;
24     cout << "c4 = "; c4.output(); cout << endl;
25     cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;
26 
27     cout << endl;
28 
29     cout << "复数运算测试: " << endl;
30     cout << "abs(c2) = " << abs(c2) << endl;
31     c1.add(c2);
32     cout << "c1 += c2, c1 = "; c1.output(); cout << endl;
33     cout << boolalpha;
34     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
35     cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
36     c4 = add(c2, c3);
37     cout << "c4 = c2 + c3, c4 = "; c4.output(); cout << endl;
38 
39 }
40 
41 int main() {
42     test();
43 
44 }

运行结果2

实验任务3

提供Complex<返回值类型> Complex+名称+real/imag直接返回虚部实部的值,虚数运算c1+c2,判等直接比较

在虚数计算和判等时直接用+-代替而不是另外编写函数

实验任务4

 1 #pragma once
 2 #include<string>
 3 
 4 class Fraction{
 5 public:
 6         static const std::string doc;
 7         Fraction(int up = 0, int down = 1);
 8         Fraction(const Fraction& f);
 9         int get_up()const;
10         int get_down()const;
11         int gcd(int m,int n)const;
12         Fraction negative()const;
13         friend Fraction add(const Fraction& f1, const Fraction& f2);
14         friend Fraction sub(const Fraction& f1,const Fraction& f2);
15         friend Fraction mul(const Fraction& f1,const Fraction& f2);
16         friend Fraction div(const Fraction& f1, const Fraction& f2);
17         friend void output(const Fraction& f);
18      void simplify();
19 private:
20     int up, down;
21     int m, n;
22 };
 1 #include "Fraction.h"
 2 #include<iostream>
 3 #include<string>
 4 #include <numeric>
 5 
 6 using std::cout;
 7 using std::endl;
 8 using std::string;
 9 
10 const std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
11 
12 
13 Fraction::Fraction(int u, int d) : up(u), down(d) {
14     simplify();
15 }
16 
17 Fraction::Fraction(const Fraction& f) :up(f.up), down(f.down) {}
18 
19 int gcd(int a, int b) {
20     while (b != 0) {
21         int temp = b;
22         b = a % b;
23         a = temp;
24     }
25     return abs(a); // 返回绝对值
26 }
27 
28 void Fraction::simplify() {
29     if (down < 0) { // 保证分母为正
30         up = -up;
31         down = -down;
32     }
33     int divisor = gcd(abs(up), abs(down));
34     up /= divisor;
35     down /= divisor;
36 }
37 
38 
39 int Fraction::get_up()const {
40     return up;
41 }
42 int Fraction::get_down()const {
43     return down;
44 }
45 Fraction Fraction::negative()const {
46     return Fraction(-up, down);
47 }
48 
49 //友元
50 void output(const Fraction& f)
51 {
52     if (f.get_up() == 0 && f.get_down() == 0)
53         printf("分母不能为0\n");
54     else if (f.get_up() == 0 && f.get_down() != 0)
55         std::cout << "0" << std::endl;
56     else
57         std::cout << f.get_up() << "/" << f.get_down() << std::endl;
58 }
59 Fraction add(const Fraction& f1, const  Fraction& f2)
60 {
61     return Fraction(f1.get_up() * f2.get_down() + f2.get_up() * f1.get_down(), f1.get_down() * f2.get_down());
62 }
63 Fraction sub(const Fraction& f1, const  Fraction& f2)
64 {
65     return Fraction(f1.get_up() * f2.get_down() - f2.get_up() * f1.get_down(), f1.get_down() * f2.get_down());
66 }
67 Fraction mul(const Fraction& f1, const  Fraction& f2)
68 {
69     return Fraction(f1.get_up() * f2.get_up(), f1.get_down() * f2.get_down());
70 }
71 Fraction div(const Fraction& f1, const  Fraction& f2) {
72     if (f2.get_up() == 0) {
73         //throw std::invalid_argument("除数不能为零");
74         printf("分母不能为0\n");
75         Fraction f(0, 0);
76         return f;
77     }
78     return Fraction(f1.get_up() * f2.get_down(), f1.get_down() * f2.get_up());
79 }
 1 #include "Fraction.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 
 6 using std::endl;
 7 
 8 void test1() {
 9     cout << "Fraction类测试: " << endl;
10     cout << Fraction::doc << endl << endl;
11     Fraction f1(5);
12     Fraction f2(3, -4), f3(-18, 12);
13     Fraction f4(f3);
14     cout << "f1 = "; output(f1); cout << endl;
15     cout << "f2 = "; output(f2); cout << endl;
16     cout << "f3 = "; output(f3); cout << endl;
17     cout << "f4 = "; output(f4); cout << endl;
18     Fraction f5(f4.negative());
19     cout << "f5 = "; output(f5); cout << endl;
20     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " <<
21 
22         f5.get_down() << endl;
23     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
24     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
25     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
26     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
27     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
28 }
29 
30 void test2() {
31     Fraction f6(42, 55), f7(0, 3);
32     cout << "f6 = "; output(f6); cout << endl;
33     cout << "f7 = "; output(f7); cout << endl;
34     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
35 }
36 int main() {
37     cout << "测试1: Fraction类基础功能测试\n";
38     test1();
39     cout << "\n测试2: 分母为0测试: \n";
40     test2();
41 }

运行结果4

实验任务5

 1 #pragma once
 2 class SavingsAccount {
 3 private:
 4     int id;
 5     double balance;
 6     double rate;
 7     int lastDate;
 8     double accumulation;
 9     static double total;
10     void record(int date, double amount);
11     double accumulate(int date)const {
12         return accumulation + balance * (date - lastDate);
13     }
14 public:
15     SavingsAccount(int date, int id, double rate);
16     int getId()const { return id; }
17     double getBalance()const { return balance;}
18     double getRate()const { return rate;}
19     static double getTotal() { return total; }
20     void deposit(int date, double amount);
21     void withdraw(int date, double amount);
22     void settle(int date);
23     void show()const;
24 };
 1 #include"account.h"
 2 #include<cmath>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 double SavingsAccount::total = 0;
 7 SavingsAccount::SavingsAccount(int date, int id, double rate) :
 8     id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
 9     cout << date << "\t#" << id << "is created" << endl;
10 }
11 void SavingsAccount::record(int date, double amount) {
12     accumulation = accumulate(date);
13     lastDate = date;
14     amount = floor(amount * 100 + 0.5) / 100;
15     balance += amount;
16     total += amount;
17     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
18 }
19 void SavingsAccount::deposit(int date, double amount) {
20     record(date, amount);
21 }
22 void SavingsAccount::withdraw(int date, double amount) {
23     if (amount > getBalance())
24         cout << "Error:not enough money" << endl;
25     else
26         record(date, -amount);
27 }
28 void SavingsAccount::settle(int date) {
29     double interest = accumulate(date) * rate / 365;
30     if (interest != 0)
31         record(date, interest);
32     accumulation = 0;
33 }
34 void SavingsAccount::show()const {
35     cout << "#" << id << "\tBalance:" << balance;
36 }
 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main()
 5 {
 6     SavingsAccount sa0(1, 21325302, 0.015);
 7     SavingsAccount sa1(1, 58320212, 0.015);
 8     sa0.deposit(5, 5000);
 9     sa1.deposit(25, 10000);
10     sa0.deposit(45, 5500);
11     sa1.withdraw(60,4000);
12     sa0.settle(90);
13     sa1.settle(90);
14     sa0.show(); cout << endl;
15     sa1.show(); cout << endl;
16     cout << "Total:" << SavingsAccount::getTotal() << endl;
17     return 0;
18 }

运行结果5

 

标签:std,const,int,double,Complex,实验,Fraction,程序设计
From: https://www.cnblogs.com/CK1NG/p/18494425

相关文章

  • 学期:2024-2025-1 学号:20241303 《计算机基础与程序设计》第5周学习总结
    作业信息这个作业属于哪个课程<班级的链接>(如2024-2025-1-计算机基础与程序设计)这个作业要求在哪里<作业要求的链接>(如2024-2025-1计算机基础与程序设计第五周作业)这个作业的目标<写上具体方面>自学教材,计算机科学概论(第七版)第6章并完成云班课测试,《C语言程序设......
  • 2024-2025-1 20241408陈烨南《计算机基础与程序设计》第五周学习总结
    这个作业属于哪个课程2024-2025-1-计算机基础与程序设计)这个作业要求在哪里https://www.cnblogs.com/rocedu/p/9577842.html#WEEK05这个作业的目标①Pep/9虚拟机②机器语言与汇编语言③算法与伪代码④测试:黑盒,白盒作业正文本博客链接教材学......
  • 实验3 c语言函数应用编程
    实验任务1task1.c1#include<stdio.h>23charscore_to_grade(intscore);//函数声明45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);//......
  • 实验2 类和对象_基础编程1
    task1:t.h1#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数12~T();......
  • 【嵌入式原理设计】实验一:软硬件环境搭建&数字端口应用
    目录一、实验目的 二、实验环境三、实验内容四、实验记录及处理五、实验小结六、成果文件提取链接一、实验目的         配置ESPArduino开发平台,熟悉实验的软硬件工作环境和基本的工作方式二、实验环境        Win10+ESP32实验开发板三、实......
  • 实验3
    task.1#include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",s......
  • Web高级开发实验:EL基本运算符与数据访问
    一、实验目的掌握EL的定义,即ExpressionLanguage,用于提高编程效率。学习和掌握在开发环境中创建Java文件,并在jsp文件中使用EL表达式去调用其中的方法与属性等。二、实验所用方法上机实操三、实验步骤及截图1、创建javaweb项目,在src文件夹下创建myClasses包。在myClasses包......
  • 实验三
    #include<stdio.h>charscore_to_grade(intscore);intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);printf("分数:%d,等级:%c\n\n",score,grade);}return0;}charscore_......
  • 面向对象程序设计
    第一次博客作业一、前言第一次作业第一次作业有五道题,前四道为入门及简单的小题,最后一题为难度较大的答题判断程序。主要考察对类的属性和方法的设计与使用,以及在规定格式下对程序用户的输入进行解析从而获取题目、答卷等信息。第二次作业第二次作业有四道题,前三道为难度入门......
  • 《DNK210使用指南 -CanMV版 V1.0》第三十三章 image元素绘制实验
    第三十三章image元素绘制实验1)实验平台:正点原子DNK210开发板2)章节摘自【正点原子】DNK210使用指南-CanMV版V1.03)购买链接:https://detail.tmall.com/item.htm?&id=7828013987504)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/ATK-DNK210.html5)......