240 likes | 421 Views
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!.
E N D
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!
//------------------------------------------ // 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; }
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; }
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…
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)
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
One Function for each Command void AddCommand(…); void DeleteCommand(…); void UpdateCommand(…); void PrintCommand(…);
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; } }
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; } }
Need a Search Function void AddCommand(…); void DeleteCommand(…); void UpdateCommand(…); void PrintCommand(…);
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.
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?
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; }
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
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; }
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
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
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 --;
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; }
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; }
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; }
Dynamic List int main() { SectionType CS143; cs143.numStudents = 0; // InputSection(CS143); ProcessCommands(CS143); return 0; }
Schedule Prog5 Due 9:30 PM, Thursday Thursday Help on Prog5 Friday Lab 206: Prog5/Prog6