从VB到C++转型第一步

VB语法上远宽松过C++,可读性强,学习容易.然而学习C++虽然一方面难度大,出错容易,可读性较差,但是代码效率高,具备汇编语言程序的优点,功能强悍,也可以使程序员思维更严密,处事更谨慎.下面的代码是语法上最常见的"不适应":

#include <iostream.h>
int min(int x,int y) //function min(x as integer,y as integer) as integer
{
int z;
if (x < y) //if x < y then
{z = x; //z = x
} //usually '{' and '}' are not exist in the same line.
else //else
{
z = y; //z = y
} //end if
return(z); //min = z
} //end function

int max(int x, int y) //this is a function, not a sentence
{
int z; //this is a sentence,must end with ';'
if (x>y) z=x;//also if (x > y) z = x ; space is not influenced
else z=y; //equals to else'/n'{z=y;} C++ is not so strict as C
return(z);
}

void main()
{
int a,b,m,n;
cout <<"please enter two integers:/n"; //"string" & 'chr' string is a collected of chr
cin >>a >>b;
m = max(a,b);
n = min(a,b);
cout <<"the maximum number is " <<m <<'/n';
cout <<"the minimum number is " <<n <<'/n';
}

对比下VB代码:

function min(x as integer,y as integer) as integer

dim z as integer

if x< y then 'if x < y then z = x else z = y

z = x

else

z = y

end if

end function

private sub command1_click

dim a as integer,b as integer

a = val(text1.text)

b = val(text2.text)

me.print "最小值为:" + str(min(a,b)) + "." 'VB无论字符还是字符串都是""

end sub

阅读更多
换一批

没有更多推荐了,返回首页