Advertisement
ben1939

targil8

Jan 16th, 2014
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Clone C 10.44 KB | None | 0 0
  1. // BiddingSystem.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. /* Maximum length of any name. */
  10. #define LENGTH 101
  11.  
  12. /* Struct definitions. */
  13. typedef struct courseRequest{
  14.     char courseName[LENGTH];
  15.     int points;
  16.     struct courseRequest *next;
  17. }CourseRequest;
  18.  
  19. typedef struct student{
  20.     char name[LENGTH];
  21.     CourseRequest* courseRequests;
  22.     struct student *next;
  23. }Student;
  24.  
  25. typedef struct course{
  26.     char courseName[LENGTH];
  27.     int numOfPlaces;
  28.     Student* students;
  29.     struct course * next;
  30. }Course;
  31.  
  32. typedef struct biddingSystem{
  33.     Student *students;
  34.     Course *courses;
  35. }BiddingSystem;
  36.  
  37. /* Function declarations. */
  38. Student *CreateNewStudent(const char *name, CourseRequest* courseRequests);
  39. CourseRequest *CreateNewCourseRequest(const char *name, int numOfPoints);
  40. Course *CreateNewCourse(const char *name, int numOfPlaces);
  41.  
  42. void FreeCourseRequests(Student* list);
  43. void FreeStudents(Student* list);
  44. void FreeCourses(Course *list);
  45.  
  46. void AddStudentToSystem(BiddingSystem *bidSys, Student *s);
  47. void AddCourseRequestToStudent(Student *s, CourseRequest *cr);
  48. void AddCourseToSystem(BiddingSystem* bidSys, Course *c);
  49.  
  50. Student *ExtractEnrolledStudents(BiddingSystem *bidSys, const char* courseName);
  51. Student *ExtractRegisteredStudents(BiddingSystem *bidSys, const char* courseName, int numOfPlaces);
  52. void RunBidding(BiddingSystem *bidSys);
  53. int NumRegisteredInCourse(BiddingSystem *bidSys, const char *name);
  54. int IsStudentInCourse(BiddingSystem *bidSys, const char *courseName, const char *studentName);
  55. void InitSystem(BiddingSystem *bids);
  56.  
  57. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. Student *ExtractEnrolledStudents(BiddingSystem *bidSys, const char* courseName)
  59. {
  60.     Student *enrolled = NULL;
  61.     Course *coursePtr = NULL;
  62.     Student *studentPtr = NULL;
  63.     CourseRequest *crPtr = NULL;
  64.     CourseRequest *newRequest = NULL;
  65.     Student *newStudent = NULL;
  66.  
  67.     //find course in the system
  68.     for (coursePtr = bidSys->courses; coursePtr != NULL; coursePtr = coursePtr->next)
  69.     {
  70.         if (strcmp(coursePtr->courseName, courseName) == 0)
  71.         {
  72.             break;
  73.         }
  74.     }
  75.  
  76.     //if course not found
  77.     if (coursePtr == NULL)
  78.     {
  79.         return NULL;
  80.     }
  81.     for (studentPtr = bidSys->students; studentPtr != NULL; studentPtr = studentPtr->next)
  82.     {
  83.         //find all of the students who enrolled for the course
  84.         for (crPtr = studentPtr->courseRequests; crPtr != NULL; crPtr = crPtr->next)
  85.         {
  86.             if (strcmp(crPtr->courseName, courseName) == 0)
  87.             {
  88.                 //add student enrollment to list
  89.                 newRequest = CreateNewCourseRequest(courseName, crPtr->points);
  90.                 newStudent = CreateNewStudent(studentPtr->name, newRequest);
  91.                 newStudent->next = enrolled;
  92.                 enrolled = newStudent;
  93.             }
  94.         }
  95.     }
  96.  
  97.     return enrolled;
  98. }
  99.  
  100. void FreeCourseRequests(Student* list)
  101. {
  102.     CourseRequest *cur, *next;
  103.     if (list == NULL)
  104.     {
  105.         return;
  106.     }
  107.     cur = list->courseRequests;
  108.     while(cur != NULL)
  109.     {
  110.         next = cur->next;
  111.         free(cur);
  112.         cur = next;
  113.     }
  114. }
  115.  
  116. void FreeStudents(Student* list)
  117. {
  118.     Student *cur, *next;
  119.     if (list == NULL)
  120.     {
  121.         return;
  122.     }
  123.     cur = list;
  124.     while(cur != NULL)
  125.     {
  126.         next = cur->next;
  127.         FreeCourseRequests(cur);
  128.         free(cur);
  129.         cur = next;
  130.     }
  131. }
  132.  
  133. void FreeCourses(Course *list)
  134. {
  135.     Course *cur, *next;
  136.     if (list == NULL)
  137.     {
  138.         return;
  139.     }
  140.     cur = list;
  141.     while(cur != NULL)
  142.     {
  143.         next = cur->next;
  144.         FreeStudents(cur->students);
  145.         free(cur);
  146.         cur = next;
  147.     }
  148. }
  149.  
  150.  
  151. void RunBidding(BiddingSystem *bidSys)
  152. {
  153.     int j = 0, i = 0, k = 0;
  154.     int numEnrolled = 0;
  155.     Student *registered = NULL;
  156.     int numOfStudents = 0;
  157.     Course *curCourse;
  158.  
  159.     //for each course, get array of enrolled students and then get array of registered students
  160.     for (curCourse = bidSys->courses; curCourse != NULL; curCourse = curCourse->next)
  161.     {
  162.         registered = ExtractRegisteredStudents(bidSys,curCourse->courseName, curCourse->numOfPlaces);
  163.  
  164.         curCourse->students = registered;
  165.     }
  166. }
  167.  
  168. int NumRegisteredInCourse(BiddingSystem *bidSys, const char *name)
  169. {
  170.     Course *curCourse = NULL;
  171.     Student *curStudent = NULL;
  172.     int numStudents = 0;
  173.  
  174.     for (curCourse = bidSys->courses; curCourse != NULL; curCourse = curCourse->next)
  175.     {
  176.         if (strcmp(curCourse->courseName, name) == 0)
  177.         {
  178.             for (curStudent = curCourse->students; curStudent != NULL; curStudent = curStudent->next)
  179.             {
  180.                 numStudents++;
  181.             }
  182.  
  183.             return numStudents;
  184.         }
  185.     }
  186.  
  187.     return -1;
  188. }
  189.  
  190. int IsStudentInCourse(BiddingSystem *bidSys, const char *courseName, const char *studentName)
  191. {
  192.     Course *curCourse = NULL;
  193.     Student *curStudent = NULL;
  194.  
  195.     for (curCourse = bidSys->courses; curCourse != NULL; curCourse = curCourse->next)
  196.     {
  197.         if (strcmp(curCourse->courseName, courseName) == 0)
  198.         {
  199.             for (curStudent = curCourse->students; curStudent != NULL; curStudent = curStudent->next)
  200.             {
  201.                 if (strcmp(curStudent->name, studentName) == 0)
  202.                 {
  203.                     return 1;
  204.                 }
  205.             }
  206.         }
  207.     }
  208.  
  209.     return 0;
  210. }
  211.  
  212. void InitSystem(BiddingSystem *bids)
  213. {
  214.     bids->courses = NULL;
  215.     bids->students = NULL;
  216. }
  217. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  218.  
  219. //===================================================================================================================================
  220. Student *CreateNewStudent(const char *name, CourseRequest* courseRequests)
  221. {
  222.     Student * newStudent = (Student *)malloc(sizeof(Student));
  223.     if (newStudent != NULL)
  224.     {
  225.         newStudent->courseRequests = courseRequests;
  226.         strcpy(newStudent->name, name);
  227.         newStudent->next = NULL;
  228.     }
  229.  
  230.     return newStudent;
  231. }
  232.  
  233. CourseRequest *CreateNewCourseRequest(const char *name, int numOfPoints)
  234. {
  235.     CourseRequest * newCourseRequest = (CourseRequest *)malloc(sizeof(CourseRequest));
  236.     if (newCourseRequest != NULL)
  237.     {
  238.         newCourseRequest->points = numOfPoints;
  239.         strcpy(newCourseRequest->courseName, name);
  240.         newCourseRequest->next = NULL;
  241.     }
  242.  
  243.     return newCourseRequest;
  244. }
  245.  
  246. Course *CreateNewCourse(const char *name, int numOfPlaces)
  247. {
  248.     Course * newCourse = (Course *)malloc(sizeof(Course));
  249.     if (newCourse != NULL)
  250.     {
  251.         newCourse->numOfPlaces = numOfPlaces;
  252.         strcpy(newCourse->courseName, name);
  253.         newCourse->students = NULL;
  254.         newCourse->next = NULL;
  255.     }
  256.  
  257.     return newCourse;
  258. }
  259.  
  260. void AddStudentToSystem(BiddingSystem *bidSys, Student *s)
  261. {
  262.     s->next = bidSys->students;
  263.     bidSys->students = s;
  264. }
  265.  
  266. void AddCourseRequestToStudent(Student *s, CourseRequest *cr)
  267. {
  268.     cr->next = s->courseRequests;
  269.     s->courseRequests = cr;
  270. }
  271.  
  272. void AddCourseToSystem(BiddingSystem* bidSys, Course *c)
  273. {
  274.     c->next = bidSys->courses;
  275.     bidSys->courses = c;
  276. }
  277.  
  278. //================================= My functions ======================================================
  279. void DeleteStudent(Student *students, Student *delStudent)
  280. {
  281.     Student *curStd = NULL, *prevStd = NULL;
  282.     if (students != NULL)
  283.     {
  284.         curStd = students, prevStd = students;
  285.         while (curStd != NULL && strcmp(curStd->name, delStudent->name) != 0)
  286.         {
  287.             prevStd = curStd;
  288.             curStd = curStd->next;
  289.         }
  290.         if (curStd != NULL)
  291.         {
  292.             prevStd->next = curStd->next;
  293.             free(curStd);
  294.         }
  295.     }
  296. }
  297.  
  298. Student * GetStudentWithMaxPointsNumPerCourse(Student *students)
  299. {
  300.     Student *studentPtr = NULL, *newStudent = NULL, *curStd = NULL, *prevStd = NULL;
  301.     CourseRequest *crPtr = NULL, *newRequest = NULL;
  302.     int iMaxPoints = 0;
  303.  
  304.     curStd = students;
  305.     for (studentPtr = students; studentPtr != NULL; studentPtr = studentPtr->next)
  306.     {
  307.         //find student with max num of points and extract him from original list
  308.         if (studentPtr->courseRequests->points > curStd->courseRequests->points)
  309.         {
  310.             curStd = studentPtr;
  311.         }
  312.     }
  313.  
  314.     //create new student
  315.     newRequest = CreateNewCourseRequest(curStd->courseRequests->courseName, curStd->courseRequests->points);
  316.     newStudent = CreateNewStudent(curStd->name, newRequest);
  317.     newStudent->next = NULL;
  318.  
  319.     //delete student with max points
  320.     DeleteStudent(students, curStd);
  321.  
  322.     return newStudent;
  323. }
  324.  
  325. //=====================================================================================================
  326. Student *ExtractRegisteredStudents(BiddingSystem *bidSys, const char* courseName, int numOfPlaces)
  327. {
  328.     Student *studentPtr = NULL, *newStudent = NULL;
  329.  
  330.     Student * EnrolledStudents = ExtractEnrolledStudents(bidSys, courseName);
  331.  
  332.     for (int i = 0; i < numOfPlaces; i++)
  333.     {
  334.         newStudent = GetStudentWithMaxPointsNumPerCourse(EnrolledStudents);
  335.         newStudent->next = studentPtr;
  336.         studentPtr = newStudent;
  337.     }
  338.  
  339.     return studentPtr;
  340.  
  341. }
  342.  
  343. //===================================================================================================================================
  344.  
  345. int _tmain(int argc, _TCHAR* argv[])
  346. {
  347.     BiddingSystem bids;
  348.     CourseRequest *courseReq1 = NULL, *courseReq2 = NULL, *courseReq3 = NULL;
  349.     int numInCourse;
  350.     Course *c1, *c2;
  351.     Student *s1, *s2, *s3;
  352.     CourseRequest *cr1, *cr2, *cr3, *cr4, *cr5;
  353.  
  354.     InitSystem(&bids);
  355.  
  356.  
  357.     c1 = CreateNewCourse("Programming", 2);
  358.     c2 = CreateNewCourse("History", 2);
  359.  
  360.     AddCourseToSystem(&bids, c1);
  361.     AddCourseToSystem(&bids, c2);
  362.  
  363.     s1 = CreateNewStudent("Dan", NULL);
  364.     s2 = CreateNewStudent("Nir", NULL);
  365.     s3 = CreateNewStudent("Nadav", NULL);
  366.     cr1 = CreateNewCourseRequest("Programming", 10);
  367.     cr2 = CreateNewCourseRequest("History", 30);
  368.     cr3 = CreateNewCourseRequest("History", 20);
  369.     cr4 = CreateNewCourseRequest("Programming", 20);
  370.     cr5 = CreateNewCourseRequest("History", 50);
  371.  
  372.  
  373.     AddCourseRequestToStudent(s1, cr1);
  374.     AddCourseRequestToStudent(s1, cr2);
  375.     AddCourseRequestToStudent(s2, cr3);
  376.     AddCourseRequestToStudent(s3, cr4);
  377.     AddCourseRequestToStudent(s3, cr5);
  378.  
  379.     AddStudentToSystem(&bids, s1);
  380.     AddStudentToSystem(&bids, s2);
  381.     AddStudentToSystem(&bids, s3);
  382.  
  383.     RunBidding(&bids);
  384.  
  385.     if (IsStudentInCourse(&bids, "Programming", "Dan"))
  386.     {
  387.         printf("%s is in course %s\n", "Dan", "Programming");
  388.     }
  389.  
  390.     if (IsStudentInCourse(&bids, "Programming", "Nadav"))
  391.     {
  392.         printf("%s is in course %s\n", "Nadav", "Programming");
  393.     }
  394.  
  395.     if (IsStudentInCourse(&bids, "History", "Nadav"))
  396.     {
  397.         printf("%s is in course %s\n", "Nadav", "History");
  398.     }
  399.  
  400.     if (IsStudentInCourse(&bids, "History", "Dan"))
  401.     {
  402.         printf("%s is in course %s\n", "Dan", "History");
  403.     }
  404.  
  405.     if (!IsStudentInCourse(&bids, "History", "Nir"))
  406.     {
  407.         printf("%s is not in course %s\n", "Nir", "History");
  408.     }
  409.  
  410.     numInCourse = NumRegisteredInCourse(&bids, "History");
  411.  
  412.     FreeCourses(bids.courses);
  413.     FreeStudents(bids.students);
  414.  
  415.     scanf("%x",&numInCourse);
  416.  
  417.     return 0;
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement