Sie sind auf Seite 1von 15

Angie NgWeeKoon (Sem1, 2012) Information Systems

ESGC 6113 Computer

Assignment 2 --Visual Basic Q1 { use If Else if , For i to n }

Private sub Form_Activate( ) Const n As Integer = 15 Dim name(n) As String, grade(n) As String Dim mark(n) As Integer Dim average As double, stdev as double Dim max As integer, min As integer, range As integer, Dim i As integer, sum As integer, sum2 As integer Input students name, mark and grade For i = 1 to n name(i) = InputBox (Enter Student Name : , Name Entry) mark(i) = Val(InputBox (Enter Student Mark : , Mark Entry)) If mark(i) <50 then grade(i) = F elseIf mark(i) >= 50 and mark(i) < 60 then grade (i) = D elseIf mark(i) >=60 and mark(i)< 70 then grade (i) = C elseIf mark(i) >=70 and mark(i)< 80 then grade (i) = B else End If Next i Calculate sum, average, standard deviation sum = 0 sum2 = 0 For i = 1 to n sum = sum + mark(i) sum2 = sum2 + mark(i)^2 grade (i) = A

Next i average = sum /n stdev = Sqr(sum2/n average^2) Calculate maximum, minimum, range max= mark(1) min= mark(1) For i = 2 to n If mark(i) > max then max = mark(i) end If if mark (i) < min then min = mark(i) end if Next i range = max - min Print Name; Tab(10); Marks; Tab(20); Grade Print For i = 1 to n Print name(i); Tab(10); mark(i); Tab(20); grade(i) Next i Print Print Average Marks = & average Print Standard Deviation = & stdeviation Print Print Maximum = & max Print Minimum = & min Print Range = & range End Sub ---------------------------------------------------------

Q2 { use Select Case } Private Sub Form_Activate() Dim x(n) As Integer Dim totalgrp1 As Integer, totalgrp2 As integer, totalgrp3 As integer, totalgrp4 As integer Dim i As integer, j As integer Dim temp As integer Dim totaleven As integer, totalmultiple5 As integer Dim n As integer , low As integer, high As integer n = 200 low = 1000 high = 9999 Calculate how many numbers in different groups of range totalgrp1=0 totalgrp2=0 totalgrp3=0 totalgrp4=0 Randomize For i = 1 to n x(i) = Int(low + (high low + 1)*Rnd) Select Case x(i) Case 1000 to 2499 totalgrp1 = totalgrp1 + 1 Case 2500 to 4999 totalgrp2 = totalgrp2 + 1 Case 5000 to 7499 totalgrp3 = totalgrp3 + 1 Case Else totalgrp4 = totalgrp4 + 1 End Select Next i Print There are & totalgrp1 numbers in the range 1000-2499 Print There are & totalgrp2 numbers in the range 2500-4999

Print There are & totalgrp3 numbers in the range 5000-7499 Print There are & totalgrp4 numbers in the range 7500-9999 Print Calculate how many even numbers and how many multiple of 5 totaleven = 0 totalmultiple5 = 0 For i = 1 to n If x(i) Mod 2 = 0 Then totaleven = totaleven + 1 End If If x(i) Mod 5 = 0 Then totalmultiple5 = totalmultiple5 + 1 End If Next i Print There are & totaleven even numbers in the list Print There are & totalmultiple5 numbers that are divisible by 5 Sort the numbers For i = 1 to n-1 For j = i+1 to n If x(i) > x(j) Then temp = x(i) x(i) = x(j) x(j) = temp End If Next j Next i Print Sorted Number List For i = 1 to n Print x(i); If i Mod 10 = 0 Then Print End If Next i 10 numbers in a row

End Sub Q3 { Payroll calculation } Private Sub CmdPayroll_Click() Const rate As Currency = 9.50 Dim workerName As String Dim wage As Single Dim hour As Integer Print WorkerName; Tab (10); Hours; Tab(20); Wage(RM) Print Open the file for input Open C:\MyDocuments\DataIn.txt For Input As #1 Do While Not EOF(1) Input #1 workerName, hour Select Case hour Case 0 To 40 wage = hour * rate Case 41 To 60 wage = 300 + (hour 40)* rate * 1.5 Case Else wage = 525 + (hour 60)* rate * 2.0 End Select Print workerName; Tab (10); hour; Tab(20); Round(wage,2) Loop Close #1 End Sub --------------------------------------

Q4 { Simulation of table-tennis } Private Sub WhoWin_Button_Click( ) Dim probA As Single Dim A As Integer, B As Integer Dim countA As Integer, countB As Integer Dim i As Integer, j As Integer, n As Integer n=5 probA = 0.7 For j = 1 To 3 Randomize Print Results of competition (probA = & probA & ) Print countA=0 countB=0 Do While countA <= 3 Or countB <= 3 For i = 1 To n A=0 B=0 Do Until A = 20 Or B = 20 If Rnd < probA Then A=A+1 Else B=B+1 End If Loop Print "Set: " & i If A = 20 Then Print "A is the winner with a score of 20 - " & B countA = countA + 1 ' A total of 5 games are played 'Probability A wins 3 different simulations with different probability 0.7, 0.6, 0.5

ElseIf B = 20 Then Print "B is the winner with a score of 20 - " & A countB = countB + 1 End If Print Next i Loop If countA = 3 Then Print Player A wins the first three games. Congratulations! Player A is the winner. ElseIf countB = 3 Then Print Player B wins the first three games. Congratulations! Player B is the winner. End If Print probA = probA 0.1 Next j End Sub ------------------------------------

Q5

{ Creating a text file using Notepad, filename is Product_list.txt }

AAA, 7.00 BBB, 2.30 CCC, 1.70 DDD, 9.00 EEE, 3.50 FFF, 6.70 GGG, 5.40 ---------------------------------------------------------Private Sub CmdPrintReceipt_Click() Dim productCode As String, entryCode As String Dim unitPrice As Single, unitAmount As Single, totalAmount As Single Dim quantity As Integer Print ; Tab(16) ; My Street Store Print ProdCode; Tab(10); Quantity; Tab(20); Price; Tab(30); Amount(RM) totalAmount = 0 For i = 1 To 3 entryCode = InputBox (Please enter the code for the product that you had purchased : ) quantity = Val(InputBox (Please enter the quantity that you had purchased : )) Open C:\MyDocuments\Product_list.txt For Input As #1 Do While Not EOF(1) Input #1, productCode, unitPrice If productCode = entryCode Then unitAmount = unitPrice * quantity totalAmount = totalAmount + unitAmount Print productCode; Tab(10); quantity; Tab(20); unitPrice; Tab(30); Print FormatNumber(unitAmount, 2) Print End If Loop Close #1 Next i Print Total purchase = RM & FormatNumber(totalAmount, 2)

End Sub Q6 { String alignment } First, create a textbox and set the property txtGreet.text = Welcome to Malaysia Create three different command buttons respond to right-justify, center-justify, and calculation of character a. Private Sub Rightbutton_Click() txtGreet.SetFocus txtGreet.Alignment = 1 Form1.Show End Sub -------------------------------------------------------Private Sub Centerbutton_Click() txtGreet.SetFocus txtGreet.Alignment = 2 Form1.Show End Sub ----------------------------------------------------Private Sub CalCharbutton_Click() Dim phrase As String Dim i As Integer, count As integer, length As Integer phrase = txtGreet.text length = Len(phrase) For i = 1 to length If Mid$ (phrase , i, 1 ) = a Then count = count + 1 End If Next i Print There are ; count ; as in this phrase. End Sub -------------------------------------------------------------------

Q7 { array[20, 20] } Private Sub Form_Activate() Dim x(20,20) As Integer Dim sumDiagonal As Integer, sumOdd As integer Static sumSquare As Integer, sumSqRoot As Integer Dim r As Integer, c As Integer Dim low As Integer, high As Integer low = 1 high = 100 Randomize For r = 1 To 20 For c = 1 To 20 x(r, c) = Int(low+(high-low+1)*Rnd) sumDiagonal = Diagonal (r, c, x(r, c)) sumOdd = Odd (x(r, c)) Next c Next r For r = 2 To 20 Step 2 For c = 1 To 20 sumSquare = sumSquare + x(r, c)^2 Next c Next r For r = 1 To 20 For c = 1 To 20 Step 2 sumSqRoot = sumSqRoot + sqr(x(r, c)) Next c Next r r is row, c is column

Print Sum of all diagonal elements = ; sumDiagonal Print Print Sum of all the odd numbers = ; sumOdd Print Print Sum of all the square of the numbers in the even rows = ; sumSquare Print Print Sum of all the square root of the numbers in the odd columns = ; sumSqroot Print End Sub ----------------------------------------------------------------------------------------------Private Sub Diagonal (r As Integer, c As Integer, x(r, c) As Integer) As Integer Static sum1 As Integer If r = c Then sum1 = sum1 + x(r, c) Diagonal = sum1 End If End Sub --------------------------------------------------------------------------------------------------Private Sub Odd (x(r, c) As Integer) As Integer Static sum2 As Integer If x(r, c) Mod 2 < > 0 Then sum2 = sum2 + x(r, c) Odd = sum2 End If End Sub ---------------------------------------------------------------------------------------------------

Q8 { correlation coefficient }

--------------------------------------------------------------

Q9 { use minimum/maximum function } Private Sub Form_Activate() Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer a = 256 b = 123 c = 204 d = 49 e = 97 Print For numbers ; a; , ; b; , ; c; , ; d; and ; e; Print Minimum value = ; min(min(a, b), min(c, min(d, e))) Print Maximum value = ; max(max(a, b), max(c, max(d, e))) Print End Sub ---------------------------------------------------------------------------------Private Function min(x As Integer, y As Integer) As Integer If x < y Then min = x Else min = y End If End Function ----------------------------------------------------------------------------------Private Function max(x As Integer, y As Integer) As Integer If x < y Then max = y Else max = x End If End Function ------------------------------------------------------------------------------------

Q10 { guessing sum of points on 2 dice } Private Sub Form_Activate() Const n As Integer = 1000 Dim points As Integer Dim high As Integer, low As Integer Dim i As Integer Dim guess As Integer high = 12 low = 2 Randomize For i = 1 To n points = Int((high - low + 1) * Rnd + low) guess = InputBox (Guess sum of points on 2 dices ( 2 to 12 ) : ) If guess = points then Print Good guess! Congratulations! Exit For Else Print Bad guess, try again. End if Next i End Sub ------------------------------------------------------------------------------------------------------------

Das könnte Ihnen auch gefallen