#include<stdio.h>
#include<stdlib.h>
/*----- Prototype -----*/
char* get_str(char*, int, int);
int check_range(int*, int);
/*_____________________*/
/*----- Variables -----*/
char* tempc;
/*_____________________*/
int main()
{
/*----- Variables -----*/
char* str_year; //Year
char* str_cnstl; //Constellation
int input[3];
int i;
/*_____________________*/
/*--- Set up strings for displaying ---*/
//str_year=(char*)malloc((2+1)*(12)*sizeof(char));
//str_cnstl=(char*)malloc((4+1)*(12)*sizeof(char));
str_year="鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪";
str_cnstl="水瓶,双鱼,牧羊,金牛,双子,巨蟹,狮子,处女,天秤,天蝎,射手,摩羯";
/*_____________________________________*/
/*--- Retrieve data from user ---*/
printf("请输入出生的 年/月/日 :\n");
printf("例 : 2012/12/21\n>");
scanf("%d/%d/%d",&input[0],&input[1],&input[2]);
/*_______________________________*/
/*--- Determine the range ---*/
//Year
printf("生肖: %s\n", get_str(str_year, 2, ((input[0]-1911)%12-1) ));
free(tempc);
//Constellation
for (i=0; i<12; ++i)
if (check_range(input, i)==1)
{
printf("星座: %s座\n", get_str(str_cnstl, 4, i ));
free(tempc);
break;
}
if (i==12)
printf("日期好像打错了,无法判断星座\n");
printf("\n");
/*___________________________*/
/*--- Free memories ---*/
//free(str_year);
//free(str_cnstl);
/*_____________________*/
system("pause");
return 0;
}
char* get_str(char* str, int symb_len, int ind)
{
int i=0;
tempc=(char*)malloc((symb_len+1)*sizeof(char));
for (i=0; i<symb_len; ++i)
tempc[i]=str[ind*(symb_len+1)+i];
tempc[i]='\0';
return tempc;
}
int check_range(int* input, int ind)
{
int range [2][12][2]={{1,20, 2,19, 3,21, 4,20, 5,21, 6,22, 7,23, 8,23, 9,23, 10,24, 11,23, 12,22},
{2,18, 3,20, 4,19, 5,20, 6,21, 7,22, 8,22, 9,22, 10,23, 11,22, 12,21, 1,19}};
int daymax;
int arr_daymax[12][2]={1,31, 2,28, 3,31, 4,30, 5,31, 6,30, 7,31, 8,31, 9,30, 10,31, 11,30, 12,31};
//Read daymax
daymax=arr_daymax[input[1]-1][1];
//Exception
if (input[1]==2)
if((input[0]%4)==0 && (input[0]%100)!=0 || (input[0]%400) ==0)
daymax=29;
//Main part
if (input[1]>=range[0][ind][0] && input[1]<=range[1][ind][0] )
if (input[1]==range[0][ind][0] && input[2]>=range[0][ind][1] || input[1]==range[1][ind][0] && input[2]<=range[1][ind][1])
if (input[2]>=1 && input[2]<=daymax)
return 1;
}