首页 > 试题广场 >

查找书籍:从键盘输入10本书的名称和定价并存入结构数组中,从

[问答题]

查找书籍:从键盘输入10本书的名称和定价并存入结构数组中,从中查找定价最高和最低的书的名称和定价,并输出。试编写相应程序。

推荐
#include<stdio.h>
#define NUMBER 10
struct book
{ char name[10];
 float price;
};
int main()
{
  int i,maxl,minl;
  struct book test[NUMBER];
  printf(“Input 10 book’s name and price\n”);
  for(i=0; i<NUMBER; i++)
    scanf(“%s%f”, test[i].name, &test[i].price);
maxl=minl=0;
for(i=1; i<NUMBER; i++)
{ if(test[maxl].price<test[i].price)  maxl=i;
  if(test[minl] .price >test[i].price)  minl=i;
}
printf(“Max Price: %f, %s\n”, test[maxl].price, test[maxl].name);
printf(“Min Price: %f, %s\n”, test[minl].price, test[minl].name);
return 0;
 
}
 

发表于 2018-05-06 21:40:54 回复(1)
#include<stdio.h>
struct book
{
    char title[20];
    double price;
};
int main(void)
{
    int i;
    struct book books[10],max,min;
    for (i = 0; i < 10; i++) {
        scanf("%s%lf", books[i].title, &books[i].price);
    }
    for (i = 0; i < 9; i++) {
        if (i == 0) max = books[i];
        if (max.price < books[i].price) max = books[i];
        if (i == 0) min = books[i];
        if (min.price > books[i].price) min = books[i];
    }
    printf("max——title:%s\tprice:%.2lf\n", max.title, max.price);
    printf("min——title:%s\tprice:%.2lf\n", min.title, min.price);
    return 0;

发表于 2019-07-25 10:55:50 回复(0)