fgets(words,STLEN,stdin) 参数意义
1、fgets()函数的第二参数指明了读入字符串的最长量,如果该参数为n,那么最多将会读入n-1个字符,或者读到换行符为止。
2、与gets()不同的是,fgets()会将读到的换行符存储在数组中,而gets()会丢弃换行符。
3、fgets()的第三个参数必须声明要读入的文件,如果从键盘读入,则声明为stdin作为参数,该标识符的定义在sdtio中。
例:
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#pragma warning (disable:4996)
typedef struct
{
char name[20];
char sex; //'F'-女,'M'-男
double height; //cm
double weight; //kg
}Person;
int main()
{
Person test;
printf("请输入姓名:");
fgets(test.name, 20, stdin);
printf("请输入性别(F/M)、身高(cm)、体重(kg):");
scanf("%c%lf%lf", &test.sex, &test.height, &test.weight);
/*男性的标准体重=(身高cm-80)×70﹪,女性的标准体重=(身高cm-70)×60﹪。
标准体重正负10﹪为正常体重;标准体重正负10﹪~ 20﹪为体重过重或过轻;
标准体重正负20﹪以上为肥胖或体重不足,
其中超重计算公式为:超重(%)=[(实际体重-标准体重)/(标准体重)]×100%*/
double a,b,c;
int t(0);
if (test.sex == 'F')
{
a = 10;
}
else if (test.sex == 'M')
{
a = 0;
}
else
{
printf("性别错误,请重新打开程序输入");
exit(0);
}
c = (test.height - 80 + a)*(70 - a) / 100.0;
b = (test.weight - c) / c;
if (b>=0)
{
t = (b > 0.1) + (b > 0.2);
switch (t)
{
case 0:printf("体重正常"); break;
case 1:printf("体重过重"); break;
case 2:printf("体重肥胖"); break;
default:break;
}
}
else
{
t = (b < -0.1) + (b < -0.2);
switch (t)
{
case 0:printf("体重正常"); break;
case 1:printf("体重过轻"); break;
case 2:printf("体重不足"); break;
default:break;
}
}
printf("\n");
return 0;
}
=