求热心C语言高手帮忙看一下这个函数体!!
先说明 我不是来要作业答案的以下的代码都是我一个字一个字打的
debug没有错 但是我知道里面很多错误
我刚才问了一下同学 我的for循环是错的
有几处文件没有关闭
但是改了之后还是有错误的
麻烦好心人帮忙看一下错误在哪里 谢谢
struct s_information
{
char name[20];
float bid;
float price;
int jinhuo;
int chuhuo;
int kucun;
int num;
struct s_information *next;
};
void shopping(int a)
{
//把商品信息文件里的信息放到p指向的链表
FILE *f_information;
f_information = fopen("D:\\f_information.txt","r");
struct s_information *head = NULL, *p;
head = (struct s_information *)malloc(sizeof(struct s_information));
p = head;
printf("\n=================================商品信息====================================\n");
printf(" 商品名称 商品价格 剩余数量\n");
while (!feof(f_information))
{
fscanf(f_information,"%s %f %f %d %d %d %d",&p->name,&p->bid,&p->price,&p->jinhuo,&p->chuhuo,&p->kucun,&p->num);
printf("%12s%12.2f%12d\n",p->name,p->price,p->kucun);
p->next = (struct s_information *)malloc(sizeof(struct s_information));
p = p->next;
}
// p->next = NULL;
//把购物记录文件里的信息放到p1指向的链表
FILE *f_record;
f_record = fopen("D:\\f_record.txt","r");
struct s_information *head1 = NULL, *p1;
head1 = (struct s_information *)malloc(sizeof(struct s_information));
p1 = head1;
struct s_information *head2 = NULL, *p2;
head2 = (struct s_information *)malloc(sizeof(struct s_information));
p2 = head2;
while (!feof(f_record))
{
fscanf(f_record,"%s %f %d\n",&p1->name,&p1->price,&p1->num);
// printf("%6s%6.2f%6d\n",p1->name,p1->price,p1->num);
p1->next = (struct s_information *)malloc(sizeof(struct s_information));
p1 = p1->next;
}
// p1->next = NULL;
//清空小票
FILE *f_receipt;
f_receipt = fopen("D:\\f_receipt.txt","w+");
//开始购买
while (1)
{
char name[20];
printf("\n请输入商品名称(输入E结束): ");
scanf("%s",name);
if (strcmp(name,"E") == 0)
break;
//判断商品名称是否被包含在已知商品中
int num, flag = 0;
for (p = head; p->next != NULL; p = p->next)
{
if (strcmp(name,p->name) == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("\n该商品不存在,请重新输入。\n\n");
shopping(a);
}
if (p->kucun == 0)
{
printf("该商品目前无货,请选购其他商品。\n");
continue;
}
//判断购买数量是否少于库存数量
printf("请输入购买数量: ");
scanf("%d",&num);
while ((num <= 0) || (num > p->kucun))
{
printf("商品数量输入错误,请重新输入。\n");
printf("请输入购买数量:");
scanf("%d",&num);
}
//到这里商品的名称和数量都已经符合要求了 开始进行文件操作
//现在p->name应该和name是一样的 即该节点就是我们需要更改商品信息的节点
p->num = num;
p->chuhuo = p->chuhuo + p->num;
p->kucun = p->kucun - p->num;
//商品信息的链表已经修改完毕了
for (p1 = head1; p1->next != NULL; p1 = p1->next)
{
if (strcmp(p->name,p1->name) == 0)
{
p1->num = p1->num + p->num;
break;
}
p1->next = (struct s_information *)malloc(sizeof(struct s_information));
}
//购物记录的链表已经修改完毕了
FILE *f_receipt;
f_receipt = fopen("D:\\f_receipt.txt","a");
fprintf(f_receipt,"%s %f %d\n",p->name,p->price,p->num);
//将更改完的商品信息链表写入文件
}
f_information = fopen("D:\\f_information.txt","w+");
for (p = head; p->next != NULL; p = p->next)
{
fprintf(f_information,"%s %f %f %d %d %d %d\n",p->name,p->bid,p->price,p->jinhuo,p->chuhuo,p->kucun,p->num);
}
//将更改完的购买记录链表写入文件
f_record = fopen("D:\\f_record.txt","w+");
for (p1 = head1; p1->next != NULL; p1 = p1->next)
{
fprintf(f_record,"%s %f %d\n",p1->name,p1->price,p1->num);
}
// f_receipt = fopen("D:\\r_receipt.txt","w+");
// for (p2 = head2; p2->next != NULL; p2 = p2->next)
// {
// fprintf(f_record,"%s %f %d\n",p2->name,p2->price,p2->num);
// }
//将小票文件里的信息打印出来
float total = 0;
f_receipt = fopen("D:\\f_receipt.txt","r");
struct s_information *head3 = NULL, *p3;
head3 = (struct s_information *)malloc(sizeof(struct s_information));
p3 = head3;
printf("\n=================================购物清单====================================\n");
while (!feof(f_receipt))
{
fscanf(f_receipt,"%s %f %d",&p3->name,&p3->price,&p3->num);
printf("%12s%12.2f%12d%12.2f\n",p3->name,p3->price,p3->num,(p3->price) * (p3->num));
total += (p3->price) * (p3->num);
p3->next = (struct s_information *)malloc(sizeof(struct s_information));
p3 = p3->next;
}
printf("\n商品总价:%.2f",total);
fclose(f_information);
fclose(f_record);
fclose(f_receipt);
menu(a);
}
[ 本帖最后由 hi_ydy 于 2013-9-6 17:49 编辑 ]