采用随机函数产生职工的工号和他所完成产品个数的数据信息,对同一职工多次完成的产品个数进行累计,按职工完成产品数量的名次、该名次每位职工完成的产品数量、同一名次的职工人数和他们的职工号格式输出。
实现要求:输出统计结果,如下所示:
Order Quantity Count Number
1 375 3 10 20 21
4 250 2 3 5
6 200 1 9
7 150 2 11 14
……………
程序设计思路:采用链表结构存储有关信息,链表中的每个结点对应于一位职工。在数据采集的同时,形成一个有序链表(按完成的产品数量和工号排序)。当一个职工有新的数据输入,在累计他的完成数量时会改变原来链表的有序性,为此应对链表进行删除、查找和插入等操作。
首先定义了一个员工结构体?Employee,包括员工编号?id、产品数量?productCount?和指向下一个员工的指针?next。
insertEmployee?函数用于插入员工信息,如果员工已存在,则累加产品数量;如果员工不存在,则创建新员工节点,并按照产品数量大小和员工编号的顺序插入链表。
sortEmployees?函数用于对员工信息进行排序,按照产品数量从大到小进行排序,同时在产品数量相同时按照员工编号进行升序排序。
printStatistics?函数用于打印员工信息的统计结果,包括排序后的员工产品数量,员工人数和员工编号列表,并对员工编号进行升序排序。
在?main?函数中,随机生成了100个员工的产品数量信息,并逐个插入链表中。随后对员工信息进行排序,然后打印统计结果。最后释放链表内存。
这段代码通过链表数据结构来存储员工信息,使用随机生成的员工编号和产品数量进行演示。在?main?函数中,通过?insertEmployee?函数和?sortEmployees?函数来实现员工信息的插入和排序,最后通过?printStatistics?函数打印统计结果
typedef struct Employee {
int id; // 员工编号
int productCount; // 产品数量
struct Employee* next; // 指向下一个员工的指针
} Employee;
int main() {
Employee* head = NULL;
srand((unsigned)time(NULL));
// 随机生成员工编号和产品数量并插入到链表中
for (int i = 0; i < 100; i++) {
int employeeId = rand() % 20 + 1;
int productCount = rand() % 100 + 1;
insertEmployee(&head, employeeId, productCount);
}
// 排序员工信息
sortEmployees(&head);
// 打印统计信息
printStatistics(head);
// 释放链表内存
while (head != NULL) {
Employee* temp = head;
head = head->next;
free(temp);
}
return 0;
}
// 插入员工信息
void insertEmployee(Employee** head, int id, int count) {
Employee* current = *head;
while (current != NULL) {
if (current->id == id) {
current->productCount += count; // 如果员工已存在,则累加产品数量
return;
}
current = current->next;
}
// 创建新员工节点
Employee* newEmployee = (Employee*)malloc(sizeof(Employee));
newEmployee->id = id;
newEmployee->productCount = count;
newEmployee->next = NULL;
// 插入新员工节点到链表中
if (*head == NULL) {
*head = newEmployee;
}
else if (count > (*head)->productCount) {
newEmployee->next = *head;
*head = newEmployee;
}
else {
current = *head;
while (current->next != NULL && count <= current->next->productCount) {
current = current->next;
}
newEmployee->next = current->next;
current->next = newEmployee;
}
}
void sortEmployees(Employee** head) {
if (*head == NULL || (*head)->next == NULL) {
return;
}
Employee* sorted = NULL;
Employee* current = *head;
while (current != NULL) {
Employee* next = current->next;
if (sorted == NULL || sorted->productCount < current->productCount) {
current->next = sorted;
sorted = current;
}
else {
Employee* temp = sorted;
while (temp->next != NULL && temp->next->productCount >= current->productCount) {
if (temp->next->productCount == current->productCount && temp->next->id > current->id) {
break;
}
temp = temp->next;
}
current->next = temp->next;
temp->next = current;
}
current = next;
}
*head = sorted;
}
void printStatistics(Employee* head) {
printf("Order\tQuantity\tCount\tNumber\n");
int order = 0;
int prevCount = -1;
int count = 0;
int employeeIds[MAX_EMPLOYEE_COUNT] = { 0 };
int employeeCount = 0;
while (head != NULL) {
if (head->productCount != prevCount) {
if (count > 0) {
printf("%d\t%d\t%d", order + 1, prevCount, count);
// 对员工编号进行排序
for (int i = 0; i < employeeCount - 1; i++) {
for (int j = 0; j < employeeCount - i - 1; j++) {
if (employeeIds[j] > employeeIds[j + 1]) {
int temp = employeeIds[j];
employeeIds[j] = employeeIds[j + 1];
employeeIds[j + 1] = temp;
}
}
}
for (int i = 0; i < employeeCount; i++) {
printf("\t%d", employeeIds[i]);
}
printf("\n");
}
order += count;
prevCount = head->productCount;
count = 1;
employeeIds[0] = head->id;
employeeCount = 1;
}
else {
count++;
employeeIds[employeeCount] = head->id;
employeeCount++;
}
head = head->next;
}
if (count > 0) {
printf("%d\t%d\t%d", order + 1, prevCount, count);
// 对最后一行中的员工编号进行排序
for (int i = 0; i < employeeCount - 1; i++) {
for (int j = 0; j < employeeCount - i - 1; j++) {
if (employeeIds[j] > employeeIds[j + 1]) {
int temp = employeeIds[j];
employeeIds[j] = employeeIds[j + 1];
employeeIds[j + 1] = temp;
}
}
}
for (int i = 0; i < employeeCount; i++) {
printf("\t%d", employeeIds[i]);
}
printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<windows.h>
#include<math.h>
#define MAX_EMPLOYEE_COUNT 100
// 定义员工结构体
typedef struct Employee {
int id; // 员工编号
int productCount; // 产品数量
struct Employee* next; // 指向下一个员工的指针
} Employee;
// 插入员工信息
void insertEmployee(Employee** head, int id, int count) {
Employee* current = *head;
while (current != NULL) {
if (current->id == id) {
current->productCount += count; // 如果员工已存在,则累加产品数量
return;
}
current = current->next;
}
// 创建新员工节点
Employee* newEmployee = (Employee*)malloc(sizeof(Employee));
newEmployee->id = id;
newEmployee->productCount = count;
newEmployee->next = NULL;
// 插入新员工节点到链表中
if (*head == NULL) {
*head = newEmployee;
}
else if (count > (*head)->productCount) {
newEmployee->next = *head;
*head = newEmployee;
}
else {
current = *head;
while (current->next != NULL && count <= current->next->productCount) {
current = current->next;
}
newEmployee->next = current->next;
current->next = newEmployee;
}
}
// 排序员工信息
void sortEmployees(Employee** head) {
if (*head == NULL || (*head)->next == NULL) {
return;
}
Employee* sorted = NULL;
Employee* current = *head;
while (current != NULL) {
Employee* next = current->next;
if (sorted == NULL || sorted->productCount < current->productCount) {
current->next = sorted;
sorted = current;
}
else {
Employee* temp = sorted;
while (temp->next != NULL && temp->next->productCount >= current->productCount) {
if (temp->next->productCount == current->productCount && temp->next->id > current->id) {
break;
}
temp = temp->next;
}
current->next = temp->next;
temp->next = current;
}
current = next;
}
*head = sorted;
}
// 打印员工统计信息
void printStatistics(Employee* head) {
printf("Order\tQuantity\tCount\tNumber\n");
int order = 0;
int prevCount = -1;
int count = 0;
int employeeIds[MAX_EMPLOYEE_COUNT] = { 0 };
int employeeCount = 0;
while (head != NULL) {
if (head->productCount != prevCount) {
if (count > 0) {
printf("%d\t%d\t%d", order + 1, prevCount, count);
// 对员工编号进行排序
for (int i = 0; i < employeeCount - 1; i++) {
for (int j = 0; j < employeeCount - i - 1; j++) {
if (employeeIds[j] > employeeIds[j + 1]) {
int temp = employeeIds[j];
employeeIds[j] = employeeIds[j + 1];
employeeIds[j + 1] = temp;
}
}
}
for (int i = 0; i < employeeCount; i++) {
printf("\t%d", employeeIds[i]);
}
printf("\n");
}
order += count;
prevCount = head->productCount;
count = 1;
employeeIds[0] = head->id;
employeeCount = 1;
}
else {
count++;
employeeIds[employeeCount] = head->id;
employeeCount++;
}
head = head->next;
}
if (count > 0) {
printf("%d\t%d\t%d", order + 1, prevCount, count);
// 对最后一行中的员工编号进行排序
for (int i = 0; i < employeeCount - 1; i++) {
for (int j = 0; j < employeeCount - i - 1; j++) {
if (employeeIds[j] > employeeIds[j + 1]) {
int temp = employeeIds[j];
employeeIds[j] = employeeIds[j + 1];
employeeIds[j + 1] = temp;
}
}
}
for (int i = 0; i < employeeCount; i++) {
printf("\t%d", employeeIds[i]);
}
printf("\n");
}
}
int main() {
Employee* head = NULL;
srand((unsigned)time(NULL));
// 随机生成员工编号和产品数量并插入到链表中
for (int i = 0; i < 100; i++) {
int employeeId = rand() % 20 + 1;
int productCount = rand() % 100 + 1;
insertEmployee(&head, employeeId, productCount);
}
// 排序员工信息
sortEmployees(&head);
// 打印统计信息
printStatistics(head);
// 释放链表内存
while (head != NULL) {
Employee* temp = head;
head = head->next;
free(temp);
}
return 0;
}