马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct Goods
{
char name[20];
int num;
float price;
struct Goods *next;
};
void getInput(struct Goods *goods)
{
printf("请输入商品名称:");
scanf("%s", goods -> name);
do
{
printf("请输入商品数量(数量小于100):");
scanf("%d", &goods -> num);
}while(goods -> num > 100);
do
{
printf("请输入商品价格(价格小于1000):");
scanf("%f", &goods -> price);
}while(goods -> price > 1000);
}
void addGoods(struct Goods **head)
{
struct Goods *goods, *temp;
goods = (struct Goods *)malloc(sizeof(struct Goods));//分配内存
assert(goods);
getInput(goods);
if(*head != NULL)//尾插
{
temp = *head;
while(temp -> next != NULL)
{
temp = temp -> next;
}
temp -> next = goods;
goods -> next = NULL;
}
else
{
*head = goods;
goods -> next = NULL;
}
}
void print_Goods(struct Goods *head)//打印链表
{
struct Goods *goods;
goods = head;
while(goods != NULL)
{
printf("商品名称:%s, 商品数量:%d, 商品价格:%0.2f", goods -> name, goods -> num, goods -> price);
goods = goods -> next;
putchar('\n');
}
}
void sum_Price(struct Goods *head)
{
struct Goods *goods;
goods = head;
while(goods != NULL)
{
printf("商品名称:%s, 商品总价:%0.2f", goods -> name, (goods -> num) * (goods -> price));
goods = goods -> next;
putchar('\n');
}
}
void menu()
{
putchar('\n');
printf("|== 欢迎来到商品菜单 ==|\n");
printf("| 1.显示所有商品 |\n");
printf("| 2.数量最多的商品名称 |\n");
printf("| 3.单价最高的商品名称 |\n");
printf("| 4.商品总价 |\n");
printf("| 5.退出系统 |\n");
printf("|== author:chenxing == |\n");
}
char *search_max_num(struct Goods *head, int max)
{
while(head != NULL)
{
if(head -> num == max)
{
return head -> name;
}
head = head -> next;
}
return NULL;
}
void num_Max_name(struct Goods *head)
{
struct Goods *current;
int max = head -> num;
while(head != NULL)
{
if(max >= head -> num)
{
max = head -> num;
}
head = head -> next;
}
char *goods = search_max_num(current, max);
if(goods == NULL)
{
printf("没有最大的价格!");
}
else
{
printf("数量最多的商品名称是:%s", goods);
}
}
char *search_max_price(struct Goods *head, float max)
{
while(head != NULL)
{
if(head -> price == max)
{
return head -> name;
}
head = head -> next;
}
return NULL;//没有匹配的价格
}
void price_Max_name(struct Goods *head)
{
struct Goods *current;
float max = head -> price;
current = head;
while(head != NULL)
{
if(max < head -> price)
{
max = head -> price;
}
head = head -> next;
}
char *goods = search_max_price(current, max);
if(goods == NULL)
{
printf("没有最大的价格!");
}
else
{
printf("售价最高的商品名称是:%s", goods);
}
}
int main(void)
{
struct Goods *head = NULL;
struct Goods *goods;
int code;
char ch;
int count = 100;
while(count--)
{
printf("请问是否需要录入商品信息(Y/N)?");
do
{
ch = getchar();
}while(ch != 'Y' && ch != 'N');
if(ch == 'Y')
{
addGoods(&head);
}
else
{
goto FINDIT;
}
}
FINDIT:
printf("商品信息录入完毕!\n");
menu();
while(1)
{
printf("\n请输入一个指令:");
scanf("%d", &code);
switch(code)
{
case 1:
print_Goods(head);
break;
case 2:
num_Max_name(head);
break;
case 3:
price_Max_name(head);
break;
case 4:
printf("\n商品总价:\n");
sum_Price(head);
break;
case 5:
exit(1);
break;
default:
printf("请输入1~5的数字!");
break;
}
}
return 0;
}
@FishC,帮我看看case2: 段错误的原因? |