Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. struct Courses{
  7. int id;
  8. int credit_Hrs;
  9. char name[7];
  10. };
  11.  
  12. struct student{
  13. int id;
  14. char name[100];
  15. int totalCourses;
  16. struct Courses courseList[4];
  17. };
  18.  
  19. int displayMenu()
  20. {
  21. int option = 0;
  22.  
  23. printf("Choose from the following options:\n");
  24. printf("\t1- Add a new student\n");
  25. printf("\t2- Add/Delete a Courses\n");
  26. printf("\t3- Search for a student\n");
  27. printf("\t4- Print fee invoice\n");
  28. printf("\t0- Exit program\n");
  29. printf("\nEnter your selection: ");
  30. scanf("%d", &option);
  31.  
  32. return option;
  33. }
  34.  
  35. struct Courses courseCreation(int id, char* name, int credit_Hrs){
  36.  
  37. struct Courses course;
  38.  
  39. course.id = id;
  40. strcpy(course.name, name);
  41. course.credit_Hrs = credit_Hrs;
  42.  
  43. return course;
  44. }
  45.  
  46. void printCourses(struct Courses c)
  47. {
  48. printf("\t\t%d\t %s\t %d\n", c.id, c.name, c.credit_Hrs);
  49. }
  50.  
  51. void displayStudents(struct student s)
  52. {
  53. printf("The details of the students are as follows:\n");
  54. printf("Name: %s\n", s.name);
  55. printf("\nCourses: ");
  56.  
  57. int i = 0;
  58.  
  59. for (i = 0;i<s.totalCourses;i++)
  60.  
  61. {
  62. //call the function printCourses() to print the
  63. //courses
  64. printCourses(s.courseList[i]);
  65. }
  66. }
  67.  
  68. void printFee(struct student s) //displayInvoice
  69. {
  70. float totalHours = 0;
  71. float total = 0;
  72.  
  73. printf("\n\tVALENCE COMMUNITY COLLEGE\n");
  74. printf("\tORLANDO FL 10101\n");
  75. printf("\t---------------------\n\n");
  76. printf("\tFee Invoice prepared for Student: \n");
  77. printf("\t%d-%s\n\n", s.id, s.name);
  78. printf("\t1 Credit hour = $ 120.25\n\n");
  79. printf("\tCRN\tCR_PREFIX\tCR_HOURS\n");
  80. for (int i = 0;i<s.totalCourses;i++)
  81. {
  82. totalHours =(float) s.courseList[i].credit_Hrs*(120.25);
  83. printf("\t%d\t %s\t %d\t $ %.2f\n", s.courseList[i].id, s.courseList[i].name, s.courseList[i].credit_Hrs, totalHours);
  84. total = total + totalHours;
  85. }
  86. printf("\n\t\tHealth & id fees\t $ 35.00\n");
  87. total = total + 35;
  88. printf("\t---------------------------------------\n");
  89. printf("\t\tTotal Payments\t $ %.2f\n", total);
  90. }
  91.  
  92. void search_student(struct student s[], int size){
  93. int id, found = 0;
  94.  
  95. printf("Enter the id of the student: ");
  96. scanf("%d", &id);
  97.  
  98. int i;
  99.  
  100.  
  101. for (i = 0;i<size;i++)
  102. {
  103. //if the student is present,
  104. if (s[i].id == id)
  105. {
  106. //then print the details
  107. //using displayStudents()
  108. displayStudents(s[i]);
  109. found = 1;
  110.  
  111. break;
  112. }
  113. }
  114. if (found == 0)
  115. {
  116. printf("\nNo Student Found!\n");
  117. }
  118. }
  119.  
  120. void checkForNewInvoice(struct student s)
  121.  
  122. {
  123. char ch = 'n';
  124.  
  125. printf("\nWant to display the new invoice(Y/N): ");
  126. scanf("%c", &ch);
  127.  
  128. if (ch == 'Y' || ch == 'y')
  129. {
  130. printFee(s);
  131. }
  132. }
  133.  
  134.  
  135. void add_del_course(struct student studentList[],
  136.  
  137. int size_s, struct Courses course_list[], int size_c)
  138.  
  139. {
  140. int sid;
  141.  
  142. printf("\nEnter the student's id: ");
  143. scanf("%d", &sid);
  144.  
  145. //get the index value i of the student with the id
  146. int i = 0;
  147.  
  148. for (i = 0;i<size_s;i++)
  149. {
  150. if (studentList[i].id == sid)
  151. {
  152. break;
  153. }
  154. }
  155.  
  156. printf("\nHere are the courses [%s] is taking:", studentList[i].name);
  157. printf("\n\t\tCRN\tPREFIX\tCR. HOURS\n");
  158.  
  159. int k;
  160.  
  161. for (k = 0; k<studentList[i].totalCourses;k++)
  162. {
  163. printCourses(studentList[i].courseList[k]);
  164. }
  165.  
  166. int numOfCourses = studentList[i].totalCourses;
  167.  
  168. char choice = 'C';
  169.  
  170. scanf("%c", &choice);
  171.  
  172. printf("\nChoose from:\n");
  173.  
  174. printf("A. Add a new Courses for [%s] \n", studentList[i].name);
  175.  
  176. printf("D. Delete a Courses from [%s]'s schedule\n", studentList[i].name);
  177.  
  178. printf("C. Cancel operation\n");
  179.  
  180. printf("\nEnter your selection: ");
  181.  
  182. choice = getchar();
  183.  
  184.  
  185. int c_id,l = 0, j = 0;
  186.  
  187. switch (choice)
  188. {
  189. //if the user want to add a course
  190.  
  191. case 'A':
  192. case 'a':
  193. if (studentList[i].totalCourses < 4)
  194. {
  195. printf("\nEnter the Courses id: ");
  196. scanf("%d", &c_id);
  197.  
  198. for (l = 0; l<size_c; l++)
  199. {
  200. //if the course are present,
  201. //then add the course to the course list of the student
  202. if (course_list[l].id == c_id)
  203. {
  204. studentList[i].courseList[numOfCourses] = course_list[l];
  205. studentList[i].totalCourses++;
  206.  
  207. break;
  208. }
  209. }
  210. //check whether the user want to print a new invoice
  211. //using the function checkForNewInvoice()
  212. checkForNewInvoice(studentList[i]);
  213. }
  214. else
  215. {
  216. printf("\nNo more courses can be added.\n");
  217. }
  218.  
  219. break;
  220. //if the user want to delete a course
  221.  
  222. case 'D':
  223.  
  224. case 'd':
  225.  
  226. printf("Enter Courses number to delete: ");
  227. scanf("%d", &c_id);
  228.  
  229. getchar();
  230.  
  231. //search for the course in the list
  232. for (l = 0;l<numOfCourses;l++)
  233. {
  234. //if present,
  235. if (studentList[i].courseList[l].id == c_id)
  236. {
  237. //print the deleted course id and name.
  238. printf("\n[%d %s] ", studentList[i].courseList[l].id, studentList[i].courseList[l].name);
  239.  
  240. for (j = l;j<numOfCourses - 1;j++)
  241. {
  242. studentList[i].courseList[j] = studentList[i].courseList[j + 1];
  243. }
  244.  
  245. break;
  246. }
  247. }
  248.  
  249. studentList[i].totalCourses--;
  250.  
  251. printf("is deleted Sucessfully!\n\n");
  252.  
  253. checkForNewInvoice(studentList[i]);
  254.  
  255. break;
  256.  
  257. case 'C':
  258.  
  259. case 'c':
  260.  
  261. break;
  262.  
  263. default:
  264.  
  265. printf("\nInvalid choice.\n");
  266.  
  267. }
  268.  
  269. }
  270.  
  271. //definition of the function addStudent()
  272.  
  273. //this function takes array of student objects,
  274.  
  275. //array of Courses object and their sizes
  276.  
  277. //adds a student to the list.
  278.  
  279. struct student addStudent(struct student studentList[], int size,
  280.  
  281. struct Courses course_list[], int size_c)
  282.  
  283. {
  284. //create and allocate memory to a student object
  285.  
  286. struct student* newStudent = (struct student*)malloc(sizeof(struct student));
  287.  
  288. //declare the variables
  289.  
  290. int i,found = 1;
  291.  
  292. int no_courses = 0;
  293.  
  294. char c;
  295.  
  296. //read the student id.
  297.  
  298. printf("\nEnter the student's id: ");
  299. scanf("%d", &newStudent->id);
  300.  
  301. //Check if the student is alread present in the list
  302.  
  303. while (found == 1 && size != 0)
  304. {
  305.  
  306. int j = 0;
  307.  
  308. for (j = 0; j< size; j++)
  309. {
  310. if (studentList[j].id == newStudent->id)
  311. {
  312. printf("The Id already exists.");
  313. printf("Enter the student's id: ");
  314. scanf("%d", &newStudent->id);
  315. found = 1;
  316.  
  317. break;
  318. }
  319. found = 0;
  320. }
  321. }
  322. scanf("%c", &c);
  323.  
  324. //read the student name
  325.  
  326. printf("Enter student's name: ");
  327. c = getchar();
  328.  
  329. i = 0;
  330.  
  331. //assign the name to the student
  332.  
  333. while (c != '\n')
  334. {
  335. newStudent->name[i] = c;
  336.  
  337. c = getchar();
  338.  
  339. i++;
  340.  
  341. }
  342.  
  343. newStudent->name[i] = '\0';
  344.  
  345. printf("\nEnter how many courses (%s) is taking (up to 4 courses)?\n\t", newStudent->name);
  346. scanf("%d", &no_courses);
  347.  
  348. while (no_courses > 4 || no_courses < 0)
  349. {
  350. printf("\nPlease enter a valid number (0-4)\n\t");
  351. scanf("%d", &no_courses);
  352. }
  353.  
  354. newStudent->totalCourses = no_courses;
  355.  
  356. int wrongCourse = 0;
  357. int courseIds[4];
  358.  
  359. while (wrongCourse == 0)
  360. {
  361. i = 0;
  362.  
  363. printf("\nEnter the %d Courses numbers\n\t", no_courses);
  364. //read the courses
  365. while (i < no_courses)
  366. {
  367. scanf("%d", &(courseIds[i]));
  368. i++;
  369. }
  370. int p;
  371.  
  372. for (p = 0;p<no_courses;p++)
  373. {
  374. wrongCourse = 0;
  375.  
  376. int q;
  377.  
  378. for (q = 0;q<size_c;q++)
  379. {
  380. if (courseIds[p] == course_list[q].id)
  381. {
  382. newStudent->courseList[p] = course_list[q];
  383. wrongCourse = 1;
  384. break;
  385. }
  386. }
  387. if (wrongCourse == 0)
  388. {
  389. printf("Wrong Crn entered.\n");
  390. break;
  391. }
  392. }
  393. }
  394. return *newStudent;
  395. }
  396.  
  397. int main()
  398. {
  399. struct Courses c_List[8];
  400. struct student students[100];
  401. int numOfStudents = 0;
  402. int id;
  403.  
  404. c_List[0] = courseCreation(4587, "MAT 236", 4);
  405. c_List[1] = courseCreation(4599, "COP 220", 3);
  406. c_List[2] = courseCreation(8997, "GOL 124", 1);
  407. c_List[3] = courseCreation(9696, "COP 100", 3);
  408. c_List[4] = courseCreation(1232, "MAC 531", 5);
  409. c_List[5] = courseCreation(9856, "STA 100", 2);
  410. c_List[6] = courseCreation(8520, "TNV 400", 5);
  411. c_List[7] = courseCreation(8977, "CMP 100", 1);
  412.  
  413. int i = 0;
  414.  
  415. printf("Welcome!\n");
  416.  
  417.  
  418. int choice = displayMenu();
  419. while (choice != 0)
  420. {
  421. switch (choice)
  422. {
  423. case 1:
  424. students[numOfStudents] = addStudent(students, numOfStudents, c_List, 7);
  425. numOfStudents++;
  426.  
  427. printf("\nStudent added successfully!\n");
  428. break;
  429.  
  430. case 2:
  431. add_del_course(students, numOfStudents, c_List, 7);
  432. break;
  433.  
  434. case 3:
  435. search_student(students, numOfStudents);
  436. break;
  437.  
  438. case 4:
  439. printf("Enter the student's Id: ");
  440. scanf("%d", &id);
  441. for (i = 0;i<numOfStudents;i++)
  442. {
  443. if (id == students[i].id)
  444. {
  445. printFee(students[i]);
  446. }
  447. }
  448. break;
  449.  
  450. default:
  451. printf("\nPlease enter a valid choice.\n");
  452. }
  453. printf("\n--------------------\n\n");
  454. choice = displayMenu();
  455. }
  456. printf("\nGoodbye!\n");
  457.  
  458. return 0;
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement