在C语言中,添加成绩通常涉及到使用数组、链表或其他数据结构来存储和管理成绩信息。以下是使用数组和链表两种方法来插入成绩的示例代码:
使用数组插入成绩
```c
include include define MAX_SIZE 100 // 假设最多有100个学生 int scores[MAX_SIZE]; // 成绩数组 int count = 0; // 记录当前已存储的成绩数量 // 插入成绩函数 void insert_score(int score) { if (count < MAX_SIZE) { scores[count++] = score; // 确保数组未满 } else { printf("成绩数组已满,无法插入新的成绩。\n"); } } int main() { int new_score; printf("请输入要插入的成绩: "); scanf("%d", &new_score); insert_score(new_score); printf("成绩插入成功,当前成绩总数: %d\n", count); return 0; } ``` 使用链表插入成绩 ```c include include include // 定义链表节点结构体 typedef struct ScoreNode { int score; struct ScoreNode* next; } ScoreNode; // 定义链表结构体 typedef struct { ScoreNode* head; } ScoreList; // 初始化链表 void init_list(ScoreList* list) { list->head = NULL; } // 插入成绩函数 void insert_score_to_list(ScoreList* list, int score) { ScoreNode* new_node = (ScoreNode*)malloc(sizeof(ScoreNode)); if (new_node == NULL) { printf("内存分配失败。\n"); return; } new_node->score = score; new_node->next = list->head; list->head = new_node; } // 打印链表成绩 void print_list(ScoreList* list) { ScoreNode* current = list->head; while (current != NULL) { printf("%d ", current->score); current = current->next; } printf("\n"); } int main() { ScoreList list; init_list(&list); int new_score; printf("请输入要插入的成绩: "); scanf("%d", &new_score); insert_score_to_list(&list, new_score); print_list(&list); return 0; } ``` 使用结构体管理成绩 ```c include include define MAX_STUDENTS 100 typedef struct { char name; int id; float score; } Student; Student students[MAX_STUDENTS]; int student_count = 0; // 插入成绩函数 void insert_student(const char* name, int id, float score) { if (student_count < MAX_STUDENTS) { strcpy(students[student_count].name, name); students[student_count].id = id; students[student_count].score = score; student_count++; } else { printf("学生数量已达上限,无法插入新的学生。\n"); } } int main() { insert_student("张三", 1, 95.0); insert_student("李四", 2, 88.0); insert_student("王五", 3, 76.0); for (int i = 0; i < student_count; i++) { printf("学生 %d: %s, 成绩: %.2f\n", i + 1, students[i].name, students[i].score); } return 0; } ``` 这些示例展示了如何在C语言中使用数组、链表和结构体来添加和管理成绩信息。你可以根据具体需求选择合适的方法来实现成绩管理系统。