职工工作量统计(课程设计)

问题描述:

采用随机函数产生职工的工号和他所完成产品个数的数据信息,对同一职工多次完成的产品个数进行累计,按职工完成产品数量的名次、该名次每位职工完成的产品数量、同一名次的职工人数和他们的职工号格式输出。

实现要求:输出统计结果,如下所示:

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;

main函数

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;
}

  • 55
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
职工工作量统计系统是一个用来帮助企业统计职工工作量的软件系统。为了设计这个系统,需要首先学习C语言的基础知识和语法规则,然后结合实际问题进行设计实现。 在C语言课程设计中,我们需要首先明确系统的功能需求和使用场景。例如,系统需要记录职工的工作时间、加班情况、请假记录等信息,并能够生成统计报表和图表。接着,我们需要设计系统的数据结构和算法,以便能够高效地处理大量的数据并进行相关的统计计算。在设计过程中,需要重点关注系统的可扩展性和稳定性,确保系统能够满足未来的需求并具有良好的性能表现。 接下来,我们需要利用C语言的编程能力来实现系统的各项功能。这就涉及到了文件操作、数据结构的应用、算法的实现以及用户界面的设计等方面。在编程过程中,需要注意代码的健壮性和可读性,确保系统的稳定性和易用性。 最后,在课程设计的过程中,还需要进行系统的测试和调试工作,以保证系统的质量。这涉及到编写测试用例、进行单元测试和集成测试,发现并解决系统中可能存在的各种bug和问题。 通过C语言课程设计,我们可以全面了解软件开发的各个环节,提高我们的编程能力和系统设计能力。同时,也能够为企业解决实际问题提供技术支持,提高我们的就业竞争力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值