2013-11-22 5 views
0

여기 내가 잘못하고 있는지 확실하지 않아 코스 노드를 지정된 값으로 연결된 목록에 추가하려고 시도했지만 링크 된 목록을 인쇄하려고하면 그것은 비어 있다고 말하면서 나는 틀린 것이 무엇인지 확신하지 못한다.C 프로그램이 연결된 목록의 노드를 인쇄하지 않습니다

나는 목록의 1/3 정도를 얻었을 때 segfaults를 제외하고 지금은 모든 것을 작동합니다. 이유는 모르겠습니다. 도와주세요!

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#include<ctype.h> 

#define MAX 50 

typedef struct courses{ 
    char *abbr; 
    char *name; 
    int credits; 
    char *prof; 
    struct courses *next; 
}courses; 

int isAbbr(char *string); 
int isName(char *string); 
int isCredit(char *string); 
int isProf(char *string); 
int isPre(char *string); 
courses *readfile(FILE *); 
courses *create_course(char *abbr, char *prof, int credits, char *fulldesc); 
courses *create_list(courses *, courses *); 
void Print(courses *); 

int main(int argc, char **argv) 
{ 
    if (argc != 2) 
    { 
      printf("Inadequate amount of arguments.\n"); 
      return 0; 
    } 
    FILE *fp = fopen(argv[1], "r"); 
    if (fp == NULL) 
    { 
      printf("File cannot be opened.\n"); 
      return 0; 
    } 

    courses* head = NULL; 
    head = readfile(fp); 

    Print(head); 

    printf("=== task1: construct a linear linked list from the catalog ===\n\n=== task2: user input and producing output ===\n\n"); 
    int choice = 0; 
    while (choice != 3) 
    { 
      printf("\nSelect your option below:\n1-Register for a Course\n2-See my total\n3-Exit\nChoice: "); 
      scanf("%d",&choice); 
    } 

    printf("\n"); 
    return 0; 
} 

courses *readfile(FILE *fp) 
{ 
    courses *head, *entry; 
    head = entry = NULL; 

    char *abbr = malloc(MAX); 
    char *name = malloc(MAX); 
    char *prof = malloc(MAX); 
    char *desc = malloc(MAX); 
    char *fulldesc = malloc(MAX); 
    char *fullName = malloc(MAX); 

    int credit; 
    int credflag = 0; 
    int nameFlag = 0; 
    int profFlag = 0; 
    int preFlag = 0; 
    int credits = 0; 
    int descflag = 0; 

    char line[MAX]; 
    while (fgets(line, MAX - 1, fp) != NULL) 
    { 
      if (line[strlen(line) - 1] == '\n') 
      { 
        line[strlen(line) - 1] = '\0'; 
      } 
      char* token = strtok(line," ,\t"); 
      while (token != NULL) 
      { 
        if (isAbbr(token) == 1) 
        { 
          abbr = malloc(sizeof(char));; 
          strcpy(abbr, token); 
          credflag = 1; 
          preFlag = 0; 
          profFlag = 1; 
        } 
        if (isName(token) == 1) 
        { 
          if (descflag == 1 && isCredit(token) != 1) 
          { 
            fulldesc = strcat(desc, token); 
          } 
          else 
          { 
            desc = malloc(sizeof(char)); 
            strcpy(desc,token); 
            descflag = 1; 
          } 
        } 
        else 
        { 
          descflag = 0; 
        } 
        if (isCredit(token) == 1 && credflag == 1) 
        { 
            credits = atoi(token); 
            credflag = 0; 
            descflag = 0; 
        } 
        if (isProf(token)== 1 && profFlag == 1) 
        { 
          if(nameFlag == 1 && name[0] != 'T' && token[0] != 'O') 
          { 
            prof = strcat(name, token); 
            entry = create_course(abbr,prof,credits, fulldesc); 
            head = create_list(head,entry); 
            profFlag = 0; 
            nameFlag = 0; 
          } 
          else 
          { 
            name = malloc(sizeof(char)); 
            strcpy(name, token); 
            nameFlag = 1; 
          } 
        } 
        else 
        { 
          nameFlag = 0; 
        } 
        token = strtok(NULL," ,\t"); 
      } 
    } 
} 

courses *create_course(char *abbr, char *prof, int credits, char *fulldesc) 
{ 
    courses *entry = (courses*)malloc(sizeof(courses)); 

    //printf("%30s %50s %30s %30d\n",abbr,fulldesc,prof,credits); 

    entry->abbr=(char*)malloc(sizeof(char)); 
    strcpy(entry->abbr, abbr); 

    entry->prof=(char*)malloc(sizeof(char)); 
    strcpy(entry->prof, prof); 

    entry->name=(char*)malloc(sizeof(char)); 
    strcpy(entry->name, fulldesc); 

    entry->credits = credits; 
    entry->next = NULL; 

    return entry; 
} 

courses *create_list(courses *head, courses *entry) 
{ 
    if (head == NULL) 
    { 
      return entry; 
    } 
    courses* curr = head; 
    while (curr->next != NULL) 
    { 
      curr = curr->next; 
    } 
    curr->next = entry; 

    return head; 
} 

void Print(courses *head) 
{ 
    if(head == NULL) 
    { 
      printf("Course list is empty\n\n"); 
      return; 
    } 
    printf("\n"); 
    while(head) 
    { 
      printf("%20s %20d\n",head->abbr,head->credits); 
    } 
    printf("\n\n"); 
} 

int isName(char *string) 
{ 
    int length = strlen(string); 
    int i; 
    if (isupper(string[0])) 
    { 
      for (i=1; i<length; i++) 
      { 
        if (isupper(string[i]) || string[i] == ':') 
        { 
          continue; 
        } 
        else 
        { 
          return 0; 
        } 
      } 
      return 1; 
    } 
    return 0; 
} 

int isPre(char *string) 
{ 
    int length = strlen(string); 
    int i; 
    if (length == 14) 
    { 
      printf("\t%s\n",string); 
      return 1; 
    } 
    return 0; 
} 

int isProf(char *string) 
{ 
    int length = strlen(string); 
    int i; 
    if (isupper(string[0])) 
    { 
      for (i=1; i<length; i++) 
      { 
        if (islower(string[i])) 
        { 
          continue; 
        } 
        else 
        { 
          return 0; 
        } 
      } 
      return 1; 
    } 
    return 0; 
} 

int isCredit(char *string) 
{ 
    int n; 
    int nfields = sscanf(string, "%d", &n); 
    if (nfields == 1) 
    { 
      return 1; 
    } 
    return 0; 
} 

int isAbbr(char *string) 
{ 
    int length = strlen(string); 
    if (length == 8 && string[4] == '-') 
    { 
      //printf(" %s\n",string); 
      return 1; 
    } 
    return 0; 
} 
+0

문제가있는 위치에 대한 약간의 힌트는 어떨까요 ... –

+0

저에게 comp-sci 숙제처럼 보입니다. –

답변

0

GDB와 같은 디버거를 사용하는 방법을 배우십시오. 이처럼 버그를 찾는 것은 사소한 일입니다. 이것은 링크드리스트이므로, 귀하의 코스에 대한 첫 번째 과제라고 생각합니다. StackOverflow를 방문한 후 발생한 모든 오류에 대한 응답을 기다리는 것은 효과가 없을 것입니다.

모든 프로그래머는 디버거 사용 방법을 알아야합니다. 당신 없이는 학교를 통과 할 수 없습니다.

관련 문제