620 likes | 740 Vues
Lecture 5:. Structured Variables & File Systems. Checking Variable Range. Enumerations Structs Arrays. Enumerations. Structs. Arrays. Specifying an Array. Declaring Jagged Arrays. using System.IO; : : namespace EmpListDemo { static class Program {
E N D
Lecture 5: Structured Variables & File Systems
Enumerations Structs Arrays
using System.IO; : : namespace EmpListDemo { staticclassProgram { staticvoid Main(string[] args) { string fname = "empstable.txt"; string txtline; List<Employee> Emps = newList<Employee>(); // a generic list of Employee List<Employee> Emps2 = newList<Employee>(); // we will make a copy of Emps // reading employee data from a text file TextReader tr = newStreamReader(fname); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split(','); Emps.Add(newEmployee(field[0].Trim(),field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4]))); } while (true); tr.Close(); : : The Generic List<T> loading data from a file
Displaying the Contents of the List Emps // display the contents of the list Emps foreach (Employee emp in Emps) { Console.WriteLine("{0} {1} {2} {3} {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage); } Wade Boggs 45 20 12.5 Robin Banks 32 13 9.5 Jerry Mander 27 6 8.1 Amanda Rekonwith 55 25 22.5 Doug Wells 38 10 25.05 Anita Break 23 2 7.5 Juan Abrew 48 7 32.2 Ben Dover 37 9 24.15 Ilene Dover 28 1 22.9
Making a Copy of a List // we are making a copy of Emps called Emps2 foreach (Employee emp in Emps) { Employee emp2 = newEmployee(); emp2 = emp; Emps2.Add(emp2); } // so why not just assign one list to the other? // Emps2 = Emps; // // because this would not make a separate copy but // rather point both Emps2 and Emps to the same records!
The RemoveAll Delegate Method // we "tag" each record that passes our criteria foreach (Employee emp in Emps2) { if (emp.Age > 39 & emp.YrsEmp >= 10) emp.Tag = true; } // now we remove all records from Emps2 that HAVE NOT // been "tagged" i.e. remove those with emp.Tag = false // this construct is implemented using a delegate Emps2.RemoveAll(delegate(Employee emp) { return !emp.Tag; }); Wade Boggs 45 20 12.5 Robin Banks 32 13 9.5 Jerry Mander 27 6 8.1 Amanda Rekonwith 55 25 22.5 Doug Wells 38 10 25.05 Anita Break 23 2 7.5 Juan Abrew 48 7 32.2 Ben Dover 37 9 24.15 Ilene Dover 28 1 22.9 age>39 and yrsemp >= 10 Wade Boggs 45 20 12.5 Amanda Rekonwith 55 25 22.5
Loading an Array from a Text File using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; namespace LoadArrayFromTextfile { classProgram { staticvoid Main(string[] args) { int[,] mat = newint[10,10]; string textline; int k; TextReader tr = newStreamReader("sample_01.txt"); for (int i = 0; i < 10; i++) { textline = tr.ReadLine(); k = 0; foreach (string str in textline.Split(' ')) { if (str != "") { mat[i, k] = Convert.ToInt32(str); k += 1; } } } tr.Close(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write("{0} ", mat[i, j]); } Console.WriteLine(); } Console.ReadKey(); } } } 5 6 4 3 2 7 7 8 6 5 4 3 2 5 3 2 3 8 8 8 1 1 2 2 3 3 6 4 5 3 3 5 5 6 4 9 7 5 2 0 4 3 8 7 0 4 3 2 5 4 1 3 2 4 3 5 4 6 5 7 7 5 6 8 7 7 5 4 7 9 1 3 2 4 3 6 5 4 3 2 8 8 9 6 5 5 3 0 0 1 1 1 1 6 6 8 8 7 6 5
Reading and Writing Textfiles List<Employee> Emps = newList<Employee>(); string txtline; TextReader tr = newStreamReader("employees.txt"); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split('\t'); Emps.Add(newEmployee(field[0].Trim(), field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4]))); } while (true); tr.Close();
Reading and Writing Text Files continued foreach (Employee emp in Emps) { Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName); } TextWriter tw = newStreamWriter("employees.txt"); foreach (Employee emp in Emps) { tw.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage); } tw.WriteLine("xxx"); tw.Close();
Dealing with Data cut & paste
An Example Project Employee Records Manager