Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BiddingSystem.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /* Maximum length of any name. */
- #define LENGTH 101
- /* Struct definitions. */
- typedef struct courseRequest{
- char courseName[LENGTH];
- int points;
- struct courseRequest *next;
- }CourseRequest;
- typedef struct student{
- char name[LENGTH];
- CourseRequest* courseRequests;
- struct student *next;
- }Student;
- typedef struct course{
- char courseName[LENGTH];
- int numOfPlaces;
- Student* students;
- struct course * next;
- }Course;
- typedef struct biddingSystem{
- Student *students;
- Course *courses;
- }BiddingSystem;
- /* Function declarations. */
- Student *CreateNewStudent(const char *name, CourseRequest* courseRequests);
- CourseRequest *CreateNewCourseRequest(const char *name, int numOfPoints);
- Course *CreateNewCourse(const char *name, int numOfPlaces);
- void FreeCourseRequests(Student* list);
- void FreeStudents(Student* list);
- void FreeCourses(Course *list);
- void AddStudentToSystem(BiddingSystem *bidSys, Student *s);
- void AddCourseRequestToStudent(Student *s, CourseRequest *cr);
- void AddCourseToSystem(BiddingSystem* bidSys, Course *c);
- Student *ExtractEnrolledStudents(BiddingSystem *bidSys, const char* courseName);
- Student *ExtractRegisteredStudents(BiddingSystem *bidSys, const char* courseName, int numOfPlaces);
- void RunBidding(BiddingSystem *bidSys);
- int NumRegisteredInCourse(BiddingSystem *bidSys, const char *name);
- int IsStudentInCourse(BiddingSystem *bidSys, const char *courseName, const char *studentName);
- void InitSystem(BiddingSystem *bids);
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Student *ExtractEnrolledStudents(BiddingSystem *bidSys, const char* courseName)
- {
- Student *enrolled = NULL;
- Course *coursePtr = NULL;
- Student *studentPtr = NULL;
- CourseRequest *crPtr = NULL;
- CourseRequest *newRequest = NULL;
- Student *newStudent = NULL;
- //find course in the system
- for (coursePtr = bidSys->courses; coursePtr != NULL; coursePtr = coursePtr->next)
- {
- if (strcmp(coursePtr->courseName, courseName) == 0)
- {
- break;
- }
- }
- //if course not found
- if (coursePtr == NULL)
- {
- return NULL;
- }
- for (studentPtr = bidSys->students; studentPtr != NULL; studentPtr = studentPtr->next)
- {
- //find all of the students who enrolled for the course
- for (crPtr = studentPtr->courseRequests; crPtr != NULL; crPtr = crPtr->next)
- {
- if (strcmp(crPtr->courseName, courseName) == 0)
- {
- //add student enrollment to list
- newRequest = CreateNewCourseRequest(courseName, crPtr->points);
- newStudent = CreateNewStudent(studentPtr->name, newRequest);
- newStudent->next = enrolled;
- enrolled = newStudent;
- }
- }
- }
- return enrolled;
- }
- void FreeCourseRequests(Student* list)
- {
- CourseRequest *cur, *next;
- if (list == NULL)
- {
- return;
- }
- cur = list->courseRequests;
- while(cur != NULL)
- {
- next = cur->next;
- free(cur);
- cur = next;
- }
- }
- void FreeStudents(Student* list)
- {
- Student *cur, *next;
- if (list == NULL)
- {
- return;
- }
- cur = list;
- while(cur != NULL)
- {
- next = cur->next;
- FreeCourseRequests(cur);
- free(cur);
- cur = next;
- }
- }
- void FreeCourses(Course *list)
- {
- Course *cur, *next;
- if (list == NULL)
- {
- return;
- }
- cur = list;
- while(cur != NULL)
- {
- next = cur->next;
- FreeStudents(cur->students);
- free(cur);
- cur = next;
- }
- }
- void RunBidding(BiddingSystem *bidSys)
- {
- int j = 0, i = 0, k = 0;
- int numEnrolled = 0;
- Student *registered = NULL;
- int numOfStudents = 0;
- Course *curCourse;
- //for each course, get array of enrolled students and then get array of registered students
- for (curCourse = bidSys->courses; curCourse != NULL; curCourse = curCourse->next)
- {
- registered = ExtractRegisteredStudents(bidSys,curCourse->courseName, curCourse->numOfPlaces);
- curCourse->students = registered;
- }
- }
- int NumRegisteredInCourse(BiddingSystem *bidSys, const char *name)
- {
- Course *curCourse = NULL;
- Student *curStudent = NULL;
- int numStudents = 0;
- for (curCourse = bidSys->courses; curCourse != NULL; curCourse = curCourse->next)
- {
- if (strcmp(curCourse->courseName, name) == 0)
- {
- for (curStudent = curCourse->students; curStudent != NULL; curStudent = curStudent->next)
- {
- numStudents++;
- }
- return numStudents;
- }
- }
- return -1;
- }
- int IsStudentInCourse(BiddingSystem *bidSys, const char *courseName, const char *studentName)
- {
- Course *curCourse = NULL;
- Student *curStudent = NULL;
- for (curCourse = bidSys->courses; curCourse != NULL; curCourse = curCourse->next)
- {
- if (strcmp(curCourse->courseName, courseName) == 0)
- {
- for (curStudent = curCourse->students; curStudent != NULL; curStudent = curStudent->next)
- {
- if (strcmp(curStudent->name, studentName) == 0)
- {
- return 1;
- }
- }
- }
- }
- return 0;
- }
- void InitSystem(BiddingSystem *bids)
- {
- bids->courses = NULL;
- bids->students = NULL;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //===================================================================================================================================
- Student *CreateNewStudent(const char *name, CourseRequest* courseRequests)
- {
- Student * newStudent = (Student *)malloc(sizeof(Student));
- if (newStudent != NULL)
- {
- newStudent->courseRequests = courseRequests;
- strcpy(newStudent->name, name);
- newStudent->next = NULL;
- }
- return newStudent;
- }
- CourseRequest *CreateNewCourseRequest(const char *name, int numOfPoints)
- {
- CourseRequest * newCourseRequest = (CourseRequest *)malloc(sizeof(CourseRequest));
- if (newCourseRequest != NULL)
- {
- newCourseRequest->points = numOfPoints;
- strcpy(newCourseRequest->courseName, name);
- newCourseRequest->next = NULL;
- }
- return newCourseRequest;
- }
- Course *CreateNewCourse(const char *name, int numOfPlaces)
- {
- Course * newCourse = (Course *)malloc(sizeof(Course));
- if (newCourse != NULL)
- {
- newCourse->numOfPlaces = numOfPlaces;
- strcpy(newCourse->courseName, name);
- newCourse->students = NULL;
- newCourse->next = NULL;
- }
- return newCourse;
- }
- void AddStudentToSystem(BiddingSystem *bidSys, Student *s)
- {
- s->next = bidSys->students;
- bidSys->students = s;
- }
- void AddCourseRequestToStudent(Student *s, CourseRequest *cr)
- {
- cr->next = s->courseRequests;
- s->courseRequests = cr;
- }
- void AddCourseToSystem(BiddingSystem* bidSys, Course *c)
- {
- c->next = bidSys->courses;
- bidSys->courses = c;
- }
- //================================= My functions ======================================================
- void DeleteStudent(Student *students, Student *delStudent)
- {
- Student *curStd = NULL, *prevStd = NULL;
- if (students != NULL)
- {
- curStd = students, prevStd = students;
- while (curStd != NULL && strcmp(curStd->name, delStudent->name) != 0)
- {
- prevStd = curStd;
- curStd = curStd->next;
- }
- if (curStd != NULL)
- {
- prevStd->next = curStd->next;
- free(curStd);
- }
- }
- }
- Student * GetStudentWithMaxPointsNumPerCourse(Student *students)
- {
- Student *studentPtr = NULL, *newStudent = NULL, *curStd = NULL, *prevStd = NULL;
- CourseRequest *crPtr = NULL, *newRequest = NULL;
- int iMaxPoints = 0;
- curStd = students;
- for (studentPtr = students; studentPtr != NULL; studentPtr = studentPtr->next)
- {
- //find student with max num of points and extract him from original list
- if (studentPtr->courseRequests->points > curStd->courseRequests->points)
- {
- curStd = studentPtr;
- }
- }
- //create new student
- newRequest = CreateNewCourseRequest(curStd->courseRequests->courseName, curStd->courseRequests->points);
- newStudent = CreateNewStudent(curStd->name, newRequest);
- newStudent->next = NULL;
- //delete student with max points
- DeleteStudent(students, curStd);
- return newStudent;
- }
- //=====================================================================================================
- Student *ExtractRegisteredStudents(BiddingSystem *bidSys, const char* courseName, int numOfPlaces)
- {
- Student *studentPtr = NULL, *newStudent = NULL;
- Student * EnrolledStudents = ExtractEnrolledStudents(bidSys, courseName);
- for (int i = 0; i < numOfPlaces; i++)
- {
- newStudent = GetStudentWithMaxPointsNumPerCourse(EnrolledStudents);
- newStudent->next = studentPtr;
- studentPtr = newStudent;
- }
- return studentPtr;
- }
- //===================================================================================================================================
- int _tmain(int argc, _TCHAR* argv[])
- {
- BiddingSystem bids;
- CourseRequest *courseReq1 = NULL, *courseReq2 = NULL, *courseReq3 = NULL;
- int numInCourse;
- Course *c1, *c2;
- Student *s1, *s2, *s3;
- CourseRequest *cr1, *cr2, *cr3, *cr4, *cr5;
- InitSystem(&bids);
- c1 = CreateNewCourse("Programming", 2);
- c2 = CreateNewCourse("History", 2);
- AddCourseToSystem(&bids, c1);
- AddCourseToSystem(&bids, c2);
- s1 = CreateNewStudent("Dan", NULL);
- s2 = CreateNewStudent("Nir", NULL);
- s3 = CreateNewStudent("Nadav", NULL);
- cr1 = CreateNewCourseRequest("Programming", 10);
- cr2 = CreateNewCourseRequest("History", 30);
- cr3 = CreateNewCourseRequest("History", 20);
- cr4 = CreateNewCourseRequest("Programming", 20);
- cr5 = CreateNewCourseRequest("History", 50);
- AddCourseRequestToStudent(s1, cr1);
- AddCourseRequestToStudent(s1, cr2);
- AddCourseRequestToStudent(s2, cr3);
- AddCourseRequestToStudent(s3, cr4);
- AddCourseRequestToStudent(s3, cr5);
- AddStudentToSystem(&bids, s1);
- AddStudentToSystem(&bids, s2);
- AddStudentToSystem(&bids, s3);
- RunBidding(&bids);
- if (IsStudentInCourse(&bids, "Programming", "Dan"))
- {
- printf("%s is in course %s\n", "Dan", "Programming");
- }
- if (IsStudentInCourse(&bids, "Programming", "Nadav"))
- {
- printf("%s is in course %s\n", "Nadav", "Programming");
- }
- if (IsStudentInCourse(&bids, "History", "Nadav"))
- {
- printf("%s is in course %s\n", "Nadav", "History");
- }
- if (IsStudentInCourse(&bids, "History", "Dan"))
- {
- printf("%s is in course %s\n", "Dan", "History");
- }
- if (!IsStudentInCourse(&bids, "History", "Nir"))
- {
- printf("%s is not in course %s\n", "Nir", "History");
- }
- numInCourse = NumRegisteredInCourse(&bids, "History");
- FreeCourses(bids.courses);
- FreeStudents(bids.students);
- scanf("%x",&numInCourse);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement