补充实验一常量、变量、常用函数与表达式[实验目标]
·正确书写不同类型的常量;
·掌握变量的赋值及使用方法;
·熟练掌握常用函数的用法;
·根据要求正确书写表达式。
[实验内容]
·常量的类型;
·变量的操作;
·常用函数;
·表达式的构建。
[实验环境]
本次实验的全部实验内容均要求在VBE的立即窗口中进行。
[方法分析与操作步骤]
1,常量
(1)数值型
? 100
? 1.45e3
? 1.45e-2
(2)字符型
? ”100”
? “a1b0c0”
? “abcd”
(3)逻辑型
? True
? False
(4)日期型
? # 06/20/12 #
? #2012/06/18#
? # 06-20-12 #
? # 2012-06-18 #
? #2012/06/18 10:32 #
? #2012/06/18 10:32 pm#
2.变量
nVar_x = 234. 5
cVar_y = “abc123”
? “nVar_ x=”, nVar_ x
? “cVar_y = “, cVar_y
? “nVar_ x=”& nVar_ x
? “cVar_y = “& cVar_y
3.函数
(l)数学函数
①abs();
? abs(36.9)
? abs( - 36.9)
②int();
? int(36.9)
? int( - 36.9)
? int(36.3)
? int( - 36.3)
③fix();
? fix(36.9)
? fix( - 36.9)
? fix(36.3)
? fix( - 36.3)
④sqr();
? sqr(9)
? sqr(3)
? sqr(0)
? sqr( -9) ‘显示出错提示框
⑤sin()、cos()、tan();
? sin(60/180*3.14) ‘计算60°角的正弦值
? cos(90/180*3.14) ‘计算90°角的余弦值
? tan(45/180*3.14) ‘计算45°角的正切值
⑥rnd();
? rnd() ’产生O~l之间的随机数
? rnd
? rnd(0) ‘产生最近生成的随机数
? int(100*rnd) ‘产生[0,99]的随机整数
? int(101*rnd) ‘产生[0,100]的随机整数
? int(100*rnd+1) ‘产生[1,100]的随机整数
? int(100 + 200*rnd) ‘产生[100,299]的随机整数
(2)字符串函数
①Instr();
? instr (“access”, ” e” )
? instr ( “access” , “E” )
? instr (1, “access” , “E” , 1)
? instr ( “access”, “s”)
? InStr (3,”aSsiAB”,”a”,1) ‘返回5(从字符S开始,检索出字符A,不区分大小写)
②len();
? len(”南京财大”)
? len(”中文Access”)
? len(“2500”)
③left( ), right( ), mid( ) ;
? left(”祖国伟大”,1)
? left(”祖国伟大”,2
? left(“hello”,2)
? left(“hello”, 4)
? right(”祖国伟大”,2)
? right(”祖国伟大”,3)
? right(“hello” ,2)
? right(“hello” , 4)
? mid(”南京财经大学”,2,2)
④space();
? ”南京财大” + ”Access”
? “南京财大” + space(6) + “Access”
? “南京财大” + space(0) + “Access”
? len(space(0))
? len(space(10))
⑤Ucase( ), Lcase( );
? Ucase(”AbcD”) ‘返回“ABCD”
? Lcase(“AbcD”) ‘返回“abcd”
⑥Ltrim( ), .Rtrim( ), .Trim( ).
cstrspace = space(2) +”江苏省南京市”+ space(3) ‘每个汉字之间有一个空格? cstrspace
? len(cstrspace)
? len(trim(cstrspace))
? len(rtrim(cstrspace))
? ltrim(cstrspace)
? len(ltrim(cstrspace))
(3) 日期时间函数
①date();
? date( )
②time();
? time()
③now();
? now()
④year();
? year(date())
⑤month();
? month(date ())
⑥day();
? day(date())
⑦weekday( ) ;
? weekday(date())
? Weekday (date() , vbMonday)
? Hour(time( ))
? Minute(time( )
? Second(time( )
⑧DateSerial ( ).
? DateSerial(1993, 11i,11) ‘返回日期# 1993 - 11 - 11#
? DateSerial(1990 – 10, 8 - 2, 1- 1) ‘返回日期# 1980 – 5 -31#
? DateSerial(1990,13, 35) ‘返回日期# 1991 – 2 – 4#
(4)数据类型转换函数
①asc();
? asc(“a”)
? asc(“A”)
? asc(“Abc”)
? asc(“江苏省”)
②chr();
? chr(66)
? chr(98)
? chr(asc(“a”) + 3)
③str();
? str( - 80)
? str(56)
④val()。
? Val(“20”) ‘返回20
? Val(“345”) ‘返回345
? Val(“78af20”) ‘返回78
(5)其他类型常用函数
①iif();
x=4
? iif(x>5,x- 5,x+ 5)
X=6
? iif(x>5,x- 5,x+ 5)
②Switch();
Score = 85
? Switch(Score<60, ”不及格”,Score<85,”及格”,Score< = 100,”良好”)
③Choose();
? Choose(weekday(date(),”星期日”,”星期一”,”星期二”,”星期三”,”星期四”,
”星期五”,”星期六”)
④Inputbox( ) ;
? inputbox(“请输入考试分数”,”成绩录入框”)
⑤MsgBox().
? MsgBox(“打开窗体成功!”,Vblnformation,”提示”)
? MsgBox(“确认要删除数据吗?”,Vbyesno + vbQuestion,”确认”)
? MsgBox(”选择无效,请重选!”,VbExclamation,”警告”)
4.表达式
(1)算术表达式
r=5
?3.14*r^2
? 2*3.14* r
X= - 100.34
? int(sqr(abs(x))) * fix(x)
(2)字符表达式
? “x=” + str(x)
x= 456
? “x=” + str(x)
? ”X =“ + ltrim(str(x))
? “123” + ”12”
? “123” & “12”
? ”123” + 12
? “123” & 12
(3)日期时间表达式
? date() + 100
? date() – 100
? date() - #2000 -1-1#
? time()
?now()
? now() + 10
? Now() – 10
(4)关系表达式
? 5>3
? 5<3
? 5>=3
? 5=3
?5<>3
? ”a” = “A”
? ”大” > “小”
? “A” > “b”
? “Tom” > “Jerry”
? date() > # 2000 – 1 – 1#
(5) 逻辑表达式
闰年的判断标准是:年份能被4整除,但不能被100整除;或者能被400整除. nyear = year(date())
? nyear mod 4 = 0 and nyear mod 100 <> 0 0r nyear mod 400 = 0。