程序实现输入年月日
输出星座生肖年龄
代码实现:
#include<stdio.h> #include<time.h> #include<stdlib.h> int main() { struct tm *Tm; time_t t; time(&t); Tm = localtime(&t); int toyear = Tm->tm_year+1900; int tomon = Tm->tm_mon+1; int today = Tm->tm_mday; int year,mon,day; char * xz[12]={"摩羯座","水瓶座","双鱼座","白羊座","金牛座","双子座","巨蟹座","狮子座","处女座","天平座","天蝎座","射手座"}; printf("请输入您的出生年月日(示例:1999 4 23):"); scanf("%d%d%d",&year,&mon,&day); int m = mon; int n = day; int shengxiao = 0; //星座判断 switch (m ) { case 1: printf("您的星座是%s\n",n<=19?xz[0]:xz[1]); break; case 2: printf("您的星座是:%s\n",n<=18?xz[1]:xz[2]); break; case 3: printf("您的星座是:%s\n",n<=20?xz[2]:xz[3]); break; case 4: printf("您的星座是%s\n",n<=19?xz[3]:xz[4]); break; case 5: printf("您的星座是:%s\n",n<=20?xz[4]:xz[5]); break; case 6: printf("您的星座是:%s\n",n<=21?xz[5]:xz[6]); break; case 7: printf("您的星座是:%s\n",n<=22?xz[6]:xz[7]); break; case 8: printf("您的星座是:%s\n",n<=22?xz[7]:xz[8]); break; case 9: printf("您的星座是:%s\n",n<=22?xz[8]:xz[9]); break; case 10:printf("您的星座是:%s\n",n<=23?xz[9]:xz[10]); break; case 11:printf("您的星座是:%s\n",n<=22?xz[10]:xz[11]); break; case 12:printf("您的星座是:%s\n",n<=21?xz[11]:xz[0]); break; default:printf("ERROR!!!!"); break; } //从1900年开始生肖为鼠 shengxiao = (year - 1899) %12; switch (shengxiao) { case 1: printf("您的生肖是:鼠\n"); break; case 2: printf("您的生肖是:牛\n"); break; case 3: printf("您的生肖是:虎\n"); break; case 4: printf("您的生肖是:兔\n"); break; case 5: printf("您的生肖是:龙\n"); break; case 6: printf("您的生肖是:蛇\n"); break; case 7: printf("您的生肖是:马\n"); break; case 8: printf("您的生肖是:羊\n"); break; case 9: printf("您的生肖是:猴\n"); break; case 10:printf("您的生肖是:鸡\n"); break; case 11:printf("您的生肖是:狗\n"); break; case 12:printf("您的生肖是:猪\n"); break; default:printf("ERROR!!!!"); break; } //年龄计算 if(mon<tomon) printf("您现在的年龄为:%d",toyear-year); else if(mon>tomon) printf("您现在的年龄为:%d",toyear-year-1); else if(mon<tomon) { if(day<today) printf("您现在的年龄为:%d",toyear-year); else printf("您现在的年龄为:%d",toyear-year-1); } return 0; }
程序难点在于当前时间的获取:
struct tm *Tm; time_t t; time(&t); Tm = localtime(&t); int toyear = Tm->tm_year+1900; int tomon = Tm->tm_mon+1; int today = Tm->tm_mday;