亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c語(yǔ)言判斷是否素?cái)?shù)程序代碼

 更新時(shí)間:2013年11月26日 12:05:25   作者:  
這篇文章主要介紹了c語(yǔ)言判斷是否素?cái)?shù)的方法和問(wèn)題,大家參考使用吧

復(fù)制代碼 代碼如下:

#include <stdio.h>

bool isPrimeNum(int x)
{
    if (x == 1)
        return false;
    else if (x <= 0)
        return false;
    else if (x == 2)
        return true;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x%i == 0)
                return false;
        }
        return true;

    }
}

int main(void)
{
    int x;
    char ch;
    do{
    printf("請(qǐng)輸入一個(gè)大于1的自然數(shù):\n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素?cái)?shù)\n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素?cái)?shù)\n",x);
    printf("是否繼續(xù)(y/n):\n");
    scanf("%c", &ch);
    }while ('y'== ch || 'Y' == ch);
    return 0;
}

但是,運(yùn)行的時(shí)候,出現(xiàn)了一個(gè)問(wèn)題.

本來(lái)寫do...while是為了省事,重復(fù)判斷.但是,卻愣是不起效果.

后來(lái),修改了一下:

復(fù)制代碼 代碼如下:

#include <stdio.h>

bool isPrimeNum(int x)
{
    if (x == 1)
        return false;
    else if (x <= 0)
        return false;
    else if (x == 2)
        return true;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x%i == 0)
                return false;
        }
        return true;

    }
}

int main(void)
{
    int x;
    char ch;
    do{
    printf("請(qǐng)輸入一個(gè)大于1的自然數(shù):\n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素?cái)?shù)\n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素?cái)?shù)\n",x);
    printf("是否繼續(xù)(y/n):\n");
    scanf(" %c", &ch);//在這里加一個(gè)空格
    }while ('y'==ch || 'Y' == ch);
    return 0;
}

這樣,問(wèn)題就神奇地解決了.

再后來(lái),再這么改:

復(fù)制代碼 代碼如下:

#include <stdio.h>

bool isPrimeNum(int x)
{
    if (x == 1)
        return false;
    else if (x <= 0)
        return false;
    else if (x == 2)
        return true;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x%i == 0)
                return false;
        }
        return true;

    }
}

int main(void)
{
    int x;
    char ch;
    do{
    printf("請(qǐng)輸入一個(gè)大于1的自然數(shù):\n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素?cái)?shù)\n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素?cái)?shù)\n",x);
    printf("是否繼續(xù)(y/n):\n");
    scanf("\n%c", &ch);//在這里加一個(gè)換行符 '\n'
    }while ('y'==ch || 'Y' == ch);
    return 0;
}

也沒問(wèn)題.

所以,總結(jié)一下,問(wèn)題出現(xiàn)在我們?cè)谳斎霐?shù)字按下回車的似乎,'\n'還保存在輸入流中

因此,看第一個(gè)代碼:

復(fù)制代碼 代碼如下:

int main(void)
{
    int x;
    char ch;
    do{
    printf("請(qǐng)輸入一個(gè)大于1的自然數(shù):\n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素?cái)?shù)\n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素?cái)?shù)\n",x);
    printf("是否繼續(xù)(y/n):\n");
    scanf("%c", &ch);  //因?yàn)?'\n'還在輸入流中  所以 就變成了  ch = '\n';
    }while ('y'==ch || 'Y' == ch);  // 'y' != '\n'  && 'Y' != '\n';
    return 0;   //因此程序就return了
}


另外,也是可以使用fflush(stdin)清空輸入緩存區(qū).

復(fù)制代碼 代碼如下:

#include <stdio.h>

bool isPrimeNum(int x)
{
    if (x == 1)
        return false;
    else if (x <= 0)
        return false;
    else if (x == 2)
        return true;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x%i == 0)
                return false;
        }
        return true;

    }
}

int main(void)
{
    int x;
    char ch;
    do{
    printf("請(qǐng)輸入一個(gè)大于1的自然數(shù):\n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素?cái)?shù)\n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素?cái)?shù)\n",x);
    printf("是否繼續(xù)(y/n):\n");
    fflush(stdin);  //清空輸入緩存區(qū)
    scanf("%c", &ch);
    }while ('y'==ch || 'Y' == ch);
    return 0;
}

相關(guān)文章

最新評(píng)論