用C语言定义一个包含学生成绩(学号,姓名,成绩)的顺序表,读入相应的成绩信息来完成学生成绩表的创建。然后,(1)查找统计不及格的学生人数,(2)删除并以学号升序方式逐行输出不及格的学生信息。如果不存在不及格同学,则输出提示信息“not found”
时间: 2023-08-31 16:37:32 浏览: 36
### 回答1:
可以回答这个问题。以下是代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
int id;
char name[20];
float score;
} Student;
typedef struct {
Student data[MAX_SIZE];
int length;
} SeqList;
void create(SeqList *list);
int count_fail(SeqList list);
void delete_fail(SeqList *list);
int main() {
SeqList list;
create(&list);
int fail_num = count_fail(list);
if (fail_num == 0) {
printf("not found\n");
} else {
printf("fail num: %d\n", fail_num);
delete_fail(&list);
}
return 0;
}
void create(SeqList *list) {
printf("input student info (id, name, score):\n");
int id;
char name[20];
float score;
int i = 0;
while (scanf("%d %s %f", &id, name, &score) != EOF) {
list->data[i].id = id;
strcpy(list->data[i].name, name);
list->data[i].score = score;
i++;
}
list->length = i;
}
int count_fail(SeqList list) {
int count = 0;
for (int i = 0; i < list.length; i++) {
if (list.data[i].score < 60) {
count++;
}
}
return count;
}
void delete_fail(SeqList *list) {
int i = 0;
while (i < list->length) {
if (list->data[i].score < 60) {
printf("%d %s %.1f\n", list->data[i].id, list->data[i].name, list->data[i].score);
for (int j = i; j < list->length - 1; j++) {
list->data[j] = list->data[j+1];
}
list->length--;
} else {
i++;
}
}
}
### 回答2:
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int number;
char name[20];
float score;
} Student;
typedef struct studentList{
Student* students;
int length;
} StudentList;
// 初始化学生成绩表
void initStudentList(StudentList* list, int num){
list->length = num;
list->students = (Student*)malloc(sizeof(Student) * num);
}
// 输入学生成绩信息
void inputStudentList(StudentList* list){
for(int i=0; i<list->length; i++){
Student* s = &(list->students[i]);
printf("请输入第%d位学生的学号:", i+1);
scanf("%d", &(s->number));
printf("请输入第%d位学生的姓名:", i+1);
scanf("%s", s->name);
printf("请输入第%d位学生的成绩:", i+1);
scanf("%f", &(s->score));
}
}
// 查找并统计不及格学生人数
int countFailStudents(StudentList* list){
int count = 0;
for(int i=0; i<list->length; i++){
if(list->students[i].score < 60){
count++;
}
}
return count;
}
// 删除并以学号递增方式逐行输出不及格的学生信息
void deleteAndPrintFailStudents(StudentList* list){
int flag = 0; // 标志是否存在不及格学生
for(int i=0; i<list->length; i++){
if(list->students[i].score < 60){
if(flag == 0){
printf("不及格的学生信息如下:\n");
}
printf("学号:%d,姓名:%s,成绩:%.2f\n", list->students[i].number, list->students[i].name, list->students[i].score);
flag = 1;
// 删除不及格学生信息
for(int j=i; j<list->length-1; j++){
list->students[j] = list->students[j+1];
}
list->length--;
i--;
}
}
if(flag == 0){
printf("not found\n"); // 不存在不及格学生
}
}
int main(){
int num;
printf("请输入学生人数:");
scanf("%d", &num);
StudentList list;
initStudentList(&list, num);
inputStudentList(&list);
int failCount = countFailStudents(&list);
printf("不及格的学生人数为:%d\n", failCount);
deleteAndPrintFailStudents(&list);
free(list.students); // 释放内存
return 0;
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)