检查以下代码的debugassertionfailed问题#include <stdio.h> #include <string.h> typedef struct { char no[7]; char name[10]; char sex[3]; int birthYear; int birthMonth; int birthDay; int grades[4]; int totalGrade; } Student; int main() { Student a[6]; int i, j, total; char d1[11], d2[11]; printf("请输入学生信息:\n"); for (i = 0; i < 6; i++) { printf("第%d个学生的学号、姓名、性别、出生日期、语文、数学、英语:\n", i + 1); scanf("%s %s %s %d-%d-%d %d %d %d", a[i].no, a[i].name, a[i].sex, &a[i].birthYear, &a[i].birthMonth, &a[i].birthDay, &a[i].grades[0], &a[i].grades[1], &a[i].grades[2]); a[i].totalGrade = a[i].grades[0] + a[i].grades[1] + a[i].grades[2]; } printf("请输入两个日期(格式:YYYY-MM-DD):\n"); scanf("%s %s", d1, d2); printf("符合条件的学生信息:\n"); for (i = 0; i < 6; i++) { if (strcmp(a[i].sex, "男") == 0 && strcmp(a[i].sex, "女") == 0) { continue; } if (strcmp(a[i].sex, "男") == 0 && strcmp(a[i].sex, "女") == 0) { continue; } int year = a[i].birthYear; int month = a[i].birthMonth; int day = a[i].birthDay; if ((year > atoi(strtok(d1, "-")) || (year == atoi(strtok(d1, "-")) && month > atoi(strtok(NULL, "-"))) || (year == atoi(strtok(d1, "-")) && month == atoi(strtok(NULL, "-")) && day >= atoi(strtok(NULL, "-")))) && (year < atoi(strtok(d2, "-")) || (year == atoi(strtok(d2, "-")) && month < atoi(strtok(NULL, "-"))) || (year == atoi(strtok(d2, "-")) && month == atoi(strtok(NULL, "-")) && day <= atoi(strtok(NULL, "-"))))) { printf("%s %s %s %d-%02d-%02d", a[i].no, a[i].name, a[i].sex, a[i].birthYear, a[i].birthMonth, a[i].birthDay); for (j = 0; j < 3; j++) { printf(" %d", a[i].grades[j]); } printf(" %d\n", a[i].totalGrade); } } return 0; }
时间: 2024-02-05 18:03:03 浏览: 20
在这段代码中可能会出现 "Debug Assertion Failed" 的问题的原因是:
在第25行和第27行中,使用了两次 `strcmp(a[i].sex, "男") == 0 && strcmp(a[i].sex, "女") == 0` 的判断条件,这是错误的。这个条件永远不会被满足,因为 `strcmp(a[i].sex, "男") == 0` 和 `strcmp(a[i].sex, "女") == 0` 是互斥的。应该改为 `strcmp(a[i].sex, "男") != 0 && strcmp(a[i].sex, "女") != 0` 来判断性别是否合法。
此外,在第40行中,`strtok` 函数被多次调用,会导致字符串被多次切割,从而出现错误。应该先将 `d1` 和 `d2` 字符串分别传给 `strtok` 函数,再分别取出年、月、日,避免重复调用 `strtok` 函数。
下面是修改后的代码:
```
#include <stdio.h>
#include <string.h>
typedef struct {
char no[7];
char name[10];
char sex[3];
int birthYear;
int birthMonth;
int birthDay;
int grades[4];
int totalGrade;
} Student;
int main() {
Student a[6];
int i, j, total;
char d1[11], d2[11];
printf("请输入学生信息:\n");
for (i = 0; i < 6; i++) {
printf("第%d个学生的学号、姓名、性别、出生日期、语文、数学、英语:\n", i + 1);
scanf("%s %s %s %d-%d-%d %d %d %d", a[i].no, a[i].name, a[i].sex, &a[i].birthYear, &a[i].birthMonth, &a[i].birthDay, &a[i].grades[0], &a[i].grades[1], &a[i].grades[2]);
a[i].totalGrade = a[i].grades[0] + a[i].grades[1] + a[i].grades[2];
}
printf("请输入两个日期(格式:YYYY-MM-DD):\n");
scanf("%s %s", d1, d2);
printf("符合条件的学生信息:\n");
for (i = 0; i < 6; i++) {
if (strcmp(a[i].sex, "男") != 0 && strcmp(a[i].sex, "女") != 0) {
int year = a[i].birthYear;
int month = a[i].birthMonth;
int day = a[i].birthDay;
char *p = strtok(d1, "-");
int y1 = atoi(p);
p = strtok(NULL, "-");
int m1 = atoi(p);
p = strtok(NULL, "-");
int d_1 = atoi(p);
p = strtok(d2, "-");
int y2 = atoi(p);
p = strtok(NULL, "-");
int m2 = atoi(p);
p = strtok(NULL, "-");
int d_2 = atoi(p);
if ((year > y1 || (year == y1 && month > m1) || (year == y1 && month == m1 && day >= d_1)) &&
(year < y2 || (year == y2 && month < m2) || (year == y2 && month == m2 && day <= d_2))) {
printf("%s %s %s %d-%02d-%02d", a[i].no, a[i].name, a[i].sex, a[i].birthYear, a[i].birthMonth, a[i].birthDay);
for (j = 0; j < 3; j++) {
printf(" %d", a[i].grades[j]);
}
printf(" %d\n", a[i].totalGrade);
}
}
}
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)