cs 1430: programming in c++ 1. class studentlist class studentlist { private: int numstudents;...

Post on 18-Jan-2018

225 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Static List int main() { StudentList CS143_S2; Student s; float maxGPA, minGPA, avgGPA; CS143_S2.Read(); CS143_S2.Write(); CS143_S2.GetStats(maxGPA, minGPA, avgGPA); s = CS143_S2.MaxGPAStudent(); s.Write(); s = Student(“Qi”, “Yang”); if (CS143_S2.UpdateStudentGPA(s, 0.5)) CS143_S2.Write(); else cout

TRANSCRIPT

CS 1430: Programming in C++

1

Class StudentListclass StudentList{private: int numStudents; Student students[MAX_SIZE];

int find(const Student& s) const

public: void Read() void Write() const float MaxGPA() const float MimGPA() const float AverageGPA() const void GetStats(float& max, float& min, float& avg) const Student MaxGPAStudent() const bool UpdateStudentGPA(const Student& s, float g) . . .}; // StudentList

2

Static Listint main(){ StudentList CS143_S2; Student s; float maxGPA, minGPA, avgGPA;

CS143_S2.Read(); CS143_S2.Write(); CS143_S2.GetStats(maxGPA, minGPA, avgGPA);

s = CS143_S2.MaxGPAStudent(); s.Write();

s = Student(“Qi”, “Yang”); if (CS143_S2.UpdateStudentGPA(s, 0.5)) CS143_S2.Write(); else cout << s.GetLast() << “ is not in the list”;

return 0;}

No Adding or Deleting!

3

Dynamic Listint main(){ StudentList CS143_S2; Student s; char command;

cin >> command; while (!cin.eof()) { if (command == ‘A’) // add a student to CS143_S2 else if (command == ‘D’) // delete a student from CS143_S2 else if (command == ‘U’) // update a student in CS143_S2 else if (command == ‘P’) // print all students in CS143_S2 else // invalid command

cin >> command; }

return 0;}

4

Dynamic Listclass Student{ . . .};

class StudentList{private: int numStudents; Student students[MAX_SIZE]; . . .public: // need a default constructor StudentList() { numStudents = 0; } . . .};

5

More Methods for Class StudentListclass StudentList{private: int numStudents; Student students[MAX_SIZE];

int find(const Student& s) const

public: StudentList() void Read() void Write() const float MaxGPA() const float MimGPA() const float AverageGPA() const void GetStats(float& max, float& min, float& avg) const Student MaxGPAStudent() const bool UpdateStudentGPA(const Student& s, float g) // Add // Delete};

6

Adding an Element to a ListAdding at the end of the list

What’s the index?

Assuming the list has 5 elements index: 5

Assuming the list has no elements index: 0

Assuming the list has numStudents elements

index: numStudents

7

Pseudo Code for Adding Method

If the list is full Can not addElse Search the list If not found Insert Student Else What to do? (do not add)

8

Add Method for Class StudentListconst int FULL_LIST = 0;const int IN_LIST = 1;const int ADDED = 2;

class StudentList{private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const

public: int Add(const Student& s) { if (numStudents == MAX_SIZE) return FULL_LIST; int index = find(s); if (index > -1) return IN_LIST; students[numStudents] = s; numStudents ++; return ADDED; } . . .};

9

Pseudo Code for Method Delete

Search in the listIf not found return falseElse delete the student from the list return true

10

Delete an Array Element

10 15 20 50 33 40 55 60 39 ? ? . . . .

10 15 20 50 33 40 60 60 39 . . . .

0 1 2 3 4 5 6 7 8

10 15 20 50 33 40 60 39 39 . . . .

// Move Array[7] to Array[6]

Array[6] = Array[7];

0 1 2 3 4 5 6 7 8

0 1 2 3 4 5 6 7

// Move Array[8] to Array[7]

Array[7] = Array[8];

// Update size

size --; // size 8; index: 0 to 7

size: 9

Element to be deleted: at index 6

11

Delete an Array Element

10 15 20 50 33 40 55 60 39 ? ? . . . .

0 1 2 3 4 5 6 7 8

Array[6] = Array[7]; Array[7] = Array[8]; size --;

// Move each element after the deleted one forward one step// Where to begin and where to stop?

for (int i = index; i < size – 1; i ++) Array[i] = Array[i + 1];

size --;

size: 9

Element to be deleted: at index 6

12

Delete Method for Class StudentListclass StudentList{private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const

public: bool Delete(Student& s) { int index = find(s);

if (index == -1) return false;

for (int i = index; i < numStudents – 1; i ++) students[i] = students[i + 1];

numStudents --; return true; } . . .};

13

Command Charint main(){ StudentList CS143_S2; Student s; char command;

cin >> command; while (!cin.eof()) { if (command == ‘A’) // add a student to CS143_S2 else if (command == ‘D’) // delete a student from CS143_S2 else if (command == ‘U’) // update a student in CS143_S2 else if (command == ‘P’) // print all students in CS143_S2 else // invalid command

cin >> command; }

return 0;}

14

Prog5: Command Stringint main(){ SellerList myList; Seller s; string command;

cin >> command; while (!cin.eof()) { if (command == ‘Add’) // add a seller to myList else if (command == ‘Output’) // print all data for a seller else if (command == ‘Update’) // update a seller in myList else if (command == ‘Quit’) // print out all winers else // invalid command (not for Prog5)

cin >> command; }

return 0;}

15

One Member Method for a Commandint main(){ SellerList myList; Seller s; string command;

cin >> command; while (!cin.eof()) { if (command == ‘Add’) myList.Add(); else if (command == ‘Output’) myList.Output(); else if (command == ‘Update’) myList.Update(); else // if (command == ‘Quit’) // No invalid command for Prog5 myList.Quit();

cin >> command; }

return 0;}

16

Class SellerListclass SellerList{private: int numSellers; Seller sellers[MAX_SIZE]; . . .public: . . . void Add()

void Output()

void Update()

void Quit() . . .}; // SellerList

17

Pseudo Code for Method AddRead in seller name

Search to see if the seller in the list

If list is full . . .Else If seller in the list . . .Else Add seller at the end of the list (update numSellers!)

18

Pseudo Code for Method Update

Read in name, type, amount, count

Search to see if name in the list

If seller in the list . . .Else . . .

19

Command & Type: Mixed Casesint main(){ . . . string command;

cin >> command; // change all chars to lower case while (!cin.eof()) { if (command == ‘add’) myList.Add(); else if (command == ‘output’) myList.Output(); else if (command == ‘update’) myList.Update(); else // if (command == ‘quit’) myList.Quit();

cin >> command; }

return 0;}

20

21

C++ String

• Class• Member Methods

– Length– Substring– . . .

• Accessing Individual Char String s = “CS1430: Programming in C++”; cout << s[5]; // which char?

Change String to Lower Case#include <string>

. . .

class SellerList{private: . . .

public: . . .

void StringToLower( string & aWord ) { for (int i = 0; i < aWord.length(); i++) aWord[i] = tolower(aWord[i]); } . . .}

22

Command & Type: Mixed Casesint main(){ SellerList myList; string command;

cin >> command; while (!cin.eof()) { myList.StringToLower(command);

if (command == ‘add’) myList.Add(); else if (command == ‘output’) myList.Output(); else if (command == ‘update’) myList.Update(); else // if (command == ‘quit’) myList.Quit();

cin >> command; }

return 0;} 23

Schedule

• Lab 9

• Program 5

Start Early!

Work as a Team!

24

top related