|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在小甲鱼的C语言教程数组章节09最后的一个程序:#define M 10
#include<stdio.h>
void main()
{
static int a[M] = {-12,0,6,16,23,56,80,100,110,115};
int n, low, mid, high, found;
low=0;
high=M-1;
found=0;
printf("Input a number to be searched:\n");
#if(0)
do
{
scanf("%d", &n);
getcher();
}while(n<a[0]||n>a[M-1]);
#endif
while(scanf("%d",&n)!=1)
{
printf("illegal input!!\nplease input again!!\n");
getchar();
}
while(low <= high)
{
mid = (low+high)/2;
if(n == a[mid])
{
found = 1;
break;
} /*找到,结束循环*/
else if(n > a[mid])
low=mid+1;
else
high=mid-1;
}
if(found == 1)
printf("The index of %d is %d\n",n,mid);
else
printf("There is not %d\n",n);
}
上面标红的那段,我想问一下scanf函数在while语句中,作为一个判断条件,那程序是如何判断输入的呢?getchar吗?
在线等~~
|
|