内容发布更新时间 : 2024/12/23 17:46:42星期一 下面是文章的全部内容请认真阅读。
1、编写事件过程Command1_Click,输入x后按下列公式计算并显示y的值。 y???log|x|?ex?522?(x?3)?(x?3)x??5 x?5Private Sub Command1_Click() '1分 Dim x As Single,y As Single '2分 x=Val(InputBox(\分 If x<-5 Then y=Log(Abs(x))*Exp(x+5) Else y=Sqr((x+3)^2+(x-3)^2) '4分 Print y '1分 End Sub
2、编写过程Command1_Click,输入10个数到整型数组a,显示每个元素各位数字之和(如a(1)为257则输出14,a(2)为-125则输出8)。要求将计算一个整数各位数字之和的运算写作自定义函数过程。
Private Function f(k As Integer) As Integer '首尾句1分 Dim i As Integer '声明、k赋初值2分 k = Abs(k)
Do while k <> 0 '2分 f = f + k Mod 10: k = k \\ 10 Loop End Funtion
Private Sub Command1_Click()
Dim a(10) As Integer, i As Integer '声明语句1分 For i = 1 To 10
a(i) = Val(InputBox(\输入语句2分 Next i
For i = 1 To n: Print f(a(i)): Next i '调用、输出2分 End Funtion
3、某文本文件以下列格式存储若干学生学号和两门课成绩,编写单击窗体的事件过程。要求:用通用对话框控件Commondialog1选择该文件;为每个学生记录增加两门课的总分。
Private Sub Form_Click()
Dim xh As String, cj1 As Integer, cj2 As Integer '声明2分 CommonDialog1.Action = 1 '对话框使用1分 Open CommonDialog1.FileName For Input As #1 '打开文件2分 Open \
Do While Not Eof(1) '读、写文件4分
1
Input #1,xh,cj1,cj2 Write #2,xh,cj1,cj2,cj1+cj2 Loop
Close #1: Close #2 '(或 Close) '文件关闭、改名3分 Kill CommonDialog1.FileName
Name xxx.txt As CommonDialog1.FileName End Sub
2、编写事件过程Command1_Click,输入x、n后按下列公式计算并显示y的值。要求将其中计算阶乘的运算写作自定义函数过程。
x2x3x4xn 1?x??????2!3!4!n!Private Function f(k As Integer) As Long '首尾句1分 Dim i As Integer '函数中其他语句2分 f=1
For i = 2 To k: f = f * i: Next i End Funtion
Private Sub Command1_Click()
Dim y As Single, i As Integer, n As Integer,x As Single '声明语句2分 x = Val(InputBox(\声明语句1分 y = 1 '过程中其他语句4分 For i = 1 To n: y = y + x^i / f(i): Next i Print y End Funtion
1、编写事件过程Command1_Click,计算Text1.Text中所有字符的ASCII码之和并显示。
Private Sub Command1_Click() '1分 Dim y As Long,i As Integer '2分 For i = 1 To Len(Text1.Text) '5分 y = y + Asc(Mid(Text1.Text,i,1)) Next i
Print y '2分 End Sub
2、编写Sub过程f2,可返回一维Single数组各元素中的最大值和最小值。
Private Sub f2(a() As Single, n As Integer, max As Single, min As Single) '首句2分 Dim i As Integer '声明、赋初值2分
2
max = a(1) : min = max
For i = 2 To n '循环结构6分 If a(i) > max Then max = a(i) If a(i) < min Then min = a(i) Next i End Sub
3、某文件以下列格式存储若干行数据,表示库存商品的编码(6个字符)和数量,编写单击窗体的事件过程。要求:以通用对话框控件CommonDialog1选择该文件,用InputBox函数输入某商品的编码,然后显示该商品的库存数量总和。
\ \ \ \ .....
Private Sub Form_Click()
Dim bm1 As String, bm2 As String,sl As Single, s as Single '1分 CommonDialog1.ShowOpen '2分 bm1 = InputBox(\输入商品编码\分 Open CommonDialog1.FileName For Input As #1 '2分 Do While Not Eof(1) '5分 Input #1,bm2,sl
If bm1 = bm2 Then s = s + sl Loop
Close #1 '1分 Print s End Sub
1、编写Sub过程sort,实现一维Single数组从大到小的排序,并返回该数组的平均值。
Private Sub sort(b() As Integer, m As Integer, avg As Single) 3分 ‘ avg声明成integer扣一分,没声明再扣一分 Dim i As Integer, j As Integer, t As Integer For i = 1 To m
avg = b(i) + avg 2分 Next i
avg = avg / m 1分 For i = 1 To m - 1 ’排序算法 4分 For j = 1 To m - i
3