整型长整型和除法
我只是编写一个将无符号整数分解为素数的过程。如果我将数据类型定义为“int”,它会正常工作,如果我将其更改为“long”,结果将是错误的。我不知道为什么。
顺便说一句,我使用 Win-TC 作为我的编译器。
代码如下:
#include "stdio.h"
#define True 0xff
#define False 0x00
char DividerIsPrime(unsigned long data);
void CheckIfDataCanBeExtracted(unsigned long data);
main()
{
unsigned long data;
printf("please input data:");
scanf("%d",&data);
printf("\n%d=",data);
CheckIfDataCanBeExtracted(data);
// printf("%d",sizeof(short));
getch();
}
void CheckIfDataCanBeExtracted(unsigned long data)
{
unsigned long divider,temp,data1;
data1=data;
for(divider=2;divider<=data;divider++)
{
temp=data1%divider;
if(temp) {continue; }
if(DividerIsPrime(divider)) {
data1 = data1/divider;
printf("%d",divider);
if(data1==1) break;
else {printf("*"); divider--;}
}
}
return;
}
/* Check if this number is a prime number */
char DividerIsPrime(unsigned long data)
{
unsigned long divider;
char status=True;
for(divider=2;divider<data;divider++)
{
if(data%divider) status=True;
else status=False;
}
return status;
}
感谢Paul的帮助,我知道哪里错了。 %d 应替换为 %ld。
I just write a procedure to decompose an unsigned integer to prime numbers. it will work normally if I define the data type as "int", if I change it to "long", result will be wrong. I don't know why.
BTW, I used Win-TC as my compiler.
Code as below:
#include "stdio.h"
#define True 0xff
#define False 0x00
char DividerIsPrime(unsigned long data);
void CheckIfDataCanBeExtracted(unsigned long data);
main()
{
unsigned long data;
printf("please input data:");
scanf("%d",&data);
printf("\n%d=",data);
CheckIfDataCanBeExtracted(data);
// printf("%d",sizeof(short));
getch();
}
void CheckIfDataCanBeExtracted(unsigned long data)
{
unsigned long divider,temp,data1;
data1=data;
for(divider=2;divider<=data;divider++)
{
temp=data1%divider;
if(temp) {continue; }
if(DividerIsPrime(divider)) {
data1 = data1/divider;
printf("%d",divider);
if(data1==1) break;
else {printf("*"); divider--;}
}
}
return;
}
/* Check if this number is a prime number */
char DividerIsPrime(unsigned long data)
{
unsigned long divider;
char status=True;
for(divider=2;divider<data;divider++)
{
if(data%divider) status=True;
else status=False;
}
return status;
}
Thanks for Paul's help, I know where is wrong. %d should be replaced by %ld.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当前编写的函数
DividerIsPrime
存在缺陷,从逻辑上讲它必须始终返回True
。原因是状态在每次迭代时都会改变。即使达到
status=False
(该数字是合数,因为除法器的模数为零),迭代仍将继续,并且在每种情况下,status=True
当divider == (data - 1)时,将在最终迭代中达到。您可以按如下方式更改此设置:
您可能会通过一些“单元测试”发现这一点,例如:
显然,对于“素性测试”有更有效的算法。
Your function
DividerIsPrime
,as currently written, has the defect that logically it must always returnTrue
.The reason for this is that status is changed at each iteration. Even if
status=False
is reached (the number is composite because the modulus came out zero for a divider), then the iterations will continue and in every case,status=True
will be reached on the final iteration when divider == (data - 1).You can change this as follows:
You would have found this with some "unit test" such as:
Obviously there are much more efficient algorithms for "primality testing".