南京邮电大学程序设计(流量套餐上网计费系统)

1 题目

程序设计题三:上网计费系统模拟

1.1 问题描述

本程序模拟根据上网清单、客户资料等生成客户上网账单的过程,该系统主要功能包括促销信息、待购信息的新增、删除、修改、查找等。所有数据都要利用文件系统保存,以备系统下次运行时使用。通过此课题,熟练掌握文件、数组、指针的各种操作,以及一些基本算法的应用。
要求编写一个程序,实现对上网费用的计算。

1.2 功能要求

代码要能提供以下几个基本功能。
(1)文件管理和存储
1)用户资料文件,存放了用户名、用户帐号以及计费方式。格式如下:
Zhang Qiang 0001 0
Liu Hui 0002 2
Ke Lei 0003 4
其中,计费方式的含义如下:
0 无任何优惠活动,按每分钟 0.03元计算。
1 每月 50元包 30个小时,如上网时长在 30小时之内(含 30小时),应缴 50元,超过部分按每分钟 0.03元计算。
2 每月 95元包 60个小时,如上网时长在 60小时之内(含 60小时),应缴 95元,超过部分按每分钟 0.03元计算。
3 每月 200元包 150个小时,如上网时长在 150小时之内(含 150小时),应缴 200元,超过部分按每分钟 0.03元计算。
4 整包,每月缴 300元,上网时长不限。
2)上网记录文件,存放了所有用户的上网记录,内容包括用户帐号、上网起始时间和上网终止时间(格式:年月日小时分钟秒)。通常每个用户应有多条上网记录。该文件的格式如下:
0003 20151201170000 20151201193024
0001 20151202190006 20151202210000
0003 20151222211343 20151223012558
(2)功能要求
1)用户资料文件由程序设计人员预先从键盘上录入,用户资料文件中的数据记录不得少于 15条。
2)上网记录文件中的数据记录不得少于 150条,首先录入一些上网记录信息,必须有跨年、跨月份的上网记录。
3)用户资料文件及上网记录文件中的数据可随时增加或减少,程序不应进行限制或限定。
4)对上网费用进行计算。首先由操作人员输入年份和月,然后由程序计算该月份所有用户的上网费用并输出账单信息到指定文件夹,格式如下:
0001 Zhang Qiang 0 xxxx xxxxx
0002。。
计费时,对于每条上网记录,不足一分钟按一分钟计算。对于跨年或跨月份的记录,只计算发生在当月的费用。例如对于 2016年 1月 31日 15:00:00至 2016年 2月 1日 01:00:00的上网记录,在计算 2016年 1月的上网费时,仅计算 2016年 1月 31日 15:00:00至 2016年 2月 1日 00:00:00的费用;在计算 2016年 2月的上网费时,仅计算 2016年 2月 1日 00:00:00至 2016年 2月 1日 01:00:00的费用。处理时须考虑闰年的情况。
选做要求:
(1)随机生成一批上网记录信息,并判断是否正确,不正确的信息剔除。
(2)设计用户可能需要的报表,或者建议该系统中还需要采集哪些信息,以便更好的提供用户需要的信息。

1.3 其他要求

(1)界面美观,交互方便。
(2)注释详细:每个变量都要求有注释说明用途;函数有注释说明功能,对参数、返回值也要以注释的形式说明用途;关键的语句段要求有注释解释。
(3)程序的层次清晰,可读性强。
(4)变量、函数命名符合规范。
(5)如有可能,可使用MFC 等开发工具,实现彩色或图形操作界面。

4 开发环境
可以选择TC2.0、TC3.0、VC++6.0等开发环境,或者与老师讨论,选择自己熟悉的开发工具与平台。

*程序设计方法
(1)根据促销或购物的要求,设计合适的数据结构(如结构数组、链表、对象数组等实现)。
(2)选择文件存储格式(文本或二进制)和文件名。
(3)采用自顶向下的设计方法,设计系统的总体模块组成和数据结构。设计主程序的流程图和界面。
(4)对于每项操作,设计一个函数(方法、子模块)进行实现,要明确函数原型。
(5)根据设计结果分步开发实现各模块,最后进行总体集成。

2 实验报告

2.1需求分析

上网计费系统的功能框架图如图1所示。
在这里插入图片描述

图1功能框架图

2.2 概要设计

2.2.1 主要存储结构

1.	//客户类
2.	class Customer  
3.	{  
4.	private:  
5.	    string Name;                                        //用户姓名  
6.	    string Id;                                          //用户Id  
7.	    int BillingMethod = 0;                              //计费方式  
8.	    int TotalOnlineTime = 0;                            //总上网时长  
9.	    int MonthOnlineTime = 0;                            //月上网时长  
10.	    double TotalOnlineCost = 0;                         //总上网费  
11.	    double MonthOnlineCost = 0;                         //月上网费  
12.	    vector<pair<Date, Date>> OnlineRecords;   //所有上网记录(用于日期计算)  
13.	    vector<pair<string, string>> rawOnlineRecords;      //原始上网记录数据  
14.	public:  
15.	    Customer();                                 //重写默认构造函数  
16.	    Customer(string, string, int);              //自定义构造函数  
17.	    int getTotalOnlineTime();                   //获取总上网时间  
18.	    string getId();                             //get函数返回Id  
19.	    string getName();                           //set函数返回Name  
20.	    int getBillingMethod();                    //get函数返回BillingMethod  
21.	    double getTotalOnlineCost();                //获取总费用  
22.	    double getOnlineCostByMonth(string, string);//获取月费用  
23.	    void addOnlineRecord(string, string);       //添加记录  
24.	    ~Customer();                                //重写析构函数  
25.	};  

2.2.2主要函数流程

(1)装载函数:用于从文本中载入信息并在内存中创建客户,将每个客户的上网记录注入到内存中进行相关数据的计算。将内存中的函数的返回值进行循环存入文件。
在这里插入图片描述

图2装载函数
(2)计算某月消费函数
在这里插入图片描述

图3计算账单函数

3 代码

在这里插入图片描述
Customer.h

#pragma once
#include <string>
#include <iostream>
#include<vector>
#include "Date.h"
using namespace std;

class Customer
{
private:
	string Name;										//用户姓名
	string Id;											//用户Id
	int BillingMethod = 0;								//计费方式
	int TotalOnlineTime = 0;							//总上网时长
	int MonthOnlineTime = 0;							//月上网时长
	double TotalOnlineCost = 0;							//总上网费
	double MonthOnlineCost = 0;							//月上网费
	vector<pair<Date, Date>> OnlineRecords;				//所有上网记录(用于日期计算)
	vector<pair<string, string>> rawOnlineRecords;		//原始上网记录数据
public:
	Customer();									//重写默认构造函数
	Customer(string, string, int);				//自定义构造函数
	int getTotalOnlineTime();					//获取总上网时间
	string getId();								//get函数返回Id
	string getName();							//set函数返回Name
	int getBillingMethod();						//get函数返回BillingMethod
	double getTotalOnlineCost();				//获取总费用
	double getOnlineCostByMonth(string, string);//获取月费用
	void addOnlineRecord(string, string);		//添加记录
	~Customer();								//重写析构函数
};

Date.h

#include <iostream>
using namespace std;
#include<string>
#define HOUR 60
#define DAY 1440
class Date
{
public:
	int year;
	int month;
	int day;
	int hour;
	int minute;
	int second;
public:
	Date(int year = 2000, short month = 1, short day = 1, int hour = 0, int minute = 0, int second = 0);
	Date(string date);
};

int dayNumber(int year, int mon, int day);
int yearNumber(int year1, int year2);
int calculateOnlineTime(Date begin, Date end);

File.h

#include<iostream>
using namespace std;

void read(string);						//读取文件内容
void addCustomer(string);				//增加用户
void addRecord(string);					//增加上网记录
void delCustomer(string, string);		//删除某一用户
void delRecord(string);					//删除上网记录(输入下标)
void readOneCustomerRecors(string);		//读取一位客户的记录
void getTotalCostByMonth(string, string);		//打印某用户的某月上网费用(OK)
void getMonthCost(string, string, string);		//计算某月所有用户的上网费用(OK)
int _getBillingMethod(string, string);			//输入ID返回计费方式
void creatRandRecords(string, string);	//创建随机客户列表和记录
void emptyData(string, string, string);			//清空数据

FiveBillingMethod.h

#define HOUR 60.0
double case0(int);		//方式0(每分钟0.03)
double case1(int);		//方式1()
double case2(int);		//方式2()
double case3(int);		//方式3()
double case4(int);		//方式4()

Time.h

#include "Date.h"
int yearNumber(int year1, int year2);			//返回日期在当年是第几天
int dayNumber(int year, int mon, int day);
int calculateOnlineTime(Date begin, Date end);

Customer.cpp

#include"Customer.h"
#include "FiveBillingMethod.h"
#include "File.h"
#include "FiveBillingMethod.h"


//重写默认构造函数
Customer::Customer() {}

//自定义有参构造函数
Customer::Customer(string id, string name, int billingMethod)
{
	this->Id = id;
	this->Name = name;
	this->BillingMethod = billingMethod;
}

//get函数返回客户ID
string Customer::getId()
{
	return Id;
}

//get函数返回客户名称
string Customer::getName()
{
	return Name;
}

//get函数返回计费方式
int Customer::getBillingMethod()
{
	return BillingMethod;
}

//返回全部上网时间
int Customer::getTotalOnlineTime()
{
	for (vector<pair<Date, Date>>::iterator it = OnlineRecords.begin(); it != OnlineRecords.end(); it++)
	{
		TotalOnlineTime += calculateOnlineTime(it->first, it->second);
	}
	return TotalOnlineTime;
}

//由总时间计算总费用
double Customer::getTotalOnlineCost()
{
	switch (this->BillingMethod)
	{
	case 0:TotalOnlineCost = case0(TotalOnlineTime); break;
	case 1:TotalOnlineCost = case1(TotalOnlineTime); break;
	case 2:TotalOnlineCost = case2(TotalOnlineTime); break;
	case 3:TotalOnlineCost = case3(TotalOnlineTime); break;
	case 4:TotalOnlineCost = case4(TotalOnlineTime); break;
	}
	return TotalOnlineCost;
}

//返回一个月的费用
double Customer::getOnlineCostByMonth(string s_year, string s_month)
{
	int month = stoi(s_month.c_str());
	int year = stoi(s_year.c_str());
	MonthOnlineTime = 0;
	int i = 0;
	vector<pair<string, string>>::iterator it_string = rawOnlineRecords.begin();
	vector<pair<Date, Date>>::iterator it_Date = OnlineRecords.begin();
	for (; it_Date != OnlineRecords.end(); it_Date++, it_string++)
	{
		pair<Date, Date> tmpPair(it_Date->first, it_Date->second);
		if (tmpPair.first.year < year || (tmpPair.first.year == year && tmpPair.first.month < month))
		{
			tmpPair.first.year = year;
			tmpPair.first.month = month;
			tmpPair.first.day = 1;
			tmpPair.first.hour = 0;
			tmpPair.first.minute = 0;
			tmpPair.first.second = 0;
		}
		if (tmpPair.first.year > year)
		{
			continue;
		}
		if (tmpPair.second.year < year)
		{
			continue;
		}
		if (tmpPair.first.year == year && tmpPair.first.month > month)
		{
			continue;
		}
		if (tmpPair.second.year == year && tmpPair.second.month < month)
		{
			continue;
		}
		if (tmpPair.second.year > year || (tmpPair.second.year == year && tmpPair.second.month > month))
		{                                                                                                        
			tmpPair.second.year = year;
			tmpPair.second.month = ++month;
			tmpPair.second.day = 1;
			tmpPair.second.hour = 0;
			tmpPair.second.minute = 0;
			tmpPair.second.second = 0;
		}
		MonthOnlineTime += calculateOnlineTime(tmpPair.first, tmpPair.second);
	}
	switch (BillingMethod)
	{
	case 0:MonthOnlineCost = case0(MonthOnlineTime); break;
	case 1:MonthOnlineCost = case1(MonthOnlineTime); break;
	case 2:MonthOnlineCost = case2(MonthOnlineTime); break;
	case 3:MonthOnlineCost = case3(MonthOnlineTime); break;
	case 4:MonthOnlineCost = case4(MonthOnlineTime); break;
	}
	return MonthOnlineCost;
}

//添加记录
void Customer::addOnlineRecord(string begin, string end)
{
	Date tmpTimeBegin(begin);
	Date tmpTimeEnd(end);
	pair<Date, Date> OnlineRecord(tmpTimeBegin, tmpTimeEnd);
	pair<string, string> rawOnlineRecord(begin, end);
	OnlineRecords.push_back(OnlineRecord);          //转化Date类型数据
	rawOnlineRecords.push_back(rawOnlineRecord);	//将原始数据存入
}

//重写析构函数
Customer::~Customer()
{

}

Date.cpp

#include"Date.h"
//自定义有参构造函数
Date::Date(int year, short month, short day,
	int hour, int minute, int second)
{
	this->year = year;
	this->month = month;
	this->day = day;
	this->hour = hour;
	this->minute = minute;
	this->second = second;
}

//分割字符串,转存为时间格式
Date::Date(string date)
{
	this->year = stoi(date.substr(0, 4).c_str());
	this->month = stoi(date.substr(4, 2).c_str());
	this->day = stoi(date.substr(6, 2).c_str());
	this->hour = stoi(date.substr(8, 2).c_str());
	this->minute = stoi(date.substr(10, 2).c_str());
	this->second = stoi(date.substr(12, 2).c_str());
}

File.cpp

#include "File.h"
#include <sstream>
#include "Customer.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

//读取文件内容(OK)
void read(string filename)
{
	string first, second, third, estr;
	int i = 0;
	fstream in;
	in.open(filename, ios::in);
	while (getline(in, estr))
	{
		istringstream is(estr);
		is >> first >> second >> third;
		cout << first << "\t" << second << "\t" << third << "\t" << "(" << i++ << ")" << endl;
		estr = "";
	}
	in.close();
}

//读取某用户全部的上网记录(OK)
void readOneCustomerRecors(string filename)
{
	int i = 0;
	fstream in, inFile;
	string id, estr;
	cout << "请输入要查找的的客户编号" << endl;
	cin >> id;
	in.open(filename, ios::in);
	while (getline(in, estr))
	{
		string fileId = estr.substr(0, 4);
		if (fileId == id)
		{
			cout << estr.c_str() << " " << "(" << ++i << ")" << endl;
		}
	}
	in.close();
}

//以追加方式记录报表(OK)
void tabel(string filename, string line)
{
	fstream ofile;
	ofile.open(filename, ios::app);
	ofile << line << endl;
	ofile.close();
}

//计算某用户某月的消费(OK)
void getTotalCostByMonth(string record, string customer)
{
	string id, estr, month, year;
	fstream in, inFile;
	string fileId, begin, end;
	in.open(record, ios::in);
	cout << "请输入要计算的的客户编号 年份 月份" << endl;
	cin >> id >> year>> month;
	Customer c(id, "0", _getBillingMethod(id, customer));
	while (getline(in, estr))
	{
		istringstream is(estr);
		is >> fileId >> begin >> end;
		if (fileId == id)
		{
			c.addOnlineRecord(begin, end);
		}
	}
	cout << id << " " << year << "年" << month << "月消费 " << c.getOnlineCostByMonth(year, month) << "元" << endl;
	in.close();
}

//计算所有用户某月的消费(OK)
void getMonthCost(string customers, string records, string tabels)
{
	fstream in, in2, inFile, inFile2;
	string fileId, begin, end, line, s_method, name, _id, bill, id, estr, estr2, month, year;
	in2.open(customers, ios::in);
	cout << "请输入要计算的年份 月份" << endl;
	cin >> year >> month;
	line ="****** "+ year + " 年 " + month + " 月 " + "报表如下 ******";
	tabel(tabels, line);
	while (getline(in2, estr2))
	{
		istringstream is2(estr2);
		is2 >> name >> _id >> s_method;
		id = _id;
		Customer c(id, "0", stoi(s_method.c_str()));
		in.open(records, ios::in);
		while (getline(in, estr))
		{
			istringstream is(estr);
			is >> fileId >> begin >> end;
			if (fileId == id)
			{
				c.addOnlineRecord(begin, end);
			}
		}
		in.close();
		cout << id << " " << year << "年" << month << "月消费 " << c.getOnlineCostByMonth(year, month) << "元" << endl;
		//写入报表文件
		line = id + "\t" + name + "\t" + s_method + "\t" + to_string(c.getOnlineCostByMonth(year, month)).substr(0, 8) + "\t\tyuan";
		tabel(tabels, line);
	}
	in2.close();
}

//输入id返回计费方式(OK)
int _getBillingMethod(string id, string customer)
{
	string line, estr, fileId, fileName, fileBillingMethod;
	fstream in(customer, ios::in);
	//fstream out("D:\\temp.txt", ios::out);//临时文件
	while (getline(in, estr))
	{
		istringstream is(estr);
		is >> fileName >> fileId >> fileBillingMethod;
		if (fileId == id)
		{
			in.close();
			//out.close();
			return stoi(fileBillingMethod.c_str());
		}
	}
	return -1;
}

//增加用户(OK)
void addCustomer(string customer)
{
	string name, id;
	fstream ofile1;
	int billingMethod;
	cout << "请输入用户名 ID 计费方式" << endl;
	cin >> name >> id;
	cin >> billingMethod;
	ofile1.open(customer, ios::app);
	ofile1 << name << " " << id << " " << billingMethod << endl;
	ofile1.close();
}

//增加上网记录(OK)
void addRecord(string record)
{
	fstream ofile2;
	string id, begin, end;
	cout << "请输入用户ID 起始时间 终止时间(空格分隔)" << endl;
	cin >> id >> begin >> end;
	ofile2.open(record, ios::app);
	ofile2 << id << " " << begin << " " << end << endl;
	ofile2.close();
}

//删除某一用户(要把客户和客户记录都删除)
void delCustomer(string customer, string record)
{
	string fileName, fileIdcustomer, fileBillingMethod, fileIdrecord, begin, end, estrcustomer, estrrecord, id;
	fstream fcustomer(customer, ios::in);
	fstream frecord(record, ios::in);
	fstream oucustomert("D:\\temp.txt", ios::out);//临时文件
	fstream outrecord("D:\\temp2.txt", ios::out);//临时文件
	cout << "客户列表如下" << endl;
	read(customer);
	cout << "输入要删除的用户ID" << endl;
	cin >> id;
	while (getline(fcustomer, estrcustomer))
	{
		istringstream is(estrcustomer);
		is >> fileName >> fileIdcustomer >> fileBillingMethod;
		if (fileIdcustomer != id)
		{
			oucustomert << estrcustomer << endl;
		}
	}
	fcustomer.close();
	oucustomert.close();
	while (getline(frecord, estrrecord))
	{
		istringstream is2(estrrecord);
		is2 >> fileIdrecord >> begin >> end;
		if (fileIdrecord != id)
		{
			outrecord << estrrecord << endl;
		}
	}
	frecord.close();
	outrecord.close();

	fstream outfile(customer, ios::out);
	fstream infile("D:\\temp.txt", ios::in);
	while (getline(infile, estrcustomer))
	{
		outfile << estrcustomer << "\n";
	}
	outfile.close();
	infile.close();

	fstream outfile2(record, ios::out);
	fstream infile2("D:\\temp2.txt", ios::in);
	while (getline(infile2, estrrecord))
	{
		outfile2 << estrrecord << "\n";
	}
	outfile2.close();
	infile2.close();

	//删去临时文件
	const char* path = "D:\\temp.txt";
	remove(path);
	const char* path2 = "D:\\temp2.txt";
	remove(path2);
	cout << "新的客户列表如下" << endl;
	read(customer);
}

//删除上网记录(下标)(OK)
void delRecord(string filename)
{
	read(filename);
	int _index;
	cout << "请输入要删除的上网记录编号" << endl;
	cin >> _index;
	string line, estr;
	fstream in(filename, ios::in);
	fstream out("D:\\temp.txt", ios::out);
	int index = -1;
	while (getline(in, estr))
	{
		index++;
		if (index != _index)
		{
			out << estr << "\n";
		}
	}
	in.close();
	out.close();

	fstream outfile(filename, ios::out);
	fstream infile("D:\\temp.txt", ios::in);
	while (getline(infile, estr))
	{
		outfile << estr << "\n";
	}
	outfile.close();
	infile.close();
	const char* path = "D:\\temp.txt";
	remove(path);
	cout << "删除成功,新的所有记录如下:" << endl;
	read(filename);
}

//生成随机客户列表和记录
void creatRandRecords(string customer, string record)
{
	srand((unsigned int)time(NULL));
	int year, month, day, hour, minute, second, b = 0, year2, month2, day2, hour2, minute2, second2;
	string k[15]{ "0001","0002","0003","0004","0005","0006","0007","0008","0009","0010","0011","0012","0013","0014","0015" };
	string month1, day1, hour1, minute1, second1, begin, end, name, id, method;
	ofstream ofile1, ofile2;
	bool flag = 1;
	ofile1.open(customer);
	ofile1.close();
	for (int s = 0; s < 15; s++)
	{
		string n = "0";
		int f = rand() % 5;
		method = to_string(f);
		ofile1.open(customer, ios::app);
		ofile1 << n << " " << k[s] << " " << method << endl;
		ofile1.close();
	}
	ofile2.open(record);
	ofile2.close();
	for (int i = 15; i > 0; )
	{
		int a = rand() % 15;
		year = rand() % 10 + 2010;

		month = rand() % 12 + 1;
		if (month - 10 < 0)
			month1 = to_string(b) + to_string(month);
		else
			month1 = to_string(month);

		day = rand() % 25 + 1;

		if (day - 10 < 0)
			day1 = to_string(b) + to_string(day);
		else
			day1 = to_string(day);

		hour = rand() % 24;
		if (hour - 10 < 0)
			hour1 = to_string(b) + to_string(hour);
		else
			hour1 = to_string(hour);

		minute = rand() % 60;
		if (minute - 10 < 0)
			minute1 = to_string(b) + to_string(minute);
		else
			minute1 = to_string(minute);

		second = rand() % 60;
		if (second - 10 < 0)
			second1 = to_string(b) + to_string(second);
		else
			second1 = to_string(second);

		begin = to_string(year) + month1 + day1 + hour1 + minute1 + second1;

		year2 = year + rand() % 3;

		month2 = rand() % 12 + 1;
		if (month > month2)
			continue;
		if (month2 - 10 < 0)
			month1 = to_string(b) + to_string(month2);
		else
			month1 = to_string(month2);
		day2 = day + rand() % 5 + 1;
		if (day2 - 10 < 0)
			day1 = to_string(b) + to_string(day2);
		else
			day1 = to_string(day2);
		if (day > day2)
			continue;
		hour2 = rand() % 24;
		if (hour2 - 10 < 0)
			hour1 = to_string(b) + to_string(hour2);
		else
			hour1 = to_string(hour2);
		if (hour > hour2)
			continue;
		minute2 = rand() % 60;
		if (minute2 - 10 < 0)
			minute1 = to_string(b) + to_string(minute2);
		else
			minute1 = to_string(minute2);
		if (minute > minute2)
			continue;
		second2 = rand() % 60;
		if (second2 - 10 < 0)
			second1 = to_string(b) + to_string(second2);
		else
			second1 = to_string(second2);
		if (second >= second2)
			continue;

		end = to_string(year2) + month1 + day1 + hour1 + minute1 + second1;

		ofile2.open(record, ios::app);
		ofile2 << k[a] << " " << begin << " " << end << endl;
		ofile2.close();
		i--;
	}
}

//清空文件
void emptyData(string customer, string record, string tabel)
{
	ofstream file1(customer, ios::trunc);
	ofstream file2(record, ios::trunc);
	ofstream file3(tabel, ios::trunc);
}

FiveBillingMethod.cpp

#include "FiveBillingMethod.h"

//每分钟 0.03 yuan
double case0(int duration)
{
	return duration* 0.03;
}

//50元包30小时 超过部分每分钟0.03元
double case1(int duration)
{
	if (duration <= 30 * HOUR)
	{
		return 50;
	}
	return (duration - 30 * HOUR) * 0.03 + 50.0;
}

//50元包60小时 超过部分每分钟0.03元
double case2(int duration)
{
	if (duration <= 60 * HOUR)
	{
		return 95.0;
	}
	return (duration - 60 * HOUR) * 0.03 + 95.0;
}

//200元包150小时 超过部分每分钟0.03元
double case3(int duration)
{
	if (duration <= 150 * HOUR)
	{
		return 200;
	}
	return (duration - 150 * HOUR) * 0.03 + 200.0;
}

//300元全包
double case4(int duration)
{
	return 300;
}

Time.cpp

#include"Time.h"

//返回日期在当年是第几天
int dayNumber(int year, int mon, int day)
{
	//12个月各多少天
	int a[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	int i, total = 0;
	for (i = 0; i < mon - 1; i++)//先算当月前有多少天
	{
		total += a[i];
	}
	if (mon > 2) //闰年的2月多一天 算闰年不能放到for里面
	{
		total += (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
	}
	total += day;//最后加上当月天数
	return total;
}

//返回year1到year2-1 有多少天
int yearNumber(int year1, int year2)
{
	int total = 0, year;
	for (year = year1; year < year2; year++)
	{
		total += 365 + (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);//year是闰年则多一天
	}
	return total;
}

int calculateOnlineTime(Date begin, Date end)
{
	int tmpDays = 0;//距离
	int tmpHours = 0, tmpMinutes = 0, tmpSeconds = 0;
	tmpHours = end.hour - begin.hour;
	tmpMinutes = end.minute - begin.minute;
	tmpSeconds = end.second - begin.second;

	if (begin.year == end.year)//同一年下
	{
		tmpDays = dayNumber(end.year, end.month, end.day)
			- dayNumber(begin.year, begin.month, begin.day);
	}
	else
	{
		tmpDays = yearNumber(begin.year, end.year)
			- dayNumber(begin.year, begin.month, begin.day)
			+ dayNumber(end.year, end.month, end.day);
	}
	if (tmpSeconds > 0) { tmpMinutes++; }	//不足一分钟按照一分钟计算
	if (tmpMinutes < 0) { tmpHours--; tmpMinutes += 60; }
	if (tmpHours < 0) { tmpDays--; tmpHours += 24; }
	return tmpDays * DAY + tmpHours * HOUR + tmpMinutes;
}

Main.cpp

#include <cstring>
#include <iostream>
using namespace std;
#include "Customer.h"
#include "File.h"
//欢迎界面
void welcome()
{
	cout << "******** 欢迎使用本系统\t\t\t ********" << endl;
	cout << "******** 1: 添加客户\t\t\t ********" << endl;
	cout << "******** 2: 显示客户列表\t\t ********" << endl;
	cout << "******** 3: 添加上网记录\t\t ********" << endl;
	cout << "******** 4: 显示某用户所有上网记录\t ********" << endl;
	cout << "******** 5: 删除一条上网记录\t\t ********" << endl;
	cout << "******** 6: 显示所有上网记录\t\t ********" << endl;
	cout << "******** 7: 计算某用户某月的上网费用\t ********" << endl;
	cout << "******** 8: 计算某月全部用户的上网费用\t ********" << endl;
	cout << "******** 9: 删除某用户\t\t\t ********" << endl;
	cout << "******* 10: 随机生成\t\t\t ********" << endl;
	cout << "******* 11: 清空数据\t\t\t ********" << endl;
	cout << "******* 99: 显示菜单\t\t\t ********" << endl;
	cout << "******** 0: 退出\t\t\t ********" << endl;
}

void functions()
{
	string customerList, recordList, tabelList;
	customerList = "D:\\用户基本资料.txt";
	recordList = "D:\\线上使用时间.txt";
	tabelList = "D:\\用户报表.txt";
	string s_choice;
	while (1)
	{
		cout << "******** 请输入功能编号\t\t\t ********" << endl;
		cin >> s_choice;
		int choice = stoi(s_choice.c_str());
		if (choice==0)
		{
			cout << "退出成功, 谢谢使用本系统!" << endl;
			break;
		}
		switch (choice)
		{
		case 1://添加客户
		{
			bool n1 = 1;
			while (n1)
			{
				addCustomer(customerList);
				cout << "是否继续添加客户(1继续, 0停止)" << endl;
				cin >> n1;
				cout << endl;
			}
			break;
		}
		case 2://打印所有客户信息(OK)
		{
			read(customerList);
			break;
		}
		case 3://添加上网记录(OK)
		{
			bool n2 = 1;
			while (n2)
			{
				addRecord(recordList);
				cout << "是否继续(1 0)" << endl;
				cin >> n2;
			}
			break;
		}
		case 4: //显示某用户所有上网记录(OK)
		{
			readOneCustomerRecors(recordList);
			break; 
		}
		case 5: // 删除一条上网记录(OK)
		{
			delRecord(recordList);
			break; 
		}
		case 6://显示所有上网记录(OK)
		{
			read(recordList);
			break; 
		}
		case 7://计算某用户某月的上网费用(OK)
		{
			getTotalCostByMonth(recordList, customerList);
			break;
		}
		case 8:// 计算某月全部用户的上网费用(OK)
		{
			getMonthCost(customerList, recordList, tabelList);
			break;
		}
		case 9://删除某用户(OK)
		{
			delCustomer(customerList, recordList);
			break;
		}
		case 10://随机生成数据
		{
			creatRandRecords(customerList, recordList);
			break;
		}
		case 11:
		{
			emptyData(customerList, recordList, tabelList);
			break;
		}
		case 99:
		{
			welcome();
			break;
		}
		}
	}
}

int main()
{
	welcome();
	functions();
	return 0;
}
  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦是远方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值