非常抱歉,看起来我没有完整回答您的问题。以下是完整的代码解决方案:
#include <stdio.h>
struct Goods {
int number; // 商品编号
char name[20]; // 商品名称
int sales[3]; // 三个季度的销售额
double averageSales; // 平均销售额
};
void inputGoods(Goods goods[], int length) {
for (int i = 0; i < length; i++) {
printf("请输入第%d个商品的编号:", i + 1);
scanf("%d", &goods[i].number);
printf("请输入第%d个商品的名称:", i + 1);
getchar(); // 获取之前输入编号时剩余的换行符
gets(goods[i].name);
for (int j = 0; j < 3; j++) {
printf("请输入第%d个商品第%d个季度的销售额:", i + 1, j + 1);
scanf("%d", &goods[i].sales[j]);
}
printf("\n");
}
}
void findMaxSalesByQuarter(Goods goods[], int length) {
for (int i = 0; i < length; i++) {
int maxSales = 0;
int maxQuarter = 0;
for (int j = 0; j < 3; j++) {
if (goods[i].sales[j] > maxSales) {
maxSales = goods[i].sales[j];
maxQuarter = j + 1;
}
}
printf("第%d个商品在第%d个季度销售额最高,销售额为:%d\n", goods[i].number, maxQuarter, maxSales);
}
}
void calculateAverageSales(Goods goods[], int length) {
for (int i = 0; i < length; i++) {
int totalSales = 0;
for (int j = 0; j < 3; j++) {
totalSales += goods[i].sales[j];
}
goods[i].averageSales = totalSales / 3.0;
printf("第%d个商品的平均销售额为:%.2f\n", goods[i].number, goods[i].averageSales);
}
}
void sortByAverageSales(Goods goods[], int length) {
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - 1 - i; j++) {
if (goods[j].averageSales < goods[j+1].averageSales) {
// 交换元素
Goods temp = goods[j];
goods[j] = goods[j+1];
goods[j+1] = temp;
}
}
}
}
void printGoods(Goods goods[], int length) {
printf("编号\t名称\t第一季度\t第二季度\t第三季度\t平均销售额\n");
for (int i = 0; i < length; i++) {
printf("%02d\t%s\t%d\t%d\t%d\t%.2f\n", goods[i].number, goods[i].name, goods[i].sales[0], goods[i].sales[1], goods[i].sales[2], goods[i].averageSales);
}
}
int main() {
const int length = 10;
Goods goods[length];
inputGoods(goods, length);
findMaxSalesByQuarter(goods, length);
calculateAverageSales(goods, length);
sortByAverageSales(goods, length);
printGoods(goods, length);
return 0;
}
请将上述代码复制粘贴到C或C++编译器中运行。这段代码实现了您提供的要求,并按照平均销售额从高到低排序了商品数据。希望能够对您有所帮助!如果还有其他问题,请随时告诉我。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |