测身高的C语言程序.doc
(2页)
本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦!
14.90 积分
此文档收集于网络,如有侵权,请联系网站删除测量身高C语言程序:主程序:// header file#include // define readonly variableconst char* const ERR_STRING = "您输入的字符有误,请重新";const char* const INFO_STRING = "*******************************************************";// will call this function when input have errorvoid PrintErr(void){ printf(ERR_STRING); while(getchar()!='\n');}// get a lower charchar GetLowerChar(const char* str){ char c; puts(str); // output infomation for input scanf(" %c",&c); return (c>='A' && c<='Z')?c+32:c;// translate to lower}// Get a char choose in a & b.// other character will make errorint LoopInputChar(const char* str, char a = 'y', char b = 'n'){ char a1 = GetLowerChar(str); for(; a1!=a && a1!=b; a1 = GetLowerChar(str+2)) PrintErr(); return a1;}// Get a double-type valuedouble GetDouble(const char* str){ double d; for(puts(str); scanf("%lf",&d)!=1; puts(str+2)) PrintErr(); return d;}// stature testvoid StatureTest(){ char c, ys; double fu,mu,wo,wo2; fu = GetDouble("请输入您的父亲身高:"); mu = GetDouble("请输入您的母亲身高:"); { // input gender c = LoopInputChar("请输入您的性别,男生M,女生F:", 'm', 'f'); if(c == 'm') wo = (fu+mu)*0.54; else wo = (fu*0.923+mu)/2; wo2 = wo; } // input other infomations c = LoopInputChar("请回答:您平时喜欢锻炼吗?(y/n):"); ys = LoopInputChar("请回答:您平时饮食规律吗?(y/n):"); if(c == 'y') // check the answer and calculate { wo2 += wo*0.02; if(ys =='y') wo2 += wo*0.015; } else if(ys =='y') { wo2 += wo*0.015; } // output printf("您的未来身高是%.2lf米!\n",wo2); puts(INFO_STRING); return ;}// main functionint main(void){ // output infomation about the test puts("现在将为您测试一下未来身高,请相信它的科学性!"); puts(INFO_STRING); do // loop input test { StatureTest(); // is exit program }while(LoopInputChar("请回答:您要退出吗?(y/n):")=='n'); return 0; // must be null此文档仅供学习与交流 关 键 词: c语言 程序 身高
天天文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。