1.知识点1:
scanf函数的返回值可以简单理解为为输入变量的个数:
Both scanf and wscanf return the number of fields successfully converted
and assigned; the return value does not include fields that were read but
not assigned. A return value of 0 indicates that no fields were assigned.
The return value is EOF for an error or if the end-of-file character or the
end-of-string character is nocountered in the first attempt to read a character.
2.知识点2:
逗号表达式取最右边的算数值作为返回值
3.知识点3:
EOF的值其实就是-1,当scanf读取内容发生错误或者读到文件结尾的时候就会返回EOF
/****************************************
输入直到文件末尾
****************************************/
while ((scanf("%d%m",&n,&m) != EOF))
{
// 循环从输入流读取m和n 直到遇到EOF为止,常见于ACM中
}
while (~scanf("%d%d",&m,&n))
{
//相当于while (scanf("%d",&n) != EOF)
}
/****************************************
输入全为0时跳出循环
****************************************/
while (scanf("%d%d",&n,&m)&&(m|n))
{
//不断读入新数据,当m,n均为0时跳出循环
}
while (scanf("%d %d", &n, &m), n+m)
{
//不断读入新数据,当m,n均为0时跳出循环
}
while ((scanf("%d%d",&n,&m) != EOF),n+m)
{
//有了前面的分析,这个代码就好理解了,神组合啊
}
/****************************************
输入两个整形数,其他类型跳出循环
****************************************/
while (scanf("%d%d", &a, &b)==2)
{
//不断读入新数据,当输入的不是两个整形数字时,跳出循环
}
while (scanf("%d%d",&n,&m))
{
//当输入不是两个整形数时,跳出循环
}
/***************************************/
有新的内容会持续更新,有错误的话感谢大家指出
while函数输入小结
最新推荐文章于 2020-08-16 14:59:43 发布