Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 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.     char ID[11];
  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.         flushall();
  37.         printf("  Enter student ID: ");
  38.         gets(p->ID);
  39.         for(i=0;i<4;i++){
  40.             printf("  %d  Enter score: ",i+1);
  41.             scanf("%lf",&p->score[i]);
  42.         }
  43.  
  44.  
  45.         if (head ==NULL){
  46.             head = tail = p;
  47.             p->next = NULL;
  48.         }
  49.         else {
  50.             tail -> next = p;
  51.             p-> next = NULL;
  52.             tail = p;
  53.         }
  54.  
  55.     }
  56.     return head;
  57. }
  58. ///
  59. struct course* CreateCourseList (){  //  רשימה מקושרת של קורסים שמקשרים אל רשימות סטודנטים
  60.     struct course *p, *head=NULL, *tail;
  61.     struct student *q;
  62.     int i,h;
  63.     double avg=0.0;
  64.  
  65.     while(1){
  66.         printf("Press esc to exit, any key to continue and create a new course: \n");
  67.         flushall();
  68.         if (getch() == 27) break;
  69.  
  70.         p = (struct course *)malloc (sizeof(struct course));
  71.         printf("\nEnter course name: ");
  72.         gets (p->CN);
  73.  
  74.         if (head == NULL){
  75.             head = tail = p;
  76.             p->next = NULL;
  77.         }
  78.         else {
  79.             tail -> next = p;
  80.             p-> next = NULL;
  81.             tail = p;
  82.         }
  83.  
  84.         p-> sh = CreateStudentList ();
  85.         if (!p->sh) continue; // ==         if (p-> sh==NULL) continue;
  86.  
  87.  
  88.         p->avg = 0.0;
  89.         for(q=p->sh,h=0; q ;q=q->next,avg=0.0,h++){
  90.  
  91.             for(i=0;i<4;i++)
  92.                 avg += (q->score[i]);
  93.  
  94.             avg = avg/4.0;
  95.             p->avg +=avg;
  96.         }
  97.         p->avg = (p->avg)/h;
  98.  
  99.         printf("There is: %d students in the %s course \n",h,p->CN);
  100.         printf("The avg score of all the students in the %s course is: %.3lf \n\n\n",p->CN,p->avg);
  101.  
  102.     }
  103.     return head;
  104. }
  105.  
  106.  
  107.  
  108. /// הדפסה:
  109. void PrintCourseAND_Students(struct course* head) {
  110.     struct student *p;
  111.     int i,b;
  112.  
  113.     for ( ;head ;head=head->next){
  114.         printf("\n\nCourse name: %s \n",head->CN);
  115.         p=head->sh;
  116.         if (p==NULL) printf("  There is no students in this course\n");
  117.  
  118.         for(i=1; p ;p=p->next,i++){
  119.             printf("\n%d Student name: %s\n",i,p->SN);
  120.             printf ("  Student age: %d\n",p->age);
  121.             printf("  Student ID: %s\n",p->ID);
  122.             for(b=0;b<4;b++){
  123.                 printf("   T%d  score: %.1lf\n",b+1,p->score[b]);
  124.             }
  125.         }
  126.     }
  127. }
  128.  
  129. /// מציאת הסטודנט הכי טוב בכל מקצוע:
  130. void BestStudent(struct course* head){
  131.     struct student *p,*q;
  132.     int i;
  133.     double avg_p,avg_q;
  134.  
  135.     for (avg_q=0;head ;head=head->next){
  136.         printf("\n\nCourse name: %s \n",head->CN);
  137.         p=head->sh;
  138.         if (p==NULL) { printf("  There is no students in this course\n"); continue;}
  139.  
  140.         for(q=p; p ;p=p->next){ // מצביע על הסטודנט הכי טוב q
  141.             for(i=0,avg_p=0;i<4;i++){
  142.                 avg_p += p->score[i];
  143.             }
  144.             avg_p = avg_p/4;
  145.             if(avg_q<avg_p){
  146.                 q=p; avg_q=avg_p;
  147.             }
  148.         }
  149.  
  150.         printf("\n  The best student: %s\n",q->SN);
  151.         printf("  age: %d\n",q->age);
  152.         printf("  ID: %s\n",q->ID);
  153.         for(i=0;i<4;i++){
  154.             printf("   T%d  score: %.1lf\n",i+1,q->score[i]);
  155.         }
  156.  
  157.     }
  158.  
  159. }
  160.  
  161. /// סטודנטים שנכשלו
  162. void FailStudent(struct course* head){
  163.     struct student *p;
  164.     int i,b;
  165.     double avg_p;
  166.  
  167.     for (;head ;head=head->next){
  168.         printf("\n\nCourse name: %s \n",head->CN);
  169.         p=head->sh;
  170.         if (p==NULL){
  171.             printf("  There is no students in this course\n");
  172.             continue;
  173.         }
  174.  
  175.         for(b=0; p ;p=p->next){
  176.             for(i=0,avg_p=0;i<4;i++)
  177.                 avg_p += p->score[i];
  178.             avg_p = avg_p/4;
  179.  
  180.             if(avg_p<55)
  181.                 printf("%d fail student: %s\n",++b,p->SN);
  182.             if (b==0)
  183.                 printf("There is no fail students in this course");
  184.         }
  185.     }
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. void main(){
  194.     struct course *p;
  195.     p = CreateCourseList ();
  196.     // PrintCourseAND_Students(p);
  197.     // BestStudent(p);
  198.     // FailStudent(p);
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement