感谢你昨晚的帮助,我能够让我的程序正确计算我的输入,但现在我无法正确格式化输出。这就是问题所在:
我的程序只应在带有素数的行上打印“is prime”。但它打印在这样的线上:
http://oi42.tinypic.com/30igbvq.jpg
我不能为我的生活弄清楚为什么这样做,我的所有功能都应该有效。
Stack我再次需要你的帮助!
#include <iostream>
using namespace std;
void primecheck(int x); // proto
void countr(int rnm, int lnm); // proto
void prime(int x) // this function finds the prime factors of a number.
{
int lastr = 2;
int count = 0;
while (lastr < x)
{
if (x % lastr == 0)
{
cout << x << " " << lastr << "*";
x /= lastr;
}
else
++lastr;
}
primecheck(x); // calls to check if number is prime, "Is prime"
}
void console(int rnum, int lnum) // this prompts the user for two numbers then stores the answers
{
cout << "please enter two numbers ";
cin >> rnum;
cin >> lnum;
countr(rnum, lnum);
}
void countr(int rnm, int lnm) // this function loops the prime function until all the numbers are computed
{
int i = rnm;
do{
prime(i);
i++;
} while (i <= lnm);
return;
}
int main() // main, calls console then pauses when finished
{
int e = 0;
int r = 0;
console(e, r);
system("PAUSE");
}
void primecheck(int x) // checks to see if then number is prime. if counter is equal to 2 than number is prime.
{
int counting = 0;
for (int a = 1; a <= x; a++)
{
if (x %a == 0)
{
counting++;
}
}
if (counting == 2)
{
cout << x << " is prime " << endl;
}
else
{
cout << x << endl;
}
}
答案 0 :(得分:2)
您在/=
中使用prime()
运算符。这是一个赋值运算符,修改 x
的值,使得x
在调用primecheck()
时始终为素数。