此文档收集于网络,如有侵权,请联系网站删除
测量身高C语言程序:
主程序:
// header file
#include // define readonly variable
const char* const ERR_STRING = "您输入的字符有误,请重新";
const char* const INFO_STRING = "*******************************************************";
// will call this function when input have error
void PrintErr(void)
{
printf(ERR_STRING);
while(getchar()!=\n);
}
// get a lower char
char 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 error
int 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 value
double GetDouble(const char* str)
{
double d;
for(puts(str); scanf("%lf",&d)!=1; puts(str+2))
PrintErr();
return d;
}
// stature test
void 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 function
int 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
此文档仅供学习与交流