C语言 模块化小程序

个人账簿管理系统
在本次的C语言程序设计中,我们可以很清晰的看到,c语言的模块程序化设计的特点,将账簿的七种相关的功能分别定义互不影响,通过主函数中switch函数进行选择与调用,简介方便,结构清楚,漂亮
#include 
#include 
#include 
#define N 100

typedef struct
{
    int year;
    int month;
    int day;
    double earn;
    double pay;
    double balance;
    double basic;
    double amuse;
    double edu;
    double med;
}ACC;

void MenuDisplay(void);
void EditDisplay(void);// 
void load(ACC acc[],int *np);
void save(ACC acc[],int n);
int FindByDate(ACC acc[],int n);
void sort(ACC acc[],int n);
void SortByEarn(ACC acc[],int n);
void SortByPay(ACC acc[],int n);
void SortByBalance(ACC acc[],int n);
void SortByDate(ACC acc[],int n);
void InputRecord(ACC acc[],int i);
void find(ACC acc[],int n);
void del(ACC acc[],int *np);
void add(ACC acc[],int *np);
void edit(ACC acc[],int n);
void DeleteRecord(ACC acc[],int *np);
void output(ACC acc[],int n);
void PrintRecord(ACC acc);

int main()
{
    system("color 2E");
    ACC acc[N];
    int n;
    int select;
    load(acc,&n);
    while(1)
    {
        MenuDisplay();
        scanf("%d",&select);
        switch(select)
        {
            case 1:add(acc,&n);break;
            case 2:edit(acc,n);break;
            case 3:sort(acc,n);break;
            case 4:find(acc,n);break;
            case 5:del(acc,&n);break;
            case 6:output(acc,n);break;
            case 7:
                system("CLS");
                printf("\n                               感谢本次使用,再见!\n");
                return 0;
        }
    }
    return 0;
}

void MenuDisplay(void)
{
    system("CLS");
    printf("                          ☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
    printf("☆☆☆☆☆☆☆☆☆☆☆☆☆☆欢迎使用本账簿管理系统☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
    printf("                          ☆☆☆☆☆☆☆☆☆☆☆☆☆\n\n");
    printf("                 说明:在本系统中欲执行某功能请输入相应数字!\n\n");
    printf("                                  **********\n");
    printf("                                  * 增添:1 *\n");
    printf("                                  * 修改:2 *\n");
    printf("                                  * 排序:3 *\n");
    printf("                                  * 查找:4 *\n");
    printf("                                  * 删除:5 *\n");
    printf("                                  * 输出:6 *\n");
    printf("                                  * 退出:7 *\n");
    printf("                                  **********\n\n");
    printf("                             请选择您要进行的操作:");
}

//将txt中记录复制到数组
void load(ACC acc[],int *np)
{
    FILE *fp;
    int i;
    if((fp=fopen("d:\\account","r"))==NULL)
    {
        *np=0;
        return;
    }
    for(i=0;fread(&acc[i],sizeof(ACC),1,fp)!=0;i++);
    *np=i;         //*np记录文件中当前学生数量
    fclose(fp);
}

//将数组中记录复制到磁盘
void save(ACC acc[],int n)
{
    FILE *fp;
    if((fp=fopen("d:\\account","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    fwrite(acc,n*sizeof(acc[0]),1,fp);
    fclose(fp);
}

//输出信息
void output(ACC acc[],int n)
{
    system("CLS");
    printf("所有账单信息如下:\n");
    int i;
    double money=0.0;
    double out=0.0;
    double in=0.0;
    double abasic=0.0;
    double aamuse=0.0;
    double aedu=0.0;
    double amed=0.0;
    printf("%8s%10s%10s%10s  %10s%9s%9s%10s\n","日期","收入","支出","储蓄","衣食住行","娱乐","教育","医疗");
    for(i=0;iacc[j].earn)
                    {
                        temp=acc[i];
                        acc[i]=acc[j];
                        acc[j]=temp;
                    }
                }
            output(acc,n);
            break;
        case 2:
            for(i=0;iacc[j].pay)
                    {
                        temp=acc[i];
                        acc[i]=acc[j];
                        acc[j]=temp;
                    }
                }
            output(acc,n);
            break;
        case 2:
            for(i=0;iacc[j].balance)
                    {
                        temp=acc[i];
                        acc[i]=acc[j];
                        acc[j]=temp;
                    }
                }
            output(acc,n);
            break;
        case 2:
            for(i=0;iacc[j].year)
                    {
                        temp=acc[i];
                        acc[i]=acc[j];
                        acc[j]=temp;
                    }
                    else if(acc[i].year==acc[j].year)
                    {
                        if(acc[i].month>acc[j].month)
                        {
                            temp=acc[i];
                            acc[i]=acc[j];
                            acc[j]=temp;
                        }
                        else if(acc[i].month==acc[j].month)
                        {
                            if(acc[i].day>acc[j].day)
                            {
                                temp=acc[i];
                                acc[i]=acc[j];
                                acc[j]=temp;
                            }
                        }
                    }
                }
            }
            output(acc,n);
            break;
        case 2:
            for(i=0;i=0)
    {
        printf("\n所查找日期账单如下:\n");
        printf("%8s%10s%10s%10s  %10s%9s%9s%10s\n","日期","收入","支出","储蓄","衣食住行","娱乐","教育","医疗");
        PrintRecord(acc[k]);
        printf("\n支出占收入的: %.1f%%\n",acc[k].pay/acc[k].earn*100);
        printf("衣食住行支出占总支出的: %.1f%%\n",acc[k].basic/acc[k].pay*100);
        printf("娱乐支出占总支出的: %.1f%%\n",acc[k].amuse/acc[k].pay*100);
        printf("教育支出占总支出的: %.1f%%\n",acc[k].edu/acc[k].pay*100);
        printf("医疗支出占总支出的: %.1f%%\n",acc[k].med/acc[k].pay*100);
    }
    else
        printf("您所输入的日期不存在!\n");
    system("pause");
    return;
}

//根据日期查找
int FindByDate(ACC acc[],int n)
{
    int i;
    int year,month,day;
    printf("请输入日期:");
    scanf("%d%d%d",&year,&month,&day);
    for(i=0;i

你可能感兴趣的:(C语言 模块化小程序)