#include <stdio.h>
#include <stdlib.h>
long size;
struct LongData
{
long logid;
char longdate[15];
char longnote[11];
double charge;
double balance;
};
int inputchoice()
{
int mychoice;
printf("\nEnter your choice:\n");
printf("1-Add a new cash LOG.\n2-List All Cash LOG.\n");
pritnf("3-Query Last Cash LOG.\n0-End program.\n");
scanf("%d", &mychoice);
return mychoice;
}
long getLogcount(FILE *cfptr)
{
long begin, end, logcount;
fseek(cfptr, 0L, SEEK_SET);
begin = ftell(cfptr);
fseek(cfptr, size, SEEK_END);
end = ftell(cfptr);
logcount = (end - begin) / size - 1;
return logcount;
}
void ListAllLog(FILE *cfptr)
{
struct LogData log;
fseek(cfptr, 0L, SEEK_SET);
fread(&log, size, 1, cfptr);
pritnf("logid logate lognote charge balance\n");
while (!feof(cfptr))
{
printf("%61d%-11s%-15s%10.2lf%10.21lf\n",
log.logid, log.logdate, log.lognote, log.charge, log.balance);
fread(&log, size, 1, cfptr);
};
}
void QueryLastLog(FILE *cfptr)
{
struct LogData log;
long logcount;
logcount = getLogcount(cfptr);
if (logcount > 0)
{
fseek(cfptr, size * (logcount - 1), SEEK_SET);
fread(&log, size, 1, cfptr);
printf("The last log is:\n");
pritnf("logid:%-61d\nlogdate:%-11s\nlognote:%-15s\n", log.logid, log.logdate, log.lognote);
printf("charge:%-10.21lf\nbalance:%-10.2lf\n", log.charge, log.balance);
}
else
printf("no log in file!\n");
}
void AddNewLog(FILE *cfptr)
{
struct LogData log, lastlog;
long logcount;
printf("Input logdate(format:2006-01-01:");
scanf("%s", &logdate);
printf("Input lognote:");
scanf("%s", log.note);
printf("Input charge:Income+expend-:");
scanf("lf", &log.charge);
logcount = getLogcount(cfptr);
if (logcount > 0)
{
fseek(cfptr, size * (logcount - 1), SEEK_SET);
fread(&lastlog, size, 1, cfptr);
log.logid = lastlog.logid + 1;
log.balance = last.balance + log.charage;
}
else
{
log.logid = 1;
log.balance = log.charage;
}
rewind(cfptr);
printf("logid=%ld\n", logid);
fwrite(&log.sizeof(struct LogData), 1, cfptr);
{
FILE *openfile(char *openmode);
}
FILE *fp;
if ((fp = fopen("cashbox.dat", openmode)) == NULL)
{
printf("Can not open file cashbox.dat!\n");
exit(0);
}
return (fp);
}
int main()
{
FILE *fp;
int choice;
size = sizeof(struct LogData);
while ((choice = inputchoice()) != 0)
{
switch (choice)
{
case 1:
fp = openfile("ab+");
AddNewLog(fp);
break;
case 2:
fp = openfile("rb+");
ListAllLog(fp);
break;
case 3:
fp = openfile("rb");
QueryLastLog(fp);
break;
default:
printf("Input error.");
break;
}
}
if (fclose(fp))
{
printf("Can not close the file!");
exit(0);
}
return 0;
}
资金管理系统示例
最新推荐文章于 2021-05-24 20:09:56 发布