Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)

Similar presentations


Presentation on theme: "CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)"— Presentation transcript:

1 CS 1430: Programming in C++ 1

2 Class StudentList class 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

3 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 << s.GetLast() << “ is not in the list”; return 0; } No Adding or Deleting! 3

4 Dynamic List int 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

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

6 More Methods for Class StudentList class 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

7 Adding an Element to a List Adding 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

8 Pseudo Code for Adding Method If the list is full Can not add Else Search the list If not found Insert Student Else What to do? (do not add) 8

9 Add Method for Class StudentList const 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

10 Pseudo Code for Method Delete Search in the list If not found return false Else delete the student from the list return true 10

11 Delete an Array Element 101520503340556039??.... 10152050334060 39.... 0 1 2 3 4 5 6 7 8 1015205033406039.... // 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

12 Delete an Array Element 101520503340556039??.... 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

13 Delete Method for Class StudentList class 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

14 Command Char int 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

15 Prog5: Command String int 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

16 One Member Method for a Command int 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

17 Class SellerList class SellerList { private: int numSellers; Seller sellers[MAX_SIZE];... public:... void Add() void Output() void Update() void Quit()... }; // SellerList 17

18 Pseudo Code for Method Add Read 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

19 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

20 Command & Type: Mixed Cases int 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? 21

22 Change String to Lower Case #include... class SellerList { private:... public:... void StringToLower( string & aWord ) { for (int i = 0; i < aWord.length(); i++) aWord[i] = tolower(aWord[i]); }... } 22

23 Command & Type: Mixed Cases int 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

24 Schedule Lab 9 Program 5 Start Early! Work as a Team! 24


Download ppt "CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)"

Similar presentations


Ads by Google