편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2020.05.13

    while문이 이상하게 돌아갑니다 scanf


    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_STACK_SIZE 100  // 전역 변수 선언
    char stack[MAX_STACK_SIZE];
    int top = -1;        
    void stackFull() {
        fprintf(stderr, "Stack is full, cannot add element");
        exit(1);
    }
    void stackEmpty() {
        fprintf(stderr, "Stack is Empty");
        exit(1);
    }
    void push(char a)
    {
        if (top >= MAX_STACK_SIZE - 1)
            stackFull();
            stack[++top] = a;  
    }
    char pop(char a)
    {
        if (top == -1)
            stackEmpty();
        return stack[top--];
    }
    void showStack() // 스택에 있는 값들을 출력
    {
        int i;
        if (top == -1) printf("스택에 저장된 값이 없습니다.\n");
        else {
            printf("스택에 저장된 값 목록\n");
            for (i = 0; i < top+1; i++) {
                printf("%03d : %c\n", i + 1, stack[i]);
            }
            printf("%d개의 값이 저장되어 있습니다.\n",top);
        }
    }
    void main()
    {
        int select=0;
        char a;
        while (select != 4) {
            printf("\n1. 스택에 값 넣기\n");
            printf("2. 스택에서 값 꺼내기\n");
            printf("3. 스택에 저장된 값 확인\n");
            printf("4. 종료\n\n");
            printf("입력 : ");
            scanf_s("%d", &select); 
            switch (select) {
            case 1: // 입력한 값을 스택에 저장한다.
                printf("저장할 값을 입력하세요 : ");
                a = getchar();
                push(a);
                break;
            case 2: // 스택에 저장된 정수값을 가져온다.
                printf("가져온 값 : %c", pop(a));
                break;
            case 3: // 스택에 저장된 값들을 보여준다.
                showStack();
                break;
            }
        }
    }
    

    이코드가 제가 생각하던 while문처럼 돌아가지 않고 한단계식 건너 뛰는 느낌으로 진행되네요 틀린것도 없는 것같은데 뭐가 문제일까요

  • 프로필 알 수 없는 사용자님의 편집
    날짜2020.05.12

    while문이 이상하게 돌아갑니다


    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_STACK_SIZE 100  // 전역 변수 선언
    char stack[MAX_STACK_SIZE];
    int top = -1;        
    void stackFull() {
        fprintf(stderr, "Stack is full, cannot add element");
        exit(1);
    }
    void stackEmpty() {
        fprintf(stderr, "Stack is Empty");
        exit(1);
    }
    void push(char a)
    {
        if (top >= MAX_STACK_SIZE - 1)
            stackFull();
            stack[++top] = a;  
    }
    char pop(char a)
    {
        if (top == -1)
            stackEmpty();
        return stack[top--];
    }
    void showStack() // 스택에 있는 값들을 출력
    {
        int i;
        if (top == -1) printf("스택에 저장된 값이 없습니다.\n");
        else {
            printf("스택에 저장된 값 목록\n");
            for (i = 0; i < top+1; i++) {
                printf("%03d : %c\n", i + 1, stack[i]);
            }
            printf("%d개의 값이 저장되어 있습니다.\n",top);
        }
    }
    void main()
    {
        int select=0;
        char a;
        while (select != 4) {
            printf("\n1. 스택에 값 넣기\n");
            printf("2. 스택에서 값 꺼내기\n");
            printf("3. 스택에 저장된 값 확인\n");
            printf("4. 종료\n\n");
            printf("입력 : ");
            scanf_s("%d", &select); 
            switch (select) {
            case 1: // 입력한 값을 스택에 저장한다.
                printf("저장할 값을 입력하세요 : ");
                a = getchar();
                push(a);
                break;
            case 2: // 스택에 저장된 정수값을 가져온다.
                printf("가져온 값 : %c", pop(a));
                break;
            case 3: // 스택에 저장된 값들을 보여준다.
                showStack();
                break;
            }
        }
    }
    

    이코드가 제가 생각하던 while문처럼 돌아가지 않고 한단계식 건너 뛰는 느낌으로 진행되네요 틀린것도 없는 것같은데 뭐가 문제일까요