整个实验比较简单,直接展示所有代码
1、global.h
#ifndef global_h
#define global_h
#define FALSE 0
#define TRUE 1
#endif /* global_h */
2、tool.h
#ifndef tool_h
#define tool_h
void timeToString(time_t t,char * pBuf);//将time_t格式时间,转换为"年-月-日 时-分"格式字符串
time_t stringToTime(char* pTime);//将"年-月-日 时-分"格式字符串,转换为time_t格式时间
#endif /* tool_h */
3、tool.c
#include <stdio.h>
#include <time.h>
//将time_t格式时间,转换为"年-月-日 时-分"格式字符串
void timeToString(time_t t,char* pBuf) {
struct tm * timeinfo; //定义一个tm类型的结构体指针
timeinfo = localtime(&t); //获得tm结构体的时间
strftime(pBuf,20,"%Y-%m-%d %H:%M",timeinfo); //将时间转换为"年-月-日 时-分"格式字符串
}
//将"年-月-日 时-分"格式字符串,转换为time_t格式时间
time_t stringToTime(char* pTime) {
struct tm tm1; //定义tm类型的结构体
time_t time1; //定义时间变量
sscanf(pTime,"%d-%d-%d %d:%d",&tm1.tm_year,&tm1.tm_mon,&tm1.tm_mday,&tm1.tm_hour,&tm1.tm_min); //将字符串格式的h时间保存到tm1变量中
tm1.tm_year -= 1900; //年份从1900年开始
tm1.tm_mon -= 1; //月份为1~12
tm1.tm_sec = 0;
tm1.tm_isdst = -1;
time1 = mktime(&tm1); //将struct tm类型变量的值转化为time_t类型变量的值
return time1; //返回转换后的格式时间
}
4、model.h
#ifndef model_h
#define model_h
//卡信息结构体
typedef struct card { //卡信息
char number[18]; //卡号
char password[8]; //卡密码
int status; //卡状态 0-未上机 1-正在上机 2-已注销 3-失效
time_t timeStart; //开卡时间
time_t timeEnd; //截止时间
float totalUse; //累计金额
time_t timeLast; //最后使用时间
int useCount; //使用次数
float balance; //余额
int del; //删除标志 0-未删除 1-已删除
struct card * next;
}card;
//计费信息结构体
typedef struct billing { //计费信息
char number[18]; //卡号
time_t timeStart; //上机时间
time_t timeEnd; //下机时间
float amount; //消费金额
int status; //消费状态 0-未结算 1-已经结算
int del; //删除标识 0-未删除 1-删除
struct billing * next;
}billing;
#endif /* model_h */
5、main.c
#include <stdio.h>
#include <stdlib.h>
#include "model.h"
#include "menu.h"
#include "service.h"
#include "card_service.h"
#include "cardfile.h"
#include "billing_service.h"
#include "billing_file.h"
#define CARDPATH "card.txt"
#define BILLINGPATH "billing.txt"
#define MONEYPATH "money.txt"
int main(){
card* head = initCardList(CARDPATH);
billing* hb = initBillingList(BILLINGPATH);
int nSelection = -1;
printf("欢迎进入计费管理系统!\n");
printf("\n");
do {
outputMenu();
nSelection = -1;
scanf("%d",&nSelection);
switch(nSelection) {
case 1:
{
add(head); //添加卡
break;
}
case 2:
{
query(head); //查询卡
break;
}
case 3:
{
logon(head,hb); //上机
break;
}
case 4:
{
settle(head,hb); //下机
break;
}
case 5:
{
addMoney(head); //充值
break;
}
case 6:
{
refundMoney(head); //退费
break;
}
case 7:
{
count(head); //查询统计
break;
}
case 8:
{
annul(head); //注销卡
break;
}
case 0:
{
exitApp(); //退出
break;
}
default:
{
printf("输入的菜单编号错误!\n");
break;
}
}
printf("\n");
}while(nSelection != 0);
return 0;
}
6、menu.h
#ifndef menu_h
#define menu_h
void outputMenu(void);
#endif /* menu_h */
7、menu.c
#include <stdio.h>
#include <stdlib.h>
void outputMenu()
{
printf("\n");
printf("------------菜单----------\n");
printf("1.添加卡\n");
printf("2.查询卡\n");
printf("3.上机\n");
printf("4.下机\n");
printf("5.充值\n");
printf("6.退费\n");
printf("7.查询统计\n");
printf("8.注销卡\n");
printf("0.退出系统\n");
printf("-------------------------\n");
printf("请输入0-8数字编号:");
}
8、service.h
#ifndef service_h
#define service_h
#include "model.h"
void add(card*);
void query(card*);
void logon(card*, billing*);
void settle(card*, billing*);
void addMoney(card*);
void refundMoney(card*);
void count(card*);
void annul(card*);
void exitApp(void);
#endif /* service_h */
9、service.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "model.h"
#include "global.h"
#include "tool.h"
#include "cardfile.h"
#include "card_service.h"
#include "billing_file.h"
#include "billing_service.h"
#define CARDPATH "card.txt"
#define BILLINGPATH "billing.txt"
#define INFORPATH "information.txt"
void add(card* head)
{
card * pNew = (card*)malloc(sizeof(card));
printf("\n----------添加卡-----------\n");
printf("请输入卡号(长度为1~18):");
char number[18] = {0}; // 输入的卡号
scanf("%s", number);
if(isExsit(number,CARDPATH) == TRUE) {
printf("此卡已存在!添加卡失败!\n");
return;
}
strcpy(pNew->number, number);
printf("请输入密码(长度为1~8):");
char password[8] = {0}; // 输入的密码
scanf("%s", password);
strcpy(pNew->password, password);
printf("请输入开卡金额(RMB):");
float m;
scanf("%f", &m);
pNew->balance = m;
pNew->totalUse = pNew->balance; // 添加卡时,累计金额等于开卡金额
pNew->del = 0; // 删除标识
pNew->status = 0; // 卡状态
pNew->useCount = 0; // 使用次数
pNew->timeEnd = pNew->timeLast = pNew->timeStart = time(NULL);
// 开卡时间,截止时间,最后使用时间都默认为当前时间。
// 根据开卡时间,计算截止时间,每张卡的有效期为一年
struct tm *endtime ; // 截止时间
struct tm *starttime ; // 开卡时间
starttime = localtime(&(pNew->timeStart));
endtime = localtime(&(pNew->timeEnd));
endtime->tm_year = starttime->tm_year + 1;
pNew->timeEnd = mktime(endtime); //截止时间为开卡时间的后一年整
if( savecard(pNew,CARDPATH) == FALSE) {
printf("添加卡失败!\n");
}else {
printf("添加卡成功!\n\n");
}
printf("\n------添加的卡信息如下------\n");
printf("卡号\t\t密码\t\t状态\t\t开卡金额\n");
printf("%s\t\t%s\t\t0\t\t%.2f\n",pNew->number,pNew->password,pNew->balance);
printf("\n");
card *p = head;
while( p->next != NULL ) {
p = p->next;
}
p->next = pNew;
p = p->next;
p->next = NULL;
}
void query(card* head)
{
printf("\n----------查询卡-----------\n");
printf("请选择查询的方式:\n");
printf("1.精确查询\n");
printf("2.模糊查询\n");
printf("请输入选择(1或2):");
int item;
scanf("%d",&item);
if(item == 1) {
queryCard(head);
}else if(item == 2) {
queryCards(head);
}else {
printf("输入错误!\n");
}
}
void logon(card* head, billing* pb)
{
printf("\n----------上机-----------\n");
card* p = NULL;
int index = 0; //卡信息在链表中的索引
printf("请输入卡号<长度为1~18>:");
char number[18];
scanf("%s",number);
printf("请输入密码(长度为1~8):");
char password[8]; // 输入的密码
scanf("%s", password);
//根据卡号和密码,从链表中获取卡信息和卡信息在链表中的索引
p = checkCard(number, password, head, &index);
//如果卡信息为空,表示没有该卡信息,上机失败
if( p == NULL ) {
printf("该卡信息不存在!上机失败!\n");
return;
}
//如果卡状态不为0,表示该卡不能上机
if( p->status != 0 ) {
printf("该卡不能上机!上机失败!\n");
return;
}
//如果卡余额为0,不能上机
if( p->balance == 0 ) {
printf("你个穷鬼还想上网???充钱去吧!!!\n");
return;
}
//如果可以上机,更新卡信息
p->status = 1; //状态为正在使用
p->useCount ++; //使用次数加1
p->timeLast = time(NULL); //更新最后使用时间为当前时间
//更新文件中的卡信息
if( updateCard(p, CARDPATH, index) == FALSE ) {
printf("无法更新!\n");
return;
}
billing* pnew;
pnew = (billing*)malloc(sizeof(struct billing));
strcpy(pnew->number,p->number);
pnew->timeStart = time(NULL);
pnew->timeEnd = 0;
pnew->amount = 0;
pnew->status = 0;
pnew->del = 0;
if( savebilling(pnew,BILLINGPATH) == FALSE) {
printf("计费信息添加失败!\n");
return;
}
billing* pp = pb;
while( pp->next != NULL ) {
pp = pp->next;
}
pp->next = pnew;
pp = pp->next;
pp->next = NULL;
printf("上机成功!\n");
}
void settle(card* head, billing* hp)
{
printf("\n----------下机-----------\n");
card* p = NULL;
int index = 0;
printf("请输入卡号<长度为1~18>:");
char number[18];
scanf("%s",number);
printf("请输入密码(长度为1~8):");
char password[8];
scanf("%s", password);
//根据卡号和密码,从链表中获取卡信息和卡信息在链表中的索引
p = checkCard(number, password, head, &index);
//如果卡信息为空,表示没有该卡信息,下机失败
if( p == NULL ) {
printf("该卡信息不存在!下机失败!\n");
return;
}
//如果卡状态不为1,表示该卡不能下机
if( p->status != 1 ) {
printf("该卡不能下机!下机失败!\n");
return;
}
//如果可以下机,更新卡信息
p->status = 0; //状态为未上机
p->useCount ++; //使用次数加1
p->timeLast = time(NULL); //更新最后使用时间为当前时间
int index2 = 0;
billing* bp = NULL;
bp = checkBilling(number, hp, &index2);
//如果一分钟一元
float every = 1;
time_t now;
time_t starttime;
time(&now);
starttime = bp->timeStart;
double c;
int minute = 0;
c = difftime(now, starttime);
minute = c / 60;
p->balance = p->balance- every * minute;
p->totalUse += every * minute;
bp->amount = every * minute;
bp->timeStart = p->timeStart;
bp->timeEnd = time(NULL);
if( savebilling(bp,INFORPATH) == FALSE) {
printf("添加卡信息失败!\n");
return;
}
//更新卡文件中的卡信息
if( updateCard(p, CARDPATH, index) == FALSE ) {
printf("更新卡信息失败!下机失败!\n");
return;
}
//更新计费文件中计费信息
if( updateBilling(bp, BILLINGPATH, index) == FALSE ) {
printf("更新计费信息失败!下机失败!\n");
return;
}
printf("应付金额:%.2f\n",every * minute);
printf("卡中剩余余额:%.2f\n",p->balance);
printf("下机成功!\n");
return;
}
void addMoney(card* head)
{
printf("\n----------充值-----------\n");
printf("请输入卡号<长度为1~18>:");
char number[18];
scanf("%s",number);
printf("请输入充值金额:");
float money;
scanf("%f",&money);
FILE * fp = NULL;
if((fp = fopen(CARDPATH,"rb")) == NULL) {
printf("卡文件打开失败!\n");
}
card *q = head->next;
while( q != NULL) {
if( strcmp(q->number, number) == 0) {
q->balance += money;
q->totalUse += money;
}
q = q->next;
}
int index = 0;
card* p = (card*)malloc(sizeof(card));
p->next = NULL;
while(!feof(fp)) {
index ++;
if(fread(p,sizeof(card),1,fp) != 0) {
if( strcmp(p->number,number) == 0) {
break;
}
}
}
p->balance += money;
p->totalUse += money;
if( updateCard(p, CARDPATH, index) == TRUE) {
printf("充值成功!\n");
}
free(p);
fclose(fp);
}
void refundMoney(card* head)
{
printf("\n----------退费-----------\n");
printf("请输入卡号<长度为1~18>:");
char number[18];
scanf("%s",number);
printf("请输入要退的金额:");
float money;
scanf("%f",&money);
card *q = head->next;
while( q != NULL) {
if( strcmp(q->number, number) == 0) {
if(q->balance < money) {
printf("余额不足!\n");
printf("请充值!\n");
return;
}else {
q->balance -= money;
q->timeLast =time(NULL);
}
}
q = q->next;
}
FILE * fp = NULL;
if((fp = fopen(CARDPATH,"rb")) == NULL) {
printf("卡文件打开失败!\n");
}
int index = 0;
card* p = (card*)malloc(sizeof(card));
while(!feof(fp)) {
index ++;
if(fread(p,sizeof(card),1,fp) != 0) {
if( strcmp(p->number,number) == 0) {
break;
}
}
}
p->balance -= money;
if( updateCard(p, CARDPATH, index) == TRUE) {
printf("退费成功!\n");
}
free(p);
fclose(fp);
}
void count(card* head)
{
printf("\n----------查询统计菜单-----------\n");
printf("1.总卡数\n");
printf("2.所有卡的信息\n");
printf("3.卡总充值金额\n");
printf("4.总营业额\n");
printf("5.查询消费记录\n");
printf("-------------------------\n");
printf("请输入1-4数字编号:");
int num;
scanf("%d",&num);
switch(num) {
case 1: {
printf("一共有%d张卡\n",getCardCount(CARDPATH));
break;
}
case 2: {
showallcards(CARDPATH);
break;
}
case 3: {
card* p = head->next;
float sum = 0;
while( p->next != NULL) {
sum += p->totalUse;
p = p->next;
}
printf("所有卡的充值金额为%f\n",sum);
break;
}
case 4: {
card* p = head->next;
float sum = 0, del = 0, re = 0;
while( p->next != NULL ) {
sum += p->totalUse;
del += p->balance;
p = p->next;
}
re = sum - del;
printf("总营业额为%f\n",re);
break;
}
case 5: {
printf("请输入要查询的卡号:");
char name[18];
scanf("%s",name);
showinformation(INFORPATH, name);
break;
}
default: {
printf("输入的数字错误!\n");
break;
}
}
}
void annul(card* head)
{
printf("\n----------注销卡-----------\n");
printf("请输入卡号<长度为1~18>:");
char number[18];
scanf("%s",number);
card *q = head->next;
while( q != NULL) {
if( strcmp(q->number, number) == 0) {
q->status = 2;
}
q = q->next;
}
FILE * fp = NULL;
if((fp = fopen(CARDPATH,"rb")) == NULL) {
printf("卡文件打开失败!\n");
}
int index = 0;
card* p = (card*)malloc(sizeof(card));
p->next = NULL;
while(!feof(fp)) {
index ++;
if(fread(p,sizeof(card),1,fp) != 0) {
if( strcmp(p->number,number) == 0) {
p->status = 2;
printf("剩余金额:%f,退费金额:%f\n",p->balance,p->balance);
printf("退费成功!\n");
if( updateCard(p, CARDPATH, index) == TRUE) {
printf("注销成功!\n");
}
free(p);
fclose(fp);
return;
}
}
}
printf("此卡不存在\n!");
free(p);
fclose(fp);
}
void exitApp()
{
printf("\n----------退出系统-----------\n");
printf("谢谢使用!\n");
exit(0);
}
10、cardfile.h
#ifndef cardfile_h
#define cardfile_h
int savecard(card *, char *);
void inputCard(card *);
void showcard(card *);
int updateCard(card*, char*, int);
int getCardCount(char*);
int isExsit(char* , char*);
void showallcards(char* );
#endif /* cardfile_h */
11、cardfile.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "model.h"
#include "global.h"
#include "tool.h"
int savecard(card *p, char *path) {
FILE *fp = NULL;
if((fp = fopen(path,"ab")) == NULL ) { //以追加的模式打开文件,如果打开失败,则以只写的模式打开文件
if((fp = fopen(path,"wb")) == NULL ) {
return FALSE;
}
}
fwrite(p,sizeof(card),1,fp);
fclose(fp);
return TRUE;
}
void showcard(card *p) {
char lastTime[50];
printf("卡号\t\t状态\t\t余额\t\t累计使用\t\t使用次数\t\t上次使用时间\n");
timeToString(p->timeLast,lastTime);
printf("%s\t\t%d\t\t%0.2f\t\t%.2f\t\t%d\t\t%s\n",p->number,p->status,p->balance,p->totalUse,p->useCount,lastTime);
}
int updateCard(card* p, char* path, int index)
{
FILE* fp = NULL; // 文件指针
int line = 0; // 文件卡信息数
long position = 0; // 文件位置标记
card bBuf;
if((fp = fopen(path, "rb+")) == NULL){
return FALSE;
}
while((!feof(fp)) && (line < index-1)){
if(fread(&bBuf, sizeof(card), 1, fp) != 0)
{ // 获取文件标识位置
position = ftell(fp);
line++;
}
}
fseek(fp, position, 0);
fwrite(p, sizeof(card), 1, fp);
fclose(fp);
return TRUE;
}
int getCardCount(char* path)
{
FILE* p = NULL; // 文件指针
int index = 0; // 卡信息数量
card* pCard = (card*)malloc(sizeof(card));
if((p = fopen(path, "rb")) == NULL){
return 0;
}
while(!feof(p)){
if(fread(pCard, sizeof(card), 1, p) != 0)
index++;
}
fclose(p);
free(pCard);
return index;
}
int isExsit(char* num,char* path)
{
FILE* p = NULL; // 文件结构体指针
char number[18]={0}; // 存放读取出的卡号
if((p = fopen(path, "rb")) == NULL) {
return FALSE;
}
while(!feof(p)) {
// 读取卡号,并比较卡号是否为当前查找的卡号
if(fread(number, sizeof(number), 1, p) != 0) {
if(strcmp(number, num) == 0) {
fclose(p);
return TRUE;
}
else {
fseek(p, sizeof(card) - sizeof(number), SEEK_CUR);
}
}
}
fclose(p);
return FALSE;
}
void showallcards(char* path) {
FILE* p = NULL;
card* pCard = (card*)malloc(sizeof(struct card));
if((p = fopen(path, "rb")) == NULL){
printf("打开卡文件失败!\n");
}
while(!feof(p)){
if(fread(pCard, sizeof(card), 1, p) != 0) {
showcard(pCard);
}
}
fclose(p);
free(pCard);
}
12、card_service.h
#ifndef card_service_h
#define card_service_h
#include "model.h"
card* initCardList(char*); //初始化卡信息链表
void releaseCardList(card*); //释放卡信息链表
void queryCard(card*); //在卡信息链表中,查询卡号相同的卡信息
void queryCards(card*); //根据输入的关键字,在卡信息链表中,查询卡号中包含关键字的卡信息
card* checkCard(char*, char*, card*, int*); //根据卡号和密码,在链表中查询卡信息,并获取查询到的卡信息在链表中的位置
#endif /* card_service_h */
13、card_service.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "model.h"
#include "global.h"
#include "tool.h"
#include "cardfile.h"
card* initCardList(char* path) {
card *head;
head = (card*)malloc(sizeof(struct card));
head->next = NULL;
card* p = head;
while(p->next != NULL) {
p = p->next;
}
FILE* fp = NULL;
if( (fp = fopen(path, "rb")) == NULL ) {
return head;
}
while(!feof(fp)) {
card* pCard = (card*)malloc(sizeof(struct card));
if(fread(pCard, sizeof(card), 1, fp) != 0) {
p->next = pCard;
p = p->next;
p->next = NULL;
}
}
fclose(fp);
return head;
}
void releaseCardList(card* p) { //释放卡信息链表
free(p);
}
void queryCard(card* head) { //在卡信息链表中,查询卡号相同的卡信息
printf("\n----------查询卡-----------\n");
printf("请输入要查询的卡号(长度为1~18):");
char number[18] = {0}; // 输入的卡号
scanf("%s", number);
card *p = head->next;
while( p != NULL) {
if( strcmp(p->number, number) == 0 ) {
showcard(p);
return;
}
p = p->next;
}
printf("此卡不存在!\n");
}
void queryCards(card* head) { //根据输入的关键字,在卡信息链表中,查询卡号中包含关键字的卡信息
printf("\n----------查询卡-----------\n");
printf("请输入要查询的关键词(长度为1~18):");
char number[18] = {0};
scanf("%s",number);
card* p = head->next;
while( p != NULL ) {
if( strstr(p->number,number) != NULL ) {
showcard(p);
}
p = p->next;
}
}
card* checkCard(char* number, char* password, card* head, int* index) { //根据卡号和密码,在链表中查询卡信息,并获取查询到的卡信息在链表中的位置
card *p = head->next;
while( p != NULL) {
(*index) ++;
if( (strcmp(p->number, number) == 0)&&(strcmp(p->password,password) == 0)) {
return p;
}
p = p->next;
}
return NULL;
}
14、billing_file.h
#ifndef billing_file_h
#define billing_file_h
int savebilling(billing*, char*);
void showbilling(billing*);
int getbillingnum(billing*, char*);
int updateBilling(billing*, char*, int);
void showinformation(char* ,char* );
#endif /* billing_file_h */
15、billing_file.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "model.h"
#include "global.h"
#include "tool.h"
int savebilling(billing* p, char *path) { //将计费信息保存到文件中
FILE *fp = NULL;
if((fp = fopen(path,"ab")) == NULL ) { //以追加的模式打开文件,如果打开失败,则以只写的模式打开文件
if((fp = fopen(path,"wb")) == NULL ) {
return FALSE;
}
}
fwrite(p,sizeof(billing),1,fp);
fclose(fp);
return TRUE;
}
void showbilling(billing* bp, char* number, char* path) { //从文件中读取计费信息
char startTime[50];
FILE* p = NULL; // 文件指针
billing* pb = (billing*)malloc(sizeof(billing));
if((p = fopen(path, "rb")) == NULL){
printf("打开文件失败!\n");
return;
}
while(!feof(p)){
if(fread(pb, sizeof(card), 1, p) != 0)
if( strcmp(pb->number, number) == 0 ) {
printf("卡号\t\t状态\t\t消费状态\t\t删除标识\t\t上机时间\n");
timeToString(pb->timeStart,startTime);
printf("%s\t\t%d\t\t%d\t\t%d\t\t%s\n",pb->number,pb->status,pb->status,pb->del,startTime);
}
}
fclose(p);
free(pb);
}
int getbillingnum(billing* hp, char* path) { //从文件中获取计费信息的数量
FILE* p = NULL; // 文件指针
int index = 0;
card* pCard = (card*)malloc(sizeof(card));
if((p = fopen(path, "rb")) == NULL){
return 0;
}
while(!feof(p)){
if(fread(pCard, sizeof(card), 1, p) != 0)
index++;
}
fclose(p);
free(pCard);
return index;
}
int updateBilling(billing* p, char* path, int index)
{
FILE* fp = NULL; // 文件指针
int line = 0; // 文件卡信息数
long position = 0; // 文件位置标记
card bBuf;
if((fp = fopen(path, "rb+")) == NULL){
return FALSE;
}
while((!feof(fp)) && (line < index-1)){
if(fread(&bBuf, sizeof(card), 1, fp) != 0)
{ // 获取文件标识位置
position = ftell(fp);
line++;
}
}
fseek(fp, position, 0);
fwrite(p, sizeof(card), 1, fp);
fclose(fp);
return TRUE;
}
void showinformation(char* path,char* name) {
FILE* p = NULL;
billing* bp = (billing*)malloc(sizeof(struct billing));
if((p = fopen(path, "rb")) == NULL){
printf("打开计费信息文件失败!\n");
}
char startTime[50];
char endTime[50];
while(!feof(p)){
if(fread( bp, sizeof(billing), 1, p) != 0) {
if( strcmp(bp->number,name) == 0) {
printf("消费状态\t\t消费金额\t\t上机时间\t\t下机时间\t\t删除标识\n");
timeToString(bp->timeStart,startTime);
timeToString(bp->timeEnd,endTime);
printf("%d\t\t\%f\t\t%s\t\t%s\t\t%d\n",bp->status,bp->amount,startTime,endTime,bp->del);
}
}
}
fclose(p);
free(bp);
}
16、billing_service.h
#ifndef billing_service_h
#define billing_service_h
billing* initBillingList(char*);
void releaseBillingList(billing*);
int getbilling(billing*, char*, char*);
billing* checkBilling(char*, billing*, int*);
#endif /* billing_service_h */
17、billing_service.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "model.h"
#include "global.h"
#include "tool.h"
billing* initBillingList(char* path) {
billing* head;
head = (billing*)malloc(sizeof(billing));
head->next = NULL;
billing* p = head;
while(p->next != NULL) {
p = p->next;
}
FILE* fp = NULL;
if((fp = fopen(path,"rb")) == NULL) {
return head;
}
while(!feof(fp)) {
billing* pBilling = (billing*)malloc(sizeof(struct billing));
if(fread(pBilling, sizeof(billing), 1, fp) != 0) {
p->next = pBilling;
p = p->next;
p->next = NULL;
}
}
fclose(fp);
return head;
}
void releaseBillingList(billing* p) { //释放计费信息链表
free(p);
}
int getbilling(billing* hp, char* path, char* num) { //从计费信息文件获取计费信息保存到计费链表中
FILE* p = NULL;
if((p = fopen(path, "rb")) == NULL) {
return FALSE;
}
billing* pp = hp;
while( pp->next != NULL ) {
pp = pp->next;
}
billing* pnew = (billing*)malloc(sizeof(struct billing));
while(!feof(p)) {
if(fread(pnew,sizeof(billing),1,p) != 0 )
if(strcmp(pnew->number, num) == 0) {
pp->next = pnew;
pp = pp->next;
pp->next = NULL;
fclose(p);
return TRUE;
}
}
fclose(p);
return FALSE;
}
billing* checkBilling(char* number, billing* hp, int* index) { //在计费信息链表中,查询对应卡的计费信息,并获取该计费信息在链表中的索引号
billing *p = hp->next;
while( p != NULL) {
(*index) ++;
if( strcmp(p->number, number) == 0) {
return p;
}
p = p->next;
}
return NULL;
}