C语言编写 某公司有四个销售员(编号:1-4),负责销售五种产品(编号:1-5)。每个销售员都将当天出售的每种产品各写一张便条交上来。每张便条包含内容:(1)销售员的代号 (2)产品的代号 (3)这种产品的当天的销售额 每位销售员每天可能上缴 0-5张便条。假设,收集到了上个月的所有便条,编写一个处理系统,读取上个月的销售情况,进行如下处理。 (1)计算上个月每个人每种产品的销售额。 (2)按销售额对销售员进行排序,输出排序结果(销售员代号)及每个销售员的销售排名。 (3)统计每种产品的总销售额,对这些产品按从高到底的顺序,输出排序结果(需输出产品的代号和销售额) (4)输出统计报表
时间: 2024-02-26 14:59:13 浏览: 33
好的,这是一个有趣的问题!这里提供一个C语言的解决方案,需要用到结构体和链表。下面是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SALESMAN_NUM 4
#define PRODUCT_NUM 5
typedef struct {
int salesman_id;
int product_id;
double sales_amount;
} SalesRecord;
typedef struct {
int salesman_id;
double total_sales;
int rank;
} Salesman;
typedef struct {
int product_id;
double total_sales;
} Product;
typedef struct _node {
SalesRecord data;
struct _node *next;
} Node;
void add_record(Node **head, SalesRecord record);
void calculate_salesman_sales(Node *head, Salesman *salesmen);
void sort_salesman(Salesman *salesmen);
void calculate_product_sales(Node *head, Product *products);
void sort_product(Product *products);
void print_report(Salesman *salesmen, Product *products);
int main() {
Node *head = NULL;
Salesman salesmen[SALESMAN_NUM];
Product products[PRODUCT_NUM];
// 读入所有销售记录
int salesman_id, product_id;
double sales_amount;
while (scanf("%d%d%lf", &salesman_id, &product_id, &sales_amount) == 3) {
SalesRecord record = {salesman_id, product_id, sales_amount};
add_record(&head, record);
}
// 计算每个销售员每种产品的销售额
calculate_salesman_sales(head, salesmen);
// 按销售额对销售员进行排序
sort_salesman(salesmen);
// 统计每种产品的总销售额,并排序
calculate_product_sales(head, products);
sort_product(products);
// 输出统计报表
print_report(salesmen, products);
return 0;
}
void add_record(Node **head, SalesRecord record) {
Node *new_node = (Node *) malloc(sizeof(Node));
new_node->data = record;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
} else {
Node *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_node;
}
}
void calculate_salesman_sales(Node *head, Salesman *salesmen) {
Node *p = head;
while (p != NULL) {
SalesRecord record = p->data;
int salesman_id = record.salesman_id - 1;
int product_id = record.product_id - 1;
double sales_amount = record.sales_amount;
salesmen[salesman_id].salesman_id = salesman_id + 1;
salesmen[salesman_id].total_sales += sales_amount;
p = p->next;
}
for (int i = 0; i < SALESMAN_NUM; i++) {
salesmen[i].rank = 1;
for (int j = 0; j < SALESMAN_NUM; j++) {
if (salesmen[j].total_sales > salesmen[i].total_sales) {
salesmen[i].rank++;
}
}
}
}
void sort_salesman(Salesman *salesmen) {
for (int i = 0; i < SALESMAN_NUM - 1; i++) {
for (int j = i + 1; j < SALESMAN_NUM; j++) {
if (salesmen[i].total_sales < salesmen[j].total_sales) {
Salesman temp = salesmen[i];
salesmen[i] = salesmen[j];
salesmen[j] = temp;
}
}
}
}
void calculate_product_sales(Node *head, Product *products) {
Node *p = head;
while (p != NULL) {
SalesRecord record = p->data;
int product_id = record.product_id - 1;
double sales_amount = record.sales_amount;
products[product_id].product_id = product_id + 1;
products[product_id].total_sales += sales_amount;
p = p->next;
}
}
void sort_product(Product *products) {
for (int i = 0; i < PRODUCT_NUM - 1; i++) {
for (int j = i + 1; j < PRODUCT_NUM; j++) {
if (products[i].total_sales < products[j].total_sales) {
Product temp = products[i];
products[i] = products[j];
products[j] = temp;
}
}
}
}
void print_report(Salesman *salesmen, Product *products) {
printf("销售员销售报表:\n");
printf("销售员编号\t总销售额\t排名\n");
for (int i = 0; i < SALESMAN_NUM; i++) {
printf("%d\t%.2lf\t\t%d\n", salesmen[i].salesman_id, salesmen[i].total_sales, salesmen[i].rank);
}
printf("\n产品销售报表:\n");
printf("产品编号\t总销售额\n");
for (int i = 0; i < PRODUCT_NUM; i++) {
printf("%d\t\t%.2lf\n", products[i].product_id, products[i].total_sales);
}
}
```
这个程序的思路是,首先读入所有销售记录,然后计算每个销售员每种产品的销售额,并且按销售额对销售员进行排序,统计每种产品的总销售额并排序,最后输出统计报表。程序中用到了链表来动态存储销售记录,用到了结构体来存储销售员和产品的信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)