I can’t even solve a simple question in C language. It’s so uncomfortable. I must write more code.

1. The user inputs the scores of each student from the screen in the following format: student number, score, line break. The student number is an integer. The range is 1-50, and the score is a floating point number.

2. If the user’s student number input is repeated, it means that the previous input was incorrect and the new result will be overwritten.

3. Users can only enter a maximum of 50 students' scores. If 50 students have been entered, it means that the scores have been entered.

4. If the user’s student number enters -1, it indicates that the results have been entered.

5. After the program sorts the scores of all students, it prints them according to the level of the scores. The printing format is as follows: student number, score line wrapping

Example

1   50

2   60

3   55

2   80

 -1

Result:

2  80

3  55

1  50

Finally, it was written by chatgpt, which meets the requirements. The source code is attached.

#include <stdio.h>

#define MAX_STUDENTS 50

struct Student 
{
    int id;
    float score;
};

void swap(struct Student *a, struct Student *b) 
{
    struct Student temp = *a;
    *a = *b;
    *b = temp;
}

void sortStudents(struct Student students[], int count) 
{
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - i - 1; j++) {
            if (students[j].score < students[j + 1].score) {
                swap(&students[j], &students[j + 1]);
            }
        }
    }
}

int main() 
{
    struct Student students[MAX_STUDENTS];
    int count = 0;
    
    printf("Enter student grades (id score) or -1 to finish:\n");
    
    while (1) {
        int id;
        float score;
        
        scanf("%d", &id);
        
        if (id == -1) {
            break;
        }
        
        scanf("%f", &score);
        
        // Check for duplicate id
        int duplicateIndex = -1;
        for (int i = 0; i < count; i++) {
            if (students[i].id == id) {
                duplicateIndex = i;
                break;
            }
        }
        
        if (duplicateIndex != -1) {
            students[duplicateIndex].score = score;
        } else {
            students[count].id = id;
            students[count].score = score;
            count++;
        }
        
        if (count >= MAX_STUDENTS) {
            printf("Maximum number of students reached.\n");
            break;
        }
    }
    
    sortStudents(students, count);
    
    printf("Result:\n");
    
    for (int i = 0; i < count; i++) {
        printf("%d\t%.2f\n", students[i].id, students[i].score);
    }
    
    return 0;
}

The running results are as follows:

Call chatgpt to implement it using a linked list, which also meets the requirements.

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

struct Node {
    int id;
    float score;
    struct Node* next;
};

struct Node* createNode(int id, float score) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->id = id;
    newNode->score = score;
    newNode->next = NULL;
    return newNode;
}

struct Node* insertOrUpdate(struct Node* head, int id, float score) {
    struct Node* newNode = createNode(id, score);

    if (head == NULL) {
        return newNode;
    }

    if (head->id == id) {
        head->score = score;
        free(newNode);
        return head;
    }

    struct Node* curr = head;
    struct Node* prev = NULL;

    while (curr != NULL) {
        if (curr->id == id) {
            prev->next = newNode;
            newNode->next = curr->next;
            free(curr);
            return head;
        }
        prev = curr;
        curr = curr->next;
    }

    prev->next = newNode;
    return head;
}

void swap(struct Node* a, struct Node* b) {
    int tempId = a->id;
    float tempScore = a->score;
    a->id = b->id;
    a->score = b->score;
    b->id = tempId;
    b->score = tempScore;
}

void sortStudents(struct Node* head) {
    int swapped;
    struct Node* ptr1;
    struct Node* lptr = NULL;

    if (head == NULL) {
        return;
    }

    do {
        swapped = 0;
        ptr1 = head;

        while (ptr1->next != lptr) {
            if (ptr1->score < ptr1->next->score) {
                swap(ptr1, ptr1->next);
                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    } while (swapped);
}

void printStudents(struct Node* head) {
    printf("Result:\n");

    while (head != NULL) {
        printf("%d\t%.2f\n", head->id, head->score);
        head = head->next;
    }
}

void freeList(struct Node* head) {
    struct Node* temp;

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

int main() {
    struct Node* head = NULL;
    int count = 0;
    
    printf("Enter student grades (id score) or -1 to finish:\n");
    
    while (1) {
        int id;
        float score;
        
        scanf("%d", &id);
        
        if (id == -1) {
            break;
        }
        
        scanf("%f", &score);
        
        if (id < 1 || id > 50) {
            printf("Invalid student ID. Please enter a valid ID (1-50).\n");
            continue;
        }
        
        head = insertOrUpdate(head, id, score);
        count++;
        
        if (count >= 50) {
            printf("Maximum number of students reached.\n");
            break;
        }
    }
    
    sortStudents(head);
    printStudents(head);
    freeList(head);
    
    return 0;
}

 The running results are as follows:

Guess you like

Origin blog.csdn.net/buhuidage/article/details/131565017