鱼C论坛

 找回密码
 立即注册
查看: 1809|回复: 5

[已解决]c语言S1E44,疫苗接种系统课后作业求助_已解决

[复制链接]
发表于 2022-6-25 19:55:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 涛4091 于 2023-2-23 11:03 编辑

屏幕截图 2022-06-25 195118.png
你好,这里debug是等于Y但是判断不了呢,为什么
最佳答案
2022-6-25 21:19:38

strcmp函数不是这么用的,如果strcmp函数发现两个字符串一样的话会返回0的,而你却直接放到了if判断里,所以你输入Y当然会进else啦
详情请查看->https://fishc.com.cn/forum.php?m ... peid%26typeid%3D583
所以应当改成这样
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
struct Date{
        int year;
        int month;
        int day;
};
struct PersonInfo{
        char name[20];
        int age;
        char firstYesOrNot[2];
        struct Date firstDate;
        char secondYesOrNot[2];
        struct Date secondDate;
};
void inputInfo(struct PersonInfo *personInfo){
        printf("请问姓名是:");        scanf("%s",personInfo->name);
        printf("请问年龄是:");scanf("%d",&personInfo->age); 
        printf("请问是否接种过疫苗(Y/N):");
        scanf("%s",personInfo->firstYesOrNot);
        if(!strcmp(personInfo->firstYesOrNot,"Y")){
                printf("请出入第一针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->firstDate.year,&personInfo->firstDate.month,&personInfo->firstDate.day);
                printf("请问是否接种第二针疫苗(Y/N):");gets(personInfo->secondYesOrNot);
                if(!strcmp(personInfo->secondYesOrNot,"Y")){
                        printf("请出入第二针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->secondDate.year,&personInfo->secondDate.month,&personInfo->secondDate.day); 
                }else if(!strcmp(personInfo->secondYesOrNot,"N")){
                        printf("请尽快接种第二针疫苗!");
                        exit(1);
                }
        }else {
                printf("请尽快接种过疫苗!");
                exit(1);
        }
}
void printInfo(struct PersonInfo *personInfo){
        printf("姓名:%s,年龄:%d \n",personInfo->name,personInfo->age);
        if(!strcmp(personInfo->firstYesOrNot,"N")){
                printf("未接种疫苗!");
        }else if(!strcmp(personInfo->firstYesOrNot,"Y")){
                printf("第一针疫苗接种日期:%d-%d-%d \n",personInfo->firstDate.year,personInfo->firstDate.month,personInfo->firstDate.day);
                if(!strcmp(personInfo->secondYesOrNot,"N")){
                        printf("未接种第二针疫苗!");
                }else if(!strcmp(personInfo->secondYesOrNot,"Y")){
                        printf("第二针疫苗接种日期:%d-%d-%d",personInfo->secondDate.year,personInfo->secondDate.month,personInfo->secondDate.day);
                }
        }
}
int main(void){
        struct PersonInfo person1,person2,person3;
        inputInfo(&person1);
        inputInfo(&person2);
        inputInfo(&person3);
        printInfo(&person1);
        printInfo(&person2);
        printInfo(&person3);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-6-25 20:11:56 | 显示全部楼层
不要用==去判断字符串是否相等
你可以用strcmp函数,需要在使用之前#include <string.h>来引用string.h头文件
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-25 21:03:26 | 显示全部楼层
临时号 发表于 2022-6-25 20:11
不要用==去判断字符串是否相等
你可以用strcmp函数,需要在使用之前#include 来引用string.h头文件

不行,用了strcmp也不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-25 21:06:11 | 显示全部楼层
涛4091 发表于 2022-6-25 21:03
不行,用了strcmp也不行

把完整代码发出来,我帮你看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-25 21:09:57 | 显示全部楼层
临时号 发表于 2022-6-25 21:06
把完整代码发出来,我帮你看看
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
struct Date{
        int year;
        int month;
        int day;
};
struct PersonInfo{
        char name[20];
        int age;
        char firstYesOrNot[2];
        struct Date firstDate;
        char secondYesOrNot[2];
        struct Date secondDate;
};
void inputInfo(struct PersonInfo *personInfo){
        printf("请问姓名是:");        scanf("%s",personInfo->name);
        printf("请问年龄是:");scanf("%d",&personInfo->age); 
        printf("请问是否接种过疫苗(Y/N):");
        scanf("%s",personInfo->firstYesOrNot);
        if(strcmp(personInfo->firstYesOrNot,"Y")){
                printf("请出入第一针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->firstDate.year,&personInfo->firstDate.month,&personInfo->firstDate.day);
                printf("请问是否接种第二针疫苗(Y/N):");gets(personInfo->secondYesOrNot);
                if(strcmp(personInfo->secondYesOrNot,"Y")){
                        printf("请出入第二针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->secondDate.year,&personInfo->secondDate.month,&personInfo->secondDate.day); 
                }else if(strcmp(personInfo->secondYesOrNot,"N")){
                        printf("请尽快接种第二针疫苗!");
                        exit(1);
                }
        }else {
                printf("请尽快接种过疫苗!");
                exit(1);
        }
}
void printInfo(struct PersonInfo *personInfo){
        printf("姓名:%s,年龄:%d \n",personInfo->name,personInfo->age);
        if(strcmp(personInfo->firstYesOrNot,"N")){
                printf("未接种疫苗!");
        }else if(strcmp(personInfo->firstYesOrNot,"Y")){
                printf("第一针疫苗接种日期:%d-%d-%d \n",personInfo->firstDate.year,personInfo->firstDate.month,personInfo->firstDate.day);
                if(strcmp(personInfo->secondYesOrNot,"N")){
                        printf("未接种第二针疫苗!");
                }else if(strcmp(personInfo->secondYesOrNot,"Y")){
                        printf("第二针疫苗接种日期:%d-%d-%d",personInfo->secondDate.year,personInfo->secondDate.month,personInfo->secondDate.day);
                }
        }
}
int main(void){
        struct PersonInfo person1,person2,person3;
        inputInfo(&person1);
        inputInfo(&person2);
        inputInfo(&person3);
        printInfo(&person1);
        printInfo(&person2);
        printInfo(&person3);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-25 21:19:38 | 显示全部楼层    本楼为最佳答案   

strcmp函数不是这么用的,如果strcmp函数发现两个字符串一样的话会返回0的,而你却直接放到了if判断里,所以你输入Y当然会进else啦
详情请查看->https://fishc.com.cn/forum.php?m ... peid%26typeid%3D583
所以应当改成这样
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
struct Date{
        int year;
        int month;
        int day;
};
struct PersonInfo{
        char name[20];
        int age;
        char firstYesOrNot[2];
        struct Date firstDate;
        char secondYesOrNot[2];
        struct Date secondDate;
};
void inputInfo(struct PersonInfo *personInfo){
        printf("请问姓名是:");        scanf("%s",personInfo->name);
        printf("请问年龄是:");scanf("%d",&personInfo->age); 
        printf("请问是否接种过疫苗(Y/N):");
        scanf("%s",personInfo->firstYesOrNot);
        if(!strcmp(personInfo->firstYesOrNot,"Y")){
                printf("请出入第一针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->firstDate.year,&personInfo->firstDate.month,&personInfo->firstDate.day);
                printf("请问是否接种第二针疫苗(Y/N):");gets(personInfo->secondYesOrNot);
                if(!strcmp(personInfo->secondYesOrNot,"Y")){
                        printf("请出入第二针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->secondDate.year,&personInfo->secondDate.month,&personInfo->secondDate.day); 
                }else if(!strcmp(personInfo->secondYesOrNot,"N")){
                        printf("请尽快接种第二针疫苗!");
                        exit(1);
                }
        }else {
                printf("请尽快接种过疫苗!");
                exit(1);
        }
}
void printInfo(struct PersonInfo *personInfo){
        printf("姓名:%s,年龄:%d \n",personInfo->name,personInfo->age);
        if(!strcmp(personInfo->firstYesOrNot,"N")){
                printf("未接种疫苗!");
        }else if(!strcmp(personInfo->firstYesOrNot,"Y")){
                printf("第一针疫苗接种日期:%d-%d-%d \n",personInfo->firstDate.year,personInfo->firstDate.month,personInfo->firstDate.day);
                if(!strcmp(personInfo->secondYesOrNot,"N")){
                        printf("未接种第二针疫苗!");
                }else if(!strcmp(personInfo->secondYesOrNot,"Y")){
                        printf("第二针疫苗接种日期:%d-%d-%d",personInfo->secondDate.year,personInfo->secondDate.month,personInfo->secondDate.day);
                }
        }
}
int main(void){
        struct PersonInfo person1,person2,person3;
        inputInfo(&person1);
        inputInfo(&person2);
        inputInfo(&person3);
        printInfo(&person1);
        printInfo(&person2);
        printInfo(&person3);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-9 06:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表