c语言之星座运势

/*星座运势
题目要求
输入出生年月,输出用户的属相和星座以及星座运势。
技能点
● 计算属相
● 计算星座
● http API请求处理
● json处理
● ...

参考资料
  //AppKey:c9f2807e79686b4a73f5a41219b26e02
用c语言编写一个函数,由星座从网站(https://www.juhe.cn/docs/api/id/58)获取星座运势并输出在屏幕上
● 获取星座运势接口 ? https://www.juhe.cn/docs/api/id/58
● json处理
项目扩展
1、制作图形版星座运势备忘录,能够显示玄幻的图片背景,让它更接近你平时看到的样子
(像QQ空间里的星座运势等)。 */

摩羯座(12.21-1.19),水瓶座(1.20-2.18),双鱼座(2.19-3.20),白羊座(3.21-4.19),金牛座(4.20-5.20),
双子座(5.21-6.21),巨蟹座(6.22-7.22),狮子座(7.23-8.22),处女座(8.23-9.22),天秤座(9.23-10.23),
天蝎座(10.24-11.22),射手座(11.23-12.21)*/

//程序功能:计算属相、星座及获取其星座运势

//输入参数:用户出生年月、要获取的星座运势类型(即today(今日) tomorrow(明日) week(本周) nextweek(下周) month(本月) year(今年))

//环境:vc6.0

//窗口大小: 130*50


#include
#include
#include 
#include 
#include


#pragma comment(lib,"ws2_32.lib") //Winsock Library 
char constellation[10]; // 星座
char zodica[5];
char type[10];
void print_zodica(int year) //输出生肖
{
printf("\t\t\t\t\t\t\t生肖: ");
switch((year+9)%12)
{
case 1:strcpy(zodica,"鼠");break;
case 2:strcpy(zodica,"牛");break;
case 3:strcpy(zodica,"虎");break;
case 4:strcpy(zodica,"兔");break;
case 5:strcpy(zodica,"龙");break;
case 6:strcpy(zodica,"蛇");break;
case 7:strcpy(zodica,"马");break;
case 8:strcpy(zodica,"羊");break;
case 9:strcpy(zodica,"猴");break;
case 10:strcpy(zodica,"鸡");break;
case 11:strcpy(zodica,"狗");break;
case 0:strcpy(zodica,"猪");break;
}
printf("%s",zodica);
printf("\n");
}


void print_constellation(int month,int day,char consteurl[]) //输出星座  并转化为相应的URL编码   //http://tool.chinaz.com/tools/urlencode.aspx
{
  switch(month)
  {
  case 12:
if(day>=21)
strcpy(constellation,"摩羯座"),strcpy(consteurl,"%e6%91%a9%e7%be%af%e5%ba%a7");
else
strcpy(constellation,"射手座"),strcpy(consteurl,"%e5%b0%84%e6%89%8b%e5%ba%a7");break;
 case 1:
if(day<=19)
strcpy(constellation,"摩羯座"),strcpy(consteurl,"%e6%91%a9%e7%be%af%e5%ba%a7");
else
strcpy(constellation,"水瓶座"),strcpy(consteurl,"%e6%b0%b4%e7%93%b6%e5%ba%a7"); break;
 case 2:
if(day<=18)
 strcpy(constellation,"水瓶座"),strcpy(consteurl,"%e6%b0%b4%e7%93%b6%e5%ba%a7");
else
 strcpy(constellation,"双鱼座"),strcpy(consteurl,"%e5%8f%8c%e9%b1%bc%e5%ba%a7"); break;
 case 3:
if(day<=20)
 strcpy(constellation,"双鱼座"), strcpy(consteurl,"%e5%8f%8c%e9%b1%bc%e5%ba%a7");
else
 strcpy(constellation,"白羊座"), strcpy(consteurl,"%e7%99%bd%e7%be%8a%e5%ba%a7"); break;
 case 4:
if(day<=19)
 strcpy(constellation,"白羊座"), strcpy(consteurl,"%e7%99%bd%e7%be%8a%e5%ba%a7");
else
 strcpy(constellation,"金牛座"), strcpy(consteurl,"%e9%87%91%e7%89%9b%e5%ba%a7"); break;
 case 5:
if(day<=20)
 strcpy(constellation,"金牛座"), strcpy(consteurl,"%e9%87%91%e7%89%9b%e5%ba%a7");
else
 strcpy(constellation,"双子座"), strcpy(consteurl,"%e5%8f%8c%e5%ad%90%e5%ba%a7"); break;
 case 6:
if(day<=21)
 strcpy(constellation,"双子座"), strcpy(consteurl,"%e5%8f%8c%e5%ad%90%e5%ba%a7");
else
 strcpy(constellation,"巨蟹座"), strcpy(consteurl,"%e5%b7%a8%e8%9f%b9%e5%ba%a7"); break;
 case 7:
if(day<=22)
 strcpy(constellation,"巨蟹座"), strcpy(consteurl,"%e5%b7%a8%e8%9f%b9%e5%ba%a7");
else
 strcpy(constellation,"狮子座"), strcpy(consteurl,"%e7%8b%ae%e5%ad%90%e5%ba%a7"); break;
 case 8:
if(day<=22)
 strcpy(constellation,"狮子座"), strcpy(consteurl,"%e7%8b%ae%e5%ad%90%e5%ba%a7");
else
 strcpy(constellation,"处女座"), strcpy(consteurl,"%e5%a4%84%e5%a5%b3%e5%ba%a7"); break;
 case 9:
if(day<=22)
 strcpy(constellation,"处女座"), strcpy(consteurl,"%e5%a4%84%e5%a5%b3%e5%ba%a7");
else
 strcpy(constellation,"天秤座"), strcpy(consteurl,"%e5%a4%a9%e7%a7%a4%e5%ba%a7"); break;
 case 10:
if(day<=22)
 strcpy(constellation,"天秤座"), strcpy(consteurl,"%e5%a4%a9%e7%a7%a4%e5%ba%a7");
else
 strcpy(constellation,"天蝎座"), strcpy(consteurl,"%e5%a4%a9%e8%9d%8e%e5%ba%a7"); break;
 case 11:
if(day<=22)
 strcpy(constellation,"天蝎座"),strcpy(consteurl,"%e5%a4%a9%e8%9d%8e%e5%ba%a7");
else
 strcpy(constellation,"射手座"),strcpy(consteurl,"%e5%b0%84%e6%89%8b%e5%ba%a7"); break;
  }
  printf("\t\t\t\t\t\t\t星座: ");
  printf("%s\n",constellation);
}




void Build_othPath(char othPath[],char consName[])
{
char key[256]="0c9578a9e6739e4c0d5305bcc1e746a0"; //申请的appkey
printf("\t\t\ttoday(今日) tomorrow(明日) week(本周) nextweek(下周) month(本月) year(今年)\n\t\t\t\t\t\tenter your type: ");
fflush(stdin); //用来清空输入缓存,以便不影响后面输入的东西
gets(type);


while((strcmp(type,"today")!=0)&&(strcmp(type,"tomorrow")!=0)&&(strcmp(type,"week")!=0)&&(strcmp(type,"nextweek")!=0)&&(strcmp(type,"month")!=0)&&(strcmp(type,"year")!=0))
{
system("cls"); //执行控制台命令cls,就是CMD下面的cls,功能是清屏,清除所有显示的信息
printf("\t\t\t************************************************************************************\n");
printf("\t\t\t******......................\t   星座运势查询  \t......................******\n");
printf("\t\t\t************************************************************************************\n");
printf("\t\t\ttoday(今日) tomorrow(明日) week(本周) nextweek(下周) month(本月) year(今年)\n");
printf("\t\t\t\t\tyour type is error,please enter your type again: ");
fflush(stdin); //用来清空输入缓存,以便不影响后面输入的东西
gets(type);
}


strcat(othPath,consName);
strcat(othPath,"&type=");
strcat(othPath,type);
strcat(othPath,"&key=");
strcat(othPath,key);
}


char* U2G(const char* utf8) //格式转换UTF-8 to GB2312  (摘自http://www.xuebuyuan.com/2199498.html)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
if (wstr)
delete[] wstr;
return str;
}


void receive(SOCKET s,char server_reply[]) //接收数据并存入.txt文件中
{
char result[10000]="";
int recv_size;
FILE *fp;
if((fp=fopen("json.txt","w"))==NULL)
        exit(-1);


while ((recv_size = recv(s, server_reply, sizeof(server_reply)-1, 0)) > 0) //接收
{
server_reply[recv_size] ='\0';
strcat(result,server_reply);
}
closesocket(s); //释放套接口描述字s
WSACleanup(); //终止Winsock 2 DLL (Ws2_32.dll) 的使用
strcpy(result, U2G(result));
fputs(result,fp);
fclose(fp);
}


void picture(char constellation0[],int col,int line) //控制台显示图片
{


char title[255]; //用于存放控制台窗口标题
HWND hwnd; //窗口的句柄,H:Handle
HDC hdc, hmemdc; //设备上下文句柄,DC:driver context
HBITMAP hbm; //图片的句柄 
BITMAP bm; //图片结构体
RECT rect; //矩形 rectangle 
system("mode con cols=col lines=line"); //把控制台窗口调成col字符宽度,line行
//1.取得窗口的句柄
GetConsoleTitleA(title, 255);//获取控制台窗口的标题
hwnd = FindWindowA(NULL, title);//通过窗口标题取得该窗口的句柄
GetWindowRect(hwnd, &rect); //获取窗口的高度和宽度
//2. 获取画笔
hdc = GetDC(hwnd);
hmemdc = CreateCompatibleDC(hdc);//创建一个兼容的DC
//3.加载图片
hbm = (HBITMAP)LoadImageA(NULL,  ("%s",constellation0), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(hbm, sizeof(BITMAP), &bm);//得到图片信息
//4.把图片放入兼容DC中
SelectObject(hmemdc, hbm);

//5.在窗口上画出图片
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hmemdc, 0, 0, SRCCOPY);
DeleteObject(hbm);
DeleteObject(hmemdc);
ReleaseDC(hwnd, hdc);
}




void printf_result(int year,int month,int day) //输出查询结果
{
int k,j;
FILE *fp;
char string[10000];
if((fp=fopen("json.txt","r"))==NULL)
        exit(-1);
if(strcmp(constellation,"巨蟹座")==0)
picture("巨蟹座.bmp",90,80);
else if(strcmp(constellation,"摩羯座")==0)
picture("摩羯座.bmp",90,80);
else if(strcmp(constellation,"水瓶座")==0)
picture("水瓶座.bmp",90,80);
else if(strcmp(constellation,"双鱼座")==0)
picture("双鱼座.bmp",90,80);
else if(strcmp(constellation,"白羊座")==0)
picture("白羊座.bmp",90,80);
else if(strcmp(constellation,"金牛座")==0)
picture("金牛座.bmp",90,80);
else if(strcmp(constellation,"双子座")==0)
picture("双子座.bmp",90,80);
else if(strcmp(constellation,"狮子座")==0)
picture("狮子座.bmp",90,80);
else if(strcmp(constellation,"处女座")==0)
picture("处女座.bmp",90,80);
else if(strcmp(constellation,"天秤座")==0)
picture("天秤座.bmp",90,80);
else if(strcmp(constellation,"天蝎座")==0)
picture("天蝎座.bmp",90,80);
else if(strcmp(constellation,"射手座")==0)
picture("射手座.bmp",90,80);
system("cls"); //执行控制台命令cls,就是CMD下面的cls,功能是清屏,清除所有显示的信息
printf("\t\t\t************************************************************************************\n");
printf("\t\t\t******......................\t   星座运势查询  \t......................******\n");
printf("\t\t\t************************************************************************************\n\n");
printf("您的生日: %d年%d月%d日\n\n",year,month,day);
printf("您查询的%s结果: \n\n",type);
printf("生肖: %s\n\n",zodica);
printf("星座: %s\n\n",constellation);
printf("星座运势: \n\n");
while(!feof(fp))        // 若未结束 
    {
fgets(string, 4096, fp);          // 从文件中读入字符串 
if(string[0]=='{')
{
k=strlen(string);
for(j=1;j {
if(string[j]=='"'&&string[j+1]==',')
{
puts("\n");
j+=3;
}
if(string[j]=='"')
j++;
printf("%c",string[j]);
}
}
    }
printf("\n");
fclose(fp);
}




void HttpRequst(char host[], char othPath[]) //实现http请求
{
WSADATA wsa; 
SOCKET s;
sockaddr_in  server;
char message[200],server_reply[2000]; 


if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) //初始化

printf ( "Failed. Error Code : %d" ,WSAGetLastError());
return ; 
}
if ((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET) //创建socket
        printf ( "Could not create socket : %d" , WSAGetLastError());


 


struct hostent  *p = gethostbyname(host);
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
memcpy(&server.sin_addr, p->h_addr, 4);


     if (connect(s , ( struct sockaddr *)&server , sizeof (server)) < 0) //连接
     { 
         puts ( "connect error" );
         return ;
     }
strcpy(message, "GET "); //创建GET请求
strcat(message,othPath);
strcat(message," HTTP/1.1\r\nHost: ");
strcat(message,host);
strcat(message,"\r\nConnection:Close\r\n\r\n");
if( send(s , message , strlen(message) , 0) < 0) //发送请求 
     { 
         puts ( "Send failed" );
         return ; 
     }
receive(s, server_reply);
}


int isValid_date(int year,int month,int day) //判断日期(年月日)是否合法,如果日期合法返回1,如果不合法返回0
{
int l;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
if(year>0&&day>0&&day<=31)
l=1;
else l=0;
} break;
case 4:
case 6:
case 9:
case 11:
{
if(year>0&&day>0&&day<=30)
l=1;
else
l=0;
} break;
case 2:
{
if(((year%4==0&&year%100!=0)||(year%400==0)))
{
if(year>0&&day>0&&day<=29)
l=1;
else
l=0;
}
else
{
if(year>0&&day>0&&day<=28)
l=1;
else
l=0;
}
} break;
default:l=0;
}
return l;
}








int main()
{
int year,month,day;
char consteurl[40]; //星座相应的URL编码
char host[100]="ytujk1532.applinzi.com";
char othPath[200]="/?constellation/getAll?consName=";
picture("timg.bmp",130,40);
printf("\t\t\t************************************************************************************\n");
printf("\t\t\t******......................\t   星座运势查询  \t......................******\n");
printf("\t\t\t************************************************************************************\n");
printf("\t\t\t\t\tplease enter your  birthday: ");


scanf("%d%d%d",&year,&month,&day);



while(isValid_date(year,month,day)==0) //判断日期(年月日)是否合法,如果日期不合法重新输入
{
system("cls"); //执行控制台命令cls,就是CMD下面的cls,功能是清屏,清除所有显示的信息
printf("\t\t\t************************************************************************************\n");
printf("\t\t\t******......................\t   星座运势查询  \t......................******\n");
printf("\t\t\t************************************************************************************\n");
printf("\t\t\t\tenter error,please enter your  birthday again: ");
fflush(stdin); //用来清空输入缓存,以便不影响后面输入的东西
scanf("%d%d%d",&year,&month,&day);
}
print_zodica(year); //输出生肖
print_constellation(month,day,consteurl); //输出星座
Build_othPath(othPath,consteurl); //建立http申请的othPath
HttpRequst(host, othPath); //连接http请求数据
printf_result(year,month,day);
system("pause");
return 0;
}


你可能感兴趣的:(简单c课程设计及综合实践项目)