MFC.cpp


// Problem definition by Eli Tilevich:

// Well,  I guess I would like to code a simple registration system with a very
// limited functionality which
// would only allow students register for classes, cancel classes, and delete
// students from the system.
// Basically, it would be something very similar to test18e.c; however I would
// like to specifically concentrate on
// handling the many-to-many relation between students and classes. I would
// like to answer the question of which
// facilities in different class libraries help the programmer handle this
// non-trivial software paradigm.
// --------------------------------------------------------------------------

// Program coded by Eli Tilevich, Nov.28/99


#include <stdio.h>
#include <string.h>
#include <afxtempl.h>

long  starttime;

#define TASK(s)    printf("%s", s);
#define CIN        starttime = clock();
#define COUT       printf("%d\t", clock()-starttime);
#define NL         printf("\n")

#define EQ_STR(s1, s2) (strcmp(s1, s2) == 0)



class Student;
class Class;
class Takes;

typedef CTypedPtrList<CPtrList, Student*> Student_list;
typedef CTypedPtrList<CPtrList, Class*>   Class_list; 
typedef CTypedPtrList<CPtrList, Takes*>   Takes_list; 

// School encapsulates your problem
class School {

Student_list	lstStudents;
Class_list		lstClasses;
Takes_list		lstSC;

public:
    
	static char *getName(int i,char *buf);

	Class* add_class(const char * name);
	
	Student * add_student(const char * name);

	Student * find_student(const char * name);

	void list_student_classes(Student * sp);

	Takes* find_student_class(const Student *sp, const char * class_name);
	

	Class* find_class(const char * name);

	void add_student_class(Student * sp, Class * cp);

	void delete_student_class(Takes * tp);

	void delete_student(Student * sp);



};


class Student {
   
char strName[1<<5];
public:
    Student(const char *name){
		strcpy(strName, name);
	
	}
	const char * GetName() const { return strName; }
};

class Class {

char strName[1<<5];
public:
    Class(const char *name){
		strcpy(strName, name);
	
	}
	const char * GetName() const { return strName; }
};

class Takes {
    int mark;
    int absentDays;
	Student *sp;
	Class	*cp;
    
public:
	Takes(Student *s = 0, Class *c = 0) : sp(s), cp(c) {}

    Student * GetStudent() const { return sp; }
	Class * GetClass() const { return cp; }

    int addAbsent(int d){absentDays+=d; return absentDays;} // for d=0 reports
    int getMark(){return mark;}
    void setMark(int m){mark=m;}
};



// pick up names including spaces, if i>0 skip the first word
char* School::getName(int i,char *buf){
    char *p;
    for(p=buf; *p!='\n'; p++)continue;
    *p='\0'; // mark the end of the name
    if(i==0)return buf;
    for(p=buf; *p!='\0' && *p!=' '; p++)continue;
    if(*p=='\0')return p;
    for( ;*p==' ' && *p!='\0'; p++);
    return p;
}



Student * School::add_student(const char * name){

	printf("new student=%s\n",name);
    Student * sp=new Student(name);
    lstStudents.AddTail(sp);
		
	return sp;
}
	
Class * School::add_class(const char * name){
	
	//printf("... new class\n");
    
	Class * cp = new Class(name);
    lstClasses.AddTail(cp);

	return cp;
	
}



Student * School::find_student(const char * name){
        
	Student * sp = NULL;
	
	POSITION pos = lstStudents.GetHeadPosition();
	
	while(pos != NULL){
		sp = lstStudents.GetNext(pos);
		if(EQ_STR(sp->GetName(), name))
			return sp;
	}
    
	return NULL;
}

void School::list_student_classes(Student * sp){

	
	printf("%s is enrolled in:\n", sp->GetName());
	
	Takes * tp;
	POSITION pos = lstSC.GetHeadPosition();
	
	while(pos != NULL){
		
		tp = lstSC.GetNext(pos);
		
		if(EQ_STR(tp->GetStudent()->GetName(), sp->GetName()))		
			printf("%s\n", tp->GetClass()->GetName());
	}
	
}


Takes* School::find_student_class(const Student *sp, const char * class_name){
    		
	Takes * tp;
	POSITION pos = lstSC.GetHeadPosition();
	
	while(pos != NULL){
	
		tp = lstSC.GetNext(pos);
		if(EQ_STR(tp->GetStudent()->GetName(), sp->GetName()) &&
		   EQ_STR(tp->GetClass()->GetName(), class_name))
			   return tp;
	}
	
	return NULL;
}



Class * School::find_class(const char * name){

	Class *cp;
	
	POSITION pos = lstClasses.GetHeadPosition();

	while(pos != NULL){
		cp = lstClasses.GetNext(pos);
		if(EQ_STR(cp->GetName(), name))
			return cp;
	
	
	}

	return NULL;
}


void School::add_student_class(Student * sp, Class * cp){
	
	Takes * tp = new Takes(sp, cp);

	lstSC.AddTail(tp);

}


void School::delete_student_class(Takes * tp){
	
	
	Takes * t;

	POSITION pos = lstSC.GetHeadPosition();
	while(pos != NULL){
		t = lstSC.GetNext(pos);
		if(t == tp){
			lstSC.RemoveAt(pos);
			delete tp;
			break;
		}

	}
	
}



void School::delete_student(Student * sp){

TASK("Timing the performace of School::delete_student (MFC)\n");
CIN;
	
	
	//delete Student's Takes relations
	Takes * t;
	POSITION pos = lstSC.GetHeadPosition();
	while(pos != NULL){

		POSITION del = pos;
		t = lstSC.GetNext(pos); 
		
		if(EQ_STR(t->GetStudent()->GetName(), sp->GetName())){
			lstSC.RemoveAt(del);
			delete t;
		}
	}
	

	pos = lstStudents.Find(sp);
	//delete Student
	lstStudents.RemoveAt(pos);
	delete sp;

COUT;
NL;


}



#define BSIZE 80
char buff[BSIZE];

main(int argc,char **argv) {
    
	School *school; Student *sp; Takes *tp; Class *cp;
    char *name,cc;

    school=new School;
    
	for(;;){
        printf("type your name:\n");
        fgets(buff,BSIZE,stdin);
        name=school->getName(0,buff);
        if(!strcmp(name,"STOP"))break;
		
		sp = school->find_student(name);
        
        if(sp){
            if(strcmp(name, "eli") != 0)
				school->list_student_classes(sp);
		
		}
        else {
            sp = school->add_student(name);
			//Generate test data for timing
			if(strcmp(name, "eli") == 0){
				
				char class_name[1<<5];               
                for(int cl = 0; cl < 100000; cl ++){
					sprintf(class_name, "CS%d", cl);
				    cp = school->add_class(class_name);
                    school->add_student_class(sp, cp);
									
				}
            			
			}
       
        }
        for(;;){
            printf("menu(A className,D className,R=remove student,E=exit):\n");
            fgets(buff,BSIZE,stdin);
            sscanf(buff,"%c",&cc);
            name = school->getName(1,buff);
            if(cc=='A'){
                // first check that this student isn't already registered there
                tp = school->find_student_class(sp, name);
				if(tp){ printf("... you are already registered\n"); continue;}

                // find the class
                cp = school->find_class(name);
				if(!cp){
                    cp = school->add_class(name);
                }
                school->add_student_class(sp, cp);
            }
            else if(cc=='D'){
                tp = school->find_student_class(sp, name);
                if(!tp){
                    printf(" you are not registered for this class\n");
                    continue;
                }
                school->delete_student_class(tp);
            }
            else if(cc=='R'){
				school->delete_student(sp);
                break;
            }
            else if(cc=='E')break;
            else printf("unknown command=%c\n",cc);
        }
    }

    return(0);
}