Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. char *sign[] = { // 星座
  4. "白羊座",
  5. "金牛座",
  6. "双子座",
  7. "巨蟹座",
  8. "狮子座",
  9. "处女座",
  10. "天秤座",
  11. "天蝎座",
  12. "射手座",
  13. "摩羯座",
  14. "水瓶座",
  15. "双鱼座"
  16. };
  17.  
  18. char *animal[] = { // 属相
  19. "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"
  20. };
  21.  
  22. /*
  23. * 判断某个日期是否在一个区间内
  24. * @month,day 待判断的日期
  25. * @month_down, day_down 日期区间的下界
  26. * @month_up, day_up 日期区间的上界
  27. */
  28. bool betweenTwoDate(int month, int day, int month_down, int day_down, int month_up, int day_up) {
  29. if(month_up == month_down) {
  30. if(month == month_up && day >= day_down && day <= day_up)
  31. return true;
  32. else
  33. return false;
  34. } else {
  35. if(month > month_down && month < month_up)
  36. return true;
  37. else if(month == month_down && day >= day_down)
  38. return true;
  39. else if(month == month_up && day <= day_up)
  40. return true;
  41. else
  42. return false;
  43. }
  44. }
  45.  
  46. /*
  47. * 根据出生月日判断星座
  48. * @month 出生月
  49. * @day 出生日
  50. */
  51. char *getSign(int month, int day) {
  52. if(betweenTwoDate(month, day, 3, 21, 4, 20))
  53. return sign[0];
  54. else if(betweenTwoDate(month, day, 4, 21, 5, 20))
  55. return sign[1];
  56. else if(betweenTwoDate(month, day, 5, 21, 6, 21))
  57. return sign[2];
  58. else if(betweenTwoDate(month, day, 6, 22, 7, 22))
  59. return sign[3];
  60. else if(betweenTwoDate(month, day, 7, 23, 8, 22))
  61. return sign[4];
  62. else if(betweenTwoDate(month, day, 8, 23, 9, 22))
  63. return sign[5];
  64. else if(betweenTwoDate(month, day, 9, 23, 10, 22))
  65. return sign[6];
  66. else if(betweenTwoDate(month, day, 10, 23, 11, 21))
  67. return sign[7];
  68. else if(betweenTwoDate(month, day, 11, 22, 12, 21))
  69. return sign[8];
  70. else if(betweenTwoDate(month, day, 12, 22, 1, 19))
  71. return sign[9];
  72. else if(betweenTwoDate(month, day, 1, 20, 2, 18))
  73. return sign[10];
  74. else if(betweenTwoDate(month, day, 2, 19, 3, 20))
  75. return sign[11];
  76. }
  77.  
  78. /*
  79. * 根据出生年判断属相
  80. * @year 出生年
  81. */
  82. char *getAnimal(int year) {
  83. return animal[(year - 1900) % 12];
  84. }
  85.  
  86. int main() {
  87. int birth_year, birth_month, birth_day; // 出生年月日
  88. int now_year, now_month, now_day; // 现在年月日
  89.  
  90. printf("请输入出生年月日:\n");
  91. scanf("%d %d %d", &birth_year, &birth_month, &birth_day);
  92.  
  93. printf("输入现在年月日:\n");
  94. scanf("%d %d %d", &now_year, &now_month, &now_day);
  95.  
  96. int age = now_year - birth_year; // 计算年龄
  97. if(betweenTwoDate(now_month, now_day, 1, 1, birth_month, birth_day)) // 如果未到生日
  98. age -= 1; // 减一岁
  99.  
  100. printf("年龄:%d\n", age);
  101. printf("星座:%s\n", getSign(birth_month, birth_day));
  102. printf("属相:%s\n", getAnimal(birth_year));
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement