loading请求处理中...

C语言课程设计

2021-12-02 22:30:20 阅读 8288次 标签: c语言程序设计 作者: 陈先生12121
一、课程设计思路


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


二、代码实现


#include //包含头文件
#include
#include
 
long size;
int inputchoice(){ //功能选择函数
    int Choice;
    printf("请输入您的操作数:n");
    printf("tt(1)Add a cash log.tt(2)List all cash log.n");
    printf("tt(3)Check balance.tt(4)Operate a cash log.ntt(0)End program.n");
    scanf("%d",&Choice);
    return Choice;
}
 
typedef struct LogData{ //定义账单存储数据的结构
    c har 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 %sn",p->Time,p->Do,p->charge,p->Who);
        p=p->next;
    }
}
 
void ListAllLog(){ //功能函数,打印所有账单记录
    head=receive_txt();
    if(head==NULL)printf("您还没有账单!nn");
    else display(head);
}
void CheckBalance(){ //功能账单,显示余额
    head=receive_txt();
    double Balance=0;
    if(head==NULL)printf("您还没有账单!nn");
    else{
        Slink p=head;
        while(p!=NULL){
            Balance+=p->charge;
            p=p->next;
        }
        printf("余额为:%.2fn",Balance);
    }
}
 
 
void OperateLog(){ //功能函数,可以对账单进行筛选查询或删除操作
    int shu;c har date1[20],did1[20],name1[20];
    head=receive_txt();
    if(head==NULL)printf("您还没有账单!nn");
    else{
        printf("请您输入需要的操作数:(1)筛选 (2)删除n");
        scanf("%d",&shu);
        if(shu==1){
        printf("请您选择筛选类型:(1)Date (2)Did (3)Namen");
        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 %sn",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 %sn",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 %sn",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;
            c har 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("nttttt欢迎您使用班费账单管理系统nn");
    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("没有该操作,请您正确输入!nn");
                break;
        }
    }
    return 0;
}
三、总结感悟


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


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

Tag: 语言

设计公司推荐

成为一品威客服务商,百万订单等您来有奖注册中

留言( 展开评论

快速发任务

价格是多少?怎样找到合适的人才?

官方顾问免费为您解答

 
相关任务
DESIGN TASK 更多
小程序设计

¥1500 已有1人投标

小程序设计

¥1300 已有3人投标

BANNER及表单程序设计

¥250 已有1人投标

物联网小程序设计

¥1400 已有1人投标

践谈小程序设计

¥3000 已有1人投标

车场管理小程序设计开发

¥5000 已有1人投标

家政服务小程序设计开发

¥20000 已有1人投标