ממבחנים שאלות מבנה הנתונים תור (queue) הוא מבנה הדומה למחסנית (stack) שנלמדה בקורס. לתור ניתן להוסיף איברים (ע"י פעולה הנקראת (enqueue וניתן להסיר את האיבר שבראש התור,(dequeue) כלומר האיבר הוותיק ביותר בתור (בניגוד למחסנית בה באיבר שמוסר הוא האחרון שנוסף). כלומר מבנה הנתונים תור צריך לתמוך בפעולות הבאות: יצירה של תור חדש. 1. הריסה של תור קיים. 2. העתקה של תור קיים. 3. השמה מתור א' לתור ב'. כך שלאחר הפעולה שני התורים יהיו זהים. 4. הוספת איבר לסוף התור. 5. הסרת האיבר הראשון בתור. 6. אם התור ריק תיזרק חריגה מסוג.QueueEmpty החזרת האיבר הראשון (הוותיק ביותר) בתור. יש להחזיר את האיבר עצמו ללא העתקה. 7. גם במקרה זה אם התור ריק תיזרק חריגה מסוג.QueueEmpty ממשו תור גנרי בשפת ++C. הקפידו לכתוב רק את הקוד הדרוש. ניתן להשתמש ב- std list של שפת ++C כמו שנלמד בתרגול על ה- STL. C++ - 2 נתונות שתי המחלקות הבאות: virtual void f() { cout << "1" << endl; ; void f() { cout << "1" << endl; ; כתוב תכנית קצרה ככל הניתן המדפיסה פלט שונה לכל אחת מהמחלקות שהוצגו. כלומר, עליכם לכתוב קוד המכיל פונקצית main ומשתמש במחלקה A כך שהפלט המודפס בהרצת הקוד שונה כאשר מקמפלים אותו עם כל אחת מהמחלקות הנתונות. template<typename T> class Queue { std::list<t> items; class QueueEmpty: public std::exception {; ; void enqueue(const T& t) { items.push_back(t); void dequeue() { if (items.empty()) throw QueueEmpty(); items.pop_front(); T& front() { return items.front(); const T& front() const { return items.front(); 4 3 1
הנכם נדרשים להרחיב את המחלקה String כפי שנלמדה בתרגול 10. עליכם להוסיף תמיכה באיטרטור אשר נע לכיוון הנגדי iterator) (reverse כך שהקוד הבא יתקמפל והפלט המתקבל בהרצתו יהיה.cba #include "string.h" int main() { String s("abc"); for (String::reverse_iterator i = s.rbegin(); i!= s.rend(); ++i) { std::cout << *i; הוסיפו וממשו את ההגדרות המתאימות עבור הטיפוס.String reverse_iterator אין צורך להוסיף תמיכה גם ב- const_reverse_iterator. class B : public A { void f() { cout << "2" << endl; ; int main(int argc, char **argv) { A* a = new B(); a->f(); 6 5 נוסיף בתוך המחלקה String את הקוד הבא: class String { //... more code... class reverse_iterator { char* index; explicit reverse_iterator(char* i) : index(i) { reverse_iterator& operator++() { --index; return *this; char& operator*() const { return *index; bool operator!=(const reverse_iterator& i) const { return index!= i.index; ; reverse_iterator rbegin() { return reverse_iterator(data + length - 1); reverse_iterator rend() { return reverse_iterator(data - 1); ; class String { char* data; int length; String(); String(const char*); String(const String&); ~String(); String& operator=(const String&); int size() const; const char& operator[](int) const; char& operator[](int); ; typedef char* iterator; typedef const char* const_iterator; תזכורת, מנשק המחלקה :String (חלקי) iterator begin() { return data; const_iterator begin() const { return data; iterator end() { return data + length; const_iterator end() const { return data + length; //... more code... 8 7 2
class Shape { friend ostream& operator<<(ostream& os, const Shape& s); virtual void print(ostream& os) const = 0; ; class Circle: public Shape { virtual void print(ostream& os) const { os << "Circle: radius=" << radius; ; class Square: public Shape { virtual void print(ostream& os) const { os << "Square: side length=" << edge; ; ostream& operator<<(ostream& os, const Shape& s) { s.print(os); return os; ראינו את אוסף המחלקות עבור צורות, להלן תזכורת של הקוד עבור המחלקות class Circle : public Shape { int radius; Circle(int x, int y, int radius) : Shape(x,y), radius(radius) { virtual double area() const { return radius*radius*pi; ; class Square : public Shape { int edge; Square(int x, int y, int edge) : Shape(x,y), edge(edge) { virtual double area() const { return edge*edge; ; class Shape { int center_x, center_y; Shape(int x, int y) : center_x(x), center_y(y) { virtual ~Shape() { virtual double area() const = 0; ; בתרגול 12 האלו: ברצוננו לאפשר הדפסת של צורות ע"י אופרטור ההדפסה >> כך שעבור עיגול שרדיוסו 3 יודפס radius=3 Circle: ואילו עבור ריבוע שאורך הצלע שלו היא 2 יודפס length=2.square: side הוסיפו תמיכה באופרטור ההדפסה למחלקות הנ"ל. תארו במדויק את שינויי הקוד שלכם והיכן הם צריכים להתבצע. 10 9 typedef... T1; typedef... T2; typedef... T3; typedef... T4; more code?... int main () { תוצאה + משמעות // { try מערכיםשל 2 הגדרת // a11(10); T1 a1(12), 10 -ו double 12 בגודל// הגדרתמערךשל int בגודל // 10 a2(10); T2 a2 = a11; // Syntax error a1 = a11; // O.K. a1[5] = a2[4]; // O.K. הדפסתמערךשלם // a1; cout << הגדרתמערךקבוע // ca1(a11); const T1 עםאתחול // ca1 = a11; // Syntax error ca1[2] = a11[3]; // Syntax error a11[3] = ca1[2]; // O.K. double c_array[] = {0.5, -7, 3.14, 0.3; הגדרת "מערךC " // הגדרתמערךואתחולו // 4); a12(c_array, T1 ע "י "מערך C" // הגדרתמערךשל 5 double בגודל // a3; T3 הגדרתמערךשל 8 double בגודל // a4; T4 a3[1] = a4[2]; // O.K. a3 = a4; // Syntax error a4 = a3; // Syntax error a1 = a4; // O.K. catch (BadIndex exc) { פלט:... is cerr << exc; //Bad-Index value עליכם לממש מחלקות גנריות עבור "מערכים בטוחים". מערך בטוח הוא מערך המכיל מידע על אורכו, המאפשר הגנה מפני גלישה בשימוש. הגנריות מתבטאת בעובדה שהמימוש מאפשר ליצור מערכים שונים עבור סוגי עצמים שונים. למשל, הפקודה Array<double> vec(12 12) תיצור מערך של double בגודל 12. כדי למנוע שכפול קוד ע"י הקומפיילר (לכל instance של ה- template ), יש לאסוף את החלקים המשותפים במחלקת בסיס class arraybase ואח"כ לבצע הורשה: template <class T> class Array: public ArrayBase {... יש לממש מחלקות כדי שהתוכנית למטה תתבצע כפי שנדרש. שימו לב: בראש הקוד הושמטו שמות המחלקות; עליכם להשלים את הקוד. מומלץ לקרוא את כל הקוד לפני פתרון השאלה. נקודות): הגדרת המחלקות: 15) א סעיף הגדירו את המחלקות,3T 1, Tו-,2T T4 עם מתודות סבירות לשימוש קל ונוח במערכים (כולל קלט/פלט). שימו לב כי יש להגדיר את כל שדות הנתונים ולהצהיר על כל הפונקציות הנדרשות. אין צורך לממש שום פונקציה. הגדירו גם את המחלקה לטיפול בחריגות. נקודות): מימוש (חלק מהפונקציות של) המחלקות: 20) ב סעיף ממשו את הפונקציות הבאות בכל מחלקה בה הן מופיעות: בנאים (constructors) אין צורך לאפס ערכים לא מאותחלים, הורסים,(destructors) אופרטור פלט,(operator<<) ופעולת אינדקס,(operator[]) טפלו נכון בשגיאות. 12 11 3
class ArrayBase { int size; bool islegal(int index) const { return index >= 0 && index < size; ArrayBase(int sz): size(sz) { ; int getsize() const { return size; typedef Array<double> T1; typedef Array<int> T2; typedef SizedArray<double, 5> T3; typedef SizedArray<double, 8> T4; class BadIndex { int index; BadIndex(int i) : index(i) { friend ostream& operator<<(ostream& os, const BadIndex& b); ; ostream& operator<<(ostream& os, const BadIndex& b) { return os << "Array index " << b.index << " is out of bounds" << endl; 14 13 ~Array() { delete[] elements; T& operator[](int i) { if (!islegal(i)) throw BadIndex(i); return elements[i]; const T& operator[](int i) const { if (!islegal(i)) throw BadIndex(i); return elements[i]; array& operator=(const array& other) { if (this == &other) { return *this; delete[] elements; fillarray(other.elements,other.size()); return *this; ; template <class T> class Array: public ArrayBase { T* elements; void fillarray(t* data,int sz){ elements = new T[sz]; size = sz; for (int i=0;i<sz;i++) elements[i] = data[i]; Array(int sz) : ArrayBase(sz), elements(new T[sz]) { Array(const Array<T>& array2) : ArrayBase(array2.size) { fillarray(array2.elements,size); Array(T* array2, int sz) : ArrayBase(sz) { fillarray(array2,size); 16 15 4
#include <iostream> using namespace std; A() { cout << "A::A()" << endl; A(const A& a) : i(a.i) { cout << "" << endl; private: T i; ; class B { B(A<T> aa) : a(aa) { cout << "B::B(A)" << endl; B(const B& b) :a(b.a) { cout << "B::B(B&)" << endl; A<T> a; ; class C: public B<int> { C(A<int> aa) : B<int> (aa), a(aa) { cout << "C::C(A aa)" << endl; ~C() { cout << "C::~C()" << endl; A<int> a; ; 18 template <class T> ostream& operator<< (ostream& out, const Array<T>& array) { for (int i=0 ; i < array.getsize() ; i++) out << array[i] << ' '; return out << endl; template <class T> istream& operator>> (istream& in, Array<T>& array) { for (int i=0 ; i < array.getsize() ; i++) in >> array[i]; return in; template <class T,int N> class SizedArray: public Array<T>{ SizedArray() : Array<T>(N) {; ; 17 מה מדפיסה התכנית הבאה? int main() { cout << "--1--" << endl; A<int> a; cout << "--2--" << endl; A<double> a1; cout << "--3--" << endl; B<int> b(a); cout << "--4--" << endl; B<int> b1(b); cout << "--5--" << endl; C c(a); cout << "--6--" << endl; B<int>& b2 = c; cout << "--7--" << endl; --1-- A::A() --2-- A::A() --3-- B::B(A) --4-- B::B(B&) --5-- B::B(A) C::C(A aa) --6-- --7-- C::~C() יודפס: הגדר מחלקה/מחלקות הנדרשות בקובץ יעבור הידור (קומפילציה). Array.h על מנת שקטע הקוד הבא שים לב: רק הצהרת המחלקה/ות נדרשת - ללא מימוש הפונקציות. יש להניח שבמימוש המחלקה ישנם מצביעים. 20 19 5
#ifndef ARRAY_H_ #define ARRAY_H_ class Array { Array(int size = 100); const T& operator[](int i) const; T& operator[](int i); Array& operator+=(const Array& arr); int size() const; Array(const Array& src); Array& operator=(const Array& src); ~Array(); private: ; #include "Array.h" #include "iostream.h" int a; A(int aa = 0) : a(aa) { ; int main() { Array<int> *a1 = new Array<int>(3); //An array with 3 elements of type int Array<double> arr[20]; //An array of 20 Arrays, each one of them //is of 100 elements of type double Array<double> sum(100); //An Array of 100 elements of type double Array<double> min(100); //An Array of 100 elements of type double sum[0] = 10; sum[1] = 20; sum[2] = 30; for (int i = 0; i < 20; i++) { cin >> arr[i]; sum += arr[i]; cout << "Sum is:" << sum << endl; min = arr[0]; for (i = 1; i < 20; i++) if (arr[i] < min) min = arr[i]; cout << "Min is: " << min << endl; if (min == arr[0]) cout <<"The first Array is the minimum"<<endl; const Array<double> c_arr = sum; for (int i = 0; i < c_arr.size(); i++) { cout <<"Element #"<<i<<": "<<c_arr[i]<<endl; delete a1; Array<A> arr_a(7); Array<A> arr_a2 = arr_a; 22 21 // the next operators are global functions ostream& operator<<(ostream& out, const Array<T>& arr); istream& operator>>(istream& inp, Array<T>& arr); // the next operators may be implemented as member functions bool operator<(const Array<T>& left, const Array<T>& right); bool operator==(const Array<T>& left, const Array<T>& right); #endif // ARRAY_H_ 23 6