一个C语言程序问题求解
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define N 10//一下遇到的N全都由10代替
struct magazine
{
char name[20];//姓名
char sex;//性别
char addr[20];//地址
char tell[12];//电话
float price;//价格
int num;//数量
char date[10];//截止日期
};
struct magazine mag;
int n=0;/*用户计数器*/
int tag=0;/*未过期的用户计数器*/
void insert()/*增订新用户*/
{
FILE *fp;//声明fp是指针,用来指向FILE类型的对象
char ch,*filename;
filename = "E:\\magazine.txt";
if(0 == n)
{
if(NULL==(fp=fopen(filename,"w")))
{
printf("此文件不存在!\n");
exit(0);
}
}
else
{
if(NULL==(fp=fopen(filename,"a")))
{
printf("此文件不存在!\n");
exit(0);
}
}
do
{
if(n<N)
{
printf("请输入姓名:");gets(mag.name);
printf("请输入性别:");scanf("%c",&mag.sex);
printf("请输入地址:");gets(mag.addr);
printf("请输入电话号码:");gets(mag.tell);
printf("请输入订阅期限:");gets(mag.date);
printf("请输入杂志单价:");scanf("%f",&mag.price);
printf("请输入订阅数:");scanf("%d",&mag.num);
fwrite(&mag,sizeof(struct magazine),1,fp);//要进行写入sizeof(struct strdent_type)字节的数据项的个数是1
n++;
}
else
printf("用户已满!\n");
getchar();
printf("\n 是否还有新订户?\n");
scanf("%c",&ch);
getchar();
system("cls");
}while('y'==ch || 'Y'==ch);
fclose(fp);//关闭fp指向的文件句柄
system("pause");//调用“pause”命令;
}
void del()/*删除过期用户*/
{
FILE *fp;
struct magazine magz[N];
char datatime[8],*filename;
int m,i=0;
filename = "E:\\magazine.txt";
if((fp=fopen(filename,"r"))==NULL)//打开文件名为filename的文件,根据fopen的返回值(fp),判断该文件是否打开
{
printf("此文件不存在");
exit(0);
}
while(fread(&mag,sizeof(struct magazine),1,fp)==1)//从文件中读取个数
{
magz[tag]=mag;
tag++;
}
fclose(fp);
printf("请输入当天日期:");
gets(datatime);
fp=fopen(filename,"w");
for(i=0;i<tag;i++)
{
m=strcmp(magz[i].date,datatime);
if(m<=0)continue;
fwrite(&magz[i],sizeof(struct magazine),1,fp);
}
fclose(fp);
system("pause");
}
void total()/*统计本期杂志订户数*/
{
FILE *fp;
float totalprice;
int totalnum=0;
float price1;
float sum=0.0;
if((fp=fopen("E:\\magazine.txt","r"))==NULL)//判断以写的方式打开文件,若打不开则返回
{
printf("此文件不存在");
exit(0);
}
printf("订户名\t 订阅数\t 单价\t 金额\n");
while(fread(&mag,sizeof(struct magazine),1,fp)==1)
{
totalnum+=mag.num;
price1=mag.price;
totalprice=mag.num*mag.price;
sum+=totalprice;
printf("%s\t%d\t%3.2f\t%3.2f\n",mag.name,mag.num,mag.price,totalprice);
}
printf("合计\t%d\t%3.2f\t%3.2f\n",totalnum,price1,sum);
fclose(fp);
system("pause");
}
void scan()//查询与修改模块
{
system("cls");
int i=0,j,y;
char scanname[10],*filename;
filename = "E:\\magazine.txt";
FILE *fp;
struct magazine *magz[N];
fp=fopen(filename,"r");
if((fp=fopen(filename,"r"))==NULL)
{
printf("此文件不存在");
exit(0);
}
for(j=0;fread(&mag,sizeof(struct magazine),1,fp);j++)
{
magz[j]=(struct magazine *)malloc(sizeof(struct magazine));
* magz[j]=mag;
}
fclose(fp);
printf("输入要查询人员的姓名:");
scanf("%s",scanname);
fp=fopen(filename,"w");
for(i=0;i<j;i++)
{
if(strcmp(magz[i]->name,scanname)==0)
{
printf("姓名:%s\n",magz[i]->name);
printf("性别:%s\n",magz[i]->sex);
printf("地址:%s\n",magz[i]->addr);
printf("电话:%s\n",magz[i]->tell);
printf("单价:%f\n",magz[i]->price);
printf("订阅数量:%d\n",magz[i]->num);
printf("订阅期限:%s\n",magz[i]->date);
printf("是否要修改'1'是,'0'否:\n");
scanf("%d",&y);
if(y)
{
system("cls");
printf("请输入姓名:");scanf("%s",magz[i]->name);
printf("请输入性别:");scanf("%s",magz[i]->sex);
printf("请输入地址:");scanf("%s",magz[i]->addr);
printf("请输入电话号码:");scanf("%s",magz[i]->tell);
printf("请输入订阅期限:");scanf("%s",magz[i]->date);
printf("请输入杂志单价:");scanf("%f",&magz[i]->price);
printf("请输入订阅数:");scanf("%d",&magz[i]->num);
fwrite(magz[i],sizeof(struct magazine),1,fp);
continue;}
fwrite(magz[i],sizeof(struct magazine),1,fp);
continue;}
fwrite(magz[i],sizeof(struct magazine),1,fp);
}
fclose(fp);
system("pause");
}
void main()//主函数模块
{
int ch;
char ch1;
system("color 0a");
do
{
printf(" \n");
printf(" \n");
printf(" \n");
printf("||||||||||||||||||||||||||||||||||||||||||||||||||||||\n");
printf("|| C 语言课程设计 ||\n");
printf("|| ||\n");
printf("|| 杂志订阅管理系统设计 ||\n");
printf("||--------------------------------------------------||\n");
printf("||--------------------------------------------------||\n");
printf("|| 制作人: 王超 制作日期: 2016.06.21 ||\n");
printf("||********************** 主菜单*********************||\n");
printf("||--------------------------------------------------||\n");
printf("||****************** 1. 增加新订户 ****************||\n");
printf("||****************** 2. 删除到期用户****************||\n");
printf("||****************** 3. 统计订户信息****************||\n");
printf("||****************** 4. 查找订户信息****************||\n");
printf("||****************** 5. 退出 ****************||\n");
printf("||**************************************************||\n");
printf("||**************************************************||\n");
printf("||**************************************************||\n");
printf("||||||||||||||||||||||||||||||||||||||||||||||||||||||\n");
scanf("%d",&ch);getchar();
switch(ch)
{
case 1:insert();getchar();break;//增添模块
case 2:del();break;//删除模块
case 3:total();getchar();break;//统计模块
case 4:scan();getchar();break;//浏览模块
case 5:exit(0);break;//退出
default:printf("输入有误!");break;//若不符合上述情况
}
printf("是否继续其他操作?\n");
scanf("%c",&ch1);
system("cls");//清屏
}while(ch1=='y'||ch1=='Y');
}[local]1[/local]
如图,地址和电话不是分开的。求大神帮助呀