大致写了,购物,出行,学习消费等输入输出模块,以及 简单的分析收支情况
分了一个头文件
5个源文件
(还是比较简陋的)
下面是代码:
头文件:
//123.h
#ifndef __MYFILE_H__
#define __MYFILE_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define SIZE 139
struct shop_Str{
double money;
char diary[SIZE];
};
struct study_Str{
double money;
char diary[SIZE];
};
struct leisure_Str{
double money;
char diary[SIZE];
};
struct sports_Str{
double money;
char diary[SIZE];
};
struct food_Str{
double money;
char diary[SIZE];
};
struct travel_Str{
double money;
char diary[SIZE];
};
struct show_Out_Str{
double money;
char diary[SIZE];
};
struct in_Str{
double money;
char diary[SIZE];
};
void menu_Main(void);
void menu_Out(void);
void menu_In(void);
void menu_Show_In(void);
void menu_Show_Out(void);
void out_Shop_File_Write(void);
void out_Study_File_Write(void);
void out_Leisure_File_Write(void);
void out_Sports_File_Write(void);
void out_Food_File_Write(void);
void out_Travel_File_Write(void);
void in_Salary_File_Write(void);
void in_Extra_File_Write(void);
void show_Out(int);
void show_Salary(void);
void show_Extra(void);
void show_Profit(void);
void show_Out_Single(int);
void show_Out_File_Single(char *);
void show_Out_Ration(void);
void show_In_Ration(void);
void write_Gross_Out(double,int);
void write_Gross_In(double,int);
void frame(void);
int setfile(void);
void set_Gross_File(void);
char * s_gets(char *,int);
void get_Money(double*);
char get_Res(void);
int get_Main(void);
int get_I(void);
void choose_Main(void);
#endif
下面是5个源文件
主函数文件:
//main.c
#include "123.h"
int main()
{
if(setfile())
{
set_Gross_File();
system("pause");
}
choose_Main();
system("pause");
return 0;
}
菜单文件:
//menu.c
#include "123.h"
void menu_Main(void)
{
frame();
printf("0)花钱了 1)挣钱了\n");
printf("2)看看最近的消费情况吧 3)看看最近的收入情况吧\n");
printf("4)看看最近的盈亏状况 5)退出程序\n");
//printf("3)预测下一年是超支了还是有钱了\n");
frame();
return;
}
void menu_Out(void)
{
int choice;
do
{
frame();
printf("哪里花钱了:\n");
printf("0)购物\t1)学习\n"
"2)娱乐\t3)运动\n"
"4)饮食\t5)出行\n"
"6)返回上一级菜单\n");
frame();
do
{
printf("请从以上菜单中选择:");
choice = get_I();
}while(choice < 0 || choice > 6);
switch(choice)
{
case 0:
out_Shop_File_Write(); //进行购物支出的写入子菜单
break;
case 1:
out_Study_File_Write();
break;
case 2:
out_Leisure_File_Write();
break;
case 3:
out_Sports_File_Write();
break;
case 4:
out_Food_File_Write();
break;
case 5:
out_Travel_File_Write();
break;
}
}while(choice != 6);
return;
}
void menu_Show_Out(void)
{
int choice;
frame();
printf("0)单项浏览近期支出情况(详细)\t1)查看近期支出单项的占比(简略)\n"
"2)返回上一级菜单\n");
frame();
do
{
printf("请从以上菜单中选择:");
choice = get_I();
}while(choice < 0 || choice > 2);
while(choice >=0 && choice < 2)
{
show_Out(choice);
frame();
printf("0)单项浏览近期支出情况(详细)\t1)查看近期支出单项的占比(简略)!!!\n"
"2)返回上一级菜单\n");
frame();
do
{
printf("请从以上菜单中选择:");
choice = get_I();
}while(choice < 0 || choice > 2);
}
return;
}
void menu_In(void)
{
int choice;
frame();
printf("0)发工资啦\t1)其他收入来源\n"
"2)返回上一级菜单.\n");
frame();
do
{
printf("请从以上选项中选择:");
choice = get_I();
}while(choice < 0 || choice > 2);
while(choice != 2)
{
switch(choice)
{
case 0:
in_Salary_File_Write();
break;
case 1:
in_Extra_File_Write();
break;
default:
break;
}
frame();
printf("0)发工资啦\t1)其他收入来源\n"
"2)返回上一级菜单.\n");
frame();
do
{
printf("请从以上选项中选择:");
choice = get_I();
}while(choice < 0 || choice > 2);
}
return;
}
void menu_Show_In(void)
{
char choice;
frame();
printf("0)看看工资收入情况\t 1)看看有什么其他收入\n"
"2)看看各收入来源的占比\t 3)返回上一级菜单\n");
frame();
do
{
printf("请根据以上菜单选择:\n");
choice = get_I();
}while(choice < 0 || choice > 3);
while(choice != 3)
{
switch(choice)
{
case 0:
show_Salary();
system("pause");
break;
case 1:
show_Extra();
system("pause");
break;
case 2:
show_In_Ration();
system("pause");
break;
default:
break;
}
frame();
printf("0)看看工资收入情况\t 1)看看有什么其他收入\n"
"2)看看各收入来源的占比\t3)返回上一级菜单\n");
frame();
do
{
printf("请根据以上菜单选择:\n");
choice = get_I();
}while(choice < 0 || choice > 3);
}
}
文件写入:
//_File_Write.c
#include "123.h"
void set_Gross_File(void)
{
FILE * fp;
double num[7] = {0};
double in[3] = {0};
fp = fopen("gross.out","r+b");
fwrite(num,sizeof(double),7,fp);
fclose(fp);
fp = fopen("gross.in","r+b");
fwrite(in,sizeof(double),3,fp);
fclose(fp);
return;
}
void out_Shop_File_Write(void) //购物支出写入
{
FILE * fp;
char res;
struct shop_Str temp;
double add_Sum = 0;
if((fp = fopen("shop.out","a+b")) == NULL)
{
fprintf(stderr,"无法打开shop.out文件\n");
exit(EXIT_FAILURE);
}
do
{
puts("请输入支出购物品名称及用途:");
s_gets(temp.diary,SIZE);
printf("金额:");
get_Money(&temp.money);
fwrite(&temp,sizeof(struct shop_Str),1,fp);
add_Sum += temp.money;
puts("继续?(y or n):");
res = get_Res();
}while('y' == res);
write_Gross_Out(add_Sum,0);
if(fclose(fp))
{
fprintf(stderr,"文件shop.out关闭异常,程序即将退出.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void out_Study_File_Write(void) //学习支出写入
{
FILE * fp;
char res;
struct study_Str temp;
double add_Sum = 0;
if((fp = fopen("study.out","a+b")) == NULL)
{
fprintf(stderr,"无法打开study.out文件\n");
system("pause");
exit(EXIT_FAILURE);
}
do
{
puts("请输入支出学习用品名称及用途:");
s_gets(temp.diary,SIZE);
printf("金额:");
get_Money(&temp.money);
fwrite(&temp,sizeof(struct study_Str),1,fp);
add_Sum += temp.money;
puts("继续?(y or n):");
res = get_Res();
}while('y' == res);
write_Gross_Out(add_Sum,1);
if(fclose(fp))
{
fprintf(stderr,"文件study.out关闭异常,程序即将退出.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void out_Leisure_File_Write(void) //娱乐支出写入
{
FILE * fp;
char res;
struct leisure_Str temp;
double add_Sum = 0;
if((fp = fopen("leisure.out","a+b")) == NULL)
{
fprintf(stderr,"无法打开leisure.out文件\n");
system("pause");
exit(EXIT_FAILURE);
}
do
{
puts("请输入支出娱乐项目名称及用途:");
s_gets(temp.diary,SIZE);
printf("金额:");
get_Money(&temp.money);
fwrite(&temp,sizeof(struct leisure_Str),1,fp);
add_Sum += temp.money;
puts("继续?(y or n):");
res = get_Res();
}while('y' == res);
write_Gross_Out(add_Sum,2);
if(fclose(fp))
{
fprintf(stderr,"文件leisure.out关闭异常,程序即将退出.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void out_Sports_File_Write(void) //运动支出写入
{
FILE * fp;
char res;
struct sports_Str temp;
double add_Sum = 0;
if((fp = fopen("sports.out","a+b")) == NULL)
{
fprintf(stderr,"无法打开sports.out文件\n");
system("pause");
exit(EXIT_FAILURE);
}
do
{
puts("请输入支出运动项目名称及用途:");
s_gets(temp.diary,SIZE);
printf("金额:");
get_Money(&temp.money);
fwrite(&temp,sizeof(struct sports_Str),1,fp);
add_Sum += temp.money;
puts("继续?(y or n):");
res = get_Res();
}while('y' == res);
write_Gross_Out(add_Sum,3);
if(fclose(fp))
{
fprintf(stderr,"文件sports.out关闭异常,程序即将退出.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void out_Food_File_Write(void) //食物支出写入
{
FILE * fp;
char res;
struct food_Str temp;
double add_Sum = 0;
if((fp = fopen("food.out","a+b")) == NULL)
{
fprintf(stderr,"无法打开food.out文件\n");
system("pause");
exit(EXIT_FAILURE);
}
do
{
puts("请输入支出食物名称及用途:");
s_gets(temp.diary,SIZE);
printf("金额:");
get_Money(&temp.money);
fwrite(&temp,sizeof(struct food_Str),1,fp);
add_Sum += temp.money;
puts("继续?(y or n):");
res = get_Res();
}while('y' == res);
write_Gross_Out(add_Sum,4);
if(fclose(fp))
{
fprintf(stderr,"文件food.out关闭异常,程序即将退出.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void out_Travel_File_Write(void) //出行支出写入
{
FILE * fp;
char res;
struct travel_Str temp;
double add_Sum = 0;
if((fp = fopen("travel.out","a+b")) == NULL)
{
fprintf(stderr,"无法打开travel.out文件\n");
system("pause");
exit(EXIT_FAILURE);
}
do
{
puts("请输入支出出行名称及用途:");
s_gets(temp.diary,SIZE);
printf("金额:");
get_Money(&temp.money);
fwrite(&temp,sizeof(struct travel_Str),1,fp);
add_Sum += temp.money;
puts("继续?(y or n):");
res = get_Res();
}while('y' == res);
write_Gross_Out(add_Sum,5);
if(fclose(fp))
{
fprintf(stderr,"文件travel.out关闭异常,程序即将退出.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void in_Salary_File_Write(void)
{
FILE * fp;
struct in_Str temp;
char res;
double add_Sum = 0;
if((fp = fopen("salary.in","a+b")) == NULL)
{
fprintf(stderr,"无法打开文件salary.in.\n");
system("pause");
exit(EXIT_FAILURE);
}
do
{
printf("请输入工资金额:");
get_Money(&temp.money);
printf("请输入对该笔工资的描述:\n");
s_gets(temp.diary,SIZE);
fwrite(&temp,sizeof(struct in_Str),1,fp);
add_Sum += temp.money;
write_Gross_In(add_Sum,0);
printf("继续 (y or n):");
res = get_Res();
}while(res == 'y');
if(fclose(fp))
{
fprintf(stderr,"无法关闭文件salary.in\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void in_Extra_File_Write(void)
{
FILE * fp;
struct in_Str temp;
char res;
double add_Sum = 0;
if((fp = fopen("extra.in","a+b")) == NULL)
{
fprintf(stderr,"无法打开文件extra.in.\n");
system("pause");
exit(EXIT_FAILURE);
}
do
{
printf("请输入此笔收入金额:\n");
get_Money(&temp.money);
printf("请输入对该笔收入的描述:\n");
s_gets(temp.diary,SIZE);
fwrite(&temp,sizeof(struct in_Str),1,fp);
add_Sum += temp.money;
write_Gross_In(add_Sum,1);
printf("继续 (y or n):");
res = get_Res();
}while(res == 'y');
if(fclose(fp))
{
fprintf(stderr,"无法关闭文件extra.in\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void write_Gross_Out(double add,int n)
{
FILE * fp;
double num[7];
int i = 0;
if((fp = fopen("gross.out","r+b")) == NULL)
{
fprintf(stderr,"无法打开文件:gross.out,即将退出程序.\n");
system("pause");
exit(EXIT_FAILURE);
}
fread(num,sizeof(double),7,fp);
num[n] += add;
num[6] += add;
rewind(fp);
fwrite(num,sizeof(double),7,fp);
if(fclose(fp))
{
fprintf(stderr,"文件gross.out关闭异常.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void write_Gross_In(double add,int n)
{
double in[3];
int i;
FILE * fp;
if((fp = fopen("gross.in","r+b")) == NULL)
{
fprintf(stderr,"无法打开文件gross.in\n");
system("pause");
exit(EXIT_FAILURE);
}
for(i = 0;i < 3;i++)
fread(in + i,sizeof(double),1,fp);
in[n] += add;
in[2] += add;
rewind(fp);
fwrite(in,sizeof(double),3,fp);
if(fclose(fp))
{
fprintf(stderr,"文件gross.in关闭异常.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
输出文件:
//show.c
#include "123.h"
void show_Out(int choice)
{
switch(choice)
{
case 0:
frame();
printf("0)购物支出情况\t1)学习支出情况\n"
"2)娱乐支出情况\n3)运动支出情况\n"
"4)食物支出情况\t5)出行支出情况\n"
"6)返回上一级菜单\n");
frame();
do
{
printf("请从以上菜单中选择:");
choice = get_I();
}while(choice < 0 || choice > 6);
while(choice >= 0 && choice < 6)
{
show_Out_Single(choice); //进入单项展示子菜单
system("pause");
frame();
printf("0)购物支出情况\t1)学习支出情况\n"
"2)娱乐支出情况\t3)运动支出情况\n"
"4)食物支出情况\t5)出行支出情况\n"
"6)返回上一级菜单\n");
frame();
do
{
printf("请从以上菜单中选择:");
choice = get_I();
}while(choice < 0 || choice > 6);
}
break;
case 1: //进入占比分析选项
show_Out_Ration();
system("pause");
break;
}
return;
}
void show_Out_Single(int choice)
{
switch(choice)
{
case 0:
show_Out_File_Single("shop.out");
break;
case 1:
show_Out_File_Single("study.out");
break;
case 2:
show_Out_File_Single("leisure.out");
break;
case 3:
show_Out_File_Single("sports.out");
break;
case 4:
show_Out_File_Single("food.out");
break;
case 5:
show_Out_File_Single("travel.out");
break;
}
return;
}
void show_Out_File_Single(char * str)
{
FILE * fp;
struct show_Out_Str temp;
double sum = 0;
if((fp = fopen(str,"r"))== NULL)
{
fprintf(stderr,"无法打开文件%s.即将退出程序\n",str);
system("pause");
exit(EXIT_FAILURE);
}
while(fread(&temp,sizeof(struct show_Out_Str),1,fp))
{
printf("%s %.2f 元\n",temp.diary,temp.money);
sum += temp.money;
}
printf("共花费 %.2f 元.\n",sum);
printf("以上是此项支出的全部内容.\n");
if(fclose(fp))
{
fprintf(stderr,"文件%s无法正常关闭,即将退出程序.\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void show_Out_Ration(void)
{
FILE * fp;
int i;
double num[7];
char * str[6] = {"购物支出","学习支出","娱乐支出","运动支出","饮食支出","出行支出"};
if((fp = fopen("gross.out","r")) == NULL)
{
fprintf(stderr,"无法打开文件gross.out.\n");
system("pause");
exit(EXIT_FAILURE);
}
fread(num,sizeof(double),7,fp);
if(fclose(fp))
{
fprintf(stderr,"文件gross.out关闭异常.\n");
system("pause");
exit(EXIT_FAILURE);
}
printf("总消费金额 %.2f 元\n",num[6]);
if(fabs(num[6] - 0) < 1e-4)
printf("哎呀,最近没有消费哟!\n");
else
{
for(i = 0;i < 6;i++)
printf("%s : %.2f 元. 占总支出比率:%.2f%%\n",str[i],num[i],100 * (num[i] / num[6]));
}
return;
}
void show_Salary(void)
{
FILE * fp;
struct in_Str temp;
double sum = 0;
if((fp = fopen("salary.in","r")) == NULL)
{
fprintf(stderr,"无法打开文件salary.in\n");
system("pause");
exit(EXIT_FAILURE);
}
while(fread(&temp,sizeof(struct in_Str),1,fp))
{
printf("%.2f 元 %s\n",temp.money,temp.diary);
sum += temp.money;
}
printf("工资共收入 %.2f 元\n",sum);
printf("以上是收入数据.\n");
if(fclose(fp))
{
fprintf(stderr,"无法关闭文件salary.in\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void show_Extra(void)
{
FILE * fp;
struct in_Str temp;
double sum = 0;
if((fp = fopen("extra.in","r")) == NULL)
{
fprintf(stderr,"无法打开文件extra.in\n");
system("pause");
exit(EXIT_FAILURE);
}
while(fread(&temp,sizeof(struct in_Str),1,fp))
{
printf("%.2f 元 %s\n",temp.money,temp.diary);
sum += temp.money;
}
printf("其他方式共收入 %.2f 元",sum);
printf("以上是收入数据.\n");
if(fclose(fp))
{
fprintf(stderr,"无法关闭文件extra.in\n");
system("pause");
exit(EXIT_FAILURE);
}
return;
}
void show_In_Ration(void)
{
char * s[2] = {"工资","其他收入"};
char * str[3] = {"占比","占比","总收入"};
double num[3];
int i;
FILE * fp;
if((fp = fopen("gross.in","r")) == NULL)
{
fprintf(stderr,"无法打开文件gross.in\n");
system("pause");
exit(EXIT_FAILURE);
}
for(i = 0;i < 3;i++)
fread(num + i,sizeof(double),1,fp);
if(fclose(fp))
{
fprintf(stderr,"文件gross.in关闭异常.\n");
system("pause");
exit(EXIT_FAILURE);
}
printf("%s : %.2f 元\n",str[2],num[2]);
if(fabs(num[2] - 0) < 1e-4)
printf("哎呀,最近没有收入哟!\n");
else
{
for(i = 0;i < 2;i++)
printf("%s : %.2f 元 %s : %.2f%%\n",s[i],num[i],str[i],100 * (num[i] / num[2]));
}
return;
}
void show_Profit(void)
{
FILE * fp;
double out, in;
if((fp = fopen("gross.out","r")) == NULL)
{
fprintf(stderr,"无法打开文件gross.out\n");
system("pause");
exit(EXIT_FAILURE);
}
fseek(fp,-sizeof(double),SEEK_END);
fread(&out,sizeof(double),1,fp);
if(fclose(fp))
{
fprintf(stderr,"文件gross.out关闭异常.\n");
system("pause");
exit(EXIT_FAILURE);
}
if((fp = fopen("gross.in","r")) == NULL)
{
fprintf(stderr,"无法打开文件gross.in\n");
system("pause");
exit(EXIT_FAILURE);
}
fseek(fp,-sizeof(double),SEEK_END);
fread(&in,sizeof(double),1,fp);
if(fclose(fp))
{
fprintf(stderr,"文件gross.in关闭异常.\n");
system("pause");
exit(EXIT_FAILURE);
}
printf("总收入 : %.2f 元\n",in);
printf("总支出 : %.2f 元\n",out);
printf("收入 - 支出 = %.2f 元\n",in - out);
if(out > in)
printf("哎呀,最近超支了,注意理性消费哟!\n");
else if(out == in && out != 0 && in != 0)
printf("呵呵,消费分寸把握的很准呀,但要注意,超支警告哟!\n");
else if(out == 0 && in == 0)
printf("首次记录,记得要合理管理自己的资金哟!\n");
else
printf("嗯,不错,你资金管理的很好!\n");
return;
}
其他函数文件:
//source.c
#include "123.h"
char * s_gets(char * str,int n) //字符串读入
{
char * ret, * ch;
ret = fgets(str,n,stdin);
if(ch = strchr(str,'\n'))
*ch = '\0';
else
fflush(stdin);
return ret;
}
void get_Money(double * money) //获得正确钱数
{
while((scanf("%lf",money) )!= 1 ||(*money < 0))
{
fflush(stdin);
printf("请输入正确的金额");
}
fflush(stdin);
}
void choose_Main(void)
{
int i;
do
{
menu_Main();
printf("请键入你的选择:");
i = get_Main();
switch(i)
{
case 0:
menu_Out(); //进入支出的下一级子菜单,进行支出项目选择
break;
case 1:
menu_In();
break;
case 2:
menu_Show_Out();
break;
case 3:
menu_Show_In();
break;
case 4:
show_Profit();
system("pause");
break;
default:
break;
}
}while(i != 5);
return;
}
int get_Main(void)
{
int n;
while((scanf("%d",&n) != 1 ) || (n < 0 || n > 5))
{
fflush(stdin);
printf("请输入一个数字,0 或 1 或 2 或 3 或 4 或 5:");
}
fflush(stdin);
return n;
}
int get_I(void)
{
int n;
while(scanf("%d",&n) != 1 )
{
fflush(stdin);
printf("请输入一个数字如0,1,2,...:");
}
fflush(stdin);
return n;
}
char get_Res(void) //获得y or n
{
char ch;
while((scanf("%c",&ch) != 1) || (ch != 'y' && ch != 'n'))
{
fflush(stdin);
printf("请输入正确的回答(y or n):");
}
fflush(stdin);
return ch;
}
int setfile(void) //创建原始文件
{
FILE * fp;
int res = 0;
if((fp = fopen("shop.out","r"))== NULL)
{
fp = fopen("shop.out","w+");
fclose(fp);
fprintf(stdout,"文件shop.out创建成功.\n");
res = 1;
}
if((fp = fopen("study.out","r"))== NULL)
{
fp = fopen("study.out","w+");
fclose(fp);
fprintf(stdout,"文件study.out创建成功.\n");
res = 1;
}
if((fp = fopen("leisure.out","r"))== NULL)
{
fp = fopen("leisure.out","w+");
fclose(fp);
fprintf(stdout,"文件leisure.out创建成功.\n");
res = 1;
}
if((fp = fopen("leisure.out","r"))== NULL)
{
fp = fopen("leisure.out","w+");
fclose(fp);
fprintf(stdout,"文件leisure.out创建成功.\n");
res = 1;
}
if((fp = fopen("sports.out","r"))== NULL)
{
fp = fopen("sports.out","w+");
fclose(fp);
fprintf(stdout,"文件sports.out创建成功.\n");
}
if((fp = fopen("food.out","r"))== NULL)
{
fp = fopen("food.out","w+");
fclose(fp);
fprintf(stdout,"文件food.out创建成功.\n");
res = 1;
}
if((fp = fopen("travel.out","r"))== NULL)
{
fp = fopen("travel.out","w+");
fclose(fp);
fprintf(stdout,"文件travel.out创建成功.\n");
res = 1;
}
if((fp = fopen("gross.out","r"))== NULL)
{
fp = fopen("gross.out","w+");
fclose(fp);
fprintf(stdout,"文件gross.out创建成功.\n");
res = 1;
}
if((fp = fopen("salary.in","r"))== NULL)
{
fp = fopen("salary.in","w+");
fclose(fp);
fprintf(stdout,"文件salary.in创建成功.\n");
res = 1;
}
if((fp = fopen("extra.in","r"))== NULL)
{
fp = fopen("extra.in","w+");
fclose(fp);
fprintf(stdout,"文件extra.in创建成功.\n");
res = 1;
}
if((fp = fopen("gross.in","r"))== NULL)
{
fp = fopen("gross.in","w+");
fclose(fp);
fprintf(stdout,"文件gross.in创建成功.\n");
res = 1;
}
return res;
}
void frame(void)
{
printf("**************************************************\n");
return;
}