편집 기록

편집 기록
  • 프로필 김지선님의 편집
    날짜2019.09.29

    c언어 동적 메모리 할당으로 순차 정렬


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct student {
            int id;
            char name[20];
        int score[4];
            struct student *next;
    };
    
    int main()
    {
    
            int count = 0, id,sc[4];
            char name[20],line[200];
            struct student *p, *head, *node = NULL;
            printf("학번과  이름을 입력하세요\n");
            fgets(line, 200, stdin);
            while (fgets(line, 200, stdin) != NULL) {
                sscanf(line, "%d %s %d %d %d %d", &id, name,
                   &sc[0],&sc[1],&sc[2],&sc[3]);
                    p = (struct student *) malloc(sizeof(struct student));
                    node = (struct student *) malloc(sizeof(struct student));
                    if (head == NULL)
                            head = p;
                    else
                            node->next = p;
                    node = p;
                    if (p == NULL) {
                            perror("malloc");
                            exit(1);
                    }
                    p->id = id;
                    strcpy(p->name, name);
                    node=p->next;
            for(int i=0; i< 4; i++)
               p->score[i]=sc[i];
            }
    
            printf("\n* 학생 정보 *\n");
            p = head;
            while (p != NULL) {
                    count++;
                    printf("학번: %d 이름: %s : ", p->id, p->name);
            for(int i=0; i< 4; i++)
               printf("%6d", p->score[i]);
            printf("\n");
                    p = p->next;
            }
    
            printf("총 %d 명입니다.\n", count);
            exit(0);
    }
    

    //부분을 고쳐서 순차적으로? 정렬하는 건 어떻게 하나요??ㅠㅠㅠㅠ 파일 자체가 순차적으로 정렬이 되어있어서 노드로 이어주면서 출력을 하면 될 것 같은데 계속 안 돼서 질문 드립니다... 모바엑스텀에서 하는 걸로 txt 파일을 리다이렉션으로 실행 할 때 넣어주는 방법으로 실행시키고 있습니다. 이걸 실행시키면 세그멘테이션 오류나 39줄이 정렬 되어야하는데 한 줄만 나오는 그런 결과가 나와서 질문합니다ㅜㅜ

  • 프로필 정영훈님의 편집
    날짜2019.09.29

    c언어 동적 메모리 할당으로 순차 정렬


    (앞부분이 코드로 계속 안 올라가네요ㅠㅠ

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct student {
            int id;
            char name[20];
        int score[4];
            struct student *next;
    };
    
    int main()
    {
    
            int count = 0, id,sc[4];
            char name[20],line[200];
            struct student *p, *head, *node = NULL;
            printf("학번과  이름을 입력하세요\n");
            fgets(line, 200, stdin);
            while (fgets(line, 200, stdin) != NULL) {
                sscanf(line, "%d %s %d %d %d %d", &id, name,
                   &sc[0],&sc[1],&sc[2],&sc[3]);
                    p = (struct student *) malloc(sizeof(struct student));
                    node = (struct student *) malloc(sizeof(struct student));
                    if (head == NULL)
                            head = p;
                    else
                            node->next = p;
                    node = p;
                    if (p == NULL) {
                            perror("malloc");
                            exit(1);
                    }
                    p->id = id;
                    strcpy(p->name, name);
                    node=p->next;
            for(int i=0; i< 4; i++)
               p->score[i]=sc[i];
            }
    
            printf("\n* 학생 정보 *\n");
            p = head;
            while (p != NULL) {
                    count++;
                    printf("학번: %d 이름: %s : ", p->id, p->name);
            for(int i=0; i< 4; i++)
               printf("%6d", p->score[i]);
            printf("\n");
                    p = p->next;
            }
    
            printf("총 %d 명입니다.\n", count);
            exit(0);
    }
    

    //부분을 고쳐서 순차적으로? 정렬하는 건 어떻게 하나요??ㅠㅠㅠㅠ 파일 자체가 순차적으로 정렬이 되어있어서 노드로 이어주면서 출력을 하면 될 것 같은데 계속 안 돼서 질문 드립니다... 모바엑스텀에서 하는 걸로 txt 파일을 리다이렉션으로 실행 할 때 넣어주는 방법으로 실행시키고 있습니다. 이걸 실행시키면 세그멘테이션 오류나 39줄이 정렬 되어야하는데 한 줄만 나오는 그런 결과가 나와서 질문합니다ㅜㅜ

  • 프로필 김지선님의 편집
    날짜2019.09.29

    c언어 동적 메모리 할당으로 순차 정렬


    (앞부분이 코드로 계속 안 올라가네요ㅠㅠ

    include

    include

    include

    struct student { int id; char name[20]; int score[4]; struct student *next; };

    int main() {

        int count = 0, id,sc[4];
        char name[20],line[200];
        struct student *p, *head, *node = NULL;
        printf("학번과  이름을 입력하세요\n");
        fgets(line, 200, stdin);
        while (fgets(line, 200, stdin) != NULL) {
            sscanf(line, "%d %s %d %d %d %d", &id, name,
               &sc[0],&sc[1],&sc[2],&sc[3]);
                p = (struct student *) malloc(sizeof(struct student));
                node = (struct student *) malloc(sizeof(struct student));
                if (head == NULL)
                        head = p;
                else
                        node->next = p;
                node = p;
                if (p == NULL) {
                        perror("malloc");
                        exit(1);
                }
                p->id = id;
                strcpy(p->name, name);
                node=p->next;
        for(int i=0; i< 4; i++)
           p->score[i]=sc[i];
        }
    
        printf("\n* 학생 정보 *\n");
        p = head;
        while (p != NULL) {
                count++;
                printf("학번: %d 이름: %s : ", p->id, p->name);
        for(int i=0; i< 4; i++)
           printf("%6d", p->score[i]);
        printf("\n");
                p = p->next;
        }
    
        printf("총 %d 명입니다.\n", count);
        exit(0);
    

    }

    //부분을 고쳐서 순차적으로? 정렬하는 건 어떻게 하나요??ㅠㅠㅠㅠ 파일 자체가 순차적으로 정렬이 되어있어서 노드로 이어주면서 출력을 하면 될 것 같은데 계속 안 돼서 질문 드립니다... 모바엑스텀에서 하는 걸로 txt 파일을 리다이렉션으로 실행 할 때 넣어주는 방법으로 실행시키고 있습니다. 이걸 실행시키면 세그멘테이션 오류나 39줄이 정렬 되어야하는데 한 줄만 나오는 그런 결과가 나와서 질문합니다ㅜㅜ

  • 프로필 정영훈님의 편집
    날짜2019.09.29

    c언어 동적 메모리 할당으로 순차 정렬


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct student {
       int id;
       char name[20];
        int score[4];
       struct student *next;
    };
    
    /* 학생 정보를 입력받아 연결 리스트에 저장하고 학생 정보를 역순으로
       출력한다.
    */
    int main()
    {
    
       int count = 0, id,sc[4];
       char name[20],line[200];
       struct student *p, *head = NULL;
       printf("학번과  이름을 입력하세요\n");
       fgets(line, 200, stdin);
       while (fgets(line, 200, stdin) != NULL) {
           sscanf(line, "%d %s %d %d %d %d", &id, name,
                   &sc[0],&sc[1],&sc[2],&sc[3]);
          p = (struct student *) malloc(sizeof(struct student));
       //node = (struct student *) malloc(sizeof(struct student));
          if(head == NULL)
     head = p;
          else
             node->next = p;
          node = p;//
          if (p == NULL) {
             perror("malloc");
             exit(1);
          }
          p->id = id;
          strcpy(p->name, name);
         [ p->next = head;
            for(int i=0; i< 4; i++)
               p->score[i]=sc[i];
          head = p;]
       }
    
       printf("\n* 학생 정보(역순) *\n");
       p = head;
       while (p != NULL) {
          count++;
          printf("학번: %d 이름: %s : ", p->id, p->name);
            for(int i=0; i< 4; i++)
               printf("%6d", p->score[i]);
            printf("\n");
          p = p->next;
    }
    
       printf("총 %d 명입니다.\n", count);
       exit(0);
    }
    

    지금 코드가 //부터 //를 빼면 모바엑스텀에서 리다이렉션으로 파일을 받으면 역순으로 출력하는 코드인데요 저 코드를 넣고 밑에 [ ]괄호친 부분을 고쳐서 순차적으로? 정렬하는 건 어떻게 하나요??ㅠㅠㅠㅠ 파일 자체가 순차적으로 정렬이 되어있어서 노드로 이어주면서 출력을 하면 될 것 같은데 계속 안 돼서 질문 드립니다...

  • 프로필 김지선님의 편집
    날짜2019.09.29

    c언어 동적 메모리 할당으로 순차 정렬


    include

    include

    include

    struct student { int id; char name[20]; int score[4]; struct student *next; };

    /* 학생 정보를 입력받아 연결 리스트에 저장하고 학생 정보를 역순으로 출력한다. */ int main() {

    int count = 0, id,sc[4]; char name[20],line[200]; struct student *p, *head = NULL; printf("학번과 이름을 입력하세요\n"); fgets(line, 200, stdin); while (fgets(line, 200, stdin) != NULL) { sscanf(line, "%d %s %d %d %d %d", &id, name, &sc[0],&sc[1],&sc[2],&sc[3]); p = (struct student *) malloc(sizeof(struct student)); //node = (struct student *) malloc(sizeof(struct student)); if(head == NULL) head = p; else node->next = p; node = p;// if (p == NULL) { perror("malloc"); exit(1); } p->id = id; strcpy(p->name, name); [ p->next = head; for(int i=0; i< 4; i++) p->score[i]=sc[i]; head = p;] }

    printf("\n* 학생 정보(역순) *\n"); p = head; while (p != NULL) { count++; printf("학번: %d 이름: %s : ", p->id, p->name); for(int i=0; i< 4; i++) printf("%6d", p->score[i]); printf("\n"); p = p->next; }

    printf("총 %d 명입니다.\n", count); exit(0); }

    지금 코드가 //부터 //를 빼면 모바엑스텀에서 리다이렉션으로 파일을 받으면 역순으로 출력하는 코드인데요 저 코드를 넣고 밑에 [ ]괄호친 부분을 고쳐서 순차적으로? 정렬하는 건 어떻게 하나요??ㅠㅠㅠㅠ 파일 자체가 순차적으로 정렬이 되어있어서 노드로 이어주면서 출력을 하면 될 것 같은데 계속 안 돼서 질문 드립니다...