时间在哪里853 2022-11-12 08:25 采纳率: 82.4%
浏览 1298
已结题

编写程序,从键盘输入n(n<=10)本书的名称和定价并存入结构数组中,从中查找定价最高和最低的书的名称和定价,并输出。提示(1)定义结构体 (2)定义结构体数组

#include<stdio.h>
struct book{
char name[80];
double price;
};
int main()
{
    int i,n,j;
    double max,min;
    char a,b;
    struct book s[10];
    printf("Input n:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
       {
    printf("Input the name,price of the %d book:",i+1);
    scanf("%s%lf",s[i].name,&s[i].price);
       }
       max=s[0].price,min=s[0].price;
       a=s[0].name,b=s[0].name;
       for(i=0;i<n;i++)
       {
           if(max<s[i].price)
            a=s[i].name;
           max=s[i].price;
       }
       for(j=0;j<n;j++)
       {
           if(min>s[j].price)
            b=s[j].name;
           min=s[j].price;
       }
       printf("The book with the max price:%s,%.1f\n",a,max);
       printf("The book with the min price:%s,%.1f\n",b,min);
    return 0;
}

为什么无法输出结果

展开全部

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-11-12 09:33
    关注

    改动处见注释,供参考:

    #include<stdio.h>
    #define N 11     //修改
    struct book{
        char name[80];
        double price;
    };
    int main()
    {
        int i,n,j;
        double max,min;
        char a,b;
        struct book s[N]; //s[10] 修改
        printf("Input n:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("Input the name,price of the %d book:",i+1);
            scanf(" %s%lf",s[i].name,&s[i].price);
        }
        max=s[0].price,min=s[0].price;
                           //a=s[0].name,b=s[0].name;修改
        for(i=0;i<n;i++)
        {
            if(max < s[i].price)
                          //a=s[i].name; 修改
                max=s[i].price;
                          //}
                          //   for(j=0;j<n;j++)
                          //   {
            if(min > s[i].price)
                          //b=s[j].name;
                min=s[i].price;
        }
        for (i=0;i<n;i++)
        {
            if (s[i].price == max)
                printf("The book with the max price:%s,%.1f\n",s[i].name,s[i].price);
                //printf("The book with the max price:%s,%.1f\n",a,max);修改
            if (s[i].price == min)
                printf("The book with the min price:%s,%.1f\n",s[i].name,s[i].price);
                //printf("The book with the min price:%s,%.1f\n",b,min);修改
        }
        return 0;
    }
    
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 11月19日
  • 已采纳回答 11月12日
  • 修改了问题 11月12日
  • 修改了问题 11月12日
  • 展开全部
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部