סדנא תרגיל 9

benny++C, Uncategorized, נכתב ע"י Benny, סדנא ב++C, תרגילים, תרגילים בסדנא ב++C2 Comments

BA.h

#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "Student.h"


class BA:public Student   // inherete fron Studet class
{
	int* grades;
	int grades_size;
public:

	void set_grades(int n);
	int get_grades(int index);
	
	double average();
	virtual void print();
	virtual	bool milga ();
	BA(int,char*,char*,int,int*);
	~BA(void);
};


BA.cpp

#include "BA.h"
#include "Student.h"
#include <iostream>
#include <string>

using namespace std;

BA::BA(int id,char* f_name,char* L_name,int num_courses,int*arr):Student(id, f_name, L_name,num_courses)
{
	grades=new int[num_courses];
	for (int i = 0; i < num_courses; i++)
	{
		grades[i]=arr[i];
	}
	this->grades_size=num_courses;
}

void BA::print()
{
	cout<<"ID: "<<this->get_id()<<endl;
	cout<<"first name: "<<this->get_f_name()<<endl;
	cout<<"last name: "<<this->get_L_name()<<endl;
	cout<<"num courses: "<<this->get_num_courses()<<endl;
	cout<<"grades: ";
	for (int i = 0; i < this->get_num_courses(); i++)
	{
		cout<<grades[i]<<" ";
	}
	cout<<endl;
}
/*=========set get ==============*/
void BA::set_grades(int n)
{
	for (int i = 0; i < this->get_num_courses(); i++)
	{
		grades[i]=n;
	}
}

int BA::get_grades(int index)
{
	return grades[index];
}


/*============================*/

double BA::average()
{
	int sum=0;
	double average;
	for (int i = 0; i < this->get_num_courses(); i++)
	{
		sum+=grades[i];
	}
	average=sum/get_num_courses();
	return average;
}


// function that return true if the studet need to get milga

bool BA:: milga ()
{
	if (this->get_num_courses()>=10&&this->average()>95)
	{
		return true;
	}
	return false;
}

BA::~BA(void)
{
    delete[]grades;
}

MA.h

#pragma once
#include <iostream>
#include <string>
#include "Student.h"
#include "BA.h"
using namespace std;
class MA:public BA   // inherete fron BA class
{
	bool research;
public:
	virtual void print();
	virtual	bool milga ();

	MA(int,char*,char*,int,int*,bool research);
	~MA(void);
};


MA.cpp

#include "MA.h"
#include "BA.h"
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;

MA::MA(int id,char* f_name,char* L_name,int num_courses,int*arr,bool research):BA(id, f_name, L_name,num_courses,arr)
{
	this->research=research;
	
}

void MA::print()
{
	cout<<"ID: "<<this->get_id()<<endl;
	cout<<"first name: "<<this->get_f_name()<<endl;
	cout<<"last name: "<<this->get_L_name()<<endl;
	cout<<"num courses: "<<this->get_num_courses()<<endl;
	cout<<"grades: ";
	for (int i = 0; i < this->get_num_courses(); i++)
	{
		cout<<get_grades(i)<<" ";
	}
	cout<<endl;
	cout<<"research: ";
	if (this->research)   // if the student do a research 
	{
		cout<<"yes"<<endl;
	}
	else                    // if the student do not do a research 
	{
		cout<<"no"<<endl;
	}
}
// function that return true if the studet need to get milga
bool MA:: milga ()
{
	if (this->get_num_courses()>=7&&this->average()>90&&(this->research))
	{
		return true;
	}
		return false;
}


MA::~MA(void)
{
}

PHD.h

#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "Student.h"
class PHD:public Student  // inherete fron Studet class
{
	int week_hours;
public:
	virtual	bool milga ();
	virtual void print();
	PHD(int,char*,char*,int,int);
	~PHD(void);
};


PHD.cpp

#include "PHD.h"
#include <iostream>
#include <string>
using namespace std;
#include "Student.h"

PHD::PHD(int id,char* f_name,char* L_name,int num_courses,int week_hours):Student(id, f_name, L_name,num_courses)
{
	this->week_hours=week_hours;
	
}


void PHD::print()
{
	cout<<"ID: "<<this->get_id()<<endl;
	cout<<"first name: "<<this->get_f_name()<<endl;
	cout<<"last name: "<<this->get_L_name()<<endl;
	cout<<"num courses: "<<this->get_num_courses()<<endl;
	cout<<"hours: "<<this->week_hours<<endl;
}

// function that return true if the studet need to get milga
 
bool PHD:: milga ()
{

	if (this->get_num_courses()==2&&this->week_hours>=25)
	{
		return true;
	}
		return false;
}


PHD::~PHD(void)
{
}


Student.h

#pragma once
#include <string>

class Student      // abstract calss (can not create Student viriable)
{
	int id;
	char* f_name;
	char* L_name;
	int num_courses;
public:

	int get_id();
	char* get_f_name();
	char* get_L_name();
	int get_num_courses();

	Student(int,char*,char*,int);
	virtual void print()=0;          // virtual function
	virtual	bool milga ()=0;
	~Student(void);
};


Student.cpp

#include "Student.h"
#include <string>
#include <iostream>
using namespace std;


Student::Student(int id,char* f_name,char* L_name,int num_courses)
{
	this->id=id;
	this->f_name=f_name;
	this->L_name=L_name;
	this->num_courses=num_courses;
}

/*=========set get ==============*/

int Student::get_id()
{
	return id;
}
char* Student::get_f_name()
{
	return f_name;
}
char* Student::get_L_name()
{
	return L_name;
}
int Student::get_num_courses()
{
	return num_courses;
}
Student::~Student(void)
{
}

main.cpp


 /******************************
 

 targil: 8/1 2 3
 lecturer: david Cohen
 discription: In this program  using STL container, which contains all kinds of students (BA, MA, PHD).
 and do oparations on it
 *******************************/
/
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include "PHD.h"
#include "MA.h"
using namespace std;
 enum option
 {
	 stop,                                         //End the program.
	 addNewStudent,                                //add new student.
     milgaList,                  //print details of all students that eligible for a scholarship.
     mostResearch,               //print name of the student that deals with the most hours of study.
     countMilgaBA,               //print students of BA that eligible for a scholarship.
     noResearchMA,              //print name of students of MA that no deals whit study.
     overheadStudent,           //print if exists student that learning 15 courses.
     removePHDFailers,          //delete students of PHD do not study any course. 
};
//the function gets vector and add student.
void add(vector<Student*>&_vec)
{
    int* grades;
	int tmp,id,_numCourse,numOfHoursInResearch;
	bool research;
	char* _firstName=new char[20];
	char* _lastName=new char[20];
	Student* tmpStu;
	cout<<"enter 1 to add a BA student\n";
	cout<<"enter 2 to add a MA student\n";
	cout<<"enter 3 to add a PHD student\n";
	cin>>tmp;
	cout<<"enter id, first name, last name and number Of Courses\n";
	cin>>id>>_firstName>>_lastName>>_numCourse;
	switch (tmp)
	{
	case 1:
		cout<<"enter "<<_numCourse<<" grades\n";
		grades=new int[_numCourse];
		for (int i = 0; i <_numCourse; i++)
			cin>>grades[i];
		tmpStu = new BA(id,_firstName,_lastName,_numCourse,grades);
		break;
	case 2:
		cout<<"enter "<<_numCourse<<" grades\n";
		grades=new int[_numCourse];
		for (int i = 0; i <_numCourse; i++)
			cin>>grades[i];
		cout<<"enter 1 if the student does research and 0 if not\n";
		cin>>research;
		tmpStu= new MA(id,_firstName,_lastName,_numCourse,grades,research);
		break;
	case 3:
		cout<<"enter number of research hours\n";
		cin>>numOfHoursInResearch;
		tmpStu = new PHD(id,_firstName,_lastName,_numCourse,numOfHoursInResearch);
		break;
	default:cout<<"error"<<endl;
		break;
	}
	_vec.push_back(tmpStu);
}
//print details of all students that eligible for a scholarship.
void milga (vector<Student*>&_vec)
{
	vector<Student*>::iterator itS;
	for(itS=_vec.begin();itS!=_vec.end();itS++) 
      {
			if((*itS)->milga())
				((*itS))->print();
	  }
}
//return the PHD that deals with the most hours of study.
Student* mostResearchHours (vector<Student*>&_vec)
{
	Student*tmp=NULL;
	int numResearch=0;
	vector<Student*>::iterator itS;
	for (itS=_vec.begin();itS!=_vec.end();itS++)
	{
		if ((*itS)->studType()=="PHD")
			if(((PHD*)(*itS))->getNumOfHoursInResearch()>numResearch)
			{
				numResearch=((PHD*)(*itS))->getNumOfHoursInResearch();
				tmp=((PHD*)(*itS));
			}
	}
	return tmp;
}
int main()
{
	string tmp;
	Student* s;
    vector<Student*>vec;
	vector<Student*>::iterator itS;
	int op,mone=0,mone2=0;
	bool exists=0;
	cout<<"enter 0-7\n";
	cin>>op;
	while(op!=stop)
	{
		switch (op)
		{
		case addNewStudent:add(vec);//add new student					
			break;
		case milgaList:milga(vec);//print details of all students that eligible for a scholarship.
			break;
		case mostResearch: 
			//print name of the student(PHD) that deals with the most hours of study.
			s=mostResearchHours(vec);	
			cout<<"PHD studnet with most research hours: ";
			cout <<s->getFirstName()<<' '<<s->getLastName()<<endl;
			break;
		case countMilgaBA:
			//print students of BA that eligible for a scholarship.
			std::for_each(vec.begin(),vec.end(),[&mone](Student *x){if(x->studType()=="BA"&& x->milga())mone++;});
			cout<<"#BA studnets for milga: "<<mone<<endl;
			break;
		case noResearchMA://print name of students of MA that no deals whit study.
			cout<<"list of no research MA students : ";
			std::for_each(vec.begin(),vec.end(),[](Student *x){if(x->studType()=="MA"&&!((MA*)x)->getParticipatedInResearch())cout<<((MA*)x)->getFirstName()<<" "<<((MA*)x)->getLastName()<<endl;});
			break;
		case overheadStudent://print if exists student that learning 15 courses.
			std::for_each(vec.begin(),vec.end(),[&exists](Student *x){if(x->getNumCourse()>=15)exists=1;cout<<"there is a student that takes more than 15 courses\n";});
			if(!exists)
				cout<<"no student takes more than 15 courses\n";	
			break;
		case removePHDFailers://delete students of PHD do not study any course. 
			std::for_each(vec.begin(),vec.end(),[&vec,&mone2](Student *x){if(x->studType()=="PHD"&&x->getNumCourse()==0)vec.erase(vec.begin()+mone2);++mone2;});
			std::for_each(vec.begin(),vec.end(),[](Student *x){x->print();});
			
			break;
		};
		cout<<"enter 0-7\n";
		cin>>op;
	}
	return 0;
} 

2 Comments on “סדנא תרגיל 9”

  1. השמות של הפונקציות בתוכנית הראשית של שאלה 9 לא מתאימים לשמות של הפונקציות / השדות של המחלקה Student ו/או במחלקות היורשות ממנה.
    מה זה אומר #91& ?
    לא הוספה הפונקציה ()string studType. כך שבתוכנית הראשית יש שגיאה כאשר כותבים ("if ((*itS)->studType()=="PHD
    תודה רבה 🙂

  2. תגובה לאפרת והערה לצוות ws:
    הבעיה היא לא בפונקציה ()studType, כי במקומות אחרים זה כן עובד, אלא הבעיה היא בהגדרה של הפונקציה mostResearchHours או בהגדרה של האיטרטור. (כי אם שמים בהערה את השורה שהזכרת מקודם התוכנית נתקעת בשורה שאחרי.)
    אשמח לתיקון. תודה רבה על העזרה!!

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *