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 SingleDim d As Single, x1 As Single, x2 As Singlea = InputBox(“请输入一元二次方程的系数 a“)b = InputBox(“请输入一元二次方程的系数 b“)c = InputBox(“请输入一元二次
2、方程的系数 c“)If a = 0 Thena = InputBox(“因为 a0,你输入的 a0,请重新输入系数 a“)End Ifd = b * b - 4 * a * cIf d = 0 Thenx1 = (-b + Sqr(d) / (2 * a)x2 = (-b - Sqr(d) / (2 * a)Print “系数为“; a; b; c; “的一元二次方程的根分别为“; x1Print “系数为“; a; b; c; “的一元二次方程的根分别为“; x2ElsePrint “此方程在实数范围内无解“End IfEnd Sub2.求两个整数的最大公约数、最小公倍数。 (两个整数从键盘
3、输入) 。Private Sub Command1_Click()m = InputBox(“输入第一个自然数 “)n = InputBox(“输入第二个自然数“)nm = n * mIf m 0)m = nn = rr = m Mod nLoopMsgBox (“最大公约数为 “ “九九乘法表“Print Tab(35); “-“For i = 1 To 9For j = 1 To ise = CStr(i) se;Next jPrintNext iEnd Sub4.用随机函数产生 20 个两位的正整数,并要求从小到大顺序输出。Dim aaa(20) As IntegerPrivate Su
4、b 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) 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 ThenPrint “false“Exit ForElsePrint “true“Exit ForEnd IfNextEnd Sub