C语言综合实验(七)之计费管理系统的扩展功能实现

实验内容

(1)进一步理解和掌握函数、文件、链表等。

(2)学习进行编码规范性检查

(3)分析设计项目需求,并完成相应的功能代码

沿用第一次的main.cpp 第三次的tool.h,tool.cpp 第六次的 card.h card.cpp card_file.h card_file.cpp

menu.h
菜单头文件

void outputmenu();
void add();
void query();
void del();
void dologin();
void dologout();
void savemoney();
void refundmoney();
void show();

menu.cpp
菜单源文件

#define _CRT_SECURE_NO_WARNINGS//消除scanf警告
#include"stdio.h"
#include"stdlib.h"
#include "string.h"
#include "menu.h"
#include "card.h"
#include"card_file.h"
#include"time.h"
Card *card = initial();
void outputmenu() {
	do {
		char i;
		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("请输入0-8数字来进行选择\n");
		scanf("%c", &i);
		switch (i) {
		case '0': {
			exit(0);
			break;
		}
		case '1': {
			add();
			break;
		}
		case '2': {
			query();
			break;
		}
		case '3': {
			dologin();
			break;
		}
		case '4': {
			dologout();
			break;
		}
		case '5': {
			savemoney();
			break;
		}
		case '6': {
			refundmoney();
			break;
		}
		case '7': {
			show();
			break;
		}
		case '8': {
			del();
			break;
		}
		default: {
			system("cls");
			printf("请输入正确的1-8数字\n");
			outputmenu();
			break;
		}
		}
	} while (1);
}
void add() {
	system("cls");
	card=readCard();
	printf("以下为添加卡的菜单界面\n");
	printf("请输入需要添加的卡号(长度1-18):\n");
	char no[50];
	scanf("%s", no);
	while (strlen(no) > 18 || queryCard(card, no) != NULL) {
		if (strlen(no) > 18) printf("卡号超过18位,请重新输入卡号(长度1-18):\n");
		else printf("该卡号已存在请重新输入卡号(长度1-18):\n");
		scanf("%s", no);
	}
	char pwd[30];
	printf("请输入密码(长度1-8):\n");
	scanf("%s", pwd);
	while (strlen(pwd) > 8) {
		printf("密码超过8位,请重新输入密码(长度1-8):\n");
		scanf("%s", pwd);
	}
	float balance;
	printf("请输入开卡金额(人民币):\n");
	scanf("%f", &balance);
	printf("添加卡的信息如下:\n");
	printf("卡号\t密码\t状态\t金额\n");
	printf("%s\t%s\t0\t%.1f\n", no, pwd, balance);
	addCard(card, no, pwd, balance);
	int i;
	printf("1.继续添加\n");
	printf("2.返回上级菜单\n");
	printf("0.退出\n");
	scanf("%d", &i);
	switch (i) {
	case 0: {
		exit(0); break;
	}
	case 1: {
		add(); break;
	}
	case 2: {
		break;
	}
	}
}
void query() {
	system("cls");
	card=readCard();
	printf("以下为查询卡的菜单界面\n");
	printf("请输入需要查询的卡号(长度1-18):\n");
	char no[50];
	scanf("%s", no);
	while (strlen(no) > 18) {
		if (strlen(no) > 18) printf("卡号超过18位,请重新输入卡号(长度1-18):\n");
		scanf("%s", no);
	}
	Card *t = queryCard(card, no);
	if (t == NULL) {
		Card *s = queryCards(card, no);
		if (s->next != NULL) {
			printf("查询卡的信息如下:\n");
			printf("卡号\t状态\t余额\t累计使用\t使用次数\t上次使用时间\n");
			while (s->next != NULL) {
				t = s->next;
				s = s->next;
				struct tm *p = localtime(&t->tLast);
				printf("%s\t%d\t%.1f\t%.1f\t\t%d\t", t->aName, t->nStatus, t->fBalance, t->fTotalUse, t->nUseCount);
				printf("%4d-", p->tm_year + 1900);
				printf("%02d-", p->tm_mon + 1);
				printf("%02d ", p->tm_mday);
				printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);
			}
		}
		else printf("查询失败!\n");
	}
	else {
		printf("查询卡的信息如下:\n");
		printf("卡号\t状态\t余额\t累计使用\t使用次数\t上次使用时间\n");
		struct tm *p = localtime(&t->tLast);
		printf("%s\t%d\t%.1f\t%.1f\t\t%d\t", t->aName, t->nStatus, t->fBalance, t->fTotalUse, t->nUseCount);
		printf("%4d-", p->tm_year + 1900);
		printf("%02d-", p->tm_mon + 1);
		printf("%02d ", p->tm_mday);
		printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);
	}
	int i;
	printf("1.继续查询\n");
	printf("2.返回上级菜单\n");
	printf("0.退出\n");
	scanf("%d", &i);
	switch (i) {
	case 0: {
		exit(0); break;
	}
	case 1: {
		query(); break;
	}
	case 2: {
		break;
	}
	}
}
void del() {
	system("cls");
	card = readCard();
	printf("以下为注销卡的菜单界面\n");
	printf("请输入需要注销的卡号(长度1-18):\n");
	char no[50];
	scanf("%s", no);
	while (strlen(no) > 18 || queryCard(card, no) == NULL) {
		if (strlen(no) > 18) printf("卡号超过18位,请重新输入卡号(长度1-18):\n");
		else printf("该卡号不存在请重新输入卡号(长度1-18):\n");
		scanf("%s", no);
	}
	Card *t = queryCard(card, no);
	char pwd[30];
	printf("请输入上机密码(长度1-8):\n");
	scanf("%s", pwd);
	while (strlen(pwd) > 8) {
		printf("密码超过8位,请重新输入密码(长度1-8):\n");
		scanf("%s", pwd);
	}
	if (strcmp(pwd, t->aPwd) == 0) {
		if (updateCard(t, 2) == 1) printf("删除成功!\n");
		else printf("删除失败!\n");
	}
	else printf("密码错误!\n");
	int i;
	printf("1.继续删除\n");
	printf("2.返回上级菜单\n");
	printf("0.退出\n");
	scanf("%d", &i);
	switch (i) {
	case 0: {
		exit(0); break;
	}
	case 1: {
		del(); break;
	}
	case 2: {
		break;
	}
	}
}
void dologin() {
	system("cls");
	card=readCard();
	printf("以下为上机的菜单界面\n");
	printf("请输入上机卡号(长度1-18):\n");
	char no[50];
	scanf("%s", no);
	while (strlen(no) > 18 || queryCard(card, no) == NULL) {
		if (strlen(no) > 18) printf("卡号超过18位,请重新输入卡号(长度1-18):\n");
		else printf("该卡号不存在请重新输入卡号(长度1-18):\n");
		scanf("%s", no);
	}
	Card *t = queryCard(card, no);
	char pwd[30];
	printf("请输入上机密码(长度1-8):\n");
	scanf("%s", pwd);
	while (strlen(pwd) > 8) {
		printf("密码超过8位,请重新输入密码(长度1-8):\n");
		scanf("%s", pwd);
	}
	if (strcmp(pwd,t->aPwd) == 0) {
		if (t->nStatus == 0) {
			if (t->fBalance > 0) {
				printf("上机信息如下:\n");
				printf("卡号\t余额\t上机时间\n");
				time_t tm = time(NULL);
				struct tm *p = localtime(&tm);
				printf("%s\t%.1f\t%04d-%02d-%02d %02d:%02d:%02d\n", t->aName, t->fBalance, p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
				t->tLast = tm;
				if (updateCard(t, 1) != 1) {
					printf("文件写入失败!\n");
				}
				else printf("上机成功!\n");
			}
			else printf("该卡余额不足,上机失败!\n");
		}
		else printf("该卡正在上机或已注销或失效,上机失败!\n");
	}
	else printf("密码不正确,上机失败!\n");
	int i;
	printf("1.继续上机\n");
	printf("2.返回上级菜单\n");
	printf("0.退出\n");
	scanf("%d", &i);
	switch (i) {
	case 0: {
		exit(0); break;
	}
	case 1: {
		dologin(); break;
	}
	case 2: {
		break;
	}
	}
}
void dologout() {
	system("cls");
	card = readCard();
	printf("以下为下机的菜单界面\n");
	printf("请输入下机卡号(长度1-18):\n");
	char no[50];
	scanf("%s", no);
	while (strlen(no) > 18 || queryCard(card, no) == NULL) {
		if (strlen(no) > 18) printf("卡号超过18位,请重新输入卡号(长度1-18):\n");
		else printf("该卡号不存在请重新输入卡号(长度1-18):\n");
		scanf("%s", no);
	}
	Card *t = queryCard(card, no);
	char pwd[30];
	printf("请输入下机密码(长度1-8):\n");
	scanf("%s", pwd);
	while (strlen(pwd) > 8) {
		printf("密码超过8位,请重新输入密码(长度1-8):\n");
		scanf("%s", pwd);
	}
	if (strcmp(pwd, t->aPwd) == 0) {
		if (t->nStatus == 1) {
			printf("下机信息如下:\n");
			printf("卡号\t消费\t余额\t上机时间\t下机时间\n");
			time_t tmp;
			time(&tmp);
			struct tm *q = localtime(&t->tLast);
			float spd = spend(t,tmp);
			t->fBalance -= spd;
			t->fTotalUse += spd;
			printf("%s\t%.1f\t%.1f\t%04d-%02d-%02d %02d:%02d:%02d\t", t->aName, spd, t->fBalance, q->tm_year + 1900, q->tm_mon + 1, q->tm_mday, q->tm_hour, q->tm_min, q->tm_sec);
			struct tm *p = localtime(&tmp);
			printf("%04d-%02d-%02d %02d:%02d:%02d\n", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
			t->tLast = tmp;
			if (updateCard(t, 0) != 1) {
				printf("文件写入失败!\n");
			}
			else printf("下机成功!\n");
		}
		else printf("该卡未上机或已注销或失效,上机失败!\n");
	}
	else printf("密码不正确,上机失败!\n");
	int i;
	printf("1.继续下机\n");
	printf("2.返回上级菜单\n");
	printf("0.退出\n");
	scanf("%d", &i);
	switch (i) {
	case 0: {
		exit(0); break;
	}
	case 1: {
		dologout(); break;
	}
	case 2: {
		break;
	}
	}
}
void savemoney() {
	system("cls");
	card=readCard();
	printf("以下为充值卡的菜单界面\n");
	printf("请输入需要充值的卡号(长度1-18):\n");
	char no[50];
	scanf("%s", no);
	while (strlen(no) > 18 || queryCard(card, no) == NULL) {
		if (strlen(no) > 18) printf("卡号超过18位,请重新输入卡号(长度1-18):\n");
		else printf("该卡号不存在请重新输入卡号(长度1-18):\n");
		scanf("%s", no);
	}
	Card *t = queryCard(card, no);
	char pwd[30];
	printf("请输入上机密码(长度1-8):\n");
	scanf("%s", pwd);
	while (strlen(pwd) > 8) {
		printf("密码超过8位,请重新输入密码(长度1-8):\n");
		scanf("%s", pwd);
	}
	float money;
	if (strcmp(pwd, t->aPwd) == 0) {
		printf("请输入充值金额:\n");
		scanf("%f", &money);
		t->fBalance = t->fBalance + money;
		printf("充值信息如下:\n");
		printf("卡号\t充值金额\t余额\n");
		printf("%s\t%.1f\t%.1f\n", t->aName, money, t->fBalance);
		if (updateCard(t, t->nStatus) != 1) {
					printf("文件写入失败!\n");
		}
		else printf("充值成功!\n");
	}
	else printf("密码不正确,充值失败!\n");
	int i;
	printf("1.继续充值\n");
	printf("2.返回上级菜单\n");
	printf("0.退出\n");
	scanf("%d", &i);
	switch (i) {
	case 0: {
		exit(0); break;
	}
	case 1: {
		savemoney(); break;
	}
	case 2: {
		break;
	}
	}
}
void refundmoney() {
	system("cls");
	card = readCard();
	printf("以下为退费的菜单界面\n");
	printf("请输入需要退费的卡号(长度1-18):\n");
	char no[50];
	scanf("%s", no);
	while (strlen(no) > 18 || queryCard(card, no) == NULL) {
		if (strlen(no) > 18) printf("卡号超过18位,请重新输入卡号(长度1-18):\n");
		else printf("该卡号不存在请重新输入卡号(长度1-18):\n");
		scanf("%s", no);
	}
	Card *t = queryCard(card, no);
	char pwd[30];
	printf("请输入上机密码(长度1-8):\n");
	scanf("%s", pwd);
	while (strlen(pwd) > 8) {
		printf("密码超过8位,请重新输入密码(长度1-8):\n");
		scanf("%s", pwd);
	}
	float money;
	if (strcmp(pwd, t->aPwd) == 0) {
		printf("退费信息如下:\n");
		printf("卡号\t退费金额\t余额\n");
		printf("%s\t%.1f\t0.0\n", t->aName, t->fBalance);
		t->fBalance = 0;
		if (updateCard(t, t->nStatus) != 1) {
			printf("文件写入失败!\n");
		}
		else printf("退费成功!\n");
	}
	else printf("密码不正确,退费失败!\n");
	int i;
	printf("1.继续退费\n");
	printf("2.返回上级菜单\n");
	printf("0.退出\n");
	scanf("%d", &i);
	switch (i) {
	case 0: {
		exit(0); break;
	}
	case 1: {
		savemoney(); break;
	}
	case 2: {
		break;
	}
	}
}
void show() {
	Card *t = readCard();
	if (t->next != NULL) {
		printf("卡号\t密码\t状态\t开卡时间\t截止时间\t累计金额\t最后使用时间\t使用次数\t余额\t删除标识\n");
		while (t->next != NULL) {
			t = t->next;
			struct tm *p = localtime(&t->tStart);
			printf("%s\t%s\t%d\t%4d-%02d-%02d %02d:%02d:%02d\t", t->aName, t->aPwd, t->nStatus, p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
			p = localtime(&t->tEnd);
			printf("%4d-%02d-%02d %02d:%02d:%02d\t%.1f\t", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, t->fTotalUse);
			p = localtime(&t->tLast);
			printf("%4d-%02d-%02d %02d:%02d:%02d\t%d\t%.1f\t%d\n", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, t->nUseCount, t->fBalance, t->nDel);
		}
	}
	int i;
	printf("1.返回上级菜单\n");
	printf("0.退出\n");
	scanf("%d", &i);
	switch (i) {
	case 0: {
		exit(0); break;
	}
	case 1: {
        break;
	}
	}
}

你可能感兴趣的:(C)