鱼C论坛

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

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

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

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

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

x
  1. void addEmployee()
  2. {
  3.     int i,m;
  4.     Employee*newEmployee =(Employee*)malloc(sizeof(Employee));
  5.     printf("职工人数(1--100):");
  6.     scanf("%d",&m);
  7.     for(i=0;i<m;i++)
  8.     {
  9.         printf("请输入员工工号:");
  10.         scanf("%d",&newEmployee->id);
  11.         printf("请输入员工密码:");
  12.         scanf("%s",newEmployee->password);
  13.         printf("请输入员工姓名:");
  14.         scanf("%s",&newEmployee->name);
  15.         printf("请输入员工工资:");
  16.         scanf("%f",&newEmployee->salary);
  17.         if(newEmployee->salary<=3000)
  18.         {
  19.             newEmployee->tax=newEmployee->salary*0.03;
  20.         }
  21.         else if(newEmployee->salary>3000&&newEmployee->salary<=12000)
  22.         {
  23.             newEmployee->tax=(newEmployee->salary-3000)*0.10;
  24.         }
  25.         else if(newEmployee->salary>12000&&newEmployee->salary<=25000)
  26.         {
  27.             newEmployee->tax=(newEmployee->salary-12000)*0.20;
  28.         }
  29.         else if(newEmployee->salary>25000&&newEmployee->salary<=35000)
  30.         {
  31.             newEmployee->tax=(newEmployee->salary-25000)*0.25;
  32.         }
  33.         else if(newEmployee->salary>35000&&newEmployee->salary<=55000)
  34.         {
  35.             newEmployee->tax=(newEmployee->salary-35000)*0.30;
  36.         }
  37.         else if(newEmployee->salary>55000&&newEmployee->salary<=80000)
  38.         {
  39.             newEmployee->tax=(newEmployee->salary-55000)*0.35;
  40.         }
  41.         else
  42.         {
  43.             newEmployee->tax=(newEmployee->salary-80000)*0.45;
  44.         }
  45.     }
  46.     newEmployee->next=head;
  47.     head=newEmployee;
  48. }
复制代码
这个怎么修改
  1. // 显示所有员工的信息

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



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

首先,定义一个结构体来表示学生的信息,包括学号、密码、姓名、工资和税金等字段。
  1. typedef struct Student {
  2.     int id;
  3.     char password[20];
  4.     char name[20];
  5.     float salary;
  6.     float tax;
  7.     struct Student* next;
  8. } Student;
复制代码

然后,在主函数中创建一个链表头指针head,用于指向链表的第一个节点。
  1. Student* head = NULL;
复制代码

接下来,修改addStudent函数,将新学生的节点插入到链表头部。
  1. void addStudent() {
  2.     int m;
  3.     printf("学生人数(1--100):");
  4.     scanf("%d", &m);
  5.     for (int i = 0; i < m; i++) {
  6.         Student* newStudent = (Student*)malloc(sizeof(Student));
  7.         
  8.         printf("请输入学生学号:");
  9.         scanf("%d", &newStudent->id);
  10.         
  11.         printf("请输入学生密码:");
  12.         scanf("%s", newStudent->password);
  13.         
  14.         printf("请输入学生姓名:");
  15.         scanf("%s", newStudent->name);
  16.         
  17.         printf("请输入学生工资:");
  18.         scanf("%f", &newStudent->salary);
  19.         
  20.         // 根据工资计算税金
  21.         if (newStudent->salary <= 3000) {
  22.             newStudent->tax = newStudent->salary * 0.03;
  23.         } else if (newStudent->salary <= 12000) {
  24.             newStudent->tax = (newStudent->salary - 3000) * 0.10;
  25.         } else if (newStudent->salary <= 25000) {
  26.             newStudent->tax = (newStudent->salary - 12000) * 0.20;
  27.         } else if (newStudent->salary <= 35000) {
  28.             newStudent->tax = (newStudent->salary - 25000) * 0.25;
  29.         } else if (newStudent->salary <= 55000) {
  30.             newStudent->tax = (newStudent->salary - 35000) * 0.30;
  31.         } else {
  32.             newStudent->tax = (newStudent->salary - 55000) * 0.35;
  33.         }
  34.         
  35.         newStudent->next = head;
  36.         head = newStudent;
  37.     }
  38. }
复制代码

最后,修改displayStudents函数,遍历链表并打印每个学生的信息。
  1. void displayStudents() {
  2.     Student* current = head;
  3.    
  4.     while (current != NULL) {
  5.         printf("学号:%d\t姓名: %s\t工资: %.2f\t税金: %.2f\n", current->id, current->name, current->salary, current->tax);
  6.         current = current->next;
  7.     }
  8. }
复制代码

这样,就可以通过调用addStudent函数添加多个学生的信息,并通过调用displayStudents函数显示所有学生的信息了。

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

使用道具 举报

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

首先,定义一个结构体来表示学生的信息,包括学号、密码、姓名、工资和税金等字段。
  1. typedef struct Student {
  2.     int id;
  3.     char password[20];
  4.     char name[20];
  5.     float salary;
  6.     float tax;
  7.     struct Student* next;
  8. } Student;
复制代码

然后,在主函数中创建一个链表头指针head,用于指向链表的第一个节点。
  1. Student* head = NULL;
复制代码

接下来,修改addStudent函数,将新学生的节点插入到链表头部。
  1. void addStudent() {
  2.     int m;
  3.     printf("学生人数(1--100):");
  4.     scanf("%d", &m);
  5.     for (int i = 0; i < m; i++) {
  6.         Student* newStudent = (Student*)malloc(sizeof(Student));
  7.         
  8.         printf("请输入学生学号:");
  9.         scanf("%d", &newStudent->id);
  10.         
  11.         printf("请输入学生密码:");
  12.         scanf("%s", newStudent->password);
  13.         
  14.         printf("请输入学生姓名:");
  15.         scanf("%s", newStudent->name);
  16.         
  17.         printf("请输入学生工资:");
  18.         scanf("%f", &newStudent->salary);
  19.         
  20.         // 根据工资计算税金
  21.         if (newStudent->salary <= 3000) {
  22.             newStudent->tax = newStudent->salary * 0.03;
  23.         } else if (newStudent->salary <= 12000) {
  24.             newStudent->tax = (newStudent->salary - 3000) * 0.10;
  25.         } else if (newStudent->salary <= 25000) {
  26.             newStudent->tax = (newStudent->salary - 12000) * 0.20;
  27.         } else if (newStudent->salary <= 35000) {
  28.             newStudent->tax = (newStudent->salary - 25000) * 0.25;
  29.         } else if (newStudent->salary <= 55000) {
  30.             newStudent->tax = (newStudent->salary - 35000) * 0.30;
  31.         } else {
  32.             newStudent->tax = (newStudent->salary - 55000) * 0.35;
  33.         }
  34.         
  35.         newStudent->next = head;
  36.         head = newStudent;
  37.     }
  38. }
复制代码

最后,修改displayStudents函数,遍历链表并打印每个学生的信息。
  1. void displayStudents() {
  2.     Student* current = head;
  3.    
  4.     while (current != NULL) {
  5.         printf("学号:%d\t姓名: %s\t工资: %.2f\t税金: %.2f\n", current->id, current->name, current->salary, current->tax);
  6.         current = current->next;
  7.     }
  8. }
复制代码

这样,就可以通过调用addStudent函数添加多个学生的信息,并通过调用displayStudents函数显示所有学生的信息了。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-9 07:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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