#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define Max 8
char key[Max];
int step = 0;
char passwords_1[Max];
typedef struct da
{
int day;
float price1;
float price2;
float price3;
struct da *next;
}data;
int login();//登陆模块
void make_note();//记录模块
void found();//查询模块
void choice();//菜单界面
void ending();//结束模块
void main()
{
for(step=1;step<Max;step++ )
{
key[step] = 1;
}
login( );
}
void choice ()
{
char choose;
while(1)
{
fflush(stdin);
printf("请选择要执行的操作:\n");
printf("A.记录账本\n");
printf("B.查询旧账目\n");
printf("C.结束程序\n");
printf("请输入您的选项:");
choose=getchar();
system("pause");
system("cls");
switch (choose)
{
case 'A':
case 'a': make_note();break;
case 'B':
case 'b': found();break;
default : ending();
}
}
}
void make_note(){
int when;
int day;//日期
float zao=0, wu=0, wan=0;//早、午、晚花销
FILE *fp;
printf("请输入记录的日期\n");
scanf("%d",&when);
fp = fopen("C:\\数据结构\\流水帐本.dat","a");
if(fp==NULL)
{
printf("无法打开此文件\n");
getch();
exit(0);
}
day=when;//创建数据编号
printf("请输入早餐费用(请输入三位数,例如xx.x):");
scanf("%f",&zao);
printf("请输入午餐费用(请输入三位数,例如xx.x):");
scanf("%f",&wu);
printf("请输入晚餐费用(请输入三位数,例如xx.x):");
scanf("%f",&wan);
fprintf(fp,"%d%3.1f%3.1f%3.1f\n",day,zao,wu,wan);
fclose(fp);
system("cls");
system("pause");
} ;
void found()
{
char aim[Max];
char day[Max];
float zao=0,wu=0,wan=0;
data *p,*t;
FILE *fp;
data *Head;
Head = (data*)malloc(sizeof(data));//为首节点分配空间
Head->next=NULL;
fp = fopen("C:\\数据结构\\流水帐本.dat","r");
if(fp == NULL)
{
printf("无法打开此文件\n");
exit(0);
}
fscanf(fp,"%s%f%f%f",day,&zao,&wu,&wan);
Head->day=atoi(day);
Head->price1=zao;
Head->price2=wu;
Head->price3=wan;
p = Head;
while(!feof(fp))
{
t=(data*)malloc(sizeof(data));
fscanf(fp,"%s%f%f%f",day,&zao,&wu,&wan);
t->day=atoi(day);
t->price1=zao;
t->price2=wu;
t->price3=wan;
t->next=NULL;
p->next=t;
p=t;
}
p=Head;
fflush(stdin);
while(1)
{
printf("\n输入要查询的日期:");
scanf("%s",aim);
for(step=0;step<8;step++)
if(aim[step] != day[step])
{
p=p->next;
if(p==NULL)
{
printf("查无此日期的内容!");
break;
}
continue;
}
else
{
break;
}
break;
}
system("cls");
printf("早餐:%3.1f\n午餐: %3.1f\n晚餐: %3.1f\n",p->price1,p->price2,p->price3);
fclose(fp);
//system("cls");
system("pause");
free(p);
free(day);
system("cls");
fflush(stdin);
};//查询模块
int login()
{
char passwords[Max+1], ch;
int i=0;
puts("输入密码:\n");
while((ch = getch()) != '\r' && i < Max)
{
if (ch == Max)
{
if (i > 0)
{
passwords[--i] = NULL;
printf("\b ");
printf("%d%d%d", Max, ' ', Max);
}
else
putchar(7); //bell
}
else
{
passwords[i++] = ch;
printf("*");
}
}
passwords[i] = '\0';
if(strcmp(key,passwords_1) != 0 )
{
printf("密码错误!\r");
getch();
exit(0);
}
printf("\n");
system("pause");
system("cls");
choice();
};//登陆主界面
void ending()
{
system("cls");
printf(" \n**************************************************************");
printf(" \n**************************************************************");
printf(" \n** **");
printf(" \n** 谢谢使用 **");
printf(" \n** **");
printf(" \n** **");
printf(" \n**************************************************************");
printf(" \n**************************************************************");
getch();
exit(0);
};//结束模块