主题:[讨论]怎样把这段C程序代码改成VB的
天天学习
[专家分:4570] 发布于 2009-10-09 12:06:00
#include <stdio.h>
int a=10000,b,c=2800,d,e,f[2801],g;
main() {
for(;b-c;f[b++]=a/5);
for(;d=0,g=c*2;c -=14,printf("%.4d",e+d/a),e=d%a)
for(b=c; d+=f[b]*a,f[b]=d%--g,d/=g--,--b; d*=b);
}
我改了一下,运行结果不一致。C语言确实是很经典的。
Sub main()
Dim a As Long
Dim b As Long
Dim c As Long
Dim d As Long
Dim e As Long
Dim f(2800) As Long
Dim g As Long
a = 10000
b = 0
c = 2800
d = 0
e = 0
g = 0
While (b - c)
f(b) = a / 5
b = b + 1
Wend
d = 0
g = c * 2
While (g)
b = c
d = d + f(b) * a
g = g - 1
f(b) = d Mod g
d = d / g
g = g - 1
b = b - 1
While (b)
d = d * b
d = d + f(b) * a
g = g - 1
f(b) = d Mod g
d = d / g
g = g - 1
b = b - 1
Wend
c = c - 14
Debug.Print Format$(e + d / a, "####"); '//? printf("%.4d") ??
'Debug.Print e + d / a;
e = d Mod a
d = 0
g = c * 2
Wend
End Sub
回复列表 (共7个回复)
沙发
tanchuhan [专家分:15140] 发布于 2009-10-09 13:00:00
在我的vc++ 2008上运行直接报:
Run-Time Check Failure #3 - The variable 'b' is being used without being initialized.
变量b未初始化就使用,这样的代码也叫经典?
板凳
天天学习 [专家分:4570] 发布于 2009-10-09 13:40:00
[quote]在我的vc++ 2008上运行直接报:
Run-Time Check Failure #3 - The variable 'b' is being used without being initialized.
变量b未初始化就使用,这样的代码也叫经典?[/quote]
在VC++6里没有报错。不过确实存在“变量未初始化就使用”的潜在bug.
我是说C语言经典,不是说这段代码经典。。
3 楼
tanchuhan [专家分:15140] 发布于 2009-10-09 14:15:00
格式化:
[code=c]
void fn()
{
int a = 10000;
int b = 0;
int c = 2800;
int d = 0;
int e = 0;
int f[2801];
int g = 0;
for (; b - c; f[b++] = a / 5);
for (; d = 0, g = c * 2; c -= 14, printf("%.4d", e + d / a), e = d % a)
for (b = c; d += f[b] * a, f[b] = d % --g, d /= g--, --b; d *= b);
}
[/code]
展开:
[code=c]
void fn2()
{
const int a = 10000;
const int array_size = 2801;
int f[array_size];
for (int b = 0; b < array_size; b++)
f[b] = a / 5;
int c = array_size - 1;
int e = 0;
while (true)
{
int d = 0;
int g = c * 2;
if (g == 0)
break;
int b = c;
while (true)
{
d += f[b] * a;
f[b] = d % --g;
d /= g--;
--b;
if (b == 0)
break;
d *= b;
}
c -= 14;
printf("%.4d", e + d / a);
e = d % a;
}
}
[/code]
转换为VB:
[code=c]
Sub Main()
Const a As Long = 10000
Const array_ubound As Long = 2800
Dim ret As String
Dim b As Long, c As Long, d As Long, e As Long, g As Long
Dim f(array_ubound) As Long
For b = 0 To array_ubound
f(b) = a \ 5
Next
c = array_ubound
Do
g = c * 2
If g = 0 Then Exit Do
b = c
Do
d = d + f(b) * a
g = g - 1
f(b) = d Mod g
d = d \ g
g = g - 1
b = b - 1
If b = 0 Then Exit Do
d = d * b
Loop
c = c - 14
ret = ret & Format(e + d \ a, "0000")
e = d Mod a
Loop
Debug.Print ret
MsgBox ret
End Sub
[/code]
结果:
[quote]
31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185
[/quote]
4 楼
tanchuhan [专家分:15140] 发布于 2009-10-09 14:27:00
[quote][quote]在我的vc++ 2008上运行直接报:
Run-Time Check Failure #3 - The variable 'b' is being used without being initialized.
变量b未初始化就使用,这样的代码也叫经典?[/quote]
在VC++6里没有报错。不过确实存在“变量未初始化就使用”的潜在bug.
我是说C语言经典,不是说这段代码经典。。[/quote]
没报错更加体现了VC6的诸多问题
在debug模式下,VC会把变量初始化为0xCCCCCCCC,要是不报错就能运行,结果肯定莫名其妙.
在release模式下,变量b的值是堆栈里分配的4BYTE内存空间的过去值,也就是说是随机不可靠的.
当然,各个编译器对此的表现不同,听说gcc会把其初始化为0.
但C标准并未对此定义,所以标准正确的做法是自己手工初始化,而不是像VB里一样期望编译器会自动帮你初始化为0.
5 楼
bcahzvip [专家分:6040] 发布于 2009-10-09 14:55:00
While (g)
b = c
d = d + f(b) * a
g = g - 1
f(b) = d Mod g
d = d \ g
g = g - 1
b = b - 1
While (b)
d = d * b
d = d + f(b) * a
g = g - 1
f(b) = d Mod g
d = d \ g
g = g - 1
b = b - 1
Wend
c = c - 14
Debug.Print Format$(e + d \ a, "0000"); '//? printf("%.4d") ??
'Debug.Print e + d / a;
e = d Mod a
d = 0
g = c * 2
Wend
6 楼
天天学习 [专家分:4570] 发布于 2009-10-09 15:09:00
[quote]While (g)
b = c
d = d + f(b) * a
g = g - 1
f(b) = d Mod g
d = d [color=red]\ [/color]g
g = g - 1
b = b - 1
While (b)
d = d * b
d = d + f(b) * a
g = g - 1
f(b) = d Mod g
d = d[color=red] \ [/color]g
g = g - 1
b = b - 1
Wend
c = c - 14
Debug.Print Format$(e + d [color=red] \ [/color] a, [color=green]"0000"[/color]); '//? printf("%.4d") ??
'Debug.Print e + d / a;
e = d Mod a
d = 0
g = c * 2
Wend
[/quote]
7 楼
wwc7654321 [专家分:1590] 发布于 2009-10-22 21:02:00
[quote]
#include <stdio.h>
int a=10000,b,c=2800,d,e,f[2801],g;
main() {
for(;b-c;f[b++]=a/5);
for(;d=0,g=c*2;c -=14,printf("%.4d",e+d/a),e=d%a)
for(b=c; d+=f[b]*a,f[b]=d%--g,d/=g--,--b; d*=b);
}
[/quote]
眼花缭乱了...
代码真的有必要写成这样吗?(可以提高效率?)
我来回复