判断一个数是完美数还是素数

发布于 2024-10-07 06:16:14 字数 547 浏览 8 评论 0原文

问题是: “编写一个函数来判断一个数是素数还是完全数。”

到目前为止,我已经首先完成了完美的部分,这就是我所拥有的:

#include <iostream>
using namespace std;
bool perfectNumber(int);
int main()
{
 int number;

 cout<<"Please enter number:\n";
 cin>>number;
 bool perfectNumber(number);

 return 0;
}
bool perfectNumber(int number)
{
 int i;

 int sum=0;
 for(i=1;i<=number/2;i++)
 {
  if(number%i==0)
  {
   sum+=i;
  }
 }
 if (sum==number)
  return i;
 else
  return 0;
}

但是,此代码似乎存在错误。 我看过这本书,但没有提到这个话题。 我想获得有关如何修复此代码的建议。

谢谢!

the problem is :
"Write a function to find out if a number is a prime or perfect number."

so far i have worked on the perfect part first and this is what i have:

#include <iostream>
using namespace std;
bool perfectNumber(int);
int main()
{
 int number;

 cout<<"Please enter number:\n";
 cin>>number;
 bool perfectNumber(number);

 return 0;
}
bool perfectNumber(int number)
{
 int i;

 int sum=0;
 for(i=1;i<=number/2;i++)
 {
  if(number%i==0)
  {
   sum+=i;
  }
 }
 if (sum==number)
  return i;
 else
  return 0;
}

HOWEVER, there seems to be errors on this code.
I have looked over the book but nothing talks about this topic.
i would like to get advice on how to fix this code.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜心 2024-10-14 06:16:14
bool perfectNumber(number);

这不会调用 perfectNumber 函数;它声明了一个名为 perfectNumber 且类型为 bool 的局部变量,并使用转换为 bool 类型的 number 值对其进行初始化。

为了调用 perfectNumber 函数,您需要使用以下内容:

bool result = perfectNumber(number);

或:

bool result(perfectNumber(number));

另请注意:如果您要从流中读取输入(例如 cin>> number),您必须检查以确保从流中提取值成功。现在,如果您输入 asdf,提取将会失败,并且 number 将保持未初始化状态。检查提取是否成功的最佳方法就是测试流的状态:

if (cin >> number) {
    bool result = perfectNumber(number);
}
else {
    // input operation failed; handle the error as appropriate
}

您可以在 basic_ios 上标志的语义。您还应该查阅一本优秀的入门级 C++ 书籍了解更多信息流使用最佳实践。

bool perfectNumber(number);

This does not call the perfectNumber function; it declares a local variable named perfectNumber of type bool and initializes it with the value of number converted to type bool.

In order to call the perfectNumber function, you need to use something along the lines of:

bool result = perfectNumber(number);

or:

bool result(perfectNumber(number));

On another note: if you are going to read input from a stream (e.g. cin>>number), you must check to be sure that the extraction of the value from the stream succeeded. As it is now, if you typed in asdf, the extraction would fail and number would be left uninitialized. The best way to check whether an extraction succeeds is simply to test the state of the stream:

if (cin >> number) {
    bool result = perfectNumber(number);
}
else {
    // input operation failed; handle the error as appropriate
}

You can learn more about how the stream error states are set and reset in Semantics of flags on basic_ios. You should also consult a good, introductory-level C++ book for more stream-use best practices.

迷荒 2024-10-14 06:16:14
void primenum(long double x) {
    bool prime = true; 
    int number2;
    number2 = (int) floor(sqrt(x));// Calculates the square-root of 'x'

    for (int i = 1; i <= x; i++) {
        for (int j = 2; j <= number2; j++) {
            if (i != j && i % j == 0) {
                prime = false;
                break;
            }
        }
        if (prime) {
            cout << " " << i << " ";
            c += 1;
        }
        prime = true;
    }
}
void primenum(long double x) {
    bool prime = true; 
    int number2;
    number2 = (int) floor(sqrt(x));// Calculates the square-root of 'x'

    for (int i = 1; i <= x; i++) {
        for (int j = 2; j <= number2; j++) {
            if (i != j && i % j == 0) {
                prime = false;
                break;
            }
        }
        if (prime) {
            cout << " " << i << " ";
            c += 1;
        }
        prime = true;
    }
}
尐偏执 2024-10-14 06:16:14
 bool isPerfect(  int number){
     int i;
     int sum=0;
     for(i=1;i<number ;i++){
         if(number %i == 0){
             cout<<"  " << i ;
             sum+=i;
         }
     }

     if (sum == number){
         cout<<"\n \t\t THIS NUMBER  >>>  "<<  number <<"   IS    PERFECT \n\n";
         return i;
     }else if (sum |= number) {
               cout<<"\nThis number >>> " <<  number <<"   IS  NOT  PERFECT \n\n";
               return 0;
     }
 }
 bool isPerfect(  int number){
     int i;
     int sum=0;
     for(i=1;i<number ;i++){
         if(number %i == 0){
             cout<<"  " << i ;
             sum+=i;
         }
     }

     if (sum == number){
         cout<<"\n \t\t THIS NUMBER  >>>  "<<  number <<"   IS    PERFECT \n\n";
         return i;
     }else if (sum |= number) {
               cout<<"\nThis number >>> " <<  number <<"   IS  NOT  PERFECT \n\n";
               return 0;
     }
 }
一人独醉 2024-10-14 06:16:14
#pragma hdrstop

#include <tchar.h>
#include <stdio.h>
#include <conio.h>
//---------------------------------------------------------------------------


bool is_prim(int nr)
{

    for (int i = 2; i < nr-1; i++) {

    if (nr%i==0) return false;

    }

    return true;

}

bool is_ptr(int nr)
{
    int sum=0;

    for (int i = 1; i < nr; i++) {

    if (nr%i==0) {
        sum=sum+i;
    }

    }

    if (sum==nr) { return true;

    }
    else return false;
}
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
    int numar;

    printf ("Number=");scanf("%d",&numar);
    if (is_prim(numar)==true) { printf("The number is prime");

    }
    else printf("The number is not prime");

    if (is_ptr(numar)==true) { printf(" The number is perfect");

    }
    else printf(" The number is not perfect");
    getch();
    return 0;
}
#pragma hdrstop

#include <tchar.h>
#include <stdio.h>
#include <conio.h>
//---------------------------------------------------------------------------


bool is_prim(int nr)
{

    for (int i = 2; i < nr-1; i++) {

    if (nr%i==0) return false;

    }

    return true;

}

bool is_ptr(int nr)
{
    int sum=0;

    for (int i = 1; i < nr; i++) {

    if (nr%i==0) {
        sum=sum+i;
    }

    }

    if (sum==nr) { return true;

    }
    else return false;
}
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
    int numar;

    printf ("Number=");scanf("%d",&numar);
    if (is_prim(numar)==true) { printf("The number is prime");

    }
    else printf("The number is not prime");

    if (is_ptr(numar)==true) { printf(" The number is perfect");

    }
    else printf(" The number is not perfect");
    getch();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文