本人在将一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;
}
求高手指点迷津