资金管理系统示例

#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超翔之逸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值