Homework6_ch5 数据的共享与保护(1)——static

  1. 超市经销大米,成袋购入,成袋售出。每袋大米的质量有2.5公斤的、有5公斤、有10公斤的等,超市记录所有大米的总质量。

设计Rice类,包含私有数据成员: 质量Weight和产地Place;还包含私有静态数据成员: 总质量TotalWeight .

Rice类的公有成员函数:

  1. 构造函数 Rice(double weight=0, string place=”XXX”);  构造新对象的同时总质量要增加;
  2. 拷贝构造函数 Rice(const Rice&); 拷贝构造新对象的同时总质量要增加;
  3. 析构函数~Rice(); 析构对象的同时总质量要减少;
  4. 静态函数static double GetTotal();
//rice.h
#pragma once
#ifndef RICE_H
#define RICE_H
#include<string>
using namespace std;
class Rice {
public:
	Rice(double weight = 0, string place = "XXX");
	Rice(const Rice&);
	~Rice();
	static double GetTotal();
	void Show();
private:
	double Weight;
	string Place;
	static double TotalWeight;
};

#endif

//rice.cpp
#include"rice.h"
#include<iostream>
using namespace std;
Rice::Rice(double weight, string place) {
	Weight = weight;
	Place = place;
	TotalWeight += weight;
}
Rice::Rice(const Rice& p) {
	Weight = p.Weight;
	Place = p.Place;
	TotalWeight += Weight;
}
Rice::~Rice() {
	TotalWeight -= this->Weight;
}
double Rice::GetTotal() {
	return TotalWeight;
}
void Rice::Show() {
	cout << Weight << " " << Place << endl;
}

//main.cpp
#include"rice.h"
#include<iostream>
double Rice::TotalWeight = 0;

int main() {
	Rice r1(2.5, "山东");
	cout << Rice::GetTotal() << endl;
	Rice r2(r1);
	cout << Rice::GetTotal() << endl;
	Rice r3;
	cout << Rice::GetTotal() << endl;
	r2.~Rice();
	cout << Rice::GetTotal() << endl;
	return 0;
}

 设计活期存款类Deposit . 参考图1,每个账户有帐号(自动编号)、户名、密码、金额、月利率(静态成员)、开户时间6项信息。银行每月1号要根据账户余额发给客户利息,利息被累计到客户存款余额中。银行可以更改利率。

帐号(自动编号

户名

密码

余额

月利率(静态数据

开户时间

查询账户信息

调整利率(静态函数)

……

操作

数据

1

本月余额计算方法:    本月余额 = 上月余额 + 上月余额*月利率

 (如有小数出现,保留小数点后2位,四舍五入)

主程序中:建立以下5个账户:

账号         户名        密码     金额   月利率  开户时间

1               张三        123456  10000  0.5%          2017-3-1

2               李四        112233  20000  0.5%          2016-2-1

3               王五        111111  15000  0.5%          2017-5-1

4               赵六        222222  58000  0.5%          2016-7-1

5               周七         888888  50000  0.5%          2017-3-1

程序运行:

2017-7-1,输出所有账户的信息;

2017-8-1,调整利率为0.4%;

2017-9-1, 输出所有账户的信息;

2017-10-1, 输出所有账户的信息;

//deposit.h
#pragma once
#ifndef DEPOSIT_H
#define DEPOSIT_H
#include<string>
#include"date.h"
using namespace std;
class Deposit {
public:
	Deposit();
	void setDeposit(string, string, double, double, int, int, int);
	void Show(const Date&, int);
	void reRate(double);
private:
	int id;
	string usr;
	string password;
	double res;
	static double rate;
	Date stime;
	static int num;
	Date ctime;
};
#endif
//date.h
#pragma once
#ifndef DATE_H
#define DATE_H
class Date {
	friend class Deposit;
public:
	Date();
	Date(int, int, int);
	~Date();
	void SetDate(int, int, int);
	void Show();
private:
	int y, m, d;
};
#endif
//deposit.cpp
#include"deposit.h"
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
void Deposit::Show(const Date& x, int f) {
	double r;
	if (ctime.y == 0)
		r = pow(1 + rate, 12 * (x.y - stime.y) + (x.m - stime.m));
	else r = pow(1 + rate, 12 * (x.y - ctime.y) + (x.m - ctime.m));
	res *= r;
	if (f) {
		cout << fixed;
		cout << setprecision(2);
		cout << setw(4) << id << setw(8) << usr << setw(10) << password << setw(10) << round(res * 100) / 100 << setw(5) << rate * 100 << "%  " << ends;
		stime.Show();
	}
	ctime = x;
}
void Deposit::reRate(double x) {

	rate = x/100.0;
}
void Deposit::setDeposit(string u, string p, double r,double rt, int y, int m, int d) {
	usr = u;
	password = p;
	rate = rt / 100.0;
	res = r;
	stime.SetDate(y, m, d);
}
Deposit::Deposit()
{
	id = num++;
	res = 0;
	ctime.SetDate(0, 0, 0);
}
//date.cpp
#include"date.h"
#include<iostream>
using namespace std;
void Date::Show() {
	cout << y << "-" << m << "-" << d << endl;
}
Date::Date()
{
	y = 0;
	m = 0;
	d = 0;
}
Date::Date(int a, int b, int c) {
	//cout << "已创建日期" << endl;
	y = a;
	m = b;
	d = c;
	//this->Show();
}
Date::~Date() {
	//cout << "删除日期" << endl;
	//this->Show();
}
void Date::SetDate(int a, int b, int c) {
	y = a;
	m = b;
	d = c;
	//this->Show();
}
//main.cpp
#pragma warning(disable:4996)
#include"deposit.h"
#include<stdio.h>
#include<iostream>
using namespace std;
Date now(2017, 7, 1);
int Deposit::num = 0;
double Deposit::rate = 0.005;
int main() {
	//cout.setf(ios_base::fixed, ios_base::floatfield);
	//FILE* f;
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	string usrname, passw, per;
	double money, rate;
	int year, month, day;
	char a, b;
	Deposit dpt[10];
	now.Show();
	for (int i = 1; i <= 5; i++) {
		cin >> usrname >> passw >> money >> rate >> per >> year >> a >> month >> b >> day;
		//cout << usrname << passw << money << rate << year << month << day << endl;
		dpt[i].setDeposit(usrname, passw, money, rate, year, month, day);
		dpt[i].Show(now,1);
	}
	cout << "更改利率" << endl;
	now.SetDate(2017, 8, 1);//八月改7~8按原来的算
	for (int i = 1; i <= 5; i++) dpt[i].Show(now, 0);
	dpt[1].reRate(0.4);
	now.SetDate(2017, 9, 1);
	now.Show();
	for (int i = 1; i <= 5; i++) dpt[i].Show(now, 1);
	now.SetDate(2017, 10, 1);
	now.Show();
	for (int i = 1; i <= 5; i++) dpt[i].Show(now, 1);
	
	fclose(stdin);
	//fclose(stdout);
	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值