Sie sind auf Seite 1von 9

Drawing a green rectangle:

Dim greenPen As New Drawing.Pen(Color.Green, 10) e.Graphics.DrawRectangle(greenPen, 10, 10, 100, 200) Sorting: Sub Selectionsort (List() As Long, min As Integer, _ max As Integer) Dim i As Integer Dim j As Integer Dim best_value As Long Dim best_j As Integer For i = min To max - 1 best_value = List(i) best_j = i For j = i + 1 To max If List(j) < best_value Then best_value = List(j) best_j = j End If Next j List(best_j) = List(i) List(i) = best_value Next i End Sub Bubble: ' min and max are the minimum and maximum indexes ' of the items that might still be out of order. Sub BubbleSort (List() As Long, ByVal min As Integer, _ ByVal max As Integer) Dim last_swap As Integer Dim i As Integer Dim j As Integer Dim tmp As Long ' Repeat until we are done. Do While min < max ' Bubble up. last_swap = min - 1 ' For i = min + 1 To max i = min + 1 Do While i <= max ' Find a bubble. If List(i - 1) > List(i) Then ' See where to drop the bubble. tmp = List(i - 1) j = i Do List(j - 1) = List(j) j = j + 1 If j > max Then Exit Do Loop While List(j) < tmp List(j - 1) = tmp last_swap = j - 1 i = j + 1 Else i = i + 1 End If Loop ' Update max.

max = last_swap - 1 ' Bubble down. last_swap = max + 1 ' For i = max - 1 To min Step -1 i = max - 1 Do While i >= min ' Find a bubble. If List(i + 1) < List(i) Then ' See where to drop the bubble. tmp = List(i + 1) j = i Do List(j + 1) = List(j) j = j - 1 If j < min Then Exit Do Loop While List(j) > tmp List(j + 1) = tmp last_swap = j + 1 i = j - 1 Else i = i - 1 End If Loop ' Update min. min = last_swap + 1 Loop End Sub Quicksort: Sub Quicksort (List() As Long, min As Integer, max As Integer) Dim med_value As Long Dim hi As Integer Dim lo As Integer Dim i As Integer ' If the list has no more than 1 element, it's sorted. If min >= max Then Exit Sub ' Pick a dividing item. i = Int((max - min + 1) * Rnd + min) med_value = List(i) ' Swap it to the front so we can find it easily. List(i) = List(min) ' Move the items smaller than this into the left ' half of the list. Move the others into the right. lo = min hi = max Do ' Look down from hi for a value < med_value. Do While List(hi) >= med_value hi = hi - 1 If hi <= lo Then Exit Do Loop If hi <= lo Then List(lo) = med_value Exit Do End If

' Swap the lo and hi values. List(lo) = List(hi) ' Look up from lo for a value >= med_value. lo = lo + 1 Do While List(lo) < med_value lo = lo + 1 If lo >= hi Then Exit Do Loop If lo >= hi Then lo = hi List(hi) = med_value Exit Do End If ' Swap the lo and hi values. List(hi) = List(lo) Loop ' Sort the two sublists Quicksort List(), min, lo - 1 Quicksort List(), lo + 1, max End Sub Towers of Hanoi: ' represents a peg in the towers of hanoi game Structure peg Dim top As Integer Dim bottom As Integer Dim name As String Dim stack() As Integer Public Sub push(ByVal value As Integer) stack(top) = value top = top - 1 End Sub Public Function pop() As Integer top = top + 1 Return (stack(top)) End Function Public Sub initialize(ByVal num_elements As Integer, ByVal name As String) ReDim stack(num_elements) top = num_elements - 1 bottom = num_elements - 1 Me.name = name End Sub Public Function print() As String Dim retval As String Dim i As Integer

retval = "" For i = top + 1 To bottom retval = retval & " " & stack(i) Next Return (retval) End Function End Structure ' this method actually plays the game Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim pegA As peg Dim pegB As peg Dim pegC As peg Dim i As Integer Dim num_disks As Integer num_disks = 20 pegA.initialize(num_disks, "pegA") pegB.initialize(num_disks, "pegB") pegC.initialize(num_disks, "pegC") For i = 0 To num_disks - 1 pegA.push(num_disks - i) Next ' print the status of pegA (top to bottom) MsgBox("pegA: " & pegA.print()) ' play the towers of hanoi game do_towers(pegA, pegB, pegC, num_disks) ' print the status of pegB (top to bottom) MsgBox("pegB: " & pegB.print()) End Sub

' moves num_disks disks from A to B via C Private Sub do_towers(ByRef pegA As peg, ByRef pegB As peg, ByRef pegC As peg, ByVal num_disks As Integer) If (num_disks > 0) Then do_towers(pegA, pegC, pegB, num_disks - 1) pegB.push(pegA.pop()) do_towers(pegC, pegB, pegA, num_disks - 1) End If End Sub N Queens (C++):

#include <stdio.h> #define N 8 int Chess(char Arr[N][N] , int row); int check(char Arr[N][N],int row,int line); //double count; int main() { char chess[N][N]={0}; Chess(chess,0);/* The call to the function*/ { int i,y; for(i=0;i<N;++i)/*prints the result*/ { printf("\n\t\t\t"); for(y=0;y<N;++y) { if(chess[i][y]==0) printf("x "); else printf("%c ",chess[i][y]); } } } printf("\n"); } int Chess(char Arr[N][N] , int row) { int line=0; //printf("%f\n",count++); if(row==N) return 1; while(line < N) { if(check(Arr,row,line)) /*check the row*/ { Arr[row][line]='Q'; /*puts a queen on the board*/ if(Chess(Arr,row+1))/*the recursion*/ return 1; Arr[row][line]=0;/*clears the last change if*/ }/*returned 0 from the recursion*/ line++; } return 0; }

int check(char Arr[N][N],int row,int line) {/*check just the left size of the board*/ int r,l; r=row; l=line; while(r >= 0 && l >= 0) { if(Arr[r][l]=='Q') return 0; --r; --l; } l=line; r=row; while(l < N && r >= 0) { if(Arr[r][l]=='Q') return 0; ++l; --r; } l=line; r=row; while(r >= 0) { if(Arr[r][l]=='Q') return 0; --r; } return 1; }
(i)The Len Function

The length function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The format is Len (Phrase) For example, Len (VisualBasic) = 11 and Len (welcome to VB tutorial) = 22 The Len function can also return the number of digits or memory locations of a number that is stored in the computer. For example, Private sub Form_Activate ( )

X=sqr (16) Y=1234 Z#=10# Print Len(x), Len(y), and Len (z) End Sub will produce the output 1, 4 , 8. The reason why the last value is 8 is because z# is a double precision number and so it is allocated more memory spaces. (ii) The Right Function The Right function extracts the right portion of a phrase. The format is Right (Phrase, n) Where n is the starting position from the right of the phase where the portion of the phrase is going to be extracted. For example, Right(Visual Basic, 4) = asic (iii)The Left Function The Left$ function extract the left portion of a phrase. The format is Left(Phrase, n) Where n is the starting position from the left of the phase where the portion of the phrase is going to be extracted. For example, Left (Visual Basic, 4) = Visu (iv) The Ltrim Function The Ltrim function trims the empty spaces of the left portion of the phrase. The format is Ltrim(Phrase) .For example, Ltrim ( Visual Basic, 4)= Visual basic (v) The Rtrim Function The Rtrim function trims the empty spaces of the right portion of the phrase. The format is Rtrim(Phrase) .For example, Rtrim (Visual Basic (vi) The Trim function , 4) = Visual basic

The Ttrim function trims the empty spaces on both side of the phrase. The format is

Trim(Phrase) .For example, Trim ( Visual Basic ) = Visual basic

(viii) The Mid Function

The Mid function extracts a substring from the original phrase or string. It takes the following format: Mid(phrase, position, n) Where position is the starting position of the phrase from which the extraction process will start and n is the number of characters to be extracted. For example, Mid(Visual Basic, 3, 6) = ual Bas (ix) The InStr function

The InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The format is Instr (n, original phase, embedded phrase) Where n is the position where the Instr function will begin to look for the embedded phrase. For example Instr(1, Visual Basic, Basic)=8 (x) The Ucase and the Lcase functions

The Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters. For example, Ucase(Visual Basic) =VISUAL BASiC Lcase(Visual Basic) =visual basic (xi) The Str and Val functions

The Str is the function that converts a number to a string while the Val function converts a string to a number. The two functions are important when we need to perform mathematical operations. (xii) The Chr and the Asc functions

The Chr function returns the string that corresponds to an ASCII code while the Asc function converts an ASCII character or symbol to the corresponding ASCII code. ASCII stands for American Standard Code for Information Interchange.

Altogether there are 255 ASCII codes and as many ASCII characters. Some of the characters may not be displayed as they may represent some actions such as the pressing of a key or produce a beep sound. The format of the Chr function is Chr(charcode) and the format of the Asc function is Asc(Character) The following are some examples: Chr(65)=A, Chr(122)=z, Chr(37)=% , Asc(B)=66, Asc(&)=38

Das könnte Ihnen auch gefallen