#include<stdio.h>
#include<string.h>
#include<windows.h>
#define bool char
#define true 1
#define false 0
#define NUM 100
// IC卡账户信息
struct cardInfo
{
char sno[20]; // 学号
char name[20]; // 姓名
char cardID[12]; // 卡号
float balance; // 余额
};
// 函数声明
bool login();
void menu(); // 主菜单
void openAccount(); // 开户
void showCardInfo(struct cardInfo card); // 显示账户信息的方法
void showCardInfos(struct cardInfo cards[], int count);
void view(); // 显示所有账户信息
void income(); // 充值
void init(); // 初始化
void saveData(); // 保存数据
void search(); // 查找
struct cardInfo cards[NUM]; // 用来存放所有的账户信息
int TOTALCOUNT = 0; // 记录数
bool isEnd = false; // 是否退出系统
void main()
{
bool bl = 0;
int count = 0; // 用户记录输入输入账号和密码的次数,最多允许输入3次
do
{
if (count == 3)
{
break; // 跳出while循环
}
if (count > 0)
{ // 说明不是第一次登陆
printf("\n\n\t账号或密码错误,请确认后重新登录...");
printf("\n\n\t您还有 %d 次机会<请按回车继续>", 3-count);
getch();
}
count++;
bl = login(); // 调用登陆的方法,实现用户登陆
}while( !bl );
// 问题:当用户登陆成功也会跳出while,如果超过3次也会跳出while
if ( !bl )
{
printf("\n\n\t对不起,您暂无权限...\n\t");
system("exit");
} else // 登陆成功
{
init(); // 初始化数据,即将数据从数据文件读取到系统中
do{
menu(); // 显示主菜单
}while(!isEnd);
}
printf("\n\n\t");
exit;
system("exit /B");
}
// 主菜单
void menu()
{
int flag = 0 ;
system("cls");
printf("\n\t**********************湖南工学院IC卡管理系统**********************");
printf("\n\t************************** 1. IC卡开户 **************************");
printf("\n\t************************** 2. 查询IC卡信息 ***********************");
printf("\n\t************************** 3. 浏览IC卡信息 ***********************");
printf("\n\t************************** 4. IC卡充值 ************************");
printf("\n\t************************** 5. 修改IC卡信息 ***********************");
printf("\n\t************************** 6. 退出系统 ************************");
printf("\n\n\t请选择您要进行的操作(1-6):");
scanf("%d",&flag);
switch(flag)
{
case 1:openAccount(); break;
case 2:search(); break;
case 3:view(); break;
case 4:income(); break;
case 5: break;
case 6:saveData(); isEnd=true; break;
default:break;
}
}
// 用户登录方法
bool login()
{
char name[20]; // 存储用户输入的账号
char pwd[20], ch; // 存储用户输入的密码
int index = 0; // 输入密码的位数
system("cls");
printf("\n\t**********************湖南工学院IC卡管理系统**********************");
printf("\n\n\t请输入您的账号:");
// 获取用户输入的账号
scanf("%s", name);
printf("\n\t请输入您的密码:");
// 获取用户输入的密码
while ( (ch=getch()) != 13 ) // 不是回车键
{
if ( ch ==8 ) // 如果是退格 Backspace
{
if(index <= 0)
{
index = 0;
}
else
{
printf("\b \b");
index --;
}
}
else
{
pwd[index++] = ch;
putch('*');
}
}
pwd[index] = '\0';
//scanf("%s", pwd); // 当用户输入密码时,如何用掩码的方式显示 ***
// 比较用户输入的账号和密码是否正确。如果用户输入的账号是yc 并且密码是 123321,则认为是合法的用户,那么跳转到主界面,否则提示错误。
if ( strcmp("yc",name) == 0 && strcmp("123321", pwd) == 0)
{
return true;
} else
{
return false;
}
}
// 开户
void openAccount()
{
struct cardInfo card; // 声明结构体变量
int result;
FILE *fp; // 声明一个文件指针,用来指向打开的这个文件
system("cls");
printf("\n\t**********************湖南工学院IC卡管理系统**********************");
printf("\n\t************************** IC卡开户 **************************");
printf("\n\t*********************** 请输入以下信息 ************************");
printf("\n\n\t请输入学号:");
scanf("%s", card.sno);
printf("\n\t请输入卡号:");
scanf("%s", card.cardID);
printf("\n\t请输入姓名:");
scanf("%s", card.name);
printf("\n\t请输入充值金额:");
scanf("%f", &card.balance);
printf("\n\n\t您输入的信息为:");
showCardInfo(card);
result = MessageBox(NULL, "您确定要添加此账号信息吗?", "确认提示", MB_YESNO|MB_ICONINFORMATION);
if (result == 6) // 说明确定添加
{
strcpy(cards[TOTALCOUNT-1].sno, card.sno);
strcpy(cards[TOTALCOUNT-1].cardID, card.cardID);
strcpy(cards[TOTALCOUNT-1].name, card.name);
cards[TOTALCOUNT-1].balance = card.balance;
TOTALCOUNT++;
// 将这个数据写入到数据文件
/*
打开一个数据文件
fopen(要打开的数据文件路径, 打开的方式);
*/
fp = fopen("cards.txt","a"); // 以追加的方式打开文件
/*
将要保存的数据写入到这个打开的文件中
fwrite() 写入数据到指定文件中,它里面包含四个参数
第一个参数:要写入数据文件的数据的地址 -> 要将用户输入的这个IC卡账号信息写入到数据文件 -> 即这个账号结构体的地址
第二个参数:要写入数据文件的数据的大小 -> 要将这个结构体数据写入到数据文件,所以我要写入的数据文件大小应该就是这个结构体大小
第三个参数:要写入几个这个的大小 -> 这里我们只需要保存一个IC卡账户信息
第四个参数:写入到那个数据文件 -> 这里是写入到我们打开的这个数据文件fp
*/
fwrite(&card, sizeof(struct cardInfo), 1, fp);
// 关闭文件
fclose(fp);
printf("\n\n\t卡户成功<请按回车键返回>\n\n\t");
}
else
{
printf("\n\n\t不添加\n\n\t");
}
}
// 用来显示指定的账户信息
void showCardInfo(struct cardInfo card)
{
printf("\n\n\t 卡号 \t\t 学号 \t\t学生姓名 \t\t 余额 ");
printf("\n\t%-15s\t%-12s\t%-20s\t%-6.2f\t",card.cardID,card.sno,card.name,card.balance);
printf("\n\n\t");
}
// 浏览账户信息
void view()
{
system("cls");
printf("\n\t**********************湖南工学院IC卡管理系统**********************");
printf("\n\t************************ 浏览账户信息 ************************");
if (TOTALCOUNT>0)
{
// 显示数据
showCardInfos(cards, TOTALCOUNT);
printf("\n\n\t<请按回车返回>");
}
else
{
printf("\n\n\t暂无账号信息<请按回车返回>");
}
getch();
}
// 显示单个账户信息
void showCardInfos(struct cardInfo cards[], int count)
{
int i = 0;
printf("\n\n\t 卡号 \t\t 学号 \t\t学生姓名 \t\t 余额 ");
for(i; i<count-1; i++)
{
printf("\n\t%-15s\t%-12s\t%-20s\t%-6.2f\t", cards[i].cardID, cards[i].sno, cards[i].name, cards[i].balance);
}
printf("\n\n\n\t************************ 共 %d 条数据 ************************", count-1);
printf("\n\n\t");
}
// 初始化系统,用来读取本系统中的数据所有数据
void init()
{
FILE *fp;
if ( (fp = fopen("cards.txt", "r") ) != NULL)
{
// 循环读取数据到结构体数组中
while( !feof(fp) ) // feof file end of: 到文件最后了吗
{
/*
fread() 用来从指定的数据文件读取数据
第一个参数:将数据读到哪里 -> 将数据从数据文件读到结构体数据中
第二个参数:每个读多大 -> 每次读取一个结构体那么大
第三个参数:每次读几个这样的大小
第四个参数:从哪里读 -> 从打开的数据文件中读
*/
fread(&cards[TOTALCOUNT], sizeof(struct cardInfo), 1, fp);
TOTALCOUNT++;
}
fclose(fp);
}
}
// 充值
void income()
{
char cardID[12]; // 用来存放用户输入的充值卡卡号
int i = 0;
float money;
system("cls");
printf("\n\t**********************湖南工学院IC卡管理系统**********************");
printf("\n\t************************ 账户充值中心 ************************");
// 先判断系统中有没有用户信息
if (TOTALCOUNT > 0) {
printf("\n\n\t请输入要充值的卡号:");
scanf("%s", cardID);
// 从系统中查找有没有这个卡号
for (i=0; i<TOTALCOUNT-1; i++)
{
if (strcmp(cardID, cards[i].cardID) == 0)
{ // 如果是相等的,则说明这个用户找到了
// 显示这个账号,然后提醒输入输入充值的金额
printf("\n\n\t您要充值的账户信息如下:");
showCardInfo(cards[i]);
printf("\n\n\t请输入充值金额:");
scanf("%f", &money);
// 修改此账户的余额
cards[i].balance += money;
// 将数据写入到数据文件
printf("\n\n\t充值成功<请按回车键返回>");
break;
}
}
// 第一种是找到了退出循环,第二种是没找到退出循环
if ( i >= TOTALCOUNT-1 )
{
printf("\n\n\t暂无该账号信息<请按回车返回>");
}
}
else
{
printf("\n\n\t暂无账号信息<请按回车返回>");
}
getch();
}
// 保存数据
void saveData()
{
FILE *fp;
int i = 0;
if (TOTALCOUNT > 0) { // 说明有数据要保存
fp = fopen("cards.txt","w"); // 以追加的方式打开文件
/*
将要保存的数据写入到这个打开的文件中
fwrite() 写入数据到指定文件中,它里面包含四个参数
第一个参数:要写入数据文件的数据的地址 -> 要将用户输入的这个IC卡账号信息写入到数据文件 -> 即这个账号结构体的地址
第二个参数:要写入数据文件的数据的大小 -> 要将这个结构体数据写入到数据文件,所以我要写入的数据文件大小应该就是这个结构体大小
第三个参数:要写入几个这个的大小 -> 这里我们只需要保存一个IC卡账户信息
第四个参数:写入到那个数据文件 -> 这里是写入到我们打开的这个数据文件fp
*/
for(i=0; i<TOTALCOUNT-1; i++)
{
fwrite(&cards[i], sizeof(struct cardInfo), 1, fp);
}
// 关闭文件
fclose(fp);
}
else
{
remove("cards.txt");
}
}
// 查找
void search()
{
char sno[20]; // 用来存放用户输入的充值卡卡号
int i=0, index = 0, len = 0, count = 0;
bool isFind = false; // 是否找到
struct cardInfo cd[NUM]; // 满足条件的账户信息
system("cls");
printf("\n\t**********************湖南工学院IC卡管理系统**********************");
printf("\n\t************************ 账户查询中心 ************************");
// 先判断系统中有没有用户信息
if (TOTALCOUNT > 0) {
printf("\n\n\t请输入要查询的学生学号:");
scanf("%s", sno);
// 从系统中查找有没有这个卡号
for (i=0; i<TOTALCOUNT-1; i++) // 模糊查找
{
isFind = true;
for(index=0,len=(int)strlen(sno); index<len; index++)
{
if(cards[i].sno[index] != sno[index] ) // 说明不匹配
{
isFind = false;
break;
}
}
// 如果所有的字符都能匹配则满足条件
if (isFind)
{
cd[count] = cards[i];
count++;
}
}
// 第一种是找到了退出循环,第二种是没找到退出循环
if ( count > 0)
{
printf("\n\n\t满足条件的信息如下:");
showCardInfos(cd, count+1);
}
else
{
printf("\n\n\t暂无该账号信息<请按回车返回>");
}
}
else
{
printf("\n\n\t暂无账号信息<请按回车返回>");
}
getch();
}