40分) 输出最贵的书籍信息:
结构体类型定义如下:
struct stu{
char name[20];
float price;
struct stu *next;
};
要求:
(1)编写自定义函数创建链表,输入书名和价格。输入Y继续创建,输入其他字符结束创建。
(2)编写自定义函数输出最贵的书籍信息。
(3)编写自定义函数释放链表内存。
**输入提示:"请输入书名 价格: \n"(循环)
**输入格式:"%s %f"
**是否继续创建的提示:"是否继续输入,按Y键继续输入,其他键就结束.\n"
**输出提示:"result:\n"
**输出格式:"%s %.2f\n"
程序运行示例:
请输入书名 价格: ↙
Algorithms 105
是否继续输入,按Y键继续输入,其他键就结束.↙
Y
请输入书名 价格: ↙
高等数学 31.5
是否继续输入,按Y键继续输入,其他键就结束.↙
Y
请输入书名 价格: ↙
C语言 35
是否继续输入,按Y键继续输入,其他键就结束.↙
n↙
result:↙
Algorithms 105.00↙
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu *app(struct stu *head);
float dis(struct stu *head, char *p);
void dele(struct stu *head);
struct stu{
char name[20];
float price;
struct stu *next;
};
int main(void)
{
char c='Y';
char name[20];
float maxprice;
struct stu *head =NULL;
while (c=='Y')
{
head = app(head);
printf("是否继续输入,按Y键继续输入,其他键就结束.\n");
scanf(" %c", &c);
}
maxprice=dis(head,name);
printf("result:\n");
printf("%s %.2f\n",name,maxprice );
dele(head);
return 0;
}
struct stu *app(struct stu *head)
{
struct stu *p=NULL,*pr=head;
char name[20];
float price;
p=(struct stu *)malloc(sizeof(struct stu));
if (p==NULL)
{
exit(0);
}
if (head == NULL)
{
head=p;
}
else
{
while (pr->next != NULL)
{
pr=pr->next;
}
pr->next=p;
}
printf("请输入书名 价格: \n");
scanf("%s %f",name,&price);
strcpy(p->name,name);
p->price=price;
p->next=NULL;
return head;
}
float dis(struct stu *head, char *p)
{
struct stu *pq=head;
float j=0;
while (pq!=NULL)
{
if (pq->price>j)
{
j=pq->price;
strcpy(p,pq->name);
}
pq=pq->next;
}
return j;
}
void dele(struct stu *head)
{
struct stu *p=head , *pr = NULL;
while (p!=NULL)
{
pr=p;
p=p->next;
free(pr);
}
}
40分是自己手写的么?太敷衍了,哈哈哈,这个程序还是非常简单的,但是态度???