Beruflich Dokumente
Kultur Dokumente
(VB)
REPORT FOR PROGRAMMING TEST
Name
Matric No.
M2016013
School
Question No
15
Question 15:
The file USPres.txt contains the name of all the presidents of United States and the file VPres.txt
contains the names of all the vice Presidents. Write a program that creates a file containing the
names of every person who served as either vice-president or president, but not both. The program
also should display the number of those names in a message box.
Solution:
In this program user can press Display button to show the names of all US Presidents, VicePresidents and also those who became president or vice-president but not both.
1. Interface:
Interface Name Used Nos.
Button
1
Label
5
Text Box
1
List Box
3
Form
1
2. Property Settings
Object
frmpresandvpres
btnDisplay
txtboth
lstpresident
lstvpresident
lstboth
lblinfo
lblpresident
lblVpresident
lblboth
lblnumber
Property
Text
Text
Settings
Question 15
Display
Text
Text
Text
Text
Text
3. Event Procedure:
In this program, the Display event of btnDisplay is handled with its event procedure as
follows.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'Read data from textfile and save to assigned name
Dim Pres() As String = IO.File.ReadAllLines("USPres.txt")
Dim VPres() As String = IO.File.ReadAllLines("VPres.txt")
'Declare nbPres As Integer()
Dim nbPres As Integer
'Application of Union and Intersection Operation in first part
IO.File.WriteAllLines("Both.txt", Pres.Intersect(VPres)) 'Writing Intersect name
to Both.txt
IO.File.WriteAllLines("Union.txt", Pres.Union(VPres))
'Writing Union name to
Union.txt
'Application of Except Operation in Second Part
Dim Pres1() As String = IO.File.ReadAllLines("Both.txt") 'Read data from both.txt
Dim VPres1() As String = IO.File.ReadAllLines("Union.txt") 'Read data from
Union.txt
IO.File.WriteAllLines("Exclude.txt", VPres1.Except(Pres1)) 'Writing file to
Exclude.txt
'Create Array to store Exclude.txt file
Dim Exclude() As String = IO.File.ReadAllLines("Exclude.txt")
'Displaying Name Of president, lstVPresident And Union of Pres and V.Pres but not
of those who became both
lstpresident.DataSource = Pres.ToArray
lstvpresident.DataSource = VPres.ToArray
lstboth.DataSource = Exclude.ToArray
nbPres = Exclude.Length
'Conversion of string into integer
txtboth.Text = CStr(nbPres)
End Sub
4. Output
If a user click on the button Display, the result is shown in the three list box and one text
box. List boxes contains name of President, Vice-President and those who served as
President or Vice-President but not both. The text box shows the total number of person
who served as President or Vice-President but not both.
5. Remarks
This program uses Array to store data file from text file, also it creates text file from results.
This program uses the intersect, Union and except function to compare between files
data and gives desired result.