2014-09-24 49 views
0

嘿,我一直在试图弄清楚这个代码中的错误我应该问用户一个正整数,然后在每一行上列出第一个emirps 5 ......米只是扁平卡住在这一点..感谢在Prime Emirp C++中需要帮助查找错误

#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"); 
+3

缩进符合真正的目的,最好独立开发新功能。 – Beta 2014-09-24 05:41:12

+0

您提供的代码将不会编译,因为反向函数最终会丢失。 – InvincibleWolf 2014-09-24 05:41:34

+0

它给我的两个错误来自 int prime = value; //将初始值存储在新变量中int emirp = value2; //////说未使用的变量 – xGrips 2014-09-24 05:54:05

回答

1

请花点时间来正确描述你的问题。我的心理能力告诉我,用户输入一个数字,然后程序将打印所有素数,直到这个数字与数字相反。 (有些喜欢说双关语的人选择了叫质数与颠倒数字的反素数。)

嘿,我一直在试图找出错误在此代码...

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

#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与调试器来了解变量是如何改变德,什么程序实际上做步骤;
  • 如何循环和范围块(花括号)工作;
  • 什么时候声明新的变量和whan使用现有的变量; (你会希望后者比你想象的更频繁);
  • 更好地组织你的程序,它会帮助你发现逻辑错误。

至于你手头的问题:有很多资源用于测试素数和颠倒SO数字的数字,这不应该很难找到。

+0

非常感谢您的意见,您似乎知道很多,对我非常有帮助。 – xGrips 2014-09-24 06:18:43