c语言新手求教

需要用到链表
希望c语言大神可以帮助

代码如下:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct _Date {
int year;
int month;
int day;
}Date;

typedef struct _Teacher {
char id[20]; // 编号
char name[20]; // 姓名
char title[20]; // 职称
int salary; // 基本工资
int allowance; // 津贴
int titleAllowance; // 职称补贴
Date birthday; // 出生日期
}Teacher;

typedef struct _Node {
Teacher teacher;
struct _Node *next;
}Node;

typedef Node *LinkList;

void LinkList_Insert(LinkList *list, const Teacher *teacher)
{
Node *node, *temp;

node = (Node *)malloc(sizeof(Node));
strcpy(node->teacher.id, teacher->id);
strcpy(node->teacher.name, teacher->name);
strcpy(node->teacher.title, teacher->title);
node->teacher.salary = teacher->salary;
node->teacher.allowance = teacher->allowance;
node->teacher.titleAllowance = teacher->titleAllowance;
node->teacher.birthday = teacher->birthday;
node->next = NULL;

if (*list == NULL) {
*list = node;
}
else {
temp = *list;
while (temp->next != NULL)
temp = temp->next;
temp->next = node;
}
}

void LinkList_Destroy(LinkList *list)
{
Node *temp;

while (*list != NULL) {
temp = *list;
*list = temp->next;
free(temp);
}
}

int main()
{
LinkList teacherList = NULL;
Node *temp;
Teacher teacher;
int total, i;

for (i = 0; i < 3; i++) {

scanf("%s%s%s%d%d%d%d%d%d", 
teacher.id, teacher.name, teacher.title,
&teacher.salary, &teacher.allowance, &teacher.titleAllowance,
&teacher.birthday.year, &teacher.birthday.month, &teacher.birthday.day);

LinkList_Insert(&teacherList, &teacher);
}

printf("\n");

temp = teacherList;

while (temp != NULL) {

total = temp->teacher.salary + temp->teacher.allowance + temp->teacher.titleAllowance;

if (total > 5000) {

printf("姓名:%s, 出生日期:%d.%d.%d 工资:%d\n",
temp->teacher.name,
temp->teacher.birthday.year,
temp->teacher.birthday.month,
temp->teacher.birthday.day,
total);

}

temp = temp->next;
}

LinkList_Destroy(&teacherList);

return 0;
}

运行结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-12-23

这题用链表有点麻烦了,其实没有必要,因为就是固定的3个数据。不如用数组省事。 

但既然有要求,那就用吧。

#include <stdio.h>
#include <stdlib.h>
#define NUM 3
/* 定义工资结构体 */
typedef struct {
    int base;
    int bonus;
    int patch;
}s_salary;
/* 定义生日结构体 */
typedef struct {
    int year;
    int month;
    int day;
}s_birthday;
/* 定义老师信息结构体 */
typedef struct s_teacher {
    char no[10];
    char name[10];
    char title[10];
    s_salary salary;
    s_birthday birthday;
    struct s_teacher *next;
}s_teacher;
void main ()
{
    s_teacher head, *teacher, *last;
    int i, total;
    /* 获取输入数据 */
    last = &head;
    for (i=0; i<NUM; i++)
    {
        teacher = malloc(sizeof(s_teacher));
        if (teacher == NULL)
        {
            printf("memory error\n");
            exit;
        }
        last->next = teacher;
        scanf("%s %s %s %d %d %d %d %d %d",
            teacher->no, teacher->name, teacher->title,
            &teacher->salary.base, &teacher->salary.bonus, &teacher->salary.patch,
            &teacher->birthday.year, &teacher->birthday.month, &teacher->birthday.day);
        teacher->next = NULL;
        last = teacher;
    }
    /* 输出所有工资大于5000的教师信息 */
    last = &head;
    for (i=0; i<NUM; i++)
    {
        teacher = last->next;
        total = teacher->salary.base + teacher->salary.bonus + teacher->salary.patch;
        if (total > 5000)
        {
            printf("姓名: %s, 出生日期:%d.%d.%d, 工资:%d\n",
                 teacher->name, teacher->birthday.year,
                 teacher->birthday.month, teacher->birthday.day,
                 total);
        }
        last = teacher;
    }        
    /* 释放所分配的内存 */
    teacher = head.next;
    for (i=0; i<NUM; i++)
    {
        last = teacher;
        teacher = last->next;
        free(last);
    }
}

运行结果:

追问

采纳了,谢谢大佬,能不能麻烦把数组的也发一下😃谢谢谢谢

追答

不用链表,直接定义一个数组,大致结构不变,但省去了分配内存和释放内存的步骤:

#include <stdio.h>
#include <stdlib.h>

#define NUM 3

/* 定义工资结构体 */
typedef struct {
    int base;
    int bonus;
    int patch;
}s_salary;

/* 定义生日结构体 */
typedef struct {
    int year;
    int month;
    int day;
}s_birthday;

/* 定义老师信息结构体 */
typedef struct s_teacher {
    char no[10];
    char name[10];
    char title[10];
    s_salary salary;
    s_birthday birthday;

    // struct s_teacher *next;   //数组不需要定义链表的指针
}s_teacher;

void main ()
{
    s_teacher teacher[NUM];   // 定义这么多的结构数组就行了
    int i, total;

    /* 获取输入数据 */
    for (i=0; i<NUM; i++)
    {
        scanf("%s %s %s %d %d %d %d %d %d",
            teacher[i].no, teacher[i].name, teacher[i].title,
            &teacher[i].salary.base, &teacher[i].salary.bonus, &teacher[i].salary.patch,
            &teacher[i].birthday.year, &teacher[i].birthday.month, &teacher[i].birthday.day);
    }

    /* 输出所有工资大于5000的教师信息 */
    for (i=0; i<NUM; i++)
    {
        total = teacher[i].salary.base + teacher[i].salary.bonus + teacher[i].salary.patch;
        if (total > 5000)
        {
            printf("姓名: %s, 出生日期:%d.%d.%d, 工资:%d\n",
                 teacher[i].name, teacher[i].birthday.year,
                 teacher[i].birthday.month, teacher[i].birthday.day,
                 total);
        }

    }

}

本回答被提问者采纳
第2个回答  2018-12-23
直接让别人帮你做有点难啊
第3个回答  2018-12-23
这不是有答案了吗?
相似回答