Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<conio.h>
- struct course {
- char CN [20];
- double avg;
- struct student *sh;
- struct course *next;
- };
- struct student {
- char SN[20];
- int age;
- char ID[11];
- double score[4];
- struct student *next;
- };
- struct student* CreateStudentList (){ // יצירת רשימה מקושרת של סטודנטים
- struct student *p, *head=NULL, *tail;
- int i;
- while(1){
- printf("Any key to add student, esc to finish: \n");
- flushall();
- if (getch() == 27) break;
- p= (struct student *)malloc (sizeof(struct student));
- printf(" Enter student name: ");
- gets (p->SN);
- printf(" Enter student age: ");
- scanf("%d",&p->age);
- flushall();
- printf(" Enter student ID: ");
- gets(p->ID);
- for(i=0;i<4;i++){
- printf(" %d Enter score: ",i+1);
- scanf("%lf",&p->score[i]);
- }
- if (head ==NULL){
- head = tail = p;
- p->next = NULL;
- }
- else {
- tail -> next = p;
- p-> next = NULL;
- tail = p;
- }
- }
- return head;
- }
- ///
- struct course* CreateCourseList (){ // רשימה מקושרת של קורסים שמקשרים אל רשימות סטודנטים
- struct course *p, *head=NULL, *tail;
- struct student *q;
- int i,h;
- double avg=0.0;
- while(1){
- printf("Press esc to exit, any key to continue and create a new course: \n");
- flushall();
- if (getch() == 27) break;
- p = (struct course *)malloc (sizeof(struct course));
- printf("\nEnter course name: ");
- gets (p->CN);
- if (head == NULL){
- head = tail = p;
- p->next = NULL;
- }
- else {
- tail -> next = p;
- p-> next = NULL;
- tail = p;
- }
- p-> sh = CreateStudentList ();
- if (!p->sh) continue; // == if (p-> sh==NULL) continue;
- p->avg = 0.0;
- for(q=p->sh,h=0; q ;q=q->next,avg=0.0,h++){
- for(i=0;i<4;i++)
- avg += (q->score[i]);
- avg = avg/4.0;
- p->avg +=avg;
- }
- p->avg = (p->avg)/h;
- printf("There is: %d students in the %s course \n",h,p->CN);
- printf("The avg score of all the students in the %s course is: %.3lf \n\n\n",p->CN,p->avg);
- }
- return head;
- }
- /// הדפסה:
- void PrintCourseAND_Students(struct course* head) {
- struct student *p;
- int i,b;
- for ( ;head ;head=head->next){
- printf("\n\nCourse name: %s \n",head->CN);
- p=head->sh;
- if (p==NULL) printf(" There is no students in this course\n");
- for(i=1; p ;p=p->next,i++){
- printf("\n%d Student name: %s\n",i,p->SN);
- printf (" Student age: %d\n",p->age);
- printf(" Student ID: %s\n",p->ID);
- for(b=0;b<4;b++){
- printf(" T%d score: %.1lf\n",b+1,p->score[b]);
- }
- }
- }
- }
- /// מציאת הסטודנט הכי טוב בכל מקצוע:
- void BestStudent(struct course* head){
- struct student *p,*q;
- int i;
- double avg_p,avg_q;
- for (avg_q=0;head ;head=head->next){
- printf("\n\nCourse name: %s \n",head->CN);
- p=head->sh;
- if (p==NULL) { printf(" There is no students in this course\n"); continue;}
- for(q=p; p ;p=p->next){ // מצביע על הסטודנט הכי טוב q
- for(i=0,avg_p=0;i<4;i++){
- avg_p += p->score[i];
- }
- avg_p = avg_p/4;
- if(avg_q<avg_p){
- q=p; avg_q=avg_p;
- }
- }
- printf("\n The best student: %s\n",q->SN);
- printf(" age: %d\n",q->age);
- printf(" ID: %s\n",q->ID);
- for(i=0;i<4;i++){
- printf(" T%d score: %.1lf\n",i+1,q->score[i]);
- }
- }
- }
- /// סטודנטים שנכשלו
- void FailStudent(struct course* head){
- struct student *p;
- int i,b;
- double avg_p;
- for (;head ;head=head->next){
- printf("\n\nCourse name: %s \n",head->CN);
- p=head->sh;
- if (p==NULL){
- printf(" There is no students in this course\n");
- continue;
- }
- for(b=0; p ;p=p->next){
- for(i=0,avg_p=0;i<4;i++)
- avg_p += p->score[i];
- avg_p = avg_p/4;
- if(avg_p<55)
- printf("%d fail student: %s\n",++b,p->SN);
- if (b==0)
- printf("There is no fail students in this course");
- }
- }
- }
- void main(){
- struct course *p;
- p = CreateCourseList ();
- // PrintCourseAND_Students(p);
- // BestStudent(p);
- // FailStudent(p);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement