C语言成绩过滤函数问题:无法正确移除指定日期成绩
问题:成绩数组过滤逻辑错误及优化问题
需求说明
- 实现
int digits_in_date()函数,接收成绩数组 - 对通过课程(成绩≥6/满分10)累计ECTS学分≥30的学生,移除其成绩中日期所含不同数字数量最少的所有记录
- 通过姓名识别同一学生,数据合法,禁止使用辅助字符串,必须用结构体实现
我编写了对应代码,部分测试用例可得到正确结果,但存在正确性问题(如失败测试用例中未按预期移除Fata Fatic的成绩),且尝试优化时易导致程序崩溃或运行效率低下。以下为我的代码及测试用例:
我的代码
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Date { int day, month, year; }; struct Course { int id; char name[100]; double ects; }; struct Grade { char fullname[100]; int grade; struct Course course; struct Date date; }; int count_different_digits(int number) { int digits[10] = {0}; while (number > 0) { digits[number % 10]++; number /= 10; } int different_digits_count = 0; for (int i = 0; i < 10; i++) { if (digits[i] > 0) { different_digits_count++; } } return different_digits_count; } int digits_in_date(struct Grade grades[], int number_of_grades) { for (int i = 0; i < number_of_grades; i++) { if (grades[i].grade >= 6) { double total_ects = 0.0; for (int j = 0; j < number_of_grades; j++) { if (strcmp(grades[i].fullname, grades[j].fullname) == 0) { total_ects += grades[j].course.ects; } } if (total_ects >= 30) { int current_date_digits = count_different_digits(grades[i].date.day) + count_different_digits(grades[i].date.month) + count_different_digits(grades[i].date.year); for (int j = 0; j < number_of_grades; j++) { if (strcmp(grades[i].fullname, grades[j].fullname) == 0 && j != i) { int date_digits = count_different_digits(grades[j].date.day) + count_different_digits(grades[j].date.month) + count_different_digits(grades[j].date.year); if (date_digits < current_date_digits) { for (int k = j; k < number_of_grades - 1; k++) { grades[k] = grades[k + 1]; } number_of_grades--; j--; // Adjust the loop index after removal } } } } } } return number_of_grades; } int main() { int number_of_grades; printf("Enter the number of grades: "); scanf("%d", &number_of_grades); struct Grade *grades = (struct Grade *)malloc(number_of_grades * sizeof(struct Grade)); printf("Enter grades:\n"); for (int i = 0; i < number_of_grades; i++) { printf("Grade %d:\n", i + 1); printf("Full name: "); scanf("%s", grades[i].fullname); printf("Grade: "); scanf("%d", &grades[i].grade); printf("Course name: "); scanf("%s", grades[i].course.name); printf("ECTS: "); scanf("%lf", &grades[i].course.ects); printf("Date (day month year): "); scanf("%d %d %d", &grades[i].date.day, &grades[i].date.month, &grades[i].date.year); } digits_in_date(grades, number_of_grades); printf("\nGrades after filtering:\n"); for (int i = 0; i < number_of_grades; i++) { printf("%s - %s - %.1f ECTS - %d.%d.%d\n", grades[i].fullname, grades[i].course.name, grades[i].course.ects, grades[i].date.day, grades[i].date.month, grades[i].date.year); } free(grades); return 0; }
测试用例
测试用例1(输出正确)
输入
struct Grade array[3] = { { "Mujo Suljic", 10, { 1, "Introduction to Programming", 25}, { 19, 1, 2020 } }, { "Fata Fatic", 8, { 1, "Introduction to Programming", 25}, { 19, 1, 2020 } }, { "Mujo Suljic", 7, { 2, "Engineering Physics 1", 10}, { 11, 1, 2020 } } }; int size = digits_in_date(array, 3); int i; for (i = 0; i < size; i++) printf("%s %s\n", array[i].fullname, array[i].course.name);
预期输出
Mujo Suljic Introduction to Programming Fata Fatic Introduction to Programming
我的输出
Mujo Suljic Introduction to Programming Fata Fatic Introduction to Programming
失败测试用例
输入
struct Grade array[6] = { { "Mujo Suljic", 10, { 1, "Introduction to Programming", 25}, { 20, 2, 2020 } }, { "Pero Peric", 6, { 5, "Fundamentals of Electrical Engineering", 25}, { 20, 2, 2020 } }, { "Fata Fatic", 8, { 1, "Introduction to Programming", 25}, { 19, 1, 2020 } }, { "Mujo Suljic", 5, { 2, "Engineering Physics 1", 10}, { 11, 1, 2020 } }, { "Mujo Suljic", 10, { 3, "Engineering Mathematics 1", 4}, { 20, 1, 2020 } }, { "Fata Fatic", 8, { 4, "Engineering Mathematics 1", 25}, { 19, 1, 2020 } } }; int size = digits_in_date(array, 6); int i; for (i = 0; i < size; i++) printf("%s %s %g\n", array[i].fullname, array[i].course.name, array[i].course.ects);
预期输出
Mujo Suljic Introduction to Programming 25 Pero Peric Fundamentals of Electrical Engineering 25 Mujo Suljic Engineering Physics 1 10 Mujo Suljic Engineering Mathematics 1 4
我的输出
Mujo Suljic Introduction to Programming 25 Pero Peric Fundamentals of Electrical Engineering 25 Fata Fatic Introduction to Programming 25 Mujo Suljic Engineering Physics 1 10 Mujo Suljic Engineering Mathematics 1 4 Fata Fatic Engineering Mathematics 1 25
内容的提问来源于stack exchange,提问作者Irma Ibrović
相关产品推荐
相关产品推荐

