VasilM

oop_decorating_3

May 19th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdio>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class Base {
  8. protected:
  9.     Base * obj;
  10. public:
  11.     Base(Base * b): obj(b) {}
  12.     virtual ~Base() {}
  13.     virtual ostream& ins( ostream& os ) const {
  14.         if(obj!= NULL) return obj->ins(os);
  15.         else return os<<"empty";
  16.     }
  17.     virtual bool reg(int fnum, int courseNum, double mark) =0;
  18.     virtual bool decoratedBy(Base * dec) {
  19.         if(obj==NULL) return false;
  20.         if(obj==dec) return true;
  21.         return obj->decoratedBy(dec);
  22.     }
  23.     Base* getOriginal() {
  24.         if(obj==NULL) return this;
  25.         return obj->getOriginal();
  26.     }
  27. };
  28. ostream& operator<<(ostream& os, const Base& obj) {
  29.     return obj.ins(os);
  30. }
  31.  
  32. Base * courses[8];
  33. Base * getCourse(int n){ return courses[n]; }
  34.  
  35. class Student: public Base {
  36.     string name;
  37.     int fnum;
  38.     int n;
  39.     double av;
  40. public:
  41.     Student(const string name, int fnum, Base* dec=NULL):
  42.       Base(dec),name(name),fnum(fnum),n(0),av(0.0) {}
  43.     ~Student() {}
  44.     ostream& ins(ostream& os) const {
  45.         os<< name <<",\tF"<< fnum;
  46.         if(n==0)os<<"\tняма положени изпити.";
  47.         else os<<"\tсреден успех: "<<setprecision(2)<<av;
  48.         return os<<endl;
  49.     }
  50.     bool reg(int fnum, int courseNum, double mark) {
  51.         if(fnum!=this->fnum) return false;
  52.         if(!decoratedBy(courses[courseNum])) return false;
  53.         av = (av*n+mark)/(n+1);
  54.         n++;
  55.         return true;
  56.     }
  57. };
  58.  
  59. class Course: public Base {
  60.     string name;
  61.     int sign;
  62. public:
  63.     Course(const string name, int sign, Base* dec=NULL):
  64.       Base(dec), name(name), sign(sign) {}
  65.     ~Course() {}
  66.     ostream& ins(ostream& os) const {
  67.         os<<"CSCB"<< sign <<" "<< name;
  68.         return os;
  69.     }
  70.     bool reg(int fnum, int courseNum, double m){ return false; }
  71. };
  72.  
  73. class Choice: public Base {
  74.     int courseNum;
  75.     double mark;
  76. public:
  77.     Choice(int courseNum, Base* dec = NULL):
  78.       Base(dec), courseNum(courseNum), mark(0.) {}
  79.     ~Choice() {}
  80.     ostream& ins(ostream& os) const {
  81.         obj->ins(os);
  82.         os<<'\t';
  83.         courses[courseNum]->ins(os);
  84.         os<<"\n\t\tоценка: ";
  85.         if(mark == 0.) os<<"неположен.";
  86.         else os<<mark;
  87.         return os<<endl;
  88.     }
  89.     bool reg(int fnum, int courseNum, double m) {
  90.         if(obj->reg(fnum, courseNum, m)) {
  91.             if(courseNum == this->courseNum) {
  92.                 mark = m;
  93.                 return true;
  94.             }
  95.         }return false;
  96.     }
  97.     bool decoratedBy(Base* dec) {
  98.         if(obj == NULL) return false;
  99.         if(dec == courses[courseNum]) return true;
  100.         return obj->decoratedBy(dec);
  101.     }
  102. };
  103.  
  104. Student * genStudent() {
  105.     static int No = 0;
  106.     char buf[15];
  107.     sprintf(buf,"Student%d", ++No);
  108.     return new Student(buf, 1000+rand()%90000);
  109. }
  110.  
  111. void fillCourses() {
  112.     courses[0] = new Course("Програмиране с Java", 302 );
  113.     courses[1] = new Course("Основи на математическия анализ", 308);
  114.     courses[2] = new Course("Бази данни и системи за управление на бази данни", 405);
  115.     courses[3] = new Course("Висша математика..", 412);
  116.     courses[4] = new Course("Увод в Microsoft Visual C# .NET", 565);
  117.     courses[5] = new Course("Програмиране с Microsoft Visual C# .NET", 567);
  118.     courses[6] = new Course("Алгоритми и структури данни", 581);
  119.     courses[7] = new Course("Скриптови езици за Интернет (PHP)", 688);
  120. }
  121.  
  122. void choices(int c[]) {
  123.     int k=0;
  124.     do{
  125.         int t = rand()%8;
  126.         int i=0;
  127.         for(; i<k; i++)if(c[i]==t) break;
  128.         if(i==k){ c[k]=t; k++; }
  129.     }while(k<6);
  130. }
  131.  
  132. void testDecoration(int n) {
  133.     setlocale(0,"");
  134.     Base ** S;
  135.     S = new Base* [n];
  136.  
  137.     cout <<"\nСтуденти:\n";
  138.     for(int i=0; i<n; i++) S[i] = genStudent();
  139.     for(int i=0; i<n; i++) cout<<*S[i] << endl;
  140.     system("PAUSE");
  141.  
  142.     cout <<"\nКурсове:\n";
  143.     for(int i=0; i<8; i++)cout<< *getCourse(i) << endl;
  144.     system("PAUSE");
  145.  
  146.     int cc[6];
  147.     cout <<"\nИзбрани курсове:\n";
  148.     for(int i=0; i<n; i++) {
  149.         cout <<*S[i] << endl;
  150.         choices(cc);
  151.         for(int k=0; k<6; k++) {
  152.             cout <<'\t'<< *courses[cc[k]] << endl;
  153.             S[i] = new Choice(cc[k],S[i]);
  154.         }
  155.     }
  156.     system("PAUSE");
  157.  
  158.     cout <<"\nСтуденти с курсове:\n";
  159.     for(int i=0; i<n; i++) {
  160.         cout <<*S[i] << endl;
  161.     }
  162.     system("PAUSE");
  163.  
  164.     cout<<"\n\tСтуденти по курсове:\n";
  165.     for(int k=0; k<8; k++) {
  166.         cout<<*courses[k]<<endl;
  167.         for(int s=0; s<n; s++) if( S[s]->decoratedBy(courses[k]) )
  168.             cout <<'\t'<<*( S[s]->getOriginal() )<< endl;
  169.     }
  170.  
  171.     cout<<"F#, C#, mark:";
  172.     int fn, cn;
  173.     double mark;
  174.     cin>> fn >> cn >> mark;
  175.     for( int i=0; i<n; i++) if( S[i]->reg(fn,cn,mark) ) cout << *S[i];
  176. }
  177.  
  178. int main() {
  179.  
  180.     fillCourses();
  181.     testDecoration(10);
  182.  
  183.     system("PAUSE");
  184.     return EXIT_SUCCESS;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment