Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <string>
- using namespace std;
- class Base {
- protected:
- Base * obj;
- public:
- Base(Base * b): obj(b) {}
- virtual ~Base() {}
- virtual ostream& ins( ostream& os ) const {
- if(obj!= NULL) return obj->ins(os);
- else return os<<"empty";
- }
- virtual bool reg(int fnum, int courseNum, double mark) =0;
- virtual bool decoratedBy(Base * dec) {
- if(obj==NULL) return false;
- if(obj==dec) return true;
- return obj->decoratedBy(dec);
- }
- Base* getOriginal() {
- if(obj==NULL) return this;
- return obj->getOriginal();
- }
- };
- ostream& operator<<(ostream& os, const Base& obj) {
- return obj.ins(os);
- }
- Base * courses[8];
- Base * getCourse(int n){ return courses[n]; }
- class Student: public Base {
- string name;
- int fnum;
- int n;
- double av;
- public:
- Student(const string name, int fnum, Base* dec=NULL):
- Base(dec),name(name),fnum(fnum),n(0),av(0.0) {}
- ~Student() {}
- ostream& ins(ostream& os) const {
- os<< name <<",\tF"<< fnum;
- if(n==0)os<<"\tняма положени изпити.";
- else os<<"\tсреден успех: "<<setprecision(2)<<av;
- return os<<endl;
- }
- bool reg(int fnum, int courseNum, double mark) {
- if(fnum!=this->fnum) return false;
- if(!decoratedBy(courses[courseNum])) return false;
- av = (av*n+mark)/(n+1);
- n++;
- return true;
- }
- };
- class Course: public Base {
- string name;
- int sign;
- public:
- Course(const string name, int sign, Base* dec=NULL):
- Base(dec), name(name), sign(sign) {}
- ~Course() {}
- ostream& ins(ostream& os) const {
- os<<"CSCB"<< sign <<" "<< name;
- return os;
- }
- bool reg(int fnum, int courseNum, double m){ return false; }
- };
- class Choice: public Base {
- int courseNum;
- double mark;
- public:
- Choice(int courseNum, Base* dec = NULL):
- Base(dec), courseNum(courseNum), mark(0.) {}
- ~Choice() {}
- ostream& ins(ostream& os) const {
- obj->ins(os);
- os<<'\t';
- courses[courseNum]->ins(os);
- os<<"\n\t\tоценка: ";
- if(mark == 0.) os<<"неположен.";
- else os<<mark;
- return os<<endl;
- }
- bool reg(int fnum, int courseNum, double m) {
- if(obj->reg(fnum, courseNum, m)) {
- if(courseNum == this->courseNum) {
- mark = m;
- return true;
- }
- }return false;
- }
- bool decoratedBy(Base* dec) {
- if(obj == NULL) return false;
- if(dec == courses[courseNum]) return true;
- return obj->decoratedBy(dec);
- }
- };
- Student * genStudent() {
- static int No = 0;
- char buf[15];
- sprintf(buf,"Student%d", ++No);
- return new Student(buf, 1000+rand()%90000);
- }
- void fillCourses() {
- courses[0] = new Course("Програмиране с Java", 302 );
- courses[1] = new Course("Основи на математическия анализ", 308);
- courses[2] = new Course("Бази данни и системи за управление на бази данни", 405);
- courses[3] = new Course("Висша математика..", 412);
- courses[4] = new Course("Увод в Microsoft Visual C# .NET", 565);
- courses[5] = new Course("Програмиране с Microsoft Visual C# .NET", 567);
- courses[6] = new Course("Алгоритми и структури данни", 581);
- courses[7] = new Course("Скриптови езици за Интернет (PHP)", 688);
- }
- void choices(int c[]) {
- int k=0;
- do{
- int t = rand()%8;
- int i=0;
- for(; i<k; i++)if(c[i]==t) break;
- if(i==k){ c[k]=t; k++; }
- }while(k<6);
- }
- void testDecoration(int n) {
- setlocale(0,"");
- Base ** S;
- S = new Base* [n];
- cout <<"\nСтуденти:\n";
- for(int i=0; i<n; i++) S[i] = genStudent();
- for(int i=0; i<n; i++) cout<<*S[i] << endl;
- system("PAUSE");
- cout <<"\nКурсове:\n";
- for(int i=0; i<8; i++)cout<< *getCourse(i) << endl;
- system("PAUSE");
- int cc[6];
- cout <<"\nИзбрани курсове:\n";
- for(int i=0; i<n; i++) {
- cout <<*S[i] << endl;
- choices(cc);
- for(int k=0; k<6; k++) {
- cout <<'\t'<< *courses[cc[k]] << endl;
- S[i] = new Choice(cc[k],S[i]);
- }
- }
- system("PAUSE");
- cout <<"\nСтуденти с курсове:\n";
- for(int i=0; i<n; i++) {
- cout <<*S[i] << endl;
- }
- system("PAUSE");
- cout<<"\n\tСтуденти по курсове:\n";
- for(int k=0; k<8; k++) {
- cout<<*courses[k]<<endl;
- for(int s=0; s<n; s++) if( S[s]->decoratedBy(courses[k]) )
- cout <<'\t'<<*( S[s]->getOriginal() )<< endl;
- }
- cout<<"F#, C#, mark:";
- int fn, cn;
- double mark;
- cin>> fn >> cn >> mark;
- for( int i=0; i<n; i++) if( S[i]->reg(fn,cn,mark) ) cout << *S[i];
- }
- int main() {
- fillCourses();
- testDecoration(10);
- system("PAUSE");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment