需要帮助在Prime Emirp C ++中找到错误

时间:2014-09-24 05:35:51

标签: c++ function reverse

嘿我一直试图弄清楚这段代码中的错误我应该问用户一个正整数然后在每一行上找出第一个emirps 5 ...我只是平了坚持到这一点..谢谢

  #include <iostream>
   using namespace std;
  int isPrime(int value); //Prototyle for "prime number function"
  int reverse (int value2); //Prototype for "emirp function"

  int main()
 {

//Ask the user for a positive number

cout << "Please enter a positive number: ";
int n;
cin >> n;

//Reject negative value input
if ( n < 1)
{
    cout << "INVALID NUMBER \n";
}
else

    //Calculate all emirps up to 'n'.
    for (int test = 0; test < n; test++)
    {
        int number = 2;

        if (isPrime(number))
        {
            cout << "\n" << reverse(number) << "\t\t\t";
        }
    }

return 0;
  }

  int isPrime(int value)
 {
//If value is prime, the remainder (count) will be zero twice--for 1 and itself.
int divisor = 1;
int count = 0;
int prime = 0;
if (value % divisor == 0)
{
    count++;
    ++divisor;
}
if ((count = 2))
{
    int prime = value; //store prime value in new variable
}

return prime;
}


int reverse(int value2)
{
//reverse the number
value2*=10;
value2 = value2 %10;
value2/=10;

//same procedure as prime function
int divisor2 = 1;
int count2 = 0;
int emirp = 0;
if (value2 % divisor2 == 0)
{//if

        count2++;
        ++divisor2;
    }
    if ((count2 = 2))
    {
        int emirp = value2;
    }
return emirp;

system ("pause");

1 个答案:

答案 0 :(得分:1)

请花点时间正确描述您的问题。我的通灵能力告诉我,用户输入一个数字然后程序将打印所有素数到这个数字,数字反转。 (有些庞斯特选择用一个反转数字称为主数字的Emirp。)

  

嘿,我一直试图弄清楚这段代码中的错误......

嘿,这里没有一个错误。代码散布着错误!

  #include <iostream>
   using namespace std;
  int isPrime(int value);
  int reverse (int value2);

  int main()
 {

cout << "Please enter a positive number: ";
int n;
cin >> n;

if ( n < 1)
{
    cout << "INVALID NUMBER \n";
}
else

    //Calculate all emirps up to 'n'.
    for (int test = 0; test < n; test++)   ## No need to test 0 or 1 for primality
    {
        int number = 2;

        if (isPrime(number))   ## You always test whether 2 is a prime here
        {
            cout << "\n" << reverse(number) << "\t\t\t";
        }
    }

return 0;
  }

  int isPrime(int value)
 {
//If value is prime, the remainder (count) will be zero twice--for 1 and itself.
int divisor = 1;
int count = 0;
int prime = 0;       ## (A)
if (value % divisor == 0)
{
    count++;         ## This statelment will be executed at most once
                     ## You should count divisors in a loop
    ++divisor;       ## Here, divisor is incremented, but never used again
                     ## Also, if this were inside a loop, you should increment
                     ## the divisor unconsitionally. The condition affects
                     ## only the count.
}
if ((count = 2))     ## This will set count to 2 and always evaluate to true
{
    int prime = value;   ## This variable will shadow the "prime" variable 
                         ## decralered at (A). This variable will cease to exist
                         ## as soon as the block closes, i.e. immedietely.
                         ## You just want "prime = 1", without the "int", which
                         ## declares a new variable.
}

return prime;       ## This prime is tze one declared at (A) and will be 0.
}


int reverse(int value2)
{
value2*=10;
value2 = value2 %10;    ## The remainder of a division by 10 of a number that
                        ## you have just multiplied by 10 is 0, rendering pretty
                        ## much the rest of the function useless ...
value2/=10;

int divisor2 = 1;       ## ... which doesn't hurt, because the rest of the
                        ## function was useless to start with. Reversing the
                        ## digits of a number isn't at all like finding a prime.
int count2 = 0;
int emirp = 0;
if (value2 % divisor2 == 0)
{

        count2++;
        ++divisor2;
    }
    if ((count2 = 2))
    {
        int emirp = value2;
    }
return emirp;

system ("pause");      ## First, this statement comes directly after a terurn, 
                       ## so it will never be reached. Second, if anywhere, it
                       ## should go into the main routine. You don't want the 
                       ## user to press a key before printing each number.

}

请学习:

  • 如何使用调试器逐步执行prigram以了解te变量如何变化以及程序实际执行的操作;
  • 循环和范围块(花括号中)如何工作;
  • 何时声明新变量并使用现有变量; (你会比你想象的更频繁地想要后者);
  • 更好地组织您的程序,它将帮助您发现逻辑错误。

关于你手头的问题:有很多资源可用于测试素数并在SO上反转数字的数字,这应该不难找到。

相关问题