怎么储存用链表储存多位学生信息
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;
}
}怎么显示多位员工的信息 要存储多位学生的信息,可以使用链表来进行储存。链表是由一系列节点组成的数据结构,每个节点包含了需要储存的信息以及一个指向下一个节点的指针。
首先,定义一个结构体来表示学生的信息,包括学号、密码、姓名、工资和税金等字段。
typedef struct Student {
int id;
char password;
char name;
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 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]