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

24
CS 1430: Programming in C++ 1

Upload: rosamund-moore

Post on 18-Jan-2018

224 views

Category:

Documents


0 download

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

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

CS 1430: Programming in C++

1

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

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

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

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

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

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

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

Dynamic Listclass Student{ . . .};

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

5

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

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

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

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

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

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

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

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

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

Pseudo Code for Method Delete

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

10

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

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

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

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

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

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

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

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

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

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

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

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

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

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

void Output()

void Update()

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

17

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

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

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

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

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

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

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

21

C++ String

• Class• Member Methods

– Length– Substring– . . .

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

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

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

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

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

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

Schedule

• Lab 9

• Program 5

Start Early!

Work as a Team!

24