我一直在尝试创建我自己的素数检查器函数,虽然奇怪的是当我调用 isPrime(7) 时它返回 1,这很好,但是当我调用 isPrime(9) 时它给我以下错误:
“Mathematics.exe”:已加载“C:\Documents and Settings\mbryant\My Documents\Visual Studio 2010\Projects\Mathematics\Debug\Mathematics.exe”,已加载符号。 “Mathematics.exe”:已加载“C:\WINDOWS\system32\ntdll.dll”,无法找到或打开 PDB 文件 “Mathematics.exe”:已加载“C:\WINDOWS\system32\kernel32.dll”,无法找到或打开 PDB 文件 “Mathematics.exe”:已加载“C:\WINDOWS\system32\msvcp100d.dll”,已加载符号。 “Mathematics.exe”:已加载“C:\WINDOWS\system32\msvcr100d.dll”,已加载符号。 线程“Win32 线程”(0x6ec) 已退出,代码为 -1073741510 (0xc000013a)。
程序“[6072] Mathematics.exe: Native”已退出,代码为 -1073741510 (0xc000013a)。
代码如下:
#include <iostream>
using namespace std;
bool isPrime(int x){
int b = 0;
int i = 2;
if(x == 2){
return 1;
}
if (x > 2){
while(i < x){
if ( (x % i) != 0){
b = b + 1;
i = i + 1;
}
}
if (b > 0){
return 1;
} if (b == 0){
return 0;
}
}
}
int main(){
cout << isPrime(9) << endl;
system("pause");
return 0;
}
帮助解决此问题将不胜感激。
最佳答案
根据:
Jobs failing on Windows with Exit Code 0xC000013A
Globally speaking, Exit Code 0xC000013A means that the application terminated as a result of a CTRL+C or closing command prompt window
我复制、编译并运行了您的代码。与 x=9
, 代码卡在 while
中永远循环,所以我不得不使用关闭按钮(右上角的 [x] 按钮)关闭程序。这生成了 0xc000013a 错误代码。 (使用 x=7
程序不会卡在 while
循环中,因此可以正常退出。)
更具体地说,对于 x=9
该程序卡在 while
中循环因为当i=3
然后 (x % i) == 0
(9 mod 3 = 0) 和语句 i = i + 1
从不执行。所以i
永远不会超过 3 和 i < x
(3 < 9) 始终为真。
所以直接的问题是您的代码永远不会退出(对于 x=9
)并且您必须停止它,大概是通过单击关闭按钮。但更大的问题是你的逻辑不好,你的程序没有按照你认为的方式工作。
例如,当x=9
和 i=2
, 然后 (x % i) != 0
这导致b = b + 1
.这意味着 b > 0
并且您的程序应该返回 1,您表示在 x=7
的情况下这意味着素数.但是 9 不是质数。
此外,isPrime
返回类型为 bool
但你回来了int
.
关于windows - native 退出,代码为 : -1073741510 (0xc000013a) while using prime checker function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45691954/