《vb公选课考查试题和答案》由会员分享,可在线阅读,更多相关《vb公选课考查试题和答案(3页珍藏版)》请在金锄头文库上搜索。
1、VB公选课考查试题姓名:邱浩 学号:110424308请完成下列问题的编程,每题(20分),做好后用A4纸打印提交。1.设计一个求解一元二次方程Ax2+Bx+C=0的程序(A、B、C的值是任意的)。Private Sub Command1_Click() Dim a As Single, b As Single, c As Single Dim d As Single, x1 As Single, x2 As Single a = InputBox(请输入一元二次方程的系数a) b = InputBox(请输入一元二次方程的系数b) c = InputBox(请输入一元二次方程的系数c) If
2、 a = 0 Then a = InputBox(因为a0,你输入的a0,请重新输入系数a) End If d = b * b - 4 * a * c If d = 0 Then x1 = (-b + Sqr(d) / (2 * a) x2 = (-b - Sqr(d) / (2 * a) Print 系数为; a; b; c; 的一元二次方程的根分别为; x1 Print 系数为; a; b; c; 的一元二次方程的根分别为; x2 Else Print 此方程在实数范围内无解 End IfEnd Sub2.求两个整数的最大公约数、最小公倍数。(两个整数从键盘输入)。Private Sub
3、Command1_Click()m = InputBox(输入第一个自然数)n = InputBox(输入第二个自然数)nm = n * mIf m n Then t = m: m = n: n = t r = m Mod n Do While (r 0) m = n n = r r = m Mod n Loop MsgBox (最大公约数为 & n)MsgBox (最小公倍数为 & nm / n)End Sub3.打印出九九乘法表(要求有表头,格式对齐)。Private Sub Form_Click()Print Tab(35); 九九乘法表Print Tab(35); -For i = 1
4、 To 9For j = 1 To ise = CStr(i) & * & CStr(j) & = & CStr(i * j)Print Tab(j - 1) * 9 + 1); se;Next jPrintNext iEnd Sub4.用随机函数产生20个两位的正整数,并要求从小到大顺序输出。Dim aaa(20) As IntegerPrivate Sub Command1_Click()RandomizeFor i = 1 To 20aaa(i) = Int(Rnd * 90 + 10)Next iFor i = 1 To 19For j = i To 20If aaa(i) aaa(j
5、) Thentemp = aaa(i)aaa(i) = aaa(j)aaa(j) = tempEnd IfNext jNext iFor i = 1 To 20Print aaa(i) & ;If i Mod 10 = 0 Then PrintNext iEnd Sub5.设计一个函数,用来判断一个整数是否为素数。给定的整数若是素数,则返回逻辑值True,否则返回False。Private Sub Form_Load()ShowDim a, i As Integera = Val(InputBox(请输入一个整数)For i = 2 To Int(Sqr(a) If a Mod i = 0 Then Print false Exit For Else Print true Exit For End IfNextEnd Sub