1 问题描述 该系统要求对一个家庭某月的支出情况进行统计和分析。通过此课题,熟练掌握数组、指针的各种操
发布网友
发布时间:2022-05-21 14:44
我来回答
共1个回答
热心网友
时间:2023-10-29 13:39
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#include "conio.h"
#include "malloc.h"
#include "windows.h"
typedef struct //定义结构体
{
char type[20];
int Getsum;
char place[20];
int time;
}family;
family fam[80];
int menu_select()
{
char c;
do{
system("cls");
printf("\t\t***************************\n");
printf("\t\t**********菜单系统*********\n");
printf("\t\t|1.输入消费金额|\n");
printf("\t\t|2.计算消费总和|\n");
printf("\t\t|3.支出情况|\n");
printf("\t\t|4.消费分析|\n");
printf("\t\t|5.添加文件|\n");
printf("\t\t|6.保存数据及结果|\n");
printf("\t\t|0.退出系统|\n");
printf("\t\t***************************\n");
printf("\t\t***************************\n");
printf("\t\t\t请对应选择所需选项(0-6):");
c=getchar();
}while(c<'0'||c>'6');
return(c-'0');
}
void password()
{
char d[6];
char m[6];
m[0]='8',m[1]='8',m[2]='0',m[3]='5',m[4]='0',m[5]='5'; //数组比较
printf("请输入六位数密码:"); //输入密码
for (int n=0;;)
{
for(int i=0;i<6;i++)
{
d[i]=getch();
if(d[i]>='0'||d[i]<='9'||d[i]>='a'||d[i]<='z'||d[i]>='A'||d[i]<='Z')
putchar('*');
}
if(d[0]==m[0]&&d[1]==m[1]&&d[2]==m[2]&&d[3]==m[3]&&d[4]==m[4]&&d[5]==m[5])
{
printf("\n欢迎使用!\n");break;
}
if(d[0]!=m[0]||d[1]!=m[1]||d[2]!=m[2]||d[3]!=m[3]||d[4]!=m[4]||d[5]!=m[5]) //当密码错误时如下
printf("\n密码错误!请重新输入:");
if (n>3)
{
printf("error\n");
exit (0);
}
n++;
}
}
int Inputitems(family sum[],int n)
{
int i=0;
char sign='y',x[10];
while(sign!='n'&&sign!='N')
{
printf("\t\t\t种类:"); //输入变量
scanf("\t\t\t%s",sum[n+i].type);
printf("\t\t\t 支出:");
scanf("\t\t\t%d",&sum[n+i].Getsum);
printf("\t\t\t 地点:");
scanf("\t\t\t%s",sum[n+i].place);
printf("\t\t\t 时间:");
scanf("\t\t\t%d",&sum[n+i].time);
if(sum[n+i].time<1||sum[n+i].time>30)
return false;
gets(x);
printf("\t\t\t需要再次输入吗?(y/n)");
scanf("\t\t\t%c",&sign);
i++;
}
return(n+i);
}
void Getsum(family sum[], int n) //求出总数
{
int getsum=0;
int i=0;
for(i=0;i<n;i++)
{
getsum+=sum[i].Getsum;
}
printf("\t\t\t%d\n",getsum);
}
void Showitems(family sum[],int n)
{
int i;
printf("\t\t\t-----------------\n");
printf("\t\t\t 种类 支出 地点 时间\n");
printf("\t\t\t-----------------\n");
for(i=1;i<n+1;i++)
{
printf("\t\t\t% -8s%-8d%-8s%d\n",sum[i-1].type,sum[i-1].Getsum,sum[i-1].place,sum[i-1].time);
if(i>1&&i%10==0)
{
printf("\t\t\t----------------\n");
printf("\t\t\t");
system("pause");
printf("\t\t\t----------------\n");
}
}
printf("\t\t\t");
system("pause");
}
void Analyseitems(family sum[],int n) //分析函数
{
float a=0;
float Analyseitems=0;
float sum1=0;
int i=0;
int j=0;
for(i=0;i<n;i++)
{
sum+=sum[i].Getsum;
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i;i++)
{
if(sum[i].type==sum[j].type)
{
sum1+=sum[j].Getsum;
}
}
}
a=(sum1/Analyseitems)*100;
printf("\t\t\t%f%\n",a);
}
int AddfromText(family sum[],int n) //增添数据
{
int i=0, num;
FILE *fp;
char filename[20];
printf("\t\t\t输入文件名:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"rb"))==NULL)
{printf("\t\t\t无法打开该文件");
printf("\t\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num);
while(i<num);
{
fscanf(fp,"%s%d%s%s",sum[n+i].type,&sum[n+i].Getsum,sum[n+i].place,sum[n+i].time);
i++;
}
n+=num;
fclose(fp);
printf("\t\t\t打开成功!\n");
printf("\t\t\t");
system("pause");
return(n);
}
void save(family sum[],int n) //保存文件
{
int i=0;
FILE *fp;
char filename[20];
printf("\t\t\t保存结果或数据\n");
printf("\t\t\t保存文件名:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\t\t\t无法打开该文件\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n);
while(i<n)
{
fprintf(fp,"%-16s%d%-16s%-16s\n",sum[i].type,sum[i].Getsum,sum[i].place,sum[i].time);
i++;
}
fclose(fp);
printf("保存成功!\n");
}
void main()
{
password();
getch();
int n=0;
for(;;)
{
switch(menu_select()) //菜单函数
{
case 1:
printf("\t\t\t请输入相应支出\n");
n=Inputitems(fam,n);
break;
case 2:
printf("\t\t\t支出总和\n");
Getsum(fam,n);
printf("\t\t\t");
system("pause");
break;
case 3:
printf("\t\t\t支出情况\n");
Showitems(fam,n);
break;
case 4:
printf("\t\t\t支出分析\n");
Analyseitems(fam,n);
printf("\t\t\t");
system("pause");
break;
case 5:
printf("\t\t\t添加文件(按从大到小)\n");
n=AddfromText(fam,n);
break;
case 6:
printf("\t\t\t保存项目\n");
save(fam,n);
printf("\t\t\t");
system("pause");
break;
case 0:
printf("\t\t\t谢谢使用!\n");
printf("\t\t\t");
system("pause");
exit(0);
}
}
}
//END
记得给分哦。。。追问好像没用到单链表吧....