当前位置: 代码迷 >> C语言 >> [原创](求助)帮找出此程序的逻辑错误.
  详细解决方案

[原创](求助)帮找出此程序的逻辑错误.

热度:203   发布时间:2006-03-07 23:01:00.0
[原创](求助)帮找出此程序的逻辑错误.

本人在将一VB代码翻成C程序时遇到的问题:
以下是VB代码:
Private Function prime(num As Integer) As Boolean
Dim i As Integer
For i = 2 To num
If num Mod i = 0 Then
Exit For
End If
Next i
If i = num Then
prime = True
Else
prime = False
End If
End Function

Private Sub Command1_Click()
Dim n As Integer
Dim p As Integer, q As Integer
Dim num As Integer
num = Val(Text1.Text)
If num Mod 2 <> 0 Then
MsgBox "您输入的不是偶数,请重新输入", vbOKOnly, "display"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Exit Sub
End If
For p = 2 To num
If prime(p) = True Then
q = num - p
If prime(q) = True Then
Print
Print " "; CStr(num) & "=" & CStr(p) & "+"; CStr(q)
Exit For
End If
End If
Next p
End Sub
运行正确并能得到正确结果.
但是此C语言程序.运行正确,但得不到理想的结果
#include "stdafx.h"
#define TRUE 1
#define FALSE 0
int prime(int num)
{
int i,k;
for(i=2;i<=num;i++)
{
if(num%i==0)
{
k=i;
i+=num;
}
}
if(k=num)
return TRUE;
else
return FALSE;
}

int main(int argc, char* argv[])
{
int p,q,num;
printf("please input a oushu:\n");
scanf("%i",&num);
if(num%2!=0)
printf("the num isn't oushu ,please retry\n");
for(p=2;p<=num;p++)
{
if(prime(p)==TRUE)
q=num-p;
else if(prime(q)==TRUE)
printf("%i=%i+%i\n",num,p,q);
}
printf("Hello World!\n");
return 0;
}

求高手指点迷津

搜索更多相关的解决方案: 逻辑  

----------------解决方案--------------------------------------------------------

#define TRUE 1
#define FALSE 0
int prime(int num)
{
int i;
for(i=2;i<=num&&num%i!=0;i++)
;
if(i==num)
return TRUE;
else
return FALSE;
}

int main(int argc, char* argv[])
{
int p,q,num;

do
{
printf("please input a oushu:\n");
scanf("%d",&num);
}while(num%2!=0);

for(p=2;p<=num/2;p++)
{
if(prime(p)==TRUE)
{
q=num-p;
if(prime(q)==TRUE)
printf("%i=%i+%i\n",num,p,q);
}
}
printf("Hello World!\n");
getch();

return 0;
}

你程序问题比较多

[此贴子已经被作者于2006-3-7 23:46:30编辑过]


----------------解决方案--------------------------------------------------------
楼上有点缺点,就是会出现些重复的,楼主修改下吧
----------------解决方案--------------------------------------------------------
  相关解决方案