c++ - 在c ++中使用运算符重载检查数字是否为素数
问题描述
如何返回中的flag
值operator++(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;
}
解决方案
推荐阅读
- tsql - 尝试使用 MAX 列解决方案将多行 ROLL-UP 成单行但不起作用
- odata - 如何使用 DataServiceContext 调用接受实体引用参数的未绑定操作?
- r - 识别截断的字符串并扩展到最长的字符串
- ruby - 如何使用 Arel::Nodes::NamedFunction 定义带有 SEPARATOR 选项的 GROUP_CONCAT
- javascript - c3.js | 将 x 轴刻度更改为年份格式
- python - Python:从 Postgres 保存数据集以供上次加载
- excel - 为什么最后一行给我一个 0 个答案
- sql - 返回具有滚动总和的行在 SQL Server 中达到所需的值
- ios - 如何在 xamarin 中让第一个响应者触摸滚动视图
- python - 如何从自然排序中获取索引?