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;
- int ID;
- 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);
- printf(" Enter student ID: ");
- scanf("%d",&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("Enter course name: ");
- gets (p->CN);
- p-> sh = CreateStudentList ();
- if (!p->sh) return NULL; // == if (p-> sh==NULL) return NULL;
- 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 %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);
- if (head == NULL){
- head = tail = p;
- p->next = NULL;
- }
- else {
- tail -> next = p;
- p-> next = NULL;
- tail = p;
- }
- }
- return head;
- }
- void main(){
- struct course *p;
- p = CreateCourseList ();
- }
Advertisement
Add Comment
Please, Sign In to add comment