Guest User

Untitled

a guest
Jan 15th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4.  
  5. struct course {
  6.     char CN [20];
  7.     double avg;
  8.     struct student *sh;
  9.     struct course *next;
  10. };
  11.  
  12. struct student {
  13.     char SN[20];
  14.     int age;
  15.     int ID;
  16.     double score[4];
  17.     struct student *next;
  18. };
  19.  
  20.  
  21.  
  22. struct student* CreateStudentList (){  // יצירת רשימה מקושרת של סטודנטים
  23.     struct student *p, *head=NULL, *tail;
  24.     int i;
  25.  
  26.     while(1){
  27.         printf("Any key to add student, esc to finish: \n");
  28.         flushall();
  29.         if (getch() == 27) break;
  30.  
  31.         p= (struct student *)malloc (sizeof(struct student));
  32.         printf("  Enter student name: ");
  33.         gets (p->SN);
  34.         printf("  Enter student age: ");
  35.         scanf("%d",&p->age);
  36.         printf("  Enter student ID: ");
  37.         scanf("%d",&p->ID);
  38.         for(i=0;i<4;i++){
  39.             printf("  %d  Enter score: ",i+1);
  40.             scanf("%lf",&p->score[i]);
  41.         }
  42.  
  43.  
  44.         if (head ==NULL){
  45.             head = tail = p;
  46.             p->next = NULL;
  47.         }
  48.         else {
  49.             tail -> next = p;
  50.             p-> next = NULL;
  51.             tail = p;
  52.         }
  53.  
  54.     }
  55.     return head;
  56. }
  57.  
  58. ///
  59.  
  60. struct course* CreateCourseList (){  //  רשימה מקושרת של קורסים שמקשרים אל רשימות סטודנטים
  61.     struct course *p, *head=NULL, *tail;
  62.     struct student *q;
  63.     int i,h;
  64.     double avg=0.0;
  65.  
  66.     while(1){
  67.         printf("Press esc to exit, any key to continue and create a new course: \n");
  68.         flushall();
  69.         if (getch() == 27) break;
  70.  
  71.         p = (struct course *)malloc (sizeof(struct course));
  72.         printf("Enter course name: ");
  73.         gets (p->CN);
  74.         p-> sh = CreateStudentList ();
  75.         if (!p->sh) return NULL; // ==      if (p-> sh==NULL) return NULL;
  76.  
  77.  
  78.         p->avg = 0.0;
  79.         for(q=p->sh,h=0; q ;q=q->next,avg=0.0,h++){
  80.  
  81.         for(i=0;i<4;i++)
  82.         avg += (q->score[i]);
  83.  
  84.         avg = avg/4.0;
  85.         p->avg +=avg;
  86.     }
  87.         p->avg = (p->avg)/h;
  88.        
  89.         printf("There is: %d students in %s course \n",h,p->CN);
  90.         printf("The avg score of all the students in the %s course is: %.3lf \n\n\n",p->CN,p->avg);
  91.  
  92.         if (head == NULL){
  93.             head = tail = p;
  94.             p->next = NULL;
  95.         }
  96.         else {
  97.             tail -> next = p;
  98.             p-> next = NULL;
  99.             tail = p;
  100.         }
  101.  
  102.     }
  103.     return head;
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. void main(){
  113.     struct course *p;
  114.     p = CreateCourseList ();
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment