高级语言程序设计课程第九次个人作业

这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/gjyycx

这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/gjyycx/homework/15595

学号:<052301346>

姓名:<郑积超>


作业内容

1.声明一个结构体类型,用来存放某个学生的姓名、学号、性别、班级、三科成绩,并且打印出来该学生信息

image

2.定义一个结构体数组,用来存放班级中N个学生以上信息,编写三个函数进行信息输入、排序和输出。分别使用数组和指针作为函数参数,完成学生信息输入、以及成绩从小到大排序、按排序顺序进行信息输出

#pragma warning(disable : 4996)
#include <stdio.h>
#include <string.h>

#define MAX_STUDENTS 100
#define SUBJECTS 3
typedef struct {
    char name[50];
    int student_id;
    char gender;
    char class_name[30];
    float scores[SUBJECTS];
    float total_score;
} Student;
void input_students(Student* students, int n) {
    int i, j;

    for (i = 0; i < n; i++) {
        printf("\n请输入第%d个学生的信息:\n", i + 1);
        printf("姓名: ");
        scanf("%s", students[i].name);
        printf("学号: ");
        scanf("%d", &students[i].student_id);
        printf("性别(M/F): \n");
        scanf("\n%c", &students[i].gender);
        printf("班级: \n");
        scanf("%s", students[i].class_name);
        students[i].total_score = 0;
        for (j = 0; j < SUBJECTS; j++) {
            printf("科目%d成绩: ", j + 1);
            scanf("%f", &students[i].scores[j]);
            students[i].total_score += students[i].scores[j];
        }
    }
}
float calculate_average(Student* student) {
    if (SUBJECTS == 0) return 0.0;
    return student->total_score / SUBJECTS;
}
void sort_students(Student students[], int n) {
    int i, j;
    Student temp;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - 1 - i; j++) {
            if (students[j].total_score > students[j + 1].total_score) {
                temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
}
void output_students(Student* students, int n) {
    int i, j;

    printf("\n%-10s %-10s %-6s %-15s", "姓名", "学号", "性别", "班级");
    for (j = 0; j < SUBJECTS; j++) {
        printf(" 科目%d", j + 1);
    }
    printf(" 总分 平均分\n");
    printf("--------------------------------------------------------------------------------\n");
    for (i = 0; i < n; i++) {
        printf("%-10s %-10d %-6c %-15s",
            students[i].name,
            students[i].student_id,
            students[i].gender,
            students[i].class_name);

        for (j = 0; j < SUBJECTS; j++) {
            printf(" %-6.1f", students[i].scores[j]);
        }
        printf(" %-6.1f %-6.1f\n",
            students[i].total_score,
            calculate_average(&students[i]));
    }
}
int main() {
    int n;
    Student students[MAX_STUDENTS];
    printf("请输入学生数量(最多%d): ", MAX_STUDENTS);
    scanf("%d", &n);
    input_students(students, n);
    printf("\n========== 原始学生信息 ==========\n");
    output_students(students, n);
    sort_students(students, n);
    printf("\n========== 按成绩排序后的学生信息 ==========\n");
    output_students(students, n);
    return 0;
}

image

设计一个程序以指针和结构体变量名分别访问结构体变量的成员,进行输出

image

设计一个程序使用typedef定义结构体类型的别名

image

建立一个链表,链表的节点个数为N(>5),使用从链尾到链头的建立方式和从链头到链尾的建立方式

用指针实现链表存储int类型数据,并实现链表的插入和删除操作

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
    int data;
    struct Node* next;
} Node;
Node* createNode(int data);
Node* buildFromTail(int n);
Node* buildFromHead(int n);
void traverseList(Node* head);
Node* insertNode(Node* head, int position, int data);
Node* deleteNode(Node* head, int position);
void freeList(Node* head);
int getListLength(Node* head);
int main() {
    int n = 8;
    Node* head1, * head2;
    printf("1. 从链尾到链头建立链表(%d个节点):\n", n);
    head1 = buildFromTail(n);
    printf("链表建立完成,链表内容:\n");
    traverseList(head1);
    printf("\n2. 从链头到链尾建立链表(%d个节点):\n", n);
    head2 = buildFromHead(n);
    printf("链表建立完成,链表内容:\n");
    traverseList(head2);
    printf("\n3. 对第一个链表进行插入操作:\n");
    printf("当前链表长度: %d\n", getListLength(head1));
    printf("(1) 在位置0插入数据999:\n");
    head1 = insertNode(head1, 0, 999);
    traverseList(head1);
    printf("\n(2) 在位置4插入数据888:\n");
    head1 = insertNode(head1, 4, 888);
    traverseList(head1);
    printf("\n(3) 在末尾插入数据777:\n");
    int len = getListLength(head1);
    head1 = insertNode(head1, len, 777);
    traverseList(head1);
    printf("\n4. 对第一个链表进行删除操作:\n");
    printf("当前链表长度: %d\n", getListLength(head1));
    printf("(1) 删除位置0的节点:\n");
    head1 = deleteNode(head1, 0);
    traverseList(head1);
    printf("\n(2) 删除位置3的节点:\n");
    head1 = deleteNode(head1, 3);
    traverseList(head1);
    printf("\n(3) 删除尾节点:\n");
    len = getListLength(head1);
    head1 = deleteNode(head1, len - 1);
    traverseList(head1);

    printf("\n5. 对第二个链表进行操作:\n");
    traverseList(head2);
    printf("\n(1) 在位置2插入数据555:\n");
    head2 = insertNode(head2, 2, 555);
    traverseList(head2);
    printf("\n(2) 删除位置5的节点:\n");
    head2 = deleteNode(head2, 5);
    traverseList(head2);

    printf("\n6. 释放链表内存:\n");
    freeList(head1);
    freeList(head2);

    return 0;
}

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        printf("内存分配失败!\n");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

Node* buildFromTail(int n) {
    if (n <= 0) return NULL;

    Node* head = NULL;
    for (int i = 1; i <= n; i++) {
        Node* newNode = createNode(i * 10);
        newNode->next = head;
        head = newNode;
    }
    return head;
}
Node* buildFromHead(int n) {
    if (n <= 0) return NULL;

    Node* head = NULL;
    Node* tail = NULL;

    for (int i = 1; i <= n; i++) {
        Node* newNode = createNode(i * 100); 
        if (head == NULL) {
            head = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            tail = newNode;
        }
    }
    return head;
}
void traverseList(Node* head) {
    if (head == NULL) {
        printf("链表为空!\n");
        return;
    }

    Node* current = head;
    int count = 0;

    printf("链表内容: ");
    while (current != NULL) {
        printf("[%d]%d", count, current->data);
        if (current->next != NULL) {
            printf(" -> ");
        }
        current = current->next;
        count++;
    }
    printf("\n链表长度: %d\n", count);
}

int getListLength(Node* head) {
    int length = 0;
    Node* current = head;

    while (current != NULL) {
        length++;
        current = current->next;
    }

    return length;
}

Node* insertNode(Node* head, int position, int data) {

    Node* newNode = createNode(data);

    if (position == 0) {
        newNode->next = head;
        return newNode; 
    }

    Node* current = head;
    int currentPos = 0;

    while (current != NULL && currentPos < position - 1) {
        current = current->next;
        currentPos++;
    }

    if (current == NULL) {
        printf("错误:插入位置%d超出链表长度!\n", position);
        free(newNode);
        return head;
    }

    newNode->next = current->next;
    current->next = newNode;

    return head;
}

Node* deleteNode(Node* head, int position) {
    if (head == NULL) {
        printf("错误:链表为空,无法删除节点!\n");
        return NULL;
    }

    if (position == 0) {
        Node* temp = head;
        head = head->next;
        printf("删除节点[0],数据: %d\n", temp->data);
        free(temp);
        return head;
    }

    Node* current = head;
    int currentPos = 0;

    while (current != NULL && currentPos < position - 1) {
        current = current->next;
        currentPos++;
    }

    if (current == NULL || current->next == NULL) {
        printf("错误:删除位置%d无效或超出链表长度!\n", position);
        return head;
    }

    Node* temp = current->next;
    current->next = temp->next;
    printf("删除节点[%d],数据: %d\n", position, temp->data);
    free(temp);

    return head;
}

void freeList(Node* head) {
    Node* current = head;
    Node* nextNode;

    while (current != NULL) {
        nextNode = current->next;
        free(current);
        current = nextNode;
    }
}

image

总结与思考

学习了结构体、指针和链表数据结构

posted @ 2025-12-13 20:34  052301346郑积超  阅读(2)  评论(0)    收藏  举报