【问题描述】
某用户购物信息的结构体定义如下:
struct order
{ int ID; //商品编号
char goods[30]; //商品名
float price; //商品价格
int quantity; //购买数量
};
编写程序,从键盘接收 10 个商品的购买信息存到结构体数组中,找出花费最多的商品,并输出该商品的商品编号、商品名、商品价格(使用%.2f格式输出)、购买数量,每项占一行输出。
注:采用以下格式输入一个商品的购买信息:scanf("%d%s%f%d", ... ) 例如:(以5个商品为例)
示例输入:
1 aaa 5.2 5
2 bbb 10.5 1
3 ccc 20.2 5
4 ddd 10.9 2
5 eee 20.0 2
示例输出:
3
ccc
20.20
5
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct order { //购物信息
int ID; //商品编号
char goods[30]; //商品名称
float price; //商品价格
int quantity; //商品数量
};
int main() {
int max(int s[]);
struct order orders[10];
int i = 0, sum[10];
struct order *p;
p = orders;
for (p = orders; p < orders + 10; p++, i++) {
scanf("%d%s%f%d", &p->ID, p->goods, &p->price, &p->quantity);
sum[i] = p->price * p->quantity;
}
p = &orders[max(sum)];
printf("%d\n%s\n%.2f\n%d", p->ID, p->goods, p->price, p->quantity);
return 0;
}
int max(int s[]) {
int m, n = 0;
for(m = 1; m < 10; m++) {
if (s[m] > s[n]) {
n = m;
}
}
return n;
}