The program, written in C, determines the driver's insurance status based on certain criteria.

 Recently, I want to be familiar with the use of the if statement, so I found such a topic:

The program written in C language determines the insurance status of the driver according to specific conditions. If the following conditions are met, the system should display that the driver is insured, otherwise it is not insured:

⑴ If the driver is married, he (she) has been insured;

⑵If the driver is an unmarried male and over the age of 30, he is already insured;

⑶ If the driver is an unmarried female and over the age of 25, she is already insured.

 Prepare test cases to check the details entered by the user, give the expected output, and display appropriate error messages if wrong data is entered.

 The code is as follows: (I am lazy and have no comments. If you have any questions, you can comment and reply when you see it)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int driver_insurance(char sex[],int age,char marry[]);
int determine(char sex[],int age,char marry[]);
void value(int sum);
char  *MARRY_= "已婚";
char *NOTMARRY_ = "未婚";
char *BOY_ = "男";
char *GIRL_ = "女";
int main()
{
    int age;
    char sex[10];
    char marry[10];
    printf("请输入您的性别,年龄,婚姻状态\n");
    while(1){
    int flag=2;
    scanf("%s %d %s" ,&sex,&age,&marry);
    switch(determine(sex, age,marry))
    {
    case 0: flag=driver_insurance(sex,age,marry); break;
    case 1: printf("请输入正确的性别!\n"); break;
    case 3: printf("请输入正确的年龄!\n"); break;
    case 5: printf("请输入正确的婚姻状态!\n"); break;
    case 4: printf("请输入正确的性别,年龄!\n"); break;
    case 6: printf("请输入正确的性别,婚姻状态!\n"); break;
    case 8: printf("请输入正确的年龄,婚姻状态!\n"); break;
    default: printf("请输入正确的性别,年龄,婚姻状态!\n");
    }
    if(flag==1)
        printf("恭喜你,您已经有保险\n");
    if(flag==0)
        printf("很遗憾,您还没有保险\n");
    }
    return 0;
}
int driver_insurance(char sex[],int age,char marry[])
{

    if(strcmp(marry,MARRY_) == 0)
    {
        return 1;
    }
    else if(strcmp(sex,BOY_)==0&&age>30)
    {
        return 1;
    }
    else if(strcmp(sex,GIRL_)==0&&age>25)
    {
        return 1;
    }
    else
        return 0;
}
int determine(char sex[],int age,char marry[])
{
    int a=strcmp(sex,GIRL_)!=0&&strcmp(sex,BOY_)!=0 ? 1:0;
    int b=age<0||age>110 ? 3:0;
    int c=strcmp(marry,MARRY_)!=0&&strcmp(marry,NOTMARRY_)!=0 ? 5:0;
    int sum=a+b+c;
    value(sum);
}
void value(int sum)
{
    return sum;
}

The following is a screenshot of the operation:

 okay.

Guess you like

Origin blog.csdn.net/qq_58861683/article/details/127152131