안녕하세요 컴퓨터공학과 재학중인 학생입니다. <div>컴퓨터 공학과 재학중임에도 c언어, 프로그래밍 전공분야에서 적성과 맞지않아 애를 많이먹고있습니다...</div> <div><br></div> <div>최근 자료구조과목을 이수하면서 교수님께서 과제를 내주셨습니다.</div> <div>수업도 열심히듣고있고, 과제해결을 하기위해 책도 여러번읽고 구글 네이버도 뒤져가며 열심히 하고있지만</div> <div>좀처럼 쉽지가 않습니다... 기초가 부족해서인지 참 어렵기만 하네요.</div> <div><br></div> <div>그래서 이렇게 도움을 요청하고자 글을 쓰게 되었습니다.</div> <div>다는 못드리겠지만, 괜찮으시다면 도움주신분 한분께 사례의 의미로 카페 기프티콘 보내드리겠습니다.</div> <div><br></div> <div>배열을 이용한 리스트 ADT에서의 라인에디터 프로그램 코드입니다.</div> <div>다음 프로그램을 실행하다 보면 정상작동 하기까지 오류가 발생하는것들이 꽤나 있습니다.</div> <div>그 것을 5가지정도 발견해서 고쳐오는것이 과제인데요,</div> <div>제가 고친것은 최초 입력단계에서 입력되지않아 fflush함수 앞에 getchar함수로 널문자를 빼서 입력받을수 있게 하는것 정도였습니다.</div> <div><br></div> <div>라인에디터에서 1번행으로 바로 삽입했을때 출력되지 않는 현상도 있었고,</div> <div>파일입력을 했을 경우에도 출력되지 않았습니다.</div> <div><br></div> <div>어떤 부분을 어떻게 고치면 될까요.. 또 c언어는 도대체 어떻게 공부해야 잘 할 수 있는걸까요.</div> <div>전공에 대한 고민이 참 많아지네요.. 도움 부탁드립니다.</div> <div><br></div> <div><div>#include <stdio.h></div> <div>#include <stdlib.h></div> <div>#include <ctype.h></div> <div>#include <string.h></div> <div><br></div> <div>#define MAX_CHAR_PER_LINE<span style="white-space:pre;"> </span>1000</div> <div>#define MAX_CHAR_PER_LINE 1000</div> <div>#define MAX_NAME<span style="white-space:pre;"> </span>256</div> <div>void warning(char *);</div> <div>void error(char *);</div> <div><br></div> <div>#define FALSE 0</div> <div>#define TRUE 1</div> <div><br></div> <div>typedef struct {</div> <div><span style="white-space:pre;"> </span>char a[MAX_CHAR_PER_LINE];</div> <div>} element;</div> <div>typedef struct ListNode {</div> <div><span style="white-space:pre;"> </span>element data;</div> <div><span style="white-space:pre;"> </span>struct ListNode *link;</div> <div>} ListNode;</div> <div>typedef struct {</div> <div><span style="white-space:pre;"> </span>ListNode *head;</div> <div><span style="white-space:pre;"> </span>int length;</div> <div>} ListType;</div> <div><br></div> <div>void insert_node(ListNode **phead, ListNode *p,</div> <div><span style="white-space:pre;"> </span>ListNode *new_node)</div> <div>{</div> <div><span style="white-space:pre;"> </span>if (*phead == NULL) {</div> <div><span style="white-space:pre;"> </span>new_node->link = NULL;</div> <div><span style="white-space:pre;"> </span>*phead = new_node;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>else if (p == NULL) {</div> <div><span style="white-space:pre;"> </span>new_node->link = *phead;</div> <div><span style="white-space:pre;"> </span>*phead = new_node;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>else {</div> <div><span style="white-space:pre;"> </span>new_node->link = p->link;</div> <div><span style="white-space:pre;"> </span>p->link = new_node;</div> <div><span style="white-space:pre;"> </span>}</div> <div>}</div> <div><br></div> <div>void remove_node(ListNode **phead, ListNode *p, ListNode *removed)</div> <div>{</div> <div><span style="white-space:pre;"> </span>if (p == NULL)</div> <div><span style="white-space:pre;"> </span>*phead = (*phead)->link;</div> <div><span style="white-space:pre;"> </span>else</div> <div><span style="white-space:pre;"> </span>p->link = removed->link;</div> <div><span style="white-space:pre;"> </span>free(removed);</div> <div>}</div> <div><br></div> <div>void init(ListType *list)</div> <div>{</div> <div><span style="white-space:pre;"> </span>if (list == NULL) return;</div> <div><span style="white-space:pre;"> </span>list->length = 0;</div> <div><span style="white-space:pre;"> </span>list->head = NULL;</div> <div>}</div> <div><br></div> <div>ListNode *get_node_at(ListType *list, int pos)</div> <div>{</div> <div><span style="white-space:pre;"> </span>int i;</div> <div><span style="white-space:pre;"> </span>ListNode *tmp_node = list->head;</div> <div><span style="white-space:pre;"> </span>if (pos < 0) return NULL;</div> <div><span style="white-space:pre;"> </span>for (i = 0; i<pos; i++)</div> <div><span style="white-space:pre;"> </span>tmp_node = tmp_node->link;</div> <div><span style="white-space:pre;"> </span>return tmp_node;</div> <div>}</div> <div><br></div> <div>int get_length(ListType *list)</div> <div>{</div> <div><span style="white-space:pre;"> </span>return list->length;</div> <div>}</div> <div><br></div> <div><br></div> <div>void add(ListType *list, int position, element data)</div> <div>{</div> <div><span style="white-space:pre;"> </span>ListNode *p;</div> <div><span style="white-space:pre;"> </span>if ((position >= 0) && (position <= list->length)) {</div> <div><span style="white-space:pre;"> </span>ListNode*node = (ListNode *)malloc(sizeof(ListNode));</div> <div><span style="white-space:pre;"> </span>if (node == NULL) error("메모리 할당에러");</div> <div><span style="white-space:pre;"> </span>node->data = data;</div> <div><span style="white-space:pre;"> </span>p = get_node_at(list, position - 1);</div> <div><span style="white-space:pre;"> </span>insert_node(&(list->head), p, node);</div> <div><span style="white-space:pre;"> </span>list->length++;</div> <div><span style="white-space:pre;"> </span>}</div> <div>}</div> <div><br></div> <div>void add_last(ListType *list, element data)</div> <div>{</div> <div><span style="white-space:pre;"> </span>add(list, get_length(list), data);</div> <div>}</div> <div><br></div> <div>void add_first(ListType *list, element data)</div> <div>{</div> <div><span style="white-space:pre;"> </span>add(list, 0, data);</div> <div>}</div> <div><br></div> <div>int is_empty(ListType *list)</div> <div>{</div> <div><span style="white-space:pre;"> </span>if (list->head == NULL) return 1;</div> <div><span style="white-space:pre;"> </span>else return 0;</div> <div>}</div> <div><br></div> <div>void delete(ListType *list, int pos)</div> <div>{</div> <div><span style="white-space:pre;"> </span>if (!is_empty(list) && (pos >= 0) && (pos < list->length)) {</div> <div><span style="white-space:pre;"> </span>ListNode *p = get_node_at(list, pos - 1);</div> <div><span style="white-space:pre;"> </span>remove_node(&(list->head), p, (p != NULL) ? p->link : NULL);</div> <div><span style="white-space:pre;"> </span>list->length--;</div> <div><span style="white-space:pre;"> </span>}</div> <div>}</div> <div><br></div> <div>element get_entry(ListType *list, int pos)</div> <div>{</div> <div><span style="white-space:pre;"> </span>ListNode *p;</div> <div><span style="white-space:pre;"> </span>if (pos >= list->length) error("위치 오류");</div> <div><span style="white-space:pre;"> </span>p = get_node_at(list, pos);</div> <div><span style="white-space:pre;"> </span>return p->data;</div> <div>}</div> <div><br></div> <div>void clear(ListType *list)</div> <div>{</div> <div><span style="white-space:pre;"> </span>int i;</div> <div><span style="white-space:pre;"> </span>for (i = 0;i<list->length;i++)</div> <div><span style="white-space:pre;"> </span>delete(list, i);</div> <div>}</div> <div><br></div> <div>void display(ListType *buffer)</div> <div>{</div> <div><span style="white-space:pre;"> </span>int i;</div> <div><span style="white-space:pre;"> </span>ListNode *tmp_node;</div> <div><span style="white-space:pre;"> </span>tmp_node = buffer->head;</div> <div><br></div> <div><span style="white-space:pre;"> </span>printf("**************\n");</div> <div><span style="white-space:pre;"> </span>for (i = 0;i<buffer->length;i++) {</div> <div><span style="white-space:pre;"> </span>printf("%s", tmp_node->data.a);</div> <div><span style="white-space:pre;"> </span>tmp_node = tmp_node->link;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>printf("**************\n");</div> <div>}</div> <div><br></div> <div>void warning(char *message)</div> <div>{</div> <div><span style="white-space:pre;"> </span>fprintf(stderr, "%s\n", message);</div> <div>}</div> <div><br></div> <div>void error(char *message)</div> <div>{</div> <div><span style="white-space:pre;"> </span>fprintf(stderr, "%s\n", message);</div> <div><span style="white-space:pre;"> </span>exit(1);</div> <div>}</div> <div><br></div> <div>void help()</div> <div>{</div> <div><span style="white-space:pre;"> </span>printf("**************\n");</div> <div><span style="white-space:pre;"> </span>printf("i: 입력\n");</div> <div><span style="white-space:pre;"> </span>printf("d: 삭제\n");</div> <div><span style="white-space:pre;"> </span>printf("r: 파일읽기\n");</div> <div><span style="white-space:pre;"> </span>printf("w: 파일쓰기\n");</div> <div><span style="white-space:pre;"> </span>printf("q: 종료\n");</div> <div><span style="white-space:pre;"> </span>printf("**************\n");</div> <div>}</div> <div><br></div> <div>void read_file(ListType *buffer)</div> <div>{</div> <div><span style="white-space:pre;"> </span>char fname[MAX_NAME];</div> <div><span style="white-space:pre;"> </span>FILE *fd;</div> <div><span style="white-space:pre;"> </span>element p;</div> <div><br></div> <div><span style="white-space:pre;"> </span>if (!is_empty(buffer)) {</div> <div><span style="white-space:pre;"> </span>clear(buffer);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>init(buffer);</div> <div><br></div> <div><span style="white-space:pre;"> </span>printf("파일 이름: ");</div> <div><span style="white-space:pre;"> </span>scanf("%s", fname);</div> <div><span style="white-space:pre;"> </span>if ((fd = fopen(fname, "r")) == NULL) {</div> <div><span style="white-space:pre;"> </span>warning("파일을 열수없습니다.");</div> <div><span style="white-space:pre;"> </span>return;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>while (fgets(p.a, MAX_CHAR_PER_LINE, fd)) {</div> <div><span style="white-space:pre;"> </span>add_last(buffer, p);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>fclose(fd);</div> <div><span style="white-space:pre;"> </span>display(buffer);</div> <div>}</div> <div><br></div> <div>void write_file(ListType *buffer)</div> <div>{</div> <div><span style="white-space:pre;"> </span>FILE *fd;</div> <div><span style="white-space:pre;"> </span>char fname[MAX_NAME];</div> <div><span style="white-space:pre;"> </span>element p;</div> <div><span style="white-space:pre;"> </span>int i;</div> <div><br></div> <div><span style="white-space:pre;"> </span>printf("파일 이름: ");</div> <div><span style="white-space:pre;"> </span>scanf("%s", fname);</div> <div><span style="white-space:pre;"> </span>if ((fd = fopen(fname, "w")) == NULL) {</div> <div><span style="white-space:pre;"> </span>printf("파일을 열수없습니다.\n");</div> <div><span style="white-space:pre;"> </span>return;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>for (i = 0;i<get_length(buffer);i++) {</div> <div><span style="white-space:pre;"> </span>p = get_entry(buffer, i);</div> <div><span style="white-space:pre;"> </span>fputs(p.a, fd);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>fclose(fd);</div> <div><span style="white-space:pre;"> </span>display(buffer);</div> <div>}</div> <div><br></div> <div>void delete_line(ListType *buffer)</div> <div>{</div> <div><span style="white-space:pre;"> </span>int position;</div> <div><br></div> <div><span style="white-space:pre;"> </span>if (is_empty(buffer))</div> <div><span style="white-space:pre;"> </span>printf("지울 라인이 없습니다.\n");</div> <div><span style="white-space:pre;"> </span>else</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>printf("지우고 싶은 라인번호를 입력하세요:\n");</div> <div><span style="white-space:pre;"> </span>scanf("%d", &position);</div> <div><span style="white-space:pre;"> </span>delete(buffer, position);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>display(buffer);</div> <div>}</div> <div><br></div> <div>{</div> <div><span style="white-space:pre;"> </span>int position;</div> <div><span style="white-space:pre;"> </span>char line[MAX_CHAR_PER_LINE];</div> <div><span style="white-space:pre;"> </span>element p;</div> <div><br></div> <div><span style="white-space:pre;"> </span>printf("입력행번호를 입력하세요: \n");</div> <div><span style="white-space:pre;"> </span>scanf("%d", &position);</div> <div><br></div> <div><span style="white-space:pre;"> </span>printf("내용을 입력하세요: ");</div> <div><span style="white-space:pre;"> </span>fflush(stdin);</div> <div><span style="white-space:pre;"> </span>fgets(line, MAX_CHAR_PER_LINE, stdin);</div> <div><span style="white-space:pre;"> </span>strcpy(p.a, line);</div> <div><span style="white-space:pre;"> </span>add(buffer, position, p);</div> <div><span style="white-space:pre;"> </span>display(buffer);</div> <div>}</div> <div><br></div> <div>void do_command(ListType *buffer, char command)</div> <div>{</div> <div><span style="white-space:pre;"> </span>switch (command)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>case 'd':</div> <div><span style="white-space:pre;"> </span>delete_line(buffer);</div> <div><span style="white-space:pre;"> </span>break;</div> <div><span style="white-space:pre;"> </span>case 'i':</div> <div><span style="white-space:pre;"> </span>insert_line(buffer);</div> <div><span style="white-space:pre;"> </span>break;</div> <div><span style="white-space:pre;"> </span>case 'r':</div> <div><span style="white-space:pre;"> </span>read_file(buffer);</div> <div><span style="white-space:pre;"> </span>break;</div> <div><span style="white-space:pre;"> </span>case 'w':</div> <div><span style="white-space:pre;"> </span>write_file(buffer);</div> <div><span style="white-space:pre;"> </span>break;</div> <div><span style="white-space:pre;"> </span>case 'q':</div> <div><span style="white-space:pre;"> </span>break;</div> <div><span style="white-space:pre;"> </span>}</div> <div>}</div> <div><br></div> <div>void main()</div> <div>{</div> <div><span style="white-space:pre;"> </span>char command;</div> <div><span style="white-space:pre;"> </span>ListType buffer;</div> <div><br></div> <div><span style="white-space:pre;"> </span>init(&buffer);</div> <div><span style="white-space:pre;"> </span>do {</div> <div><span style="white-space:pre;"> </span>help();</div> <div><span style="white-space:pre;"> </span>command = getchar();</div> <div><span style="white-space:pre;"> </span>do_command(&buffer, command);</div> <div><span style="white-space:pre;"> </span>fflush(stdin);</div> <div><span style="white-space:pre;"> </span>} while (command != 'q');</div> <div>}</div></div> <div><br></div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.