首页 > 编程语言 >试验2 类和对象——基础编程2

试验2 类和对象——基础编程2

时间:2023-10-21 13:11:18浏览次数:27  
标签:const cout 对象 double void 编程 试验 int include

任务3

1.代码

complex.hpp:

 1 #ifndef complex_H
 2 #define complex_H
 3 
 4 #include <iostream>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 class Complex
 9 {
10     public:
11         Complex(double x=0,double y=0):real{x},imag{y}{}
12         Complex(const Complex &t):real{t.real},imag{t.imag}{}
13         double get_real()const
14         {
15             return real;
16         }    
17         double get_imag()const
18         {
19             return imag;
20         }
21         void show()const
22         {
23             if(imag<0)
24             std::cout<<real<<imag<<"i"<<std::endl;
25             if(imag>0)
26             std::cout<<real<<"+"<<imag<<"i"<<std::endl;
27             if(imag==0)
28             std::cout<<real<<std::endl;
29         }
30         double add(const Complex &a)
31         {
32             real+=a.real;
33             imag+=a.imag;        
34         }
35         
36         friend Complex add(const Complex &p1, const Complex &p2);
37         friend bool is_equal(const Complex &p1, const Complex &p2);
38         friend double abs(const Complex &p1);
39     private:
40         double real,imag;
41 }; 
42 Complex add(const Complex &p1, const Complex &p2) 
43     {
44         Complex x;
45         x.real=p1.real + p2.real;
46         x.imag=p1.imag + p2.imag;
47         return (x);
48     }
49 bool is_equal(const Complex &p1, const Complex &p2)
50     {
51         if((p1.real==p2.real)&&(p1.imag == p2.imag))
52         {
53             return true;
54         }
55         else
56         return false;
57     }
58 double abs(const Complex &p1)
59     {
60         double mo=0;
61         mo=sqrt(p1.real*p1.real+p1.imag*p1.imag);
62         return mo;
63     }
64 void test() {
65     using namespace std;
66     Complex c1(3, -4);
67         const Complex c2(4.5);
68     Complex c3(c1);
69     cout << "c1 = ";
70     c1.show();
71     cout << endl;
72     cout << "c2 = ";
73     c2.show();
74     cout << endl;
75     cout << "c2.imag = " << c2.get_imag() << endl;
76     cout << "c3 = ";
77     c3.show();
78     cout << endl;
79     cout << "abs(c1) = ";
80     cout << abs(c1) << endl;
81     cout << boolalpha;
82     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
83     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
84     Complex c4;
85     c4 = add(c1, c2);
86     cout << "c4 = c1 + c2 = ";
87     c4.show();
88     cout << endl;
89     c1.add(c2);
90     cout << "c1 += c2, " << "c1 = ";
91     c1.show();
92     cout << endl;
93 }
94 
95 #endif
View Code

main.cpp:

1 #include"complex.hpp"
2 int main() {
3 test();
4 }
View Code

2.图片:

任务4:

1.代码:
user.hpp:

 1 #ifndef user_H
 2 #define user_H
 3 
 4 #include<iostream>
 5 #include<string>
 6 #include <vector>
 7 
 8 using namespace std;
 9 class User
10 {
11     private:
12         string name;
13         string password;
14         string email;
15     private:
16         static int n;
17     public:
18          static void print_n();
19     public:
20         User(string x,string y="111111",string z="      "):name{x},password{y},email{z}
21         {
22             ++n;
23         }
24         ~User(){--n;
25         }
26     void set_email()
27     {
28         string a;
29         cout<<"Enter email address:";
30         cin>>a;
31         email=a;
32         cout<<"email is set successfully..."<<endl;
33     }        
34     void change_passwd()
35     {
36         int count=0;
37         string b,c;
38         cout<<"Enter old passowrd:";
39         cin>>b;
40         while(b!=password)
41         {
42             count++;
43             cout<<"passowrd input error. please re-enter again:";
44             cin>>b;
45             if(count>=2)
46             {
47                 cout<<"passowrd input error. please try after a while:"<<endl;
48                 break;
49             }
50         }
51         if(b==password)
52         {
53             cout<<"Enter new passowrd:";
54             cin>>c;
55             password=c;
56             cout<<"new password is set successfully..."<<endl;
57             
58         }
59     }
60     void print_info()
61     {
62         string s2(password.size(), '*');
63         cout<<"name:      "<<name<<endl;
64         cout<<"password:  "<<s2<<endl;
65         cout<<"email:     "<<email<<endl;
66     }
67     
68 };
69 
70     int User::n=0;
71     void User::print_n()
72     {
73         std::cout<<"there are"<< n <<"users"<<std::endl;
74     };
75 
76 
77 #endif
View Code

main.cpp:

 1 #include "User.hpp"
 2 #include <iostream>
 3 // 测试User类
 4 void test() {
 5 using std::cout;
 6 using std::endl;
 7 cout << "testing 1......\n";
 8 User user1("Jonny", "92197", "[email protected]");
 9 user1.print_info();
10 cout << endl<< "testing 2......\n\n";
11 User user2("Leonard");user2.print_info();
12 user2.change_passwd();
13 user2.set_email();
14 user2.print_info();
15 cout<<endl;
16 User::print_n();
17 }
18 int main() {
19 test();
20 }
View Code

2.图片:

 

 

任务5:

1.代码:

1.account.h

 1 #ifndef __ACCOUNT_H__
 2 #define __ACCOUNT_H__
 3 
 4 class SavingsAccount { 
 5 private:
 6     int id;
 7     double balance;
 8     double rate;
 9     int lastDate;
10     double accumulation;
11     static double total;
12     void record(int date, double amount);
13     double accumulate(int date) const {
14         return accumulation+balance*(date-lastDate);
15     }
16 public:
17     SavingsAccount(int date, int id, double rate);
18     int getId() const {return id;}
19     double getBalance() const {return balance;}
20     double getRate() const {return rate;}
21     static double getTotal() {return total;}
22     void deposit(int date, double amount);
23     void withdraw(int date, double amount); 
24     void settle(int date);
25     void show() const;
26 };
27 
28 #endif 
View Code

2.account.cpp

 1 #include<cmath>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 double SavingsAccount::total = 0;
 6 SavingsAccount::SavingsAccount(int date, int id, double rate)
 7 :id(id),balance(0),rate(rate),lastDate(date),accumulation(0){
 8     cout << date << "\t#" << id << " is created" << endl;
 9 } 
10 void SavingsAccount::record(int date, double amount) {
11     accumulation=accumulate(date);
12     lastDate = date;
13     amount = floor(amount * 100 + 0.5) / 100; 
14     balance += amount;
15     total += amount;
16     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
17 }
18 void SavingsAccount::deposit(int date, double amount) {
19     record(date, amount);
20 }
21 void SavingsAccount::withdraw(int date, double amount) {
22     if ( amount > getBalance())
23     cout << "Error: not enough money" << endl;
24     else
25     record(date, - amount);
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 }
View Code

3.main.cpp

 1 #include "account.h"
 2 #include <iostream>
 3 using namespace std;
 4 int main() {
 5     SavingsAccount sa0(1, 21325302, 0.015);
 6     SavingsAccount sa1(1, 58320212, 0.015);
 7     sa0.deposit(5, 5000) ;
 8     sa1.deposit(25, 10000);
 9     sa0.deposit(45, 5500);
10     sa1.withdraw(60, 4000);
11     sa0.settle(90);
12     sa1.settle(90);
13     sa0.show();cout << endl;
14     sa1.show();cout << endl;
15     cout << "Total:" << SavingsAccount::getTotal() << endl;
16     return 0;
17 }
View Code

2.图片:

 

标签:const,cout,对象,double,void,编程,试验,int,include
From: https://www.cnblogs.com/yili123/p/17770803.html

相关文章

  • 编程周边辅助
    Vim操作删除空行:g/^$/d删除并包括空白g全局命令对所有与地址匹配的行,%:代表这文件本身每一行,%==g/.*/:g/^\s*$/d:%s/^\s*$\n//非贪婪匹配.\{-}vim查看当前与改动:w!diff%-#然后如果还需要合并,那就只能:w一个新的文件,#然后两个文件使用vimdiff,手动操作合并到......
  • Unix/Linux系统编程自学笔记-第三章:Unix/Linux进程管理
    Unix/Linux系统编程自学笔记-第三章:Unix/Linux进程管理1、概念介绍多任务处理计算机技术概念中的多任务处理指的是同时执行若干独立任务。无论是在多处理机系统还是单处理机系统都可以实现多任务处理。对于单处理机系统,多任务处理的实现依靠着多路复用技术,通过上下文的快速......
  • 从零开始的Java编程:教你如何实现“超级马里奥”游戏!
    引言超级马里奥,这个名字对于游戏迷来说一定不陌生。它是一款经典的游戏系列,以一个勇敢的水管工人——马里奥为主角,讲述了他在蘑菇王国中的冒险故事。在这个充满挑战和刺激的游戏中,玩家需要控制马里奥跳跃、躲避障碍物,并与邪恶的蘑菇和食人花敌人战斗,最终抵达城堡的胜利之地。游......
  • 小程序底层技术机制解读 - JavaScript编程语言
    JavaScript是小程序的核心编程语言之一,它在小程序中起着至关重要的作用。本文将深入探讨JavaScript在小程序底层技术机制中的作用,以及如何利用JavaScript来构建小程序应用。同时,我们还将提供一个简单的代码演示,以帮助读者更好地理解JavaScript在小程序中的应用。JavaScript在小程序......
  • make clean命令清理在不同目录中编译的对象
    gnu-makemakefile UsingMakefiletocleansubdirectories是否可以从父目录执行makeclean,而该父目录又递归清除所有子目录,而不必在每个子目录中都包含makefile?例如,当前在我的Makefile中,我有类似以下内容:123456789SUBDIRS=src,src1.PHONY:cleansubdirs$(S......
  • map遍历数组返回包含所需字段的对象
    假如dataList为后台假数据,我想分别得到number和chargeTime、number和freeTime,来分别画图,就可以这么写,当然直接for循环更可以。1constdataList={2list:[3{4number:"0",5chargeTime:2,6freeTime:57......
  • 实验2 C语言分支与循环基础应用编程
    1.实验任务1task1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));//以当前......
  • Mojo——会燃的 AI 编程语言
    点击链接了解详情导语:本文简介Mojo的背景与特点,并分享如何通过腾讯云Cloudstudio的WebIDE和分享社区快速学习和上手Mojo。......
  • 发现一不错的编程助手 Amazon CodeWhisperer
    AmazonCodeWhisperer 是一款AI编程助手,旨在为开发人员提供智能化的编程辅助工具。作为一款基于人工智能的编程助手,CodeWhisperer 的目标是提高开发人员的生产效率、降低开发成本,并提供高质量的编程解决方案。1.安装过程参考官网https://aws.amazon.com/cn/codewhisperer/re......
  • DM8通过触发器实现用户建表自动为其他用户授予对象权限
    需求业务用户A会周期性的创建临时表,这部分临时表又有需要被其他用户B访问的需求。手动授权仅能将授权时存在的表进行授权,我们尝试通过触发器实现。处理方法--前提1:DDL_TV_TRIGGER=1(默认0,静态参数,添加后需要重启数据库)--前提2:SP_INIT_DBMS_SCHEDULER_SYS(1);/*SYSDBA创......