网吧计费程序,高手看看?
/*这个程序主要用来网吧计费*/
/*头文件head.h*/
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*主函数main*/
#include "fees\head.h"
#include "fees\up.c" /*上机函数up*/
#include "fees\down.c" /*下机函数down*/
#include "fees\money.c" /*加钱函数money*/
main()
{
while(1)
{int button;
clrscr();
printf("Press the button : \n");
printf("1 Let's go ! \n");
printf("2 Goodbye ! \n");
printf("3 Give me money ! \n");
printf("4 End the program.\n");
scanf("%d",&button);
switch(button)
{case 1:up();break;
case 2:down();break;
case 3:money();break;
case 4:exit(0);
default:printf("You press the wrong botton ! \n");
printf("Press again ! \n");
}
}
}
/*上机函数up*/
#include"fees\head.h"
void up()
{
struct student /*这个结构体在其它函数中变量名相同,作用相同*/
/*实际上这几个函数的作用机制都相同,一看就明白*/
{long int num; /*上机者的卡号*/
float money; /*上机者的钱*/
time_t *times; /*上机者的上机时间*/
}stdt,std; /*stdt用来暂时存放数据,std用来从文件中读取数据*/
FILE *fp;
fp=fopen("fees\student.txt","r+"); /*关键就在这一句,找不到合适的,r+实际上是先读完再写,但这里我要求,一打开就又能读又能写,该用什么?*/
printf("Please enter the number : \n");
scanf("%ld",&stdt.num);
rewind(fp); /*文件指针指向文件开始处*/
while(fread(&std,sizeof(struct student),1,fp)==1)
{if(stdt.num==std.num)
{stdt.money=std.money;
fseek(fp,-sizeof(struct student),1);
break;}
}
if(feof(fp))
{printf("Please enter the money : \n");
scanf("%f",&stdt.money);}
printf("Your number is : %ld",stdt.num);
printf("\nYour money is : %.2f",stdt.money);
sleep(3);
time(stdt.times); /*获得当前时间*/
fwrite(&stdt,sizeof(struct student),1,fp);
fclose(fp);
}
/*下机函数down*/
#include"fees\head.h"
void down()
{
float hour,differ;
struct student
{long int num;
float money;
time_t *times;
}stdt,std;
time_t t,*timee=&t;
FILE *fp;
fp=fopen("fees\student.txt","r+");
printf("Please enter the number : \n");
scanf("%ld",&stdt.num);
rewind(fp);
while(fread(&std,sizeof(struct student),1,fp)==1)
{if(stdt.num==std.num)
{stdt.money=std.money;
*stdt.times=*std.times;
break;}
}
time(timee);
differ=(float)(difftime(*timee,*stdt.times));
hour=differ/60/60;
stdt.money=stdt.money-hour*1.5;
printf("Your number is : %ld",stdt.num);
printf("\nYour money is : %.2f",stdt.money);
sleep(3);
if(stdt.money<=0) printf("You must add some money ! \n");
fseek(fp,-sizeof(struct student),1);
fwrite(&stdt,sizeof(struct student),1,fp);
fclose(fp);
}
/*加钱函数money*/
#include"fees\head.h"
money()
{
float money;
struct student
{long int num;
float money;
time_t *times;
}stdt,std;
FILE *fp;
fp=fopen("fees\student.txt","r+");
printf("Please enter the number : \n");
scanf("%ld",&stdt.num);
rewind(fp);
while(fread(&std,sizeof(struct student),1,fp)==1)
{if(stdt.num==std.num)
{stdt.money=std.money;
*stdt.times=*std.times;
break;}
}
printf("Please enter the money : \n");
scanf("%f",&money);
stdt.money=stdt.money+money;
printf("Your number is : %ld",stdt.num);
printf("\nYour money is : %.2f",stdt.money);
sleep(3);
fseek(fp,-sizeof(struct student),1);
fwrite(&stdt,sizeof(struct student),1,fp);
fclose(fp);
}
现在这个程序就只有一个问题,每次加进去的钱或是上网花的钱可以算出来,但是无法在文件中留住。为什么?
问题点数:20、回复次数:1Top
1 楼dengwei007(邓蔚)回复于 2002-07-07 18:54:20 得分 20
先把文件全部read一遍再writeTop




