69,382
社区成员
发帖
与我相关
我的任务
分享
//产品销售记录包括 产品代码 名称 单价 数量 金额 等信息
//1)编写input函数 输入10个产品销售记录的如上信息
//2)编写sum函数 计算金额 = 单价 * 数量
//3)编写sort函数 按金额从大到小排序
#include<stdio.h>
#define PRODUCTS 10 //按题目要求10个产品
void input();
float sum(float a,int n);
void sort();
typedef struct{ //自定义结构体类型
int code;
char name[21];
float price;
int quantity;
}PRODUCT;
PRODUCT products[PRODUCTS];//定义10个产品记录的数组
void main()
{
input(); //输入
sort(); //排序,从大到小
for(int i = 0; i < PRODUCTS; i++){ //打印输出
printf("code = %d | quantity = %d | price = %.2f | name = %s | sum = %.2f\n",
products[i].code,products[i].quantity,products[i].price,products[i].name,
sum(products[i].price,products[i].quantity));
}
}
void input(){
printf("Enter example:code,quantity,price,name(30)\n");
for(int i = 0; i < PRODUCTS; i++){
scanf("%d %d %f %s", &products[i].code,&products[i].quantity,
&products[i].price,products[i].name);
}
}
float sum(float a,int n)
{
return a * n;
}
void sort(){
PRODUCT temp;
for (int i = 0; i <PRODUCTS -1 ;i++){
for (int k = 0; k < PRODUCTS - 1 -i ;k++){
if((products[k].price * products[k].quantity)<
(products[k+1].price * products[k+1].quantity))
{
temp = products[k];
products[k] = products[k+1];
products[k+1] = temp;
}
}
}
}
//Enter example:code,quantity,price,name(30)
//11 20 2.00 aa
//22 30 1.00 bb
//33 10 3.00 cc
//code = 11 | quantity = 20 | price = 2.00 | name = aa | sum = 40.00
//code = 22 | quantity = 30 | price = 1.00 | name = bb | sum = 30.00
//code = 33 | quantity = 10 | price = 3.00 | name = cc | sum = 30.00
//请按任意键继续. . .