输出0~N之间的素数

修改于上篇判断是否为素数

#include <stdio.h>//输入一个数字N输出1~N之间的素数 
#include <string.h>
using namespace std; 
bool type1(char N1[],int N2);
bool isPrime(unsigned int N);
unsigned int a2ul(char *s);
int main(void)
{
    char str[100],m='1';
    while(m=='1')
    {    
        int j=0;
        printf("please input the string: 
");
        scanf("%s",str); 
        j=strlen(str);//得到的结果是存储的长度 
        sizeof(str);//得到的结果是30
        if(type1(str,j))
        {
            unsigned int i=a2ul(str);
            //printf("%d",i);
            printf("0~%d之间的素数有:
",i);
            int a=0,b=0;
            for(;a<i+1;a++){
                if(isPrime(a)){
                    printf("%d ",a);
                    b++;
                }
            }
            printf("
共有%d个",b);
            
        }else
        {
            //continue;
        }
        puts("
1:继续
2:结束");
        while(scanf("%c",&m)&&m!='1'&&m!='2')
        {
            puts("选项无效!
请重新输入。");
            puts("
1:继续
2:结束");
        }
        
    }   
    return 0;
}
bool type1(char N1[],int N2)
{
     for(int i=0;i<N2;i++)
        if(N1[i]>='0'&&N1[i]<='9') 
        {
            //continue;
            //cout<<"该字符为数字"<<endl;
        }else
        {
            printf("%c 不是数字!请重新输入:",N1[i]);
            return false;
        }
     return true;    
}
unsigned int a2ul(char *s)
{
    unsigned long n;
    for(n=0;*s;s++)
    {
        n=n*10+*s-'0';
    }
    return n;
}
bool isPrime(unsigned int N)//判断是否为素数 
{
    if (N == 1)
        return false;
    if (N % 2 == 0)
        return false;
 
    for (int i = 3; i*i <= N; i += 2)
    {
        if (N % i == 0)
            return false;
    }
    return true;
}
/*数组转数字 
原文地址:https://www.cnblogs.com/Babylon/p/8022475.html