鱼C论坛

 找回密码
 立即注册
查看: 427|回复: 1

[已解决]怎么储存用链表储存多位学生信息

[复制链接]
发表于 2023-12-26 15:09:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
void addEmployee()
{
    int i,m;
    Employee*newEmployee =(Employee*)malloc(sizeof(Employee));
    printf("职工人数(1--100):");
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        printf("请输入员工工号:");
        scanf("%d",&newEmployee->id);
        printf("请输入员工密码:");
        scanf("%s",newEmployee->password); 
        printf("请输入员工姓名:");
        scanf("%s",&newEmployee->name);
        printf("请输入员工工资:");
        scanf("%f",&newEmployee->salary);
        if(newEmployee->salary<=3000)
        {
            newEmployee->tax=newEmployee->salary*0.03;
        }
        else if(newEmployee->salary>3000&&newEmployee->salary<=12000)
        {
            newEmployee->tax=(newEmployee->salary-3000)*0.10;
        }
        else if(newEmployee->salary>12000&&newEmployee->salary<=25000)
        {
            newEmployee->tax=(newEmployee->salary-12000)*0.20;
        }
        else if(newEmployee->salary>25000&&newEmployee->salary<=35000)
        {
            newEmployee->tax=(newEmployee->salary-25000)*0.25;
        }
        else if(newEmployee->salary>35000&&newEmployee->salary<=55000)
        {
            newEmployee->tax=(newEmployee->salary-35000)*0.30;
        }
        else if(newEmployee->salary>55000&&newEmployee->salary<=80000)
        {
            newEmployee->tax=(newEmployee->salary-55000)*0.35;
        }
        else
        {
            newEmployee->tax=(newEmployee->salary-80000)*0.45;
        }
    }
    newEmployee->next=head;
    head=newEmployee;
}
这个怎么修改
// 显示所有员工的信息

void displayEmployees()
{
    Employee* current =head;
    while(current !=NULL)
    {
        printf("工号:%d\t姓名: %s\t工资: %.2f\t税金: %.2f\t",current->id,current->name,current->salary,current->tax);
        current=current->next;
    }



}
怎么显示多位员工的信息
最佳答案
2023-12-26 15:10:09
要存储多位学生的信息,可以使用链表来进行储存。链表是由一系列节点组成的数据结构,每个节点包含了需要储存的信息以及一个指向下一个节点的指针。

首先,定义一个结构体来表示学生的信息,包括学号、密码、姓名、工资和税金等字段。
typedef struct Student {    int id;    char password[20];    char name[20];    float salary;    float tax;    struct Student* next;} Student;
然后,在主函数中创建一个链表头指针head,用于指向链表的第一个节点。
Student* head = NULL;
接下来,修改addStudent函数,将新学生的节点插入到链表头部。
void addStudent() {    int m;    printf("学生人数(1--100):");    scanf("%d", &m);    for (int i = 0; i < m; i++) {        Student* newStudent = (Student*)malloc(sizeof(Student));                printf("请输入学生学号:");        scanf("%d", &newStudent->id);                printf("请输入学生密码:");        scanf("%s", newStudent->password);                 printf("请输入学生姓名:");        scanf("%s", newStudent->name);                printf("请输入学生工资:");        scanf("%f", &newStudent->salary);                // 根据工资计算税金        if (newStudent->salary <= 3000) {            newStudent->tax = newStudent->salary * 0.03;        } else if (newStudent->salary <= 12000) {            newStudent->tax = (newStudent->salary - 3000) * 0.10;        } else if (newStudent->salary <= 25000) {            newStudent->tax = (newStudent->salary - 12000) * 0.20;        } else if (newStudent->salary <= 35000) {            newStudent->tax = (newStudent->salary - 25000) * 0.25;        } else if (newStudent->salary <= 55000) {            newStudent->tax = (newStudent->salary - 35000) * 0.30;        } else {            newStudent->tax = (newStudent->salary - 55000) * 0.35;        }                newStudent->next = head;        head = newStudent;    }}
最后,修改displayStudents函数,遍历链表并打印每个学生的信息。
void displayStudents() {    Student* current = head;        while (current != NULL) {        printf("学号:%d\t姓名: %s\t工资: %.2f\t税金: %.2f\n", current->id, current->name, current->salary, current->tax);        current = current->next;    }}
这样,就可以通过调用addStudent函数添加多个学生的信息,并通过调用displayStudents函数显示所有学生的信息了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-26 15:10:09 | 显示全部楼层    本楼为最佳答案   
要存储多位学生的信息,可以使用链表来进行储存。链表是由一系列节点组成的数据结构,每个节点包含了需要储存的信息以及一个指向下一个节点的指针。

首先,定义一个结构体来表示学生的信息,包括学号、密码、姓名、工资和税金等字段。
typedef struct Student {    int id;    char password[20];    char name[20];    float salary;    float tax;    struct Student* next;} Student;
然后,在主函数中创建一个链表头指针head,用于指向链表的第一个节点。
Student* head = NULL;
接下来,修改addStudent函数,将新学生的节点插入到链表头部。
void addStudent() {    int m;    printf("学生人数(1--100):");    scanf("%d", &m);    for (int i = 0; i < m; i++) {        Student* newStudent = (Student*)malloc(sizeof(Student));                printf("请输入学生学号:");        scanf("%d", &newStudent->id);                printf("请输入学生密码:");        scanf("%s", newStudent->password);                 printf("请输入学生姓名:");        scanf("%s", newStudent->name);                printf("请输入学生工资:");        scanf("%f", &newStudent->salary);                // 根据工资计算税金        if (newStudent->salary <= 3000) {            newStudent->tax = newStudent->salary * 0.03;        } else if (newStudent->salary <= 12000) {            newStudent->tax = (newStudent->salary - 3000) * 0.10;        } else if (newStudent->salary <= 25000) {            newStudent->tax = (newStudent->salary - 12000) * 0.20;        } else if (newStudent->salary <= 35000) {            newStudent->tax = (newStudent->salary - 25000) * 0.25;        } else if (newStudent->salary <= 55000) {            newStudent->tax = (newStudent->salary - 35000) * 0.30;        } else {            newStudent->tax = (newStudent->salary - 55000) * 0.35;        }                newStudent->next = head;        head = newStudent;    }}
最后,修改displayStudents函数,遍历链表并打印每个学生的信息。
void displayStudents() {    Student* current = head;        while (current != NULL) {        printf("学号:%d\t姓名: %s\t工资: %.2f\t税金: %.2f\n", current->id, current->name, current->salary, current->tax);        current = current->next;    }}
这样,就可以通过调用addStudent函数添加多个学生的信息,并通过调用displayStudents函数显示所有学生的信息了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-6 03:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表