Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何让程序根据输入验证读取素数? #包括 #包括 #包括 #包括 使用名称空间std; bool-isPrime(int-num); int main() { int-num; 流输出文件; outputFile.open(“PrimeOut.txt”);_C++_Loops_Validation_Numbers - Fatal编程技术网

C++ 如何让程序根据输入验证读取素数? #包括 #包括 #包括 #包括 使用名称空间std; bool-isPrime(int-num); int main() { int-num; 流输出文件; outputFile.open(“PrimeOut.txt”);

C++ 如何让程序根据输入验证读取素数? #包括 #包括 #包括 #包括 使用名称空间std; bool-isPrime(int-num); int main() { int-num; 流输出文件; outputFile.open(“PrimeOut.txt”);,c++,loops,validation,numbers,C++,Loops,Validation,Numbers,//检查文件是否打开 如果(输出文件) { 我不能肯定你的家庭作业 如果您查找素数上的代码段,那么您将在这里找到SO bool isPrime(整数) { 如果((!(数字和1))&&number!=2)| |(数字

//检查文件是否打开 如果(输出文件) {
我不能肯定你的家庭作业

如果您查找素数上的代码段,那么您将在这里找到SO

bool isPrime(整数)
{
如果((!(数字和1))&&number!=2)| |(数字<2)| |(数字%3==0&&number!=3))
返回(假);
对于(整数k=1;36*k*k-12*k
有了这个(蛮力算法),你可以发现,如果一个数字是素数(对于小数字)

<>你的程序非常的不安全,基本上是不安全的。你应该通过几个C++书籍仔细学习。然后你需要阅读你自己的代码并看到问题。


你的帖子不适合这样做。

我投票将这个问题作为离题题结束,因为OP为一个明显的作业练习提供了一个基本的代码结构,但没有努力解决作业的实质内容(在这种情况下,检测整数值是否为素数的逻辑)希望其他人能完成作业。所以作业的重点是我在运行一个程序,在这个程序中我可以打印出从1到输入数字范围内的素数。我只是想知道如何让代码读取素数
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
bool isPrime(int num);

int main()
{
   int num;
   ofstream outputFile;

   outputFile.open("PrimeOut.txt");
   while (num < 1 && num > 100)
   {
      cout << "The number must be between 1 and 100" << endl;
   }


   if(isPrime(num) == true)
   {
      cout << num << endl;
   }
    return 0;
}
// Function for finding prime numbers
bool isPrime(int num)
{
   bool valPrime;
   for (int i = 2; i < num; i++)
   {
      valPrime = true;
   }
   return valPrime;
}
bool isPrime( int number )
{
    if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) )
        return (false);

    for( int k = 1; 36*k*k-12*k < number;++k)
        if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) )
            return (false);
    return true;
}