#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;
}
为什么无法输出结果
改动处见注释,供参考:
#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;
}