关于fflush(stdin)的用法有些不明白

杜子腾哦 2013-11-08 03:30:08
新手,这是我写的一段折半查找的算法




#include <stdio.h>

#define N 5

int main()

{ int i,number,top,bott,mid,loca,a[N],flag=1,sign;

char c;

printf("请按大小顺序输入N个数字\n");
for(i=0;i<N;i++)
{scanf("%d",&a[i]);}



while(flag)

{
printf("input number to look for:");

scanf("%d",&number);

sign=0;

top=0; //top是查找区间的起始位置

bott=N-1; //bott是查找区间的最末位置

if ((number<a[0])||(number>a[N-1])) //要查的数不在查找区间内

loca=-1; // 表示找不到

while ((!sign) && (top<=bott))

{mid=(bott+top)/2;

if (number==a[mid])

{loca=mid;

printf("Has found %d, its position is %d\n",number,loca+1);

sign=1;

}

else if (number<a[mid])

bott=mid-1;

else

top=mid+1;

}

if(!sign||loca==-1)

printf("cannot find %d.\n",number);;

printf("continue or not(Y/N)?");

fflush(stdin);//????

scanf("%c",&c);

if (c=='N'||c=='n')

flag=0;

}

return 0;

}
正常结果
如果去掉这句就会出现这个结果
...全文
140 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
杜子腾哦 2013-11-09
  • 打赏
  • 举报
回复
引用 4 楼 A812185083 的回复:
配图反了 fflush是用来清空缓冲区 清空之后重新换行
能说的具体点吗?不了解这是怎么运行的,缓冲区里有啥?为什么要清空?
自信男孩 2013-11-09
  • 打赏
  • 举报
回复
fflush是刷新缓冲区的,因为上次的数如会将换行符滞留在设备缓冲区里,fflush是将数据都刷到内存里。
AnYidan 2013-11-08
  • 打赏
  • 举报
回复
引用 4 楼 A812185083 的回复:
配图反了 fflush是用来清空缓冲区 清空之后重新换行
担心输入缓存中有残留的字符
  • 打赏
  • 举报
回复
配图反了 fflush是用来清空缓冲区 清空之后重新换行
赵4老师 2013-11-08
  • 打赏
  • 举报
回复
http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express 点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装 再参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\bsearch.c
/***
*bsearch.c - do a binary search
*
*   Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*   defines bsearch() - do a binary search an an array
*
*******************************************************************************/

#include <cruntime.h>
#include <stdlib.h>
#include <search.h>
#include <internal.h>

#if defined (_M_CEE)
#define __fileDECL  __clrcall
#else  /* defined (_M_CEE) */
#define __fileDECL  __cdecl
#endif  /* defined (_M_CEE) */
/***
*char *bsearch() - do a binary search on an array
*
*Purpose:
*   Does a binary search of a sorted array for a key.
*
*Entry:
*   const char *key    - key to search for
*   const char *base   - base of sorted array to search
*   unsigned int num   - number of elements in array
*   unsigned int width - number of bytes per element
*   int (*compare)()   - pointer to function that compares two array
*           elements, returning neg when #1 < #2, pos when #1 > #2, and
*           0 when they are equal. Function is passed pointers to two
*           array elements.
*
*Exit:
*   if key is found:
*           returns pointer to occurrence of key in array
*   if key is not found:
*           returns NULL
*
*Exceptions:
*   Input parameters are validated. Refer to the validation section of the function.
*
*******************************************************************************/

#ifdef __USE_CONTEXT
#define __COMPARE(context, p1, p2) (*compare)(context, p1, p2)
#else  /* __USE_CONTEXT */
#define __COMPARE(context, p1, p2) (*compare)(p1, p2)
#endif  /* __USE_CONTEXT */

#if !defined (_M_CEE)
_CRTIMP
#endif  /* !defined (_M_CEE) */

SECURITYSAFECRITICAL_ATTRIBUTE
#ifdef __USE_CONTEXT
void * __fileDECL bsearch_s (
    REG4 const void *key,
    const void *base,
    size_t num,
    size_t width,
    int (__fileDECL *compare)(void *, const void *, const void *),
    void *context
    )
#else  /* __USE_CONTEXT */
void * __fileDECL bsearch (
    REG4 const void *key,
    const void *base,
    size_t num,
    size_t width,
    int (__fileDECL *compare)(const void *, const void *)
    )
#endif  /* __USE_CONTEXT */
{
    REG1 char *lo = (char *)base;
    REG2 char *hi = (char *)base + (num - 1) * width;
    REG3 char *mid;
    size_t half;
    int result;

    /* validation section */
    _VALIDATE_RETURN(base != NULL || num == 0, EINVAL, NULL);
    _VALIDATE_RETURN(width > 0, EINVAL, NULL);
    _VALIDATE_RETURN(compare != NULL, EINVAL, NULL);

        /*
        We allow a NULL key here because it breaks some older code and because we do not dereference
        this ourselves so we can't be sure that it's a problem for the comparison function
        */

    while (lo <= hi)
    {
        if ((half = num / 2) != 0)
        {
            mid = lo + (num & 1 ? half : (half - 1)) * width;
            if (!(result = __COMPARE(context, key, mid)))
                return(mid);
            else if (result < 0)
            {
                hi = mid - width;
                num = num & 1 ? half : half-1;
            }
            else
            {
                lo = mid + width;
                num = half;
            }
        }
        else if (num)
            return (__COMPARE(context, key, lo) ? NULL : lo);
        else
            break;
    }

    return NULL;
}

#undef __fileDECL
#undef __COMPARE
derekrose 2013-11-08
  • 打赏
  • 举报
回复
不要只看表面,去耐心理解文档
杜子腾哦 2013-11-08
  • 打赏
  • 举报
回复
配图反了。。。我去,下面是正常的,第一张是如果去掉这句就会出现这个结果

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧