简体   繁体   English

需要帮助在Prime Emirp C ++中发现错误

[英]Need help finding error in Prime Emirp C++

Hey I've been trying to figure out the error in this code Im supposed to ask the user for a positive integer then pint out the first emirps 5 on each line... I'm just flat out stuck at this point ..thanks 嘿,我一直在尝试找出此代码中的错误,我想问用户一个正整数,然后在每行上画出第一个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");

Please take some time to describe your problem properly. 请花一些时间正确描述您的问题。 My psychic powers tell me that the user enters a number and the program then will print all prime number up to this number with the digits reversed. 我的灵性告诉我,用户输入一个数字,然后程序将打印所有素数,直到该数字以相反的数字显示。 (Some punster chose to call a prime number with reversed digits an Emirp.) (某些punster选择将带有反转数字的素数称为Emirp。)

Hey I've been trying to figure out the error in this code ... 嘿,我一直试图找出这段代码中的错误...

Hey, there's not one single error here. 嘿,这里没有一个错误。 The code is strewn with errors! 该代码散布着错误!

  #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.

} }

Please, learn: 请学习:

  • how to step through a prigram with a debugger to learn how te variables change and what a program actually does; 如何与调试器一起完成prigram,以了解变量如何变化以及程序实际执行的操作;
  • how loops and scope blocks (in curly braces) work; 循环和作用域块(花括号)如何工作;
  • when to declare new variables and whan to use existing variables; 何时声明新变量并希望使用现有变量; (You'll want the latter more often than you think); (比起您想的,您会经常需要后者);
  • to organise your program better, it will help you to spot logical errors. 为了更好地组织程序,它将帮助您发现逻辑错误。

As to your problems at hand: There are plenty of resources for testing for prime numbers and reversing the digits of a number on SO, which shouldn't be hard to find. 关于您眼前的问题:有很多资源可用于测试素数和反转SO上的数字,这并不难找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM