Skip to content

Instantly share code, notes, and snippets.

@myguaguagua
Created July 29, 2017 13:30
Show Gist options
  • Save myguaguagua/0402ec2ddf7b2927c8de27018c1bcdf7 to your computer and use it in GitHub Desktop.
Save myguaguagua/0402ec2ddf7b2927c8de27018c1bcdf7 to your computer and use it in GitHub Desktop.
判断年龄,星座, 属相
#include<stdio.h>
char *sign[] = { // 星座
"白羊座",
"金牛座",
"双子座",
"巨蟹座",
"狮子座",
"处女座",
"天秤座",
"天蝎座",
"射手座",
"摩羯座",
"水瓶座",
"双鱼座"
};
char *animal[] = { // 属相
"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"
};
/*
* 判断某个日期是否在一个区间内
* @month,day 待判断的日期
* @month_down, day_down 日期区间的下界
* @month_up, day_up 日期区间的上界
*/
bool betweenTwoDate(int month, int day, int month_down, int day_down, int month_up, int day_up) {
if(month_up == month_down) {
if(month == month_up && day >= day_down && day <= day_up)
return true;
else
return false;
} else {
if(month > month_down && month < month_up)
return true;
else if(month == month_down && day >= day_down)
return true;
else if(month == month_up && day <= day_up)
return true;
else
return false;
}
}
/*
* 根据出生月日判断星座
* @month 出生月
* @day 出生日
*/
char *getSign(int month, int day) {
if(betweenTwoDate(month, day, 3, 21, 4, 20))
return sign[0];
else if(betweenTwoDate(month, day, 4, 21, 5, 20))
return sign[1];
else if(betweenTwoDate(month, day, 5, 21, 6, 21))
return sign[2];
else if(betweenTwoDate(month, day, 6, 22, 7, 22))
return sign[3];
else if(betweenTwoDate(month, day, 7, 23, 8, 22))
return sign[4];
else if(betweenTwoDate(month, day, 8, 23, 9, 22))
return sign[5];
else if(betweenTwoDate(month, day, 9, 23, 10, 22))
return sign[6];
else if(betweenTwoDate(month, day, 10, 23, 11, 21))
return sign[7];
else if(betweenTwoDate(month, day, 11, 22, 12, 21))
return sign[8];
else if(betweenTwoDate(month, day, 12, 22, 1, 19))
return sign[9];
else if(betweenTwoDate(month, day, 1, 20, 2, 18))
return sign[10];
else if(betweenTwoDate(month, day, 2, 19, 3, 20))
return sign[11];
}
/*
* 根据出生年判断属相
* @year 出生年
*/
char *getAnimal(int year) {
return animal[(year - 1900) % 12];
}
int main() {
int birth_year, birth_month, birth_day; // 出生年月日
int now_year, now_month, now_day; // 现在年月日
printf("请输入出生年月日:\n");
scanf("%d %d %d", &birth_year, &birth_month, &birth_day);
printf("输入现在年月日:\n");
scanf("%d %d %d", &now_year, &now_month, &now_day);
int age = now_year - birth_year; // 计算年龄
if(betweenTwoDate(now_month, now_day, 1, 1, birth_month, birth_day)) // 如果未到生日
age -= 1; // 减一岁
printf("年龄:%d\n", age);
printf("星座:%s\n", getSign(birth_month, birth_day));
printf("属相:%s\n", getAnimal(birth_year));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment