Untitled
unknown
plain_text
a year ago
3.8 kB
4
Indexable
#include <iostream> #include <string> using namespace std; class Index; class Note { friend class Index; private: string subject; float note; public: Note(string subject_="math", float note_=3) : subject(subject_), note(note_) { } ~Note() { } void SetSubject(string subject_) { subject = subject_; } bool SetNote(float note_) { if (note_ == 2. || note_ == 3. || note_ == 3.5 || note_ == 4. || note_ == 4.5 || note_ == 5.) { note = note_; return true; } else { return false; } } string GetSubject() { return subject; } float GetNote() { return note; } void Show() { cout << "Subject: " << GetSubject() << " - " << GetNote() << endl; } }; class Index { private: int nnotes; Note *notes; public: Index(int nnotes_) : nnotes(nnotes_) { notes = new Note[nnotes_]; }; ~Index() { delete[] notes; nnotes = 0; }; bool Set(int index, string subject_, float note_) { if (index >= 0 && index < nnotes) { notes[index].subject = subject_; notes[index].SetNote(note_); return 1; } return 0; } bool Change(string subject_, float note_); float Average(); void Show() { cout << "##### Index: #####" << endl; for (int i = 0; i < nnotes; i++) { cout << i + 1 << ". "; notes[i].Show(); } cout << "##### ###### #####" << endl; } void Add(string subject_, float note_); bool Remove(string subject_); friend float FindNote(Index* ind_, string subject_); }; bool Index::Change(string subject_, float note_) { for (int i = 0; i < nnotes; i++) { if (notes[i].GetSubject() == subject_) { notes[i].SetNote(note_); return true; } } return false; } float Index::Average() { if (nnotes == 0) return 2.0; float sum = 0.; for (int i = 0; i < nnotes; i++) { sum += notes[i].note; } return sum / nnotes; } void Index::Add(string subject_, float note_) { Note* temporaryNotes = new Note[nnotes + 1]; for (int i = 0; i < nnotes; i++) temporaryNotes[i] = notes[i]; delete[] notes; temporaryNotes[nnotes].SetNote(note_); temporaryNotes[nnotes].subject = subject_; notes = temporaryNotes; nnotes++; } bool Index::Remove(string subject_) { int element = -1; for (int i = 0; i < nnotes; i++) { if (notes[i].subject == subject_) { element = i; break; } } if (element < 0) return false; Note* temporaryNote = new Note[nnotes - 1]; for (int i = 0; i < element; i++) temporaryNote[i] = notes[i]; for (int i = element + 1; i < nnotes; i++) temporaryNote[i - 1] = notes[i]; delete[] notes; notes = temporaryNote; nnotes--; return true; } float FindNote(Index *ind_, string subject_) { for (int i = 0; i < ind_->nnotes; i++) { if (ind_->notes[i].GetSubject() == subject_) { return ind_->notes[i].GetNote(); } } return -999.; } int main() { cout << "Hello World!\n"; Index ind(3); ind.Set(0, "Mathematical Analysis", 3.0); ind.Set(1, "Algebra", 4.5); ind.Set(2, "Physics", 3.5); ind.Show(); cout << "Algebra: " << FindNote(&ind, "Algebra") << endl; ind.Change("Algebra", 3.0); ind.Show(); ind.Add("C++Programing", 5.0); ind.Show(); ind.Remove("Physics"); ind.Show(); return 0; }
Editor is loading...
Leave a Comment