VB教材章習題答案2019

2022-12-06 15:06:04 字數 5285 閱讀 3978

《大學程式設計基礎-visual basic>教材課後習題答案

第1章一、選擇題

第2章一、選擇題

二、填空題

1. rem或』 _ :

23. 5x2-3x-2sina/3

4. 321456

5. 300

三、操作題

1、程式段:

private sub command1_click()

dim x as integer, y as integer

dim s as long, c as long

x =y =s = x * y

c = (x + y) * 2

= "長方形的面積為" + str(s)

= "長方形的周長為" + str(c)

end sub

2、dim x as string, t as textbox

private sub cmdcopy_click()

x =end sub

private sub cmdcut_click()

x == ""

end sub

private sub cmdpaste_click()

= xend sub

private sub text1_gotfocus()

set t = text1

end sub

private sub text2_gotfocus()

set t = text2

end sub

3、程式段:

private sub command1_click()

= "第一"

end sub

private sub command2_click()

= "第二"

end sub

4、程式**:

private sub text1_change()

= 10

end sub

5、程式**:

private sub text1_click()

= 0= len(

end sub

第3章一、選擇題

二、填空題

1. false

2. b^2-4*a*c else

三、操作題

1.從鍵盤輸入三個值,判斷它們能否構成三角形的三個邊。如果能構成乙個三角形,則計算三角形的面積。

dim a as integer, b as long, c as long, s as single, p as single

a = val(inputbox("請輸入乙個值:"))

b = val(inputbox("請輸入乙個值:"))

c = val(inputbox("請輸入乙個值:"))

if (a + b > c and a + c > b and b + c > a) then

p = (a + b + c) / 2

s = sqr(p * (p - a) * (p - b) * (p - c))

print a & "," & b & "," & c & "能構成乙個三角形,它的面積為:" & s

end if

2.編寫程式,任意輸入乙個整數,判定該整數奇偶性。

dim n as integer

n = val(inputbox("請輸入乙個整數:"))

if n mod 2 = 0 then

print n & "是偶數"

else

print n & "是奇數"

end if

3.求一元二次方程的a*x^2+b*x+c=0的根。

dim a as integer, b as integer, c as integer, delta as single, x1 as single, x2 as single

a = val(inputbox("請輸入乙個整數:"))

b = val(inputbox("請輸入乙個整數:"))

c = val(inputbox("請輸入乙個整數:"))

delta = b ^ 2 - 4 * a * c

if delta = 0 then

x1 = -b / (2 * a)

print "一元二次方程有兩個相等的實根x1=x2=" & x1

else

if delta > 0 then

x1 = (-b + sqr(delta)) / (2 * a)

x2 = (-b - sqr(delta)) / (2 * a)

print "一元二次方程有兩個不等的實根x1=" & x1 & ",x2=" & x2

else

x1 = (-b) / (2 * a)

x2 = sqr(-delta) / (2 * a)

print "一元二次方程有兩個不等的虛根x1=" & x1 & "+" & x2 & "i,x2=" & x1 & "-" & x2 & "i"

end if

end if

4. 任意輸入三個數,輸出最大數。

dim a as integer, b as integer, c as integer, max as integer

a = val(inputbox("請輸入乙個整數:"))

b = val(inputbox("請輸入乙個整數:"))

c = val(inputbox("請輸入乙個整數:"))

max = a

if max < b then max = b

if max < c then max = c

print "最大數是" & max

5.任意輸入三個數,由大到小排序輸出。

dim a as integer, b as integer, c as integer, temp as integer

a = val(inputbox("請輸入乙個整數:"))

b = val(inputbox("請輸入乙個整數:"))

c = val(inputbox("請輸入乙個整數:"))

if a < b then

temp = a

a = b

b = temp

end if

if b > c then

print a, b, c

else

if a > c then

print a, c, b

else

print c, a, b

end if

end if

第4章一、選擇題

二、填空題

1. 9

2. 1 2 3 ( 此3個數縱向排列)

3. x=int(rnd*100)+100 (此處修改語句);x mod 5=0

4. 6

5. 5

三、 程式設計題

1.鍵盤輸入幾個不同的數,將它們從大到小排序。

(1)冒泡法排序:

private sub command1_click()

dim a() as integer

n = inputbox(" 輸入資料個數n:")

redim a(n) as integer

randomize

for k = 1 to n

a(k) = int(100 * rnd)

print a(k);

next

print

for i = 1 to n - 1

for j = 1 to n - i

if a(j) < a(j + 1) then

w = a(j): a(j) = a(j + 1): a(j + 1) = w

end if

next

for q = 1 to n

print a(q);

next

print

next

end sub

(2)選擇法排序:

private sub command2_click()

dim a() as integer

n = inputbox(" 輸入資料個數n:")

redim a(n) as integer

randomize

for k = 1 to n

a(k) = int(100 * rnd)

print a(k);

next

print

for i = 1 to n - 1

p = i

for j = i + 1 to n

if a(p) < a(j) then p = j

next

if p <> i then

w = a(p): a(p) = a(i): a(i) = w

end if

for q = 1 to n

print a(q);

next

print

next

end sub

2.設計程式,求出s=1+(1+2)+(1+2+3)+…+(1+2+3+4+…+n)的值。

dim i as integer, sn as long, s as long, n as integer

sn = 0

s = 0

n = inputbox("輸入n的值:")

for i = 1 to n

sn = sn + i

s = s + sn

next

print s

執行結果:若n為10,s為220。

3.輸入x的值,求s=x-x^3/3!+x^5/5!- …的前20項的和值。

dim x as single, i as integer, s as double

x = inputbox("輸入x值:")

s = x

for i = 1 to 19

p = 1

for j = 1 to 2 * i + 1

p = p * j

next

s = s + (-1) ^ (i) * x ^ (2 * i + 1) / p

next

print s

執行結果:當x為2時,s為0.91(精確到小數點後兩位)

4.勾股定理中3個數的關係是:a2+b2=c2。編寫程式,輸出30以內滿足上述關係的整數組合,例如3、4、5就是乙個整數組合。

dim a as integer, b as integer, c as integer, k as integer

for c = 1 to 30

for b = 1 to c

VB書面習題答案 第4章

習題四p94 1 15題 參 1 結構化程式設計的三種基本結構是什麼?答 順序結構 選擇結構和迴圈結構 2 指出下列賦值語句中的錯誤 包括執行時要產生的錯誤 10 x sin x y答 10x是非法變數名 c 3 sqr 3答 待求平方根的數為負數 c x y c y答 賦值符號的左邊是表示式 x ...

vb複習題 題目答案

visual basic 期末複習題庫 單項選擇題判斷題填空題程式設計題 1 下列程式執行後,輸出的結果為 b 1do while b 40 b b b 1 loop print b a 42 b 39 c 6 d 1 答案 a 2 設執行以下程式段時依次輸入1 3 5,執行結果為 dim a 4 ...

2章習題答案

第2章習題 2.1.1 如右下圖所示電路中,d為矽二極體,則二極體d和和電阻r上的電壓各為多少?流過二極體的電流多大?解 二極體正偏導通 2.1.2 在下圖中的各電路圖中,v,二極體d的正向壓降忽略不計。試分別畫出輸出電壓的波形。abc 解 2.1.3二極體電路如圖所示,試分別判斷圖 a 和 b 中...