i am doing my assignment, but ...
here is my code:
here is data:Code:#include<stdio.h> #include<string.h> #include<stdlib.h> struct node{ char* name; struct node *next; struct node *follow; }; typedef struct node Node; Node* newNode(char*); void Search(char*,char*,char*,Node*); void print(Node*); int main(){ char line[BUFSIZ]; Node* student=newNode(""); while(fgets(line,BUFSIZ,stdin)!=NULL){ char* l= line; char person[BUFSIZ]=""; char* p = person; char course[BUFSIZ]=""; char* c = course; char mark[3]=""; char* m = mark; while(isspace(*l)!= 0){ l++; } int length = strlen(l); char* rLine = strchr(l,' '); int rlength = strlen(rLine); int i = length - rlength; strncpy(p,l,i); /*name*/ l += i; while(isspace(*l)!=0){ l++; } length = strlen(l); rLine = strchr(l,' '); rlength = strlen(rLine); i = length - rlength; strncpy(c,l,i); /*course*/ l += i; while(isspace(*l)!=0){ l++; } while(isspace(*l)==0){ strncpy(m,l,1); m++; l++; } m = mark; Search(p,c,m,student); } print(student); } Node* newNode(char* name){ Node* temp; temp=(Node*)malloc(sizeof(Node)); strcpy(temp->name,name); temp->next = NULL; temp->follow = NULL; return temp; } void Search(char* person,char* course,char* mark,Node* student){ Node* ptr = student; while(strcmp(person,ptr->name) !=0 && (ptr->next != NULL)){ ptr = ptr->next; } if(strcmp(person,ptr->name)==0){ Node* ptr1 = ptr->follow; while(strcmp(course,ptr1->name)!=0&&ptr1->next!=NULL){ ptr1 = ptr1->next; } if(strcmp(course,ptr1->name)==0){ Node* ptr2 = ptr1->follow; while(ptr2->next != NULL){ ptr2 = ptr2->next; } ptr2->next = newNode(mark); } else if(ptr1->next == NULL){ ptr1->next = newNode(course); ptr1 = ptr1->next; ptr1->follow = newNode(mark); } } else if(ptr->next == NULL){ ptr->next = newNode(person); ptr = ptr->next; ptr->follow = newNode(course); ptr->follow->follow = newNode(mark); } } void print(Node* student){ Node* ptr = student; ptr = ptr->next; for(ptr;ptr!=NULL;ptr=ptr->next){ char output[BUFSIZ]=""; char* out = output; strcat(out,ptr->name); strcat(out,":"); Node* ptr1 = ptr->follow; for(ptr1;ptr1!=NULL;ptr1=ptr1->next){ strcat(out," "); strcat(out,ptr1->name); Node* ptr2 = ptr1->follow; for(ptr2;ptr2!=NULL;ptr2=ptr2->next){ strcat(out," "); strcat(out,ptr2->name); } } strcat(out,"\n"); fputs(out,stdout); } }
Jane CS1100 A
Art CS1100 D
Meng CS1100 A
Art CS1100 C-
Jane Math1000 B+
Art CS1100 A-
Art Math1000 A
Meng CS1101 A
Art CS1100 B-
Meng CS1101 A+
why i always get Mati1000 instead of Math1000?
and, why strcmp(person, ptr->next) = -1, when person="Art" and
ptr->next="Art"?
thanks



LinkBack URL
About LinkBacks


