首页 > 其他分享 >实验5

实验5

时间:2023-12-03 17:35:49浏览次数:29  
标签:const string double Date 实验 date return

任务5:

 1 #ifndef_ _DATE_H_ _
 2 #define_ _DATE_H_ _
 3 class Date{
 4     private:
 5         int year;
 6         int month;
 7         int day;
 8         int totalDays;
 9         public:
10             Date(int year,int month,int year);
11             int getYear() const{return year;}
12             int getMonth() const{return month;}
13             int getDay() const{return day;}
14             int getMaxDay() const;
15             bool isLeapYear() const{
16             return year%4==0&&year%100!=0||year%400==0;}
17             void show() const;
18             int distance(const Date& date) const{
19             return totalDays-date.totalDays;}
20             int operator-(const Date& date) const{
21             return tptalDays-date.totalDays;}
22 };
23 #endif
date.h
 1 #include"date.h"
 2 #include<iostream>
 3 #include<cstdlib>
 4 using namespace std;
 5 namespace{
 6     const int DAYS_BEFORE_MONTH[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
 7     
 8 }
 9 Date::Date(int year,int month,int day):year(year),month(month),day(day){
10     if(day<=0||day>getMaxDay()){
11         cout<<"Invalid date: ";
12         show();
13         cout<<endl;
14         exit(1);
15     }
16     int years=year-1;
17     totalDays=years*356+years/4-years/100+years/400+DAYS_BEFORE_MONTH[month-1]+day;
18     if(isLeapYear()&&month>2) totalDays++;
19 }
20 int Date::getMaxDay() const{
21 if(isLeapYear()&&month==2)
22 return 29;
23 else
24 return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1];}
25 void Date::show() const {
26 cout<<getYear()<<"-"<<getMonth()<<"-"<<getDay();}
date.cpp
 1 #ifndef_ _ACCUMULATOR_H_ _
 2 #define_ _ACCUMULATOR_H_ _
 3 #include"date.h"
 4 class Accumulator{
 5     private:
 6         Date lastDate;
 7         double value;
 8         double sum;
 9         public:
10             double getSum(const Date &date) const{
11             return sum+value*(date-lastDate);}
12             Accumulator(const Date &date,double value)
13             :lastDate(date),value(value),sum(0){
14             }
15             double getSum(const Date& date) const{
16             return sum+value*date.distance(lastDate);}
17             void change(const Date &date,double value){
18                 lastDate=date;this->value=value;
19             }
20             void reset(const Date& date,double value){
21                 lastDate=date;this->value=value;sum=0;
22             }
23 };
24 #endif
accumulator.h
 1 #ifndef_ _ACCOUNT_H_ _
 2 #define_ _ACCOUNT_H_ _
 3 #include"date.h"
 4 #include"accumulator.h"
 5 #include<string>
 6 class Account{
 7     private:
 8         std::string id;
 9         double balance;
10         static double total;
11         protected:
12             Account(const Date &date,const std::string &id);
13             void record(const Date &date,double amount ,const std::string &desc);
14             void error(const std::string &msg) const;
15             public:
16                 const std::string &getId() const{return id;}
17                 public:
18                     const std::string &getId() const{return id;}
19                     double getBalance() const{return balance;}
20                     static double getTotal() {return total;
21                     }
22                     virtual void deposit(const Date &date,double amount,const std::string &desc)=0;
23                     virtual void withdraw(const Date &date,double amount,const std::string &desc)=0;
24                     virtual void settle(const Date &date)=0;
25                     virtual void show() const;
26 };
27 class SavingAccount:public Account{
28     private:
29         Accumulator acc;
30         double rate;
31         public:
32             SavingAccount(const Date &date,const std::string &id,double rate);
33             double getRate() const{return rate;}
34             void deposit(const Date &date,double amount,const std::string &desc);
35             void withdraw(const Date &date,double amount,const std::string &desc);
36             void settle(const Date &date);
37             
38 };
39 class CreditAccount:public Account{
40     private:
41         Accumulator acc;
42         double credit;
43         double rate;
44         double fee;
45         double getDebt() const{
46         double balance=getBalance();
47         return (balance<0?balance:0);}
48         public:
49             CreditAccount(const Date &date,const std::string &id,double credit,double rate,double fee);
50             double getCredit() const{return credit;}
51             double getRate() const{return rate;}
52             double getFee() const{return fee;}
53             double getAvailableCredit() const{
54             if(getBalance()<0)
55             return credit+getBalance();
56             else
57             return credit;}
58             void deposit(const Date &date,double amount,const std::string &desc);
59             void withdraw(const Date &date,double amount,const std::string &desc);
60             void settle(const Date &date);
61             void show() const;
62             
63 };
account.h
 1 #include"account.h"
 2 #include<cmath>
 3 #include<iostream>
 4 using namespace std;
 5 double Account::total=0;
 6 Account::Account(const Date &date,const string &id):id(id),balance(0){
 7     date.show()
 8 ;cout<<"\t#"<<id<<"created"<<endl;}
 9 void Account::record(const Date &date,double amount ,const string &desc){
10     amount=floor(amount*100+0.5)/100;
11     balance+=amount;total+=amount;
12     date.show()
13 ;cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;}
14 void Account::show() const{cout<<id<<"\tBalance: "<<balance;}
15 void Account::error(const string &msg) const{
16 cout<<"Error(#"<<id<<"): "<<msg<<endl;}
17 SavingsAccount::SavingsAccount(const Date &date,const string &id,double rate):
18     Account(date,id),rate(rate),acc(date,0){
19     }
20     void SavingsAccount::deposit(const Date &date,double amount,const string &desc){
21         record(date,amount,desc);
22         acc.change(date,getBalance());
23     }
24     void SavingsAccount::withdraw(const Date &date,double amount,const string &desc){
25         if(amount>getBalance()){
26             error("not enough money");
27             
28         }else{
29             record(date,-amount,desc);
30             acc.change(date,getBalance());
31         }
32     }
33     void SavingsAccount::settle(const Date &date){
34         if(date.getMonth()==1){
35             double interest=acc.getSum(date)*rate/(date-Date(date.getYear()-1,1,1));
36             if(interest!=0)
37             record(date,interest,"interest");
38             acc.reset(date,getBalance());
39         }
40     }
41     CreditAccount::CreditAccount(const Date& date,const string &id,double credit,double rate,double fee)
42     :Account(date,id),credit(credit),rate(rate),fee(fee),acc(date,0){
43     }
44     void CreditAccount::deposit(const Date &date,double amount,const string &desc){
45         if(amount-getBalance()>credit){
46             error("not enough credit");
47             
48         }else {
49             record(date,-amount,desc);
50             acc.change(date,getDebt());
51         }
52     }
53     void CreditAccount::settle(const Date &date){
54         double interest=acc.getSum(date)*rate;
55         if(interest!=0)record(date,interest,"interest");
56         if(date.getMonth()==1)
57         record(date,-fee,"annual fee");
58         acc.reset(date,getDebt());
59     }
60     void CreditAccount::show() const{
61     Account::show();
62     cout<<"\tAvailable credit: "<<getAvailableCredit();}
63     
64     
account.cpp
 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main(){
 5     Date date(2008,11,1);
 6     SavingAccount sa1(date,"S3755217",0.015);
 7     SavingAccount sa2(date,"02342342",0.015);
 8     CreditAccount  ca(date,"c5392394",10000,0.0005,50);
 9     Account*accounts[]={&sa1,&sa2,&ca};
10     const int n=sizeof(accounts)/sizeof(Account*);
11     cout<<"(d)deposit(w)withdraw(s)show(c)change day(n)next month (e)exit"<<endl;
12     char cmd;
13     do{
14         date.show();
15         cout<<"\tTotal: "<<Account::getTotal()<<"\tcommand>";
16         int index,day;
17         double amount;
18         string desc;
19         cin>>cmd;
20         swith(cmd){
21             case'd':
22                 cin>>index>>amount;
23                 getline(cin,desc);
24                 accounts[index]->withdraw(date,amount,desc);
25                 break;
26                 case'w':
27                     cin>>index>>amount;
28                     getline(cin,desc);
29                     accounts[index]->withdraw(date,amount,desc);
30                     break;
31                     case's':
32                         for(int i=0;i<n;i++){
33                             cout<<"["<<i<<"]";
34                             accounts[i]->show();
35                             cout<<endl;
36                         }
37                         break;
38         }
39         case'c':
40             cin>>day;
41             if(day<date.getDay())
42                 cout<<"You cannot specify a previous day";
43                 
44             else if(day>date.getMaxDay())
45             cout<<"Invalid day";
46             else
47             date=Date(date.getYear(),date.getMonth(),day);
48             break;
49             case'n':
50                 if(date.getMonth()==12)
51                 date=Date(date.getYear()+1,1,1);
52                 else
53                 date=Date(date.getYear(),date.getMonth()+1,1);
54                 for(int i=0;i<n;i++)
55                 accounts[i]->settle(date);
56                 break;}
57     }while(cmd!='e');
58 return 0;
59 }
8_8.cpp

 

任务3:

#pragma once
#include<iostream>
#include<string>
using namespace std;
class MachinePets{
    public:
        MachinePets(const string s):nickname(s){}
        public:
    virtual     string talk()=0;
string get_nickname(){
    return nickname;
}
        
    
    private:
        string nickname;
    
};
class PetCats: public MachinePets{
    public:
        PetCats(const string s):MachinePets(s){
        }
    
        string talk() override{
            return "miao wu~";
        }
    
};
class PetDogs:public MachinePets{
    public:
        PetDogs(const string s):MachinePets(s){
        }
        
        string talk() override{
            return "wang wang ~";
        }
};
pets.hpp
 1 #include <iostream>
 2 #include "pets.hpp"
 3 void play(MachinePets &obj) {
 4 std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
 5 }
 6 void test() {
 7 PetCats cat("miku");
 8 PetDogs dog("da huang");
 9 play( cat );
10 play( dog );
11 }
12 int main() {
13 test();
14 }
task3.cpp

 

任务4:

 1 #pragma once
 2 #include<string>
 3 #include<iostream>
 4 
 5 
 6 class Person{
 7     private:
 8         string name,telephone,email;
 9         public:
10             Person();
11             Person(const string &a,const string &b,const string &c=" "):name(a),telephone(b),email(c){
12             }
13             Person(const Person& c):name(c.name),telephone(c.telephone),email(c.email){
14             }
15             void update_telephone(){
16                 cout<<"请输入新的手机号码:"<<endl;
17                 cin>>telephone;
18             }
19             void update_email(){
20                 cout<<"请输入新的邮箱地址:"<<endl;
21                 cin>>email;
22             }
23             friend std::ostream& operator<<(std::ostream& out,const Person& p){
24                 out<<"name: "<<p.name<<"telephone: "<<p.telephone<<"email: "<<p.email<<endl;
25                 return out;
26             }
27             //重载插入运算符<<为友元函数,支持使用诸如 cout << p << endl; 的方式输出联系人信息 
28           friend std::istream& operator>>(std::istream& in,  Person& p){
29               in>>p.name>>p.telephone>>p.email;
30               return in;
31           }
32             //重载提取运算符>>为友元函数,支持使用诸如 cin >> p 的方式输入联系人信息
33             friend bool operator==(const Person& p1,const Person &p2){
34                 return p1.name==p2.name&&p1.telephone==p2.telephone&&p1.email==p2.email;
35             }
36 //            重载关系运算符==为友元函数,支持对两个联系人信息进行关系判断。
37 //只有当姓名和手机号都相同时,才返回true,否则,返回false。
38 };
Person.hpp
 1 #include <iostream>
 2 #include <vector>
 3 #include "Person.hpp"
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     vector<Person> phone_book;
 9     Person p;
10 
11     cout << "输入一组联系人的联系方式,E直至按下Ctrl+Z终止\n";
12     while(cin >> p) 
13         phone_book.push_back(p);
14     
15     cout << "\n更新phone_book中索引为0的联系人的手机号、邮箱:\n";
16     phone_book.at(0).update_telephone();
17     phone_book.at(0).update_email();
18 
19     cout << "\n测试两个联系人是否是同一个:\n";
20     cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
21 }
22 
23 int main() {
24     test();
25 }
task4.cpp

标签:const,string,double,Date,实验,date,return
From: https://www.cnblogs.com/shaoshuai-nuist/p/17873435.html

相关文章

  • 实验5 继承和多态
    四、实验结论实验任务3pets.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststring&s="");stringget_nickname()const;public:virtualstringtalk()......
  • 实验5
    #include<iostream>#include<string>usingnamespacestd;classMachinePets{private:stringnickname;public:MachinePets(conststrings):nickname{s}{}stringget_nickname()const{returnnickname;}virtualstringtalk()......
  • 实验五
     找出最大最小值 指向x【0】的地址 返回最大值的地址  sizeof计算数据类型所占据的空间,比如字节长度,而strlen计算字符串的长度,从第一个字符遇到结束符停止。sizeof包含“\0”,strlen则不计算。     #defineN80voidencoder(char*str);//函数声......
  • 百度api实验总结
    通过这次实验还是反映了之前讲到的,修改代码必须要建立在看懂的情况下,同样避免错误也是一样的,刚开始拿到这样一个json返回值的时候很蒙,饭后想到了之前看到的提取其中的值,我就说查一下吧查到了不过是python的不过原理都一样我就说改改吧但是不成功,用为python的json包导进去很简单,直......
  • 实验5
    3.hpp#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings);MachinePets();stringget_nickname()const;public:virtualstringtalk()=0;protected:......
  • C0P8000计算机组成原理实验系统24位控制位功能
    因为做到了这个课设所以存一下相关内容24位控制位XRD:外部设备读信号,当给出了外设的地址后,输出此信号,从指定外设读数据。EMWR:程序存储器EM写信号。EMRD:程序存储器EM读信号。PCOE:将程序计数器PC的值送到地址总线ABUS上。EMEN:将程序存储器EM与数据总线DBUS......
  • 实验五
    任务一publisher.hpp1#pragmaonce23#include<iostream>4#include<string>56usingstd::cout;7usingstd::endl;8usingstd::string;910//发行/出版物类:Publisher(抽象类)11classPublisher{12public:13Publisher(conststr......
  • .Net实验一 语言基础
    一、实验目的熟悉VisualStido.NET实验环境;掌握控制台程序的编写方法;掌握C#程序设计语言的语法基础;掌握控制语句和数组的使用。二、实验要求根据题目要求,编写C#程序,并将程序代码和运行结果写入实验报告。三、实验内容编写一个控制台应用程序,输入三角形或者长方形边长,计......
  • 软件设计实验 24:模板方法模式
      实验24:模板方法模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解模板方法模式的动机,掌握该模式的结构;2、能够利用模板方法模式解决实际问题。 [实验任务一]:数据库连接对数据库的操作一般包括连接、打开、使用、关闭等步骤,在数据库操作模板类中我......
  • 第五次实验
    任务1-1//验证性实验:指针变量作为函数参数,指针变量作为函数的返回值#include<stdio.h>#defineN5voidinput(intx[],intn);voidoutput(intx[],intn);voidfind_min_max(intx[],intn,int*pmin,int*pamx);intmain(){ inta[N]; intmin,max; printf("录入%d......