微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从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

原文地址:https://www.jb51.cc/vb/262932.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐