Lecture 5:

62
Lecture 5: Structured Variables & File Systems

description

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 { - PowerPoint PPT Presentation

Transcript of Lecture 5:

Page 1: Lecture 5:

Lecture 5:

Structured Variables & File Systems

Page 2: Lecture 5:
Page 3: Lecture 5:
Page 4: Lecture 5:
Page 5: Lecture 5:
Page 6: Lecture 5:
Page 7: Lecture 5:
Page 8: Lecture 5:

Checking Variable Range

Page 9: Lecture 5:
Page 10: Lecture 5:
Page 11: Lecture 5:
Page 12: Lecture 5:

Enumerations

Structs

Arrays

Page 13: Lecture 5:

Enumerations

Page 14: Lecture 5:

Structs

Page 15: Lecture 5:

Arrays

Page 16: Lecture 5:

Specifying an Array

Page 17: Lecture 5:
Page 18: Lecture 5:
Page 19: Lecture 5:
Page 20: Lecture 5:

Declaring Jagged Arrays

Page 21: Lecture 5:
Page 22: Lecture 5:
Page 23: Lecture 5:
Page 24: Lecture 5:
Page 25: Lecture 5:
Page 26: Lecture 5:
Page 27: Lecture 5:
Page 28: Lecture 5:
Page 29: Lecture 5:
Page 30: Lecture 5:

The Generic List<T>loading data from a file

using System.IO; : :namespace EmpListDemo{ static class Program { static void Main(string[] args) { string fname = "empstable.txt"; string txtline;

List<Employee> Emps = new List<Employee>(); // a generic list of Employee List<Employee> Emps2 = new List<Employee>(); // we will make a copy of Emps // reading employee data from a text file TextReader tr = new StreamReader(fname); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split(','); Emps.Add(new Employee(field[0].Trim(),field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4])));

} while (true); tr.Close(); : :

Page 31: Lecture 5:

// display the contents of the list Empsforeach (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.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9

Displaying the Contents of the List Emps

Page 32: Lecture 5:

// we are making a copy of Emps called Emps2foreach (Employee emp in Emps){ Employee emp2 = new Employee(); 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!

Making a Copy of a List

Page 33: Lecture 5:

// we "tag" each record that passes our criteriaforeach (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 delegateEmps2.RemoveAll(delegate(Employee emp){ return !emp.Tag;});

The RemoveAll Delegate Method

age>39 and yrsemp >= 10Wade Boggs 45 20 12.5Amanda Rekonwith 55 25 22.5

Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9

Page 34: Lecture 5:

5 6 4 3 2 7 7 8 6 54 3 2 5 3 2 3 8 8 81 1 2 2 3 3 6 4 5 33 5 5 6 4 9 7 5 2 04 3 8 7 0 4 3 2 5 41 3 2 4 3 5 4 6 5 77 5 6 8 7 7 5 4 7 91 3 2 4 3 6 5 4 3 28 8 9 6 5 5 3 0 0 11 1 1 6 6 8 8 7 6 5

using System.IO;using System.Collections.Generic;using System.Linq;using System.Text;

namespace LoadArrayFromTextfile{ class Program { static void Main(string[] args) { int[,] mat = new int[10,10]; string textline; int k;

TextReader tr = new StreamReader("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();

Loading an Array from a Text File

for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write("{0} ", mat[i, j]); } Console.WriteLine(); } Console.ReadKey(); } }}

Page 35: Lecture 5:

List<Employee> Emps = new List<Employee>();string txtline;

TextReader tr = new StreamReader("employees.txt");

do{ txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split('\t'); Emps.Add(new Employee(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 Textfiles

Page 36: Lecture 5:

foreach (Employee emp in Emps){ Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName);}

TextWriter tw = new StreamWriter("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();

Reading and Writing Text Filescontinued

Page 37: Lecture 5:

Dealing with Datacut & paste

Page 38: Lecture 5:

Pasting into Word or PPT Preserves Cells

Page 39: Lecture 5:

Pasting to a Text Editor Creates Separators e.g. Tabs

Page 40: Lecture 5:
Page 41: Lecture 5:
Page 42: Lecture 5:

An Example ProjectEmployee Records Manager

Page 43: Lecture 5:
Page 44: Lecture 5:
Page 45: Lecture 5:

Main Form Concept

Page 46: Lecture 5:

Defining Test Sets

Page 47: Lecture 5:

Incremental Software Development

Page 48: Lecture 5:
Page 49: Lecture 5:
Page 50: Lecture 5:
Page 51: Lecture 5:
Page 52: Lecture 5:
Page 53: Lecture 5:
Page 54: Lecture 5:
Page 55: Lecture 5:
Page 56: Lecture 5:
Page 57: Lecture 5:
Page 58: Lecture 5:

Messages to User

changed is used to indicate that there is a current edit

alerted is used to indicate that user has been alerted to an issue

saved is used to indicate that the current employess list has been saved to the hard drive

Page 59: Lecture 5:
Page 60: Lecture 5:
Page 61: Lecture 5:
Page 62: Lecture 5:

Summary

Type Conversions

Enumerations

Structs

Arrays

String Manipulation

Generic Type List<T>