#include <stdio.h>
int k=0; struct list { int num; struct list *next; };
struct student { int ID_student; int grade[6]; struct student *next;
};
typedef struct student student; student *headpo=NULL; student *lastpo=NULL;
void AddGread (int grd[],int numofstudent,int i) { char name[10]; int t=0; student *list; if ((list=(struct student *)malloc(sizeof(struct student)))== NULL) { printf("error"); exit (42); }
list->next=NULL; list->ID_student=numofstudent+1; for(t=0;t<i;t++) { list->grade[t]=grd[t]; } if (headpo==NULL) headpo=list; else lastpo->next=list; lastpo=list; list=list->next; } void PrintToFile() { int i=0,z=0; student *tmp=headpo; FILE *fp; fp = fopen("save.txt","w+"); if (fp==NULL) { printf ("the file can't be open\n"); exit(42); } fprintf(fp,"the grades of the students \n"); printf("the grades of the students \n"); while (tmp!=NULL) { fprintf(fp,"student number %d -> ",tmp->ID_student); printf("student number %d -> ",tmp->ID_student); for (z=0;(tmp->grade[z]>0);z++) { fprintf (fp,"%d ",tmp->grade[z]); printf ("%d ",tmp->grade[z]); } if (tmp->grade[z]<0) { fprintf(fp,"\n"); printf("\n"); } tmp=tmp->next; } } void main() { int ziun[6]={0},num=0,i=0; while (num==0) { i=0; k=0; printf ("select your wish\n"); printf ("1) To add greads for the students\n"); printf ("2) To print to file all the data\n"); printf ("0) To exit\n"); scanf ("%d",&num); if (num==1) { while (k<6) { i=0; while (i<6 || ziun[i]==-1) { if (k==5) printf("enter the greads of student number %d the last one. (-1 to exit) -> ",k+1); else printf("enter the greads of student number %d the %d grade. (-1 to next) -> ",k+1,i+1); scanf("%d",&ziun[i]); if (ziun[0]!=-1) { if (ziun[i]==-1 || (i==6)) { AddGread(&ziun,k,i); break; } } else { i=6; k=6; } i++; } k++; } num=0; } else if (num==2) { PrintToFile(); num=0; } else if (num==0) exit(2); } printf("good bay\n"); }
|