1. ユーザーは画面から生徒番号、得点、改行の形式で各生徒の得点を入力します。学生番号は整数です。範囲は 1 ~ 50 で、スコアは浮動小数点数です。
2. ユーザーの学籍番号の入力が繰り返された場合、前の入力が間違っていたことを意味し、新しい結果で上書きされます。
3. ユーザーは最大 50 人の生徒のスコアを入力することができますが、50 人の生徒が入力されていれば、スコアが入力されたことになります。
4. ユーザーの学籍番号に「-1」が入力されていれば、結果が入力されたことを示します。
5. プログラムはすべての生徒のスコアをソートした後、スコアのレベルに応じて印刷します。印刷形式は次のとおりです: 生徒番号、スコアラインの折り返し
例
1 50
2 60
3 55
2 80
-1
結果:
2 80
3 55
1 50
最終的には要件を満たすchatgptで作成したので、ソースコードを添付します。
#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;
}
実行結果は次のとおりです。
chatgpt を呼び出して、リンク リストを使用して実装します。これも要件を満たします。
#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;
}
実行結果は次のとおりです。