2014-09-24 47 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