#include<stdio.h>
#include<stdlib.h>
FILE*p;
struct record
{
char j[10];
float k;
char l[10];
int num;
float yu_e;
}m;//每次记录de结构体
int main()
{
void write();
void inspect();
void insert();
void delete_();
void clear();
int b,c;
printf("welcome to the cashbox!Please input the password:\n");
scanf("%d",&b);
if(b!=1998)
{
printf("The password is faulse.\n");
exit(0);
}//输入密码
else
{
while(1==1)
{
printf("\n............................\n1.write 2.inspect 3.insert 4.delete 5.clear 6.exit \n");
scanf("%d",&b);
if(b==1)write();
if(b==2)inspect();
if(b==3)insert();
if(b==4)delete_();
if(b==5)clear();
if(b==6)exit(0);//六个功能
}
}
return 0;
}
void write()//写入记录
{
char a;
int i;
if((p=fopen("C:\\Users\\xxxx\\Desktop\\cashbox.txt","a+"))==NULL)
{
printf("Error!\a");
exit(0);
}
printf("Please input according to the tips\ndate|money|way|\n");
fseek(p,-sizeof(struct record),2);
fread(&m,sizeof(struct record),1,p);
m.num++;
scanf("%s%f%s",m.j,&m.k,m.l);
m.yu_e=m.yu_e+m.k;
fseek(p,-0,2);
fwrite(&m,sizeof(struct record),1,p);
fclose(p);
}
void inspect()//查看记录
{
int i,j;
if((p=fopen("C:\\Users\\xxxx\\Desktop\\cashbox.txt","r"))==NULL)
{
printf("Error!\a");
exit(0);
}
else
{
fseek(p,-sizeof(struct record),2);
fread(&m,sizeof(struct record),1,p);
j=m.num;
rewind(p);
if(j==0)printf("No record.Hit 1 to add.\n");
else
{
printf("\nThe records of consumption of JK Zhang:\n");
for(i=0;i<=j-1;i++){
printf("-----------------------\n");
fread(&m,sizeof(struct record),1,p);
printf("NO.%d\ndate:%s\nmoney:%.2f\nway:%s\nleft:%.2f\n",m.num,m.j,m.k,m.l,m.yu_e);
}
}
fclose(p);
}
}
void insert()//插入记录
{
int i,j,h=0,b,d;
struct records
{
struct record o;
struct records*next;
}n;
struct records*head,*p1,*p2,*p3,*p4;
if((p=fopen("C:\\Users\\xxxx\\Desktop\\cashbox.txt","rb"))==NULL)//注意这里以“rb”形式打开
{
printf("Error!\a");
exit(0);
}
else//将数据读入链表并进行操作
{
fseek(p,-sizeof(struct record),2);
fread(&m,sizeof(struct record),1,p);
j=m.num;
rewind(p);
p1=p2=(struct records*)malloc(sizeof(struct records));
while(h<=j-1)
{
h++;
if(h==1)head=p1;
else p2->next=p1;
p2=p1;
fread(&m,sizeof(struct record),1,p);
p1->o=m;
p1=(struct records*)malloc(sizeof(struct records));
}
p2->next=NULL;
fclose(p);
if((p=fopen("C:\\Users\\xxxx\\Desktop\\cashbox.txt","wb"))==NULL)//这里以wb形式打开
{
printf("Error!\n\a");
exit(0);
}
else
{
printf("Enter the number of before which record you want to insert one:");
scanf("%d",&b);//
printf("Please input according to the tips\ndate|money|way|\n");
p3=(struct records*)malloc(sizeof(struct records));
scanf("%s%f%s",m.j,&m.k,m.l);
m.num=0;
m.yu_e=0;
p3->o=m;
p1=head;
while((p1->o).num!=b-1)
{
p1=p1->next;
}
p4=p1->next;
p1->next=p3;
p3->next=p4;
p1=head;
m=p1->o;
m.num=1;
d=0;
m.yu_e=d+m.k;
d=m.yu_e;
fwrite(&m,sizeof(struct record),1,p);
p1=p1->next;
for(i=1;i<=j;i++)//关键在于在读入de时候搞定NO.和余额,考虑指针还有顺序
{
m=p1->o;
m.num=i+1;
m.yu_e=d+m.k;
d=m.yu_e;
fwrite(&m,sizeof(struct record),1,p);
p1=p1->next;
}
}
fclose(p);
}
}
void delete_()//删除记录
{
int i,j,h=0,b,d;
struct records
{
struct record o;
struct records*next;
}n;
struct records*head,*p1,*p2,*p3,*p4;
if((p=fopen("C:\\Users\\xxxx\\Desktop\\cashbox.txt","rb"))==NULL)//注意这里以“rb”形式打开
{
printf("Error!\a");
exit(0);
}
else//将数据读入链表并进行操作
{
fseek(p,-sizeof(struct record),2);
fread(&m,sizeof(struct record),1,p);
j=m.num;
rewind(p);
p1=p2=(struct records*)malloc(sizeof(struct records));
while(h<=j-1)
{
h++;
if(h==1)head=p1;
else p2->next=p1;
p2=p1;
fread(&m,sizeof(struct record),1,p);
p1->o=m;
p1=(struct records*)malloc(sizeof(struct records));
}
p2->next=NULL;
fclose(p);//链表已构成
if((p=fopen("C:\\Users\\xxxx\\Desktop\\cashbox.txt","wb"))==NULL)//这里以wb形式打开
{
printf("Error!\n\a");
exit(0);
}
else
{
printf("Enter the number of which record you want to delete:\n");
scanf("%d",&b);
p1=head;
if(b>j)printf("There is no such a records.\n\a");
else
{
if(b==1)head=p1->next;
else
{
for(i=0;i<b-2;i++)
{
p1=p1->next;
}
p3=p1;
p4=p1->next;
p3->next=p4->next;
}
p1=head;
m=p1->o;
m.num=1;
d=0;
m.yu_e=d+m.k;
d=m.yu_e;
fwrite(&m,sizeof(struct record),1,p);
p1=p1->next;
for(i=1;i<=j-2;i++)//关键在于在读入de时候搞定NO.和余额,考虑指针还有顺序
{
m=p1->o;
m.num=i+1;
m.yu_e=d+m.k;
d=m.yu_e;
fwrite(&m,sizeof(struct record),1,p);
p1=p1->next;
}
}
fclose(p);
}
}
}
void clear()//清空记录
{
if((p=fopen("C:\\Users\\23954\\Desktop\\cashbox.txt","wb"))==NULL)//只写形式打开实际上就是删除了内容
{
printf("Error!\n\a");
exit(0);
}
fclose(p);
}