首页 > 其他分享 >实验2

实验2

时间:2024-11-04 14:09:07浏览次数:1  
标签:std const int void Complex 实验 Fraction

  1 #pragma once
  2 
  3 #include <string>
  4 
  5 // 类T: 声明
  6 class T {
  7 // 对象属性、方法
  8 public:
  9     T(int x = 0, int y = 0);   // 普通构造函数
 10     T(const T &t);  // 复制构造函数
 11     T(T &&t);       // 移动构造函数
 12     ~T();           // 析构函数
 13 
 14     void adjust(int ratio);      // 按系数成倍调整数据
 15     void display() const;           // 以(m1, m2)形式显示T类对象信息
 16 
 17 private:
 18     int m1, m2;
 19 
 20 // 类属性、方法
 21 public:
 22     static int get_cnt();          // 显示当前T类对象总数
 23 
 24 public:
 25     static const std::string doc;       // 类T的描述信息
 26     static const int max_cnt;           // 类T对象上限
 27 
 28 private:
 29     static int cnt;         // 当前T类对象数目
 30 
 31 // 类T友元函数声明
 32     friend void func();
 33 };
 34 
 35 // 普通函数声明
 36 void func();
 37 
 38 
 39 // 类T: 实现
 40 // 普通函数实现
 41 
 42 #include "t.h"
 43 #include <iostream>
 44 #include <string>
 45 
 46 using std::cout;
 47 using std::endl;
 48 using std::string;
 49 
 50 // static成员数据类外初始化
 51 const std::string T::doc{"a simple class sample"};
 52 const int T::max_cnt = 999;
 53 int T::cnt = 0;
 54 
 55 
 56 // 对象方法
 57 T::T(int x, int y): m1{x}, m2{y} { 
 58     ++cnt; 
 59     cout << "T constructor called.\n";
 60 } 
 61 
 62 T::T(const T &t): m1{t.m1}, m2{t.m2} {
 63     ++cnt;
 64     cout << "T copy constructor called.\n";
 65 }
 66 
 67 T::T(T &&t): m1{t.m1}, m2{t.m2} {
 68     ++cnt;
 69     cout << "T move constructor called.\n";
 70 }    
 71 
 72 T::~T() {
 73     --cnt;
 74     cout << "T destructor called.\n";
 75 }           
 76 
 77 void T::adjust(int ratio) {
 78     m1 *= ratio;
 79     m2 *= ratio;
 80 }    
 81 
 82 void T::display() const {
 83     cout << "(" << m1 << ", " << m2 << ")" ;
 84 }     
 85 
 86 // 类方法
 87 int T::get_cnt() {
 88    return cnt;
 89 }
 90 
 91 // 友元
 92 void func() {
 93     T t5(42);
 94     t5.m2 = 2049;
 95     cout << "t5 = "; t5.display(); cout << endl;
 96 }
 97 
 98 
 99 #include "t.h"
100 #include <iostream>
101 
102 using std::cout;
103 using std::endl;
104 
105 void test();
106 
107 int main() {
108     test();
109     cout << "\nmain: \n";
110     cout << "T objects'current count: " << T::get_cnt() << endl;
111 }
112 
113 void test() {
114     cout << "test class T: \n";
115     cout << "T info: " << T::doc << endl;
116     cout << "T objects'max count: " << T::max_cnt << endl;
117     cout << "T objects'current count: " << T::get_cnt() << endl << endl;
118 
119 
120     T t1;
121     cout << "t1 = "; t1.display(); cout << endl;
122 
123     T t2(3, 4);
124     cout << "t2 = "; t2.display(); cout << endl;
125 
126     T t3(t2);
127     t3.adjust(2);
128     cout << "t3 = "; t3.display(); cout << endl;
129 
130     T t4(std::move(t2));
131     cout << "t3 = "; t4.display(); cout << endl;
132 
133     cout << "T objects'current count: " << T::get_cnt() << endl;
134 
135     func();
136 }
task1

 问题1:不能运行。

 函数定义时没有指明所属类。

问题2:(1)普通构造函数:初始化对象的成员变量,在创建对象时为对象设置初始状态。(2)复制构造函数:用一个已有对象初始化新对象,在对象被复制时调用。(3)移动构造函数:将一个临时对象的资源移动到新对象中,在创建对象时调用。(4)析构函数:在对象生命周期结束时被调用,清理无用资源。

问题3:不能。

 

  1 #ifndef COMPLEX_H
  2 #define COMPLEX_H
  3 
  4 #include <iostream>
  5 #include <cmath>
  6 
  7 class Complex {
  8 private:
  9     double real;
 10     double imag;
 11 
 12 public:
 13 
 14     Complex(double r = 0.0, double i = 0.0);
 15 
 16     double get_real() const;
 17     double get_imag() const;
 18     void set_real(double r);
 19     void set_imag(double i);
 20 
 21     void add(const Complex& other);
 22 
 23 
 24     double abs() const;
 25 
 26 
 27     bool is_equal(const Complex& other) const;
 28     bool is_not_equal(const Complex& other) const;
 29 
 30 
 31     void output() const;
 32 };
 33 
 34 #endif
 35 
 36 
 37 
 38 #include "Complex.h"
 39 Complex::Complex(double r, double i) : real(r), imag(i) {}
 40 
 41 double Complex::get_real() const {
 42     return real;
 43 }
 44 
 45 double Complex::get_imag() const {
 46     return imag;
 47 }
 48 
 49 void Complex::set_real(double r) {
 50     real = r;
 51 }
 52 
 53 void Complex::set_imag(double i) {
 54     imag = i;
 55 }
 56 
 57 void Complex::add(const Complex& other) {
 58     real += other.real;
 59     imag += other.imag;
 60 }
 61 
 62 double Complex::abs() const {
 63     return std::sqrt(real * real + imag * imag);
 64 }
 65 
 66 bool Complex::is_equal(const Complex& other) const {
 67     return (real == other.real) && (imag == other.imag);
 68 }
 69 
 70 bool Complex::is_not_equal(const Complex& other) const {
 71     return !is_equal(other);
 72 }
 73 
 74 void Complex::output() const {
 75     if (imag >= 0)
 76         std::cout << real << " + " << imag << "i";
 77     else
 78         std::cout << real << " - " << -imag << "i";
 79 }
 80 
 81 
 82 double Complex::abs() const {
 83     return std::sqrt(real * real + imag * imag);
 84 }
 85 
 86 bool Complex::is_equal(const Complex& other) const {
 87     return (real == other.real) && (imag == other.imag);
 88 }
 89 
 90 void Complex::output() const {
 91     if (imag >= 0)
 92         std::cout << real << " + " << imag << "i";
 93     else
 94         std::cout << real << " - " << -imag << "i";
 95 }
 96 
 97 
 98 
 99 #include"Complex.h"
100 #include <iostream>
101 using std::cout;
102 using std::endl;
103 using std::boolalpha;
104 void test() {
105 cout << "类成员测试: " << endl;
106 cout << Complex::doc << endl;
107 cout << endl;
108 cout << "Complex对象测试: " << endl;
109 Complex c1;
110 Complex c2(3, -4);
111 const Complex c3(3.5);
112 Complex c4(c3);
113 
114 cout << "c1 = "; output(c1); cout << endl;
115 cout << "c2 = "; output(c2); cout << endl;
116 cout << "c3 = "; output(c3); cout << endl;
117 cout << "c4 = "; output(c4); cout << endl;
118 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag()
119 << endl;
120 cout << endl;
121 cout << "复数运算测试: " << endl;
122 cout << "abs(c2) = " << abs(c2) << endl;
123 c1.add(c2);
124 cout << "c1 += c2, c1 = "; output(c1); cout << endl;
125 cout << boolalpha;
126 cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
127 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
128 c4 = add(c2, c3);
129 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
130 }
131 int main() {
132 test();
133 }
task2

 

 1 #include <iostream>
 2 #include <complex>
 3 using std::cout;
 4 using std::endl;
 5 using std::boolalpha;
 6 using std::complex;
 7 void test() {
 8 cout << "标准库模板类comple测试: " << endl;
 9 complex<double> c1;
10 complex<double> c2(3, -4);
11 const complex<double> c3(3.5);
12 complex<double> c4(c3);
13 cout << "c1 = " << c1 << endl;
14 cout << "c2 = " << c2 << endl;
15 cout << "c3 = " << c3 << endl;
16 cout << "c4 = " << c4 << endl;
17 cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() <<
18 endl;
19 cout << endl;
20 cout << "复数运算测试: " << endl;
21 cout << "abs(c2) = " << abs(c2) << endl;
22 c1 += c2;
23 cout << "c1 += c2, c1 = " << c1 << endl;
24 cout << boolalpha;
25 cout << "c1 == c2 : " << (c1 == c2) << endl;
26 cout << "c1 != c3 : " << (c1 != c3) << endl;
27 c4 = c2 + c3;
28 cout << "c4 = c2 + c3, c4 = " << c4 << endl;
29 }
30 int main() {
31 test();
32 }
task3

 

 1 #include<iostream>
 2 using namespace std;
 3 class Fraction {
 4 public:
 5     Fraction(int fz=0, int fm = 1);
 6     Fraction(const Fraction& f);
 7     int get_up()const;
 8     int get_down()const;
 9     Fraction negative() const;
10     friend void output(const Fraction &f);
11     friend Fraction add(const Fraction& f1, const Fraction& f2);
12     friend Fraction sub(const Fraction& f1, const Fraction& f2);
13     friend Fraction mul(const Fraction& f1, const Fraction& f2);
14     friend Fraction div(const Fraction& f1,const Fraction& f2);
15     static string doc();
16 private:
17       int up;
18       int down;
19   };
20   void output(const Fraction& f);
21   Fraction add(const Fraction& f1,const Fraction& f2);
22   Fraction sub(const Fraction& f1,const Fraction& f2);
23   Fraction mul(const Fraction& f1,const Fraction& f2);
24   Fraction div(const Fraction& f1,const Fraction& f2);
25 
26 
27 
28 
29 
30 #include"Fraction.h"
31     int gcd(int x, int y) {
32     if (x == 0 || y == 0) {
33         return x ? y == 0 : y;
34     }
35     if (x%y == 0)
36             return y;
37         else
38             return gcd(y, x % y);
39      }
40      Fraction::Fraction(int fz,int fm):up(fz),down(fm){
41          int flag = 0;
42          if (up * down < 0) {
43              flag = 1;
44          }
45          fz = abs(up);
46          fm = abs(down);
47          int t = gcd(fz, fm);
48         fz /= t;
49          fm /= t;
50          if (flag == 1) {
51              up = fz * -1;
52             down = fm;
53           }
54          else {
55               up = fz;
56               down = fm;
57           }
58 
59 
60 
61 #include "Fraction.h"
62 void test1() {
63     cout << "Fraction类测试: " << endl;
64     cout << Fraction::doc() << endl << endl;
65     Fraction f1(5);
66     Fraction f2(3, -4), f3(-18, 12);
67     Fraction f4(f3);
68     cout << "f1 = "; output(f1); cout << endl;
69     cout << "f2 = "; output(f2); cout << endl;
70     cout << "f3 = "; output(f3); cout << endl;
71     cout << "f4 = "; output(f4); cout << endl;
72     Fraction f5(f4.negative());
73     cout << "f5 = "; output(f5); cout << endl;
74     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " <<
75     f5.get_down() << endl;
76     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
77     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
78     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
79     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
80     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
81 
82 }
83 void test2() {
84     Fraction f6(42, 55), f7(0, 3);
85     cout << "f6 = "; output(f6); cout << endl;
86     cout << "f7 = "; output(f7); cout << endl;
87     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
88  }
89  int main() {
90     cout << "测试1: Fraction类基础功能测试\n";
91     test1();
92     cout << "\n测试2: 分母为0测试: \n";
93     test2();
94 }
task4

 

 

标签:std,const,int,void,Complex,实验,Fraction
From: https://www.cnblogs.com/syxlyw/p/18494451

相关文章

  • 7.2、实验二:被动接口和单播更新
    源文件链接:7.2、实验二:被动接口和单播更新:https://url02.ctfile.com/d/61945102-63671890-6af6ec?p=2707(访问密码:2707)一、被动接口1.介绍定义:在路由协议的配置中,一个被动接口指的是一个接口不发送路由更新包的配置方式,但仍然可以接收和处理传入的路由更新。作用:......
  • 实验3 串的实现
    c++:string就是一个数组看成str[N]数组就可以了,其他的好像都一样!点击查看代码#include<iostream>#include<string>usingnamespacestd;constintN=1e7+5;structstrs{stringst;intlen;}s,c,c1,c2;intnext1[N];intmumo(){cout<<"您可以......
  • 华为eNSP实验:MAC地址漂移
    MAC地址漂移是指在同一个VLAN内,一个MAC地址有两个出接口,并且后学习到的出接口覆盖原出接口的现象。MAC地址漂移是网络设备(如交换机)上的一种现象,它发生在当同一个VLAN内有两个或多个端口学习到同一个MAC地址时,后学习到的MAC地址表项会覆盖原有的表项。这种现象通常意味着网络......
  • 实验8:适配器模式
    [实验任务一]:双向适配器实现一个双向适配器,使得猫可以学狗叫,狗可以学猫抓老鼠。实验要求:1. 对应的类图: 2. 源代码:Cat接口: publicinterfaceCat{  voidcry();  voidcatchMouse();} 实体Cat类: publicclassConcreteCatimplementsCat{    @Over......
  • 20222310 2024-2025-1 《网络与系统攻防技术》实验四实验报告
    一、实验内容(一)恶意代码文件类型标识、脱壳与字符串提取对提供的rada恶意代码样本,进行文件类型识别,脱壳与字符串提取,以获得rada恶意代码的编写作者,具体操作如下:(1)使用文件格式和类型识别工具,给出rada恶意代码样本的文件格式、运行平台和加壳工具;(2)使用超级巡警脱壳机等脱壳软件,......
  • 实验9:桥接模式
    [实验任务一]:两个维度的桥接模式用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。 1.类图   1. 源代码1.Car.javapackagetest9; publicclassCarimplementsVehicle{    @Override    publicvoiddrive(){   ......
  • 20222425 2024-2025-1 《网络与系统安全技术》实验四报告
    202224252024-2025-1《网络与系统安全技术》实验四报告目录1.实践内容2.实践过程2.1恶意代码样本的文件类型识别,脱壳与字符串提取2.1.1使用文件格式和类型识别工具,给出rada恶意代码样本的文件格式、运行平台和加壳工具2.1.2使用脱壳软件,对rada恶意代码样本进行脱壳处理......
  • 实验2 类和对象_基础编程1
    任务1:#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数voidadjust(intratio);//按系数成倍调整数据voiddisp......
  • 数据科学实验三 死亡原因分析
    一、实验目标和要求对墨西哥的个体死亡记录和死亡原因数据进行分析,掌握数据处理的三类主要工具:数据操作、数据可视化、数据建模。二、实验环境百度飞桨三、实验内容¶1)数据操作:读入数据文件deaths.xlsx和icd-main.xlsx,对数据进行筛选、去空值、分组统计等操作,计算出每......
  • 20222403 2024-2025-1 《网络与系统攻防技术》实验四实验报告
    1.实验内容一、恶意代码文件类型标识、脱壳与字符串提取对提供的rada恶意代码样本,进行文件类型识别,脱壳与字符串提取,以获得rada恶意代码的编写作者,具体操作如下:(1)使用文件格式和类型识别工具,给出rada恶意代码样本的文件格式、运行平台和加壳工具;(2)使用超级巡警脱壳机等脱壳软件......