首页 > 其他分享 >实验二

实验二

时间:2024-10-24 20:43:59浏览次数:1  
标签:const int up down Complex 实验 Fraction

实验一代码:

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

运行截图:

 

问题1:

不能,func()函数未声明,所以不存在该友元函数

 

问题2:构造函数的作用就是在对象被创建时利用特定的值构造对象,将对象初始化为一个特定的状态。1.复制构造函数:其形参是本类的对象的引用。

其作用是使用一个已经存在的对象(由复制构造函数的参数指定),去初始化同类的一个新对象。。2.移动构造函数:左值是位于赋值语句左侧的对象

变量,右值是赋值语句右侧的值,不依附于对象。3.所有构造函数调用完后,析构函数依次调用。

 

问题3:

不能

 

实验二代码:

 1 #pragma once
 2 
 3 #include<string>
 4  
 5  
 6 class Complex{
 7     public:
 8         Complex (double a=0, double b=0);
 9         Complex (const Complex &c);         friend void output(const Complex &c);
10         const double get_real();
11         const double get_imag();
12         friend double abs(const Complex &c);
13         friend bool is_equal(const Complex &c1, const Complex &c2);         
14         friend bool is_not_equal(const Complex &c1, const Complex &c2);
15         friend Complex add(const Complex &c1, const Complex &c2);
16         Complex add(const Complex &c);
17         static std::string doc;
18         ~Complex();
19     private:
20         double real,imag;
21         
22 }; 
23 void output(const Complex &c);
24 double abs(const Complex &c);
25 bool is_equal(const Complex &c1, const Complex &c2);
26 bool is_not_equal(const Complex &c1, const Complex &c2);
27 Complex add(const Complex &c1, const Complex &c2);
28 
29 
30 
31 
32 #include "Complex.h"
33 #include<iostream>
34 #include<string>
35 #include<cmath>
36 
37 using namespace std;
38 
39 Complex::Complex(double a,double b): real{a},imag{b}{}
40 Complex::Complex(const Complex &c){
41     real=c.real;
42     imag=c.imag;
43 }
44 Complex add(const Complex &c1,const Complex &c2){
45     Complex c;
46     c.real=c1.real+c2.real;
47     c.imag=c1.imag+c2.imag;
48     return c;
49 }
50 Complex Complex::add(const Complex &c){
51     real+=c.real;
52     imag+=c.imag;
53 }
54 const double Complex::get_real(){
55     return real;
56 }
57 const double Complex::get_imag(){
58     return imag;
59 }
60 double abs(const Complex &c){
61     double a=0;
62     a=c.real*c.real+c.imag*c.imag;
63     a=sqrt(a);
64 }
65 bool is_equal(const Complex &c1,const Complex &c2){
66     if(c1.real==c2.real && c1.imag==c2.imag)
67        return 1;
68     else
69        return 0;
70 }
71 bool is_not_equal(const Complex &c1,const Complex &c2){
72     if(c1.real!=c2.real || c1.imag!=c2.imag)
73        return 1;
74     else
75        return 0;
76 }
77 Complex::~Complex()=default;
78 string Complex::doc="a simplified Complex class";
79 void output(const Complex &c){
80     if(c.imag<0)
81        cout<<c.real<<c.imag<<"i";
82     else
83        cout<<c.real<<"+"<<c.imag<<"i";
84 }
View Code

 

运行截图:

 

实验三代码:

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

 

运行截图:

 

实验四代码:

  1 #include "Fraction.h"
  2 
  3 #include<iostream>
  4 
  5 using namespace std;
  6 
  7 Fraction::Fraction(int a,int b):up{a},down{b}{}
  8 Fraction::Fraction(const Fraction &f){
  9     up=f.up;
 10     down=f.down;
 11 }
 12 Fraction Fraction::negative(){
 13     Fraction f;
 14     f.up=up*-1;
 15     f.down=down;
 16     return f;
 17 }
 18 Fraction::~Fraction()=default;
 19 int Fraction::get_down(){
 20         int a,b,c,d,e;
 21         a=up;
 22         b=down;
 23         d=up;
 24         e=down;
 25         while(b!=0)
 26         {
 27             c=b;
 28             b=a%b;
 29             a=c;
 30         }
 31         d=d/a;
 32         e=e/a;
 33     return e;
 34 }
 35 int Fraction::get_up(){
 36         int a,b,c,d,e;
 37         a=up;
 38         b=down;
 39         d=up;
 40         e=down;
 41         while(b!=0)
 42         {
 43             c=b;
 44             b=a%b;
 45             a=c;
 46         }
 47         d=d/a;
 48         e=e/a;
 49     return d;
 50 }
 51 string Fraction::doc="Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
 52 Fraction add(const Fraction &f1,const Fraction &f2){
 53     Fraction f;
 54     f.down=f1.down*f2.down;
 55     f.up=f1.up*f2.down+f2.up*f1.down;
 56     return f;
 57 }
 58 Fraction sub(const Fraction &f1,const Fraction &f2){
 59     Fraction f;
 60     f.down=f1.down*f2.down;
 61     f.up=f1.up*f2.down-f2.up*f1.down;
 62     return f;
 63 }
 64 Fraction mul(const Fraction &f1,const Fraction &f2){
 65     Fraction f;
 66     f.down=f1.down*f2.down;
 67     f.up=f1.up*f2.up;
 68     return f;
 69 }
 70 Fraction div(const Fraction &f1,const Fraction &f2){
 71     Fraction f;
 72     f.down=f1.down*f2.up;
 73     f.up=f1.up*f2.down;
 74     return f;
 75 }
 76 void output(const Fraction &f){
 77     int a,b,c,d,e;
 78     if(f.down==0)
 79         cout<<"分母不能为0";
 80     else
 81     {
 82         a=f.up;
 83         b=f.down;
 84         d=f.up;
 85         e=f.down;
 86         while(b!=0)
 87         {
 88             c=b;
 89             b=a%b;
 90             a=c;
 91         }
 92         d=d/a;
 93         e=e/a;
 94         if(e==1)
 95            cout<<d;
 96         else if(e<0)
 97            cout<<d*-1<<"/"<<e*-1;
 98         else
 99            cout<<d<<"/"<<e;
100     }        
101 }
102 
103 
104 
105 
106 #pragma once
107 
108 #include<string>
109 class Fraction{
110     public:
111         Fraction(int a=1,int b=1);
112         Fraction(const Fraction &f);
113         int get_up();
114         int get_down();
115         Fraction negative();
116         static std::string doc;
117         friend void output(const Fraction &f);
118         friend Fraction add(const Fraction &f1,const Fraction &f2);
119         friend Fraction sub(const Fraction &f1,const Fraction &f2);
120         friend Fraction mul(const Fraction &f1,const Fraction &f2);
121         friend Fraction div(const Fraction &f1,const Fraction &f2);
122         ~Fraction();
123     private:
124         int up,down;
125 };
126 void output(const Fraction &f);
127 Fraction add(const Fraction &f1,const Fraction &f2);
128 Fraction sub(const Fraction &f1,const Fraction &f2);
129 Fraction mul(const Fraction &f1,const Fraction &f2);
130 Fraction div(const Fraction &f1,const Fraction &f2); 
View Code

 

运行截图:

 

实验五代码:

 1 #include"account.h"
 2 #include<cmath>
 3 #include<iostream>
 4 using namespace std;
 5 double SavingsAccount::total = 0;
 6 SavingsAccount::SavingsAccount(int date, int id, double rate) :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
 7     cout << date << "\t#" << id << "is created" << endl;
 8 }
 9 void SavingsAccount::record(int date, double amount) {
10     accumulation = accumulate(date);
11     lastDate = date;
12     amount = floor(amount * 100 + 0.5) / 100;
13     balance += amount;
14     total += amount;
15     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
16 }
17 void SavingsAccount::deposit(int date, double amount) {
18     record(date, amount);
19 }
20 void SavingsAccount::withdraw(int date,double amount) {
21     if (amount > getBalance())
22         cout << "Error:not enough money" << endl;
23     else {
24         record(date, -amount);
25     }
26 }
27 void SavingsAccount::settle(int date) {
28     double interest = accumulate(date) * rate / 365;
29     if (interest != 0)
30         record(date, interest);
31     accumulation = 0;
32 }
33 void SavingsAccount::show()const {
34     cout << "#" << id << "\tBalance:" << balance;
35 }
36 
37 
38 
39 #include"account.h"
40 #include<iostream>
41 using namespace std;
42 int main() {
43     SavingsAccount sa0(1, 21325302, 0.015);
44     SavingsAccount sa1(1, 58320212, 0.015);
45     sa0.deposit(5, 5000);
46     sa1.deposit(25, 10000);
47     sa0.deposit(45, 5500);
48     sa1.withdraw(60, 4000);
49     sa0.settle(90);
50     sa1.settle(90);
51     sa0.show(); cout << endl;
52     sa1.show(); cout << endl;
53     cout << "Total: " << SavingsAccount::getTotal() << endl;
54     return 0;
55 }
56 
57 
58 
59 
60 #pragma once
61 class SavingsAccount {
62 private:
63     int id;
64     double balance;
65     double rate;
66     int lastDate;
67     double accumulation;
68     static double total;
69     void record(int date, double amount);
70     double accumulate(int date) const {
71         return accumulation + balance * (date - lastDate);
72     }
73 public:
74     SavingsAccount(int date, int id, double rate);
75     int getId()const { return id; }
76     double getBalance()const { return balance; }
77     double getRate()const { return rate; }
78     static double getTotal() { return total; }
79     void deposit(int date, double amount);
80     void withdraw(int date, double amount);
81     void settle(int date);
82    
83  void show()const;
84 };
View Code

 

运行截图:

 

 

标签:const,int,up,down,Complex,实验,Fraction
From: https://www.cnblogs.com/ywq121/p/18494463

相关文章

  • 20222403 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    1.实验内容1.1.实践内容(1)正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧正确使用msf编码器,使用msfvenom生成如jar之类的其他文件veil,加壳工具使用C+shellcode编程(2)通过组合应用各种技术实现恶意代码免杀如果成功实现了免杀的,简单语言描述原......
  • 实验2 类和对象_基础编程1
    实验任务1:实验代码:1#include<string>23//类T:声明4classT{5//对象属性、方法6public:7T(intx=0,inty=0);//普通构造函数8T(constT&t);//复制构造函数9T(T&&t);//移动构造函数10~T();......
  • 王爽汇编实验12
    下面是实验十二的代码assumecs:codecodesegmentstart: ;do0的安装程序,只需要安装一次,以后无需此步骤 movax,cs movds,ax movsi,offsetdo0 ;设置ds:si指向do0程序所在位置 movax,0 moves,ax movdi,200h ;设置es:di指向中断......
  • 实验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\n",score,grade);......
  • 实验3
    任务1:#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%d,等级:%......
  • 实验3
    任务11#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11printf(&......
  • 实验2 类和对象_基础编程1
    实验任务1:t.h源码:1#pragmaonce23#include<string>45//类T:声明6classT{7//对象属性、方法8public:9T(intx=0,inty=0);//普通构造函数10T(constT&t);//复制构造函数11T(T&&t);//移动构造函数12......
  • 20222305 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    网络攻防实验报告姓名:田青学号:20222305实验日期:2024/10/18—2024/10/31实验名称:后门原理与实践指导教师:王志强1.实验内容本周学习内容总结:1.用户态(ring3)和内核态(ring0),切换时机:系统调用、中断、异常。2.自启动技术。3.进程隐藏技术实现:1>改名2>基于系统服务的伪装3>......
  • 实验3
    task11#include<stdio.h>23charscore_to_grade(intscore);//函数声明45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);//函数调用11......
  • 实验三
    #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&q......