중복 출력 문제...(큐)...

sulbang의 이미지

#include "stdio.h"
#include "conio.h"

#define MAX 5
#define IS_FULL(ptr) (!(ptr))
#define IS_EMPTY(ptr) printf(ptr)

void Insert(int *, char *);
void Delete(int *, int );
void Prt_Sdule(int *, int *);
char Sdule_Menu(void);

char *Sdule[MAX];
int front = -1;
int rear = -1;

void main(void)
{ 
	char Select;
	char str[100];
	
	printf("아래 항목 중 하나를 선택하세요\n"
		   "최대 5번 까지 추가 또는 삭제 가능\n"
		   "a. 입력할 스케줄\n"
		   "b. 입력한 스케줄 삭제\n"
		   "c. 현재 스케줄 출력\n"
		   "q. 나가기\n");
	
	do { 
		Select = Sdule_Menu();
		switch(Select) {
			case 'a':
			  Insert(&rear,str);
			  break;
			case 'b':
			  Delete(&front,rear);
			  break;
			case 'c':
			  Prt_Sdule(&front,&rear);
			  break;
		}
	} while(Select!='q');
	
}

char Sdule_Menu()
{
	char sel;
	
	printf("\n해당 문자를 선택하세요 : ");
	sel = getche();
	printf("\n");

	return sel;
}

void Insert(int *rear,char *str1)
{
	if(*rear == MAX-1) {
		printf("추가 불가능",IS_FULL(rear));
		return;
	}
	
	printf("스케줄 입력\n");
	gets(str1);
	Sdule[++*rear] = str1; 
	
	
}

void Delete(int *front, int rear)
{
	if(*front == rear) {  
		printf("%d",IS_EMPTY("삭제불가"));
		
	}	
	
	printf("스케줄 삭제\n");
    Sdule[++*front];
	
}

void Prt_Sdule(int *front, int *rear)
{
	int j;
	printf("현재 스케줄 목록\n");
	for(j=*front+1;j<=*rear;j++)
	  printf("%s\n",*(Sdule+j));
}

데이터를 처음에 추가하고 출력하면 제대로 나오는데
두번째 부터 출력하면 두번째 입력한 값이 중복이 됩니다
어느 부분에서 잘못이 되었는지 .....
참고로 큐를 구현한 겁니다...

이 부분이 문제인것 같은데 ...


void Prt_Sdule(int *front, int *rear)
{
	int j;
	printf("현재 스케줄 목록\n");
	for(j=*front+1;j<=*rear;j++)
	  printf("%s\n",*(Sdule+j));
}
sodomau의 이미지

출력하는 부분이 문제가 아니라
char * array인 Sdule에
str의 주소만을 넘겨주는데 문제가 있습니다.

 printf("스케줄 입력\n"); 
 gets(str1); 
 Sdule[++*rear] = str1; 

이 부분이 문제입니다.
이런 코드에서는
Sdule 배열의 각각에 str1 의 값이 들어가게 됩니다.
여기서 str1의 값이란 문자열이 아닌 문자열을 담은 배열의
시작 주소입니다.
따라서 Sdule 배열의 element마다 결과적으로 똑같은 주소를
집어넣게 되고,
출력하면 당연히 중복되어 나올 수밖에 없습니다.
고친다면

Sdule[++*rear] = malloc(strlen(str1));
strcpy(Sdule[*rear],str1);

이정도가 되겠네요.

그리고 좀 더 얘기하자면
큐 구현이 썩 매끄럽지가 않아 보이네요.
front와 rear는 큐의 내부에서 사용될 변수들인데,
main에서 그것들을 이용해 호출하고 하는 방식은
좋은 방식이 아닙니다.

sulbang의 이미지

아 그렇군요 .. 답변 감사합니다

전 이렇게 고쳤습니다...

Sdule[++*rear] = (char *) malloc(sizeof(str1));
strcpy(Sdule[*rear],str1);

흠 .....

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.