从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

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...