首页 > 解决方案 > 在c ++中使用运算符重载检查数字是否为素数

问题描述

如何返回中的flagoperator++(int)?为了使程序没有错误,我不得不返回p. 但这不是我的输出。如果我可以返回该flag值,则检查flag==0可能会很好。我也怀疑我是否把所有的语法都写得很好。在这种情况下甚至可以使用运算符重载吗?我想要prime operator++(prime y); 传递一个值和素数运算符 ++(int z ) 以捕获该值并计算标志值。

#include <iostream>
using namespace std;

class prime
{
    private:
        int i , ch ,ans ,j ,flag=0 , y , z; 
    public:
        prime()
        {
            cout <<"1. Check a no to be prime\n";
            cout <<"2. Prime no <100\n";
            cout <<"Enter your choice\n";
            cin >>ch;
            do
            {
                switch(ch)
                {
                    case 1:
                    {
                        int x=45;
                        x=y;
                        prime operator++(prime y);
                        if(flag==2)
                            cout<<"the given no. "<<y<<" is a prime no."<<endl;
                        else
                            cout<<"the given no. "<<y<<" is not a prime no."<<endl;
                        break;
                    }
                    case 2:
                    {
                        cout<<"the primes numbers below 100 are"<<endl;
                        for(i=1;i<100;i++)
                        {
                            i=y;
                            prime operator++(prime y);
                        }
                        if(flag==2)
                            cout<<i ;
                        break;
                    }
                    default:
                    {
                        cout<<"wrong choice"<<endl;
                    }
                    cout<<"press 0 to continue or 1 to exit."<<endl; 
                    cin>>ans;   
                }  // end of switch
            }while(ans==0);// end of do while
        }// end of prime constructor

        prime operator++(int z)
        { 
            prime p;
            for(j=1;j<=p.z;j++)
            {
                if(p.z%i==0)
                    flag++;
            }
            return p;
        }// end of prime operator function;
};//end of class prime

int main()
{
    prime op;
    op++;
    return 0;
}

图片

标签: c++numbersreturnoperator-overloadingprimes

解决方案


推荐阅读