1 / 24

Struct StudentType

Struct StudentType. struct StudentType { string id; string firstName, lastName; float gpa; };. Struct SectionType. const int MAX_SIZE = 30; struct SectionType { int numStudents; StudentType students[MAX_SIZE]; }; // A list of students // Size is inside the structure!.

chloe
Télécharger la présentation

Struct StudentType

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Struct StudentType struct StudentType { string id; string firstName, lastName; float gpa; };

  2. Struct SectionType const int MAX_SIZE = 30; struct SectionType { int numStudents; StudentType students[MAX_SIZE]; }; // A list of students // Size is inside the structure!

  3. //------------------------------------------ // The function inputs data to a // SectionType sec. // Parameter: ( out ) //------------------------------------------ void inputSection(SectionType& sec) { cin >> sec.numStudents; for (int i = 0; i < sec.numStudents; i ++) GetStudent(sec.students[i]); // sec.students[i] = GetStudent(); return; }

  4. Static List int main() { SectionType CS143; int index; // Input data to CS143 inputSection(CS143); // Update gpa of the last student ModifyGpa(CS143.students[CS143.numStudents - 1]); index = IndexOfMaxGPA(CS143); cout << "The student with highest GPA: " << endl; DisplayStudent(CS143.students[index]); return 0; }

  5. Dynamic List SectionType CS143; string command; cs143.numStudents = 0; // Empty list at the beginning // inputSection(CS143); cin >> command; // Prime Read! while (command != “Quit”) { // Process command cin >> command; } // Command: Add, Delete // Update, Display…

  6. Dynamic List SectionType CS143; char command; // string command; cs143.numStudents = 0; // Empty list at the beginning // inputSection(CS143); cin >> command; // Prime Read! while (command != ‘Q’) { // Process command cin >> command; } // Command: A(dd), U(pdate), D(elete), // S(how)

  7. Dynamic List Input File Add 123 Qi Yang 2.6 Add 234 Tom Scanlan 3.5 Add 345 Mike Rowe 2.5 Update 123 -0.3 Update 345 0.5 Print 123 Delete 123 Add 456 Rob Hasker 3.7 Quit

  8. One Function for each Command void AddCommand(…); void DeleteCommand(…); void UpdateCommand(…); void PrintCommand(…);

  9. Function to Process Commands void ProcessCommands(…) { string command; cin >> command; // Prime read! while (command != “Quit”) { if (command == “Add”) AddCommand(…); else if (command == “Update”) UpdateCommand(…); else if (command == “Delete”) DeleteCommand(…); else if (command == “Print”) PrintCommand(…); else cout << “Invalid command!”; cin >> command; } }

  10. Switch Statement void ProcessCommands(…) { char command; cin >> command; // Prime read! while (command != ‘Q’) { switch (command) { case ‘A’: AddCommand(…); break; case ‘U’: UpdateCommand(…); break; case ‘D’: DeleteCommand(…); break; case ‘P’: PrintCommand(…); break; default: cout << “Invalid command!”; } cin >> command; } }

  11. Need a Search Function void AddCommand(…); void DeleteCommand(…); void UpdateCommand(…); void PrintCommand(…);

  12. Search Function //---------------------------------------------- // The function has two parameters: // sec: SectionType // id : string // The function searches id in sec based on ID. // It returns the index of the student if id // is found in sec, or -1 otherwise. // Parameters: ( In , In ) //---------------------------------------------- int FindStudent(const SectionType& sec, string id) { for (int i = 0; i < sec.numStudents; i++) if (sec.students[i].id == id) return i; return -1; } // Could search on firstName and lastName, or DOB. // Works for an empty list.

  13. Function AddCommand Input data Add 123 Qi Yang 2.6 Pseudo Code Input data If the list is full Message: Can not add Else Search ID in the list If ID not found Insert Student Else What to do?

  14. Command Add Input data Add 123 Qi Yang 2.6 Pseudo Code Input data If the list is full Display message Else Search ID in the list If ID not found Insert Student Else Display message void CommandAdd(SectionType& sec ) { StudentType s; int index; GetStudent( s ); if (sec.numStudents == MAX_SIZE) { // Display message return; } // Search the list index = FindStudent(sec, s.id); if (index == -1) { // Where to insert: at the end // At index sec.numStudents sec.students[sec.numStudents] = s; sec.numStudents ++; } else cout << “message.”; return; }

  15. Command Update Input Data Update 123 0.3 Pseudo Code Input data Search ID in the list If not found Display message Else Update GPA

  16. Command Update Input Data Update 123 0.3 Pseudo Code Input data Search ID in the list If not found Display message Else Update GPA // Parameters: ( ? ) void CommandUpdate(SectionType& sec) { string id; float amount; cin >> id >> amount; int index = FindStudent(sec, id); if (index == -1) cout << “message.”; else sec.students[index].gpa += amount; return; }

  17. Command Delete Input Data Delete 123 Pseudo Code Input data Search ID in the list If not found Display message Else Delete the student from the list

  18. Delete an Array Element size: 9 Element to be deleted: at index 6 0 1 2 3 4 5 6 7 8 // Move Array[7] to Array[6] Array[6] = Array[7]; 0 1 2 3 4 5 6 7 8 // Move Array[8] to Array[7] Array[7] = Array[8]; 0 1 2 3 4 5 6 7 // Update size size --; // size 8; index: 0 to 7

  19. Delete an Array Element size: 9 Element to be deleted: at index 6 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 --;

  20. Command Delete Input Data Delete123 Pseudo Code Input data Search ID in the list If not found Display message Else Delete the student // Parameters: ( ? ) void CommandDelete(SectionType& sec) { int index; string id; cin >> id; index = FindStudent(sec, id); if (index == -1) cout << “message”; else { int i; for (i = index; i < sec.numStudents – 1; i ++) sec.students[i] = sec.students[i + 1]; sec.numStudents --; } return; }

  21. Dynamic List int main() { SectionType CS143; string command; cs143.numStudents = 0; // InputSection(CS143); cin >> command; // Prime Read! while (command != “Quit”) { if (command == “Add”) CommandAdd(sec); else if (command = “Update”) CommandUpdate(sec); else if (command = “Delete”) CommandDelete(sec); else cout << “\nInvalid command!”; cin >> command; } return 0; }

  22. int main() { SectionType CS143; Transaction trans; cs143.numStudents = 0; GetTransaction( trans ); // Prime Read! while (trans.command != ‘Q’) { switch (trans.command) { case ‘A’: CommandAdd(sec); break; case ‘U’: CommandUpdate(sec); break; case ‘D’ CommandDelete(sec); break; case others: cout << “\nInvalid command!”; GetTransaction( trans ); } return 0; }

  23. Dynamic List int main() { SectionType CS143; cs143.numStudents = 0; // InputSection(CS143); ProcessCommands(CS143); return 0; }

  24. Schedule Prog5 Due 9:30 PM, Thursday Thursday Help on Prog5 Friday Lab 206: Prog5/Prog6

More Related