C语言课程设计——账单管理

        学了一学期C语言程序设计,期末需要完成一份课程设计;以下为我的期末课程设计,特向大家分享一下。本人初学菜鸟,代码中难免会有错误,请大家谅解,我还在努力学习中。

一、课程设计思路

        C语言的控制和数据结构丰富灵活,语句表达简洁高效,并且程序结构很清晰。为了实现对C语言的结构、指针和文件的综合运用,因此利用账单管理的基本操作来表现他们。功能包括添加账单、罗列账单、操作账单和账户余额。

二、代码实现

#include //包含头文件
#include
#include

long size;
int inputchoice(){ //功能选择函数
    int Choice;
    printf("请输入您的操作数:\n");
    printf("\t\t(1)Add a cash log.\t\t(2)List all cash log.\n");
    printf("\t\t(3)Check balance.\t\t(4)Operate a cash log.\n\t\t(0)End program.\n");
    scanf("%d",&Choice);
    return Choice;
}

typedef struct LogData{ //定义账单存储数据的结构
    char Time[20],Do[20],Who[20];
    double charge;
    struct LogData *next;
}Snode,*Slink;
Slink head=NULL;

long getlogcount(FILE *fp){ //统计文件里的字节数量
    long begin,end,logcount;
    fseek(fp,0L,SEEK_SET);
    begin=ftell(fp);
    fseek(fp,size,SEEK_END);
    end=ftell(fp);
    logcount=(end-begin)/size-1;
    return logcount;
}

void AddNewLog(){ //功能函数,添加新帐单
    int num=1;
    struct LogData Thing;
    FILE *fp;
    if((fp=fopen("MoneyNote.txt","a"))==NULL){
        printf("Open Error!\n");
        exit(0);
    }
    while (num!=0){
        printf("Input Date(例:2006-01-01):");
        scanf("%s",Thing.Time);
        printf("Input Did:");
        scanf("%s",Thing.Do);
        printf("Input Charge(支出需加'-'):");
        scanf("%lf",&Thing.charge);
        printf("Input Name:");
        scanf("%s",Thing.Who);
        fprintf(fp," %s %s %lf %s",Thing.Time,Thing.Do,Thing.charge,Thing.Who);
        printf("按0结束,任意数字继续输入!\n");
        scanf("%d",&num);
        if(num!=0)fprintf(fp,"\n");
    }
    fclose(fp);
}

Slink receive_txt(){ //读取文件的数据,并记录在h中
    FILE *fp;
    fp=fopen("MoneyNote.txt","r");
    if(fp==NULL){
        printf("Open Error!\n");
        exit(0);
    }
    rewind(fp);
    Slink h=NULL;
    Slink r=NULL;
    Slink s=NULL;
    while(fgetc(fp)!=EOF){
        s=(Slink)malloc(sizeof(Snode));
        fscanf(fp,"%s%s%lf%s",s->Time,s->Do,&s->charge,s->Who);
        if(h==NULL){
            h=s;
            r=s;
        }
        else{
            r->next=s;
            r=s;
        }
        r->next=NULL;
    }
    fclose(fp);
    return h;
}

void display(Slink head){ //打印形参head里的数据
    Slink p=head;
    while(p!=NULL){
        printf("%s %s %.2f %s\n",p->Time,p->Do,p->charge,p->Who);
        p=p->next;
    }
}

void ListAllLog(){ //功能函数,打印所有账单记录
    head=receive_txt();
    if(head==NULL)printf("您还没有账单!\n\n");
    else display(head);
}
void CheckBalance(){ //功能账单,显示余额
    head=receive_txt();
    double Balance=0;
    if(head==NULL)printf("您还没有账单!\n\n");
    else{
        Slink p=head;
        while(p!=NULL){
            Balance+=p->charge;
            p=p->next;
        }
        printf("余额为:%.2f\n",Balance);
    }
}


void OperateLog(){ //功能函数,可以对账单进行筛选查询或删除操作
    int shu;char date1[20],did1[20],name1[20];
    head=receive_txt();
    if(head==NULL)printf("您还没有账单!\n\n");
    else{
        printf("请您输入需要的操作数:(1)筛选 (2)删除\n");
        scanf("%d",&shu);
        if(shu==1){
        printf("请您选择筛选类型:(1)Date (2)Did (3)Name\n");
        scanf("%d",&shu);
        Slink p=head;
        if(shu==1){
            printf("Input Date(例:2006-01-01):");
            scanf("%s",date1);
            while(p!=NULL){
                if(!strcmp(p->Time, date1))
                    printf("%s %s %.2f %s\n",p->Time,p->Do,p->charge,p->Who);
                p=p->next;
            }
        }
        else if(shu==2){
            printf("Input Did:");
            scanf("%s",did1);
            while(p!=NULL){
                if(!strcmp(p->Do,did1))
                    printf("%s %s %.2f %s\n",p->Time,p->Do,p->charge,p->Who);
                p=p->next;
            }

        }
        else if(shu==3){
            printf("Input Name:");
            scanf("%s",name1);
            while(p!=NULL){
                if(!strcmp(p->Who,name1))
                    printf("%s %s %.2f %s\n",p->Time,p->Do,p->charge,p->Who);
                p=p->next;
            }

        }
        else
            printf("没有这个筛选类型!\n");
        }
        else if(shu==2){
            FILE *fp;struct LogData Thing;int rember=0;
            char date2[20],did2[20],name2[20];
            printf("请您正确输入账户信息(Date Did Name):\n");
            scanf("%s%s%s",date2,did2,name2);
            fp=fopen("MoneyNote.txt","r");
            if(fp==NULL){
                printf("Open Error!\n");
                exit(0);
            }
            rewind(fp);
            Slink h=NULL,r=NULL,s=NULL;
            while(fgetc(fp)!=EOF){
                fscanf(fp,"%s%s%lf%s",Thing.Time,Thing.Do,&Thing.charge,Thing.Who);
                if(!strcmp(Thing.Time,date2)&&!strcmp(Thing.Do, did2)&&!strcmp(Thing.Who, name2)){
                    rember++;
                    continue;
                }
                else{
                    s=(Slink)malloc(sizeof(Snode));
                    *s=Thing;
                    if(h==NULL){
                        h=s;
                        r=s;
                    }
                    else{
                        r->next=s;
                        r=s;
                    }
                    r->next=NULL;
                }
            }
            if(rember>0)printf("已删除%d笔账单\n",rember);
            else printf("没有这笔账单,请您正确输入!\n");
            fclose(fp);
            fp=fopen("MoneyNote.txt","w");
            if(fp==NULL){
                printf("Open Error!\n");
                exit(0);
            }
            while(h!=NULL){
                fprintf(fp," %s %s %lf %s",h->Time,h->Do,h->charge,h->Who);
                if(h->next!=NULL)fprintf(fp,"\n");
                h=h->next;
            }
            fclose(fp);
        }
        else
            printf("没有这个操作!\n");

    }
}

int main(){ //主函数,调用功能函数
    int Choice;
    system("color 34");
    printf("\n\t\t\t\t\t欢迎您使用班费账单管理系统\n\n");
    size=sizeof(struct LogData);
    while((Choice=inputchoice())!=0) {
        switch (Choice) {
            case 1:
                AddNewLog();
                break;
            case 2:
                ListAllLog();
                break;
            case 3:
                CheckBalance();
                break;
            case 4:
                OperateLog();
                break;
            default:
                printf("没有该操作,请您正确输入!\n\n");
                break;
        }
    }
    return 0;
}

三、总结感悟

        1、代码实现需要一步一个脚印,不可能一口气吃掉一只羊;2、模块化编程,条理清晰查看方便;3、局部优化,节点上可增加'printf'。

        C语言是学编程的首选入门语言,因为它简洁灵活,所以容易理解掌握C语言。基础牢了,学习其他编程语言很轻松就上手,学习无止境。

        关注博主微信公众号“订阅号分拣站”,获取更多其他干货!

 

你可能感兴趣的:(C语言程序设计)