hi,
this is my 3rd assigment, it's about text files
the program is multi-operations program, that user have to enter a letter of operation code that is defined in the main ...
with no compilation error, i beleive that i have some logic errors, because the program isn't working well
plz, if any one have any notation that could help .. help me !
thanz for reading ,
CODE
/**************************************************************/
/**************************************************************/
/********************** Assigment 3 *****************************/
/**************************************************************/
/**************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
int id;
char fn[30];
char ln[30];
float md;
float final;
struct student *next;
}node;
/**************************************************************/
/* which add a node to a list in its correct order position */
/**************************************************************/
void insert_in_order(node *p,FILE *in,FILE *out)
{
node *t,*s,*curr,*prev;
int flag=0;
t=(node*)malloc(sizeof(node));
fscanf(in,"%d",&(t->id));
fscanf(in,"%s",t->fn);
fscanf(in,"%s",t->ln);
fscanf(in,"%f",&(t->md));
fscanf(in,"%f",&(t->final));
t->next=NULL;
s=p;
while(s!=NULL&&flag!=1)
{
if(t->id==s->id)
{
fprintf(out,"ID is already insert");
flag=1;
}
s=s->next;
}
if(flag==0)
{
if(p==NULL)
p=t;
else if (strcmp(p->fn,t->fn)==1)
{
t->next=p;
p=t;
}
else
{
curr=p;
while (strcmp(t->fn,curr->fn)==1&&curr!=NULL)
{
prev=curr;
curr=curr->next;
}
prev->next=t;
t->next=curr;
}
}
}
/**************************************************************/
/* which removes a particular node from the list using ID */
/**************************************************************/
void delet_node(node *p,FILE *in,FILE *out)
{
node *prev=NULL,*curr=p;
int id;
fscanf(in,"id",&id);
while(curr!=NULL&&curr->id!=id)
{
prev=curr;
curr=curr->next;
}
if(curr==NULL)
fprintf(out,"The ID not exist");
else
{
prev->next=curr->next;
curr->next=NULL;
free(curr);
}
}
/**************************************************************/
/* which lists all the records in the linked list */
/**************************************************************/
void list_records(node *p,FILE *out)
{
if(p!=NULL)
{
fprintf(out,"%d ",(p->id));
fprintf(out,"%s ",(p->fn));
fprintf(out,"%s ",p->ln);
fprintf(out,"%f ",p->md);
fprintf(out,"%f\n",p->final);
list_records(p->next,out);
}
}
/**************************************************************/
/* which calculates and returns the average of final exams */
/**************************************************************/
float calc_average(node *p)
{
static float sum=0;
static int c=0;
if(p==NULL)
return (sum/c);
else
{
sum=sum+p->final;
c++;
return (calc_average(p->next));
}
}
/**************************************************************/
/* which findes and returns a particular record using the ID */
/* you should print the whole record from the main . */
/**************************************************************/
node *find_record(node *p,FILE *in)
{
int id;
fscanf(in,"%d",&id);
if(p==NULL)
return NULL;
else if(p->id==id)
return p;
else
return (find_record(p->next,in));
}
main()
{
FILE *in,*out;
node *p=NULL,*r;
char ch;
in=fopen("in.txt","r");
out=fopen("out.txt","w");
if(in!=NULL&&out!=NULL)
{
fprintf(out,"1)I for insert anew record.\n");
fprintf(out,"2)D for deleting arecord from the array.\n");
fprintf(out,"3)F for the average of final exam.\n");
fprintf(out,"4)C for the smallest and largest score of the final exam.\n");
fprintf(out,"5)S for sort the array by descending order according to the first name .\n");
fprintf(out,"6)Q to quit the program.\n");
fprintf(out,"Enter an opration cod:");
fscanf(in," %c",&ch);
while(ch!='Q'&&ch!='q')
{
if(ch=='I'||ch=='i')
insert_in_order(p,in,out);
else if(ch=='D'||ch=='d')
{
delet_node(p,in,out);
list_records(p,out);
}
else if(ch=='L'||ch=='l')
list_records(p,out);
else if(ch=='C'||ch=='c')
fprintf(out,"The average of final exams=%f\n",calc_average(p));
else if(ch=='F'||ch=='f')
{
r=find_record(p,in);
list_records(r,out);
}
else
fprintf(out,"wrong operation cod...");
fprintf(out,"Enter an opration cod:");
fscanf(in," %c",&ch);
}
fclose(in);
}
else
printf("The file not exist");
return 0;
}