2016-09-06 2 views
0

저는 C 언어의 초보자이며, 그 안에 요소를 끌어 넣고 표시 할 수있는 스택 프로그램을 작성하려고했습니다. 나는 그 뿌리가 어디인지 정확히 지적 할 수없는 몇 가지 문제에 직면 해 있었다. 다음은 지금까지 내 코드입니다 : 11.474의 을 눌러 계속하려면 아무 키나 :C (Simple Program)의 스택

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 

#define STACK_SIZE 100 

char ch; 
int contents[STACK_SIZE], top = 0; 

void display(); 

int stack_overflow(void) 
{ 
     printf("Expression is too complex\n"); 
     exit(EXIT_FAILURE); 
} 

int stack_underflow(void) 
{ 
     printf("Not enough operands in expression\n"); 
     exit(EXIT_FAILURE); 
} 

void make_empty(void) 
{ 
    top = 0;    //makes the element at the top of the stack = 0 
} 

bool is_empty(void) 
{ 
    return top == 0;  //returns 1 if the top is equal to 0 
} 

bool is_full(void) 
{ 
    return top == STACK_SIZE; //returns 1 if the top is the maximun stack size (already full) 
} 

void push(char i) 
{ 
    if (is_full()) 
     stack_overflow(); 
    else 
     contents[top++] = i; 
} 

char pop(void) 
{ 
    if (is_empty()) 
     stack_underflow(); 
    else 
     return contents[--top]; 
} 


int main(void) 
{ 
    int choice; 
    int option = 1; 
    int num; 



    printf ("STACK OPERATION\n"); 
    while (option) 
    { 
     printf ("------------------------------------------\n"); 
     printf (" 1 --> PUSH \n"); 
     printf (" 2 --> POP \n"); 
     printf (" 3 --> DISPLAY \n"); 
     printf (" 4 --> EXIT \n"); 
     printf ("------------------------------------------\n"); 

     printf ("Enter your choice\n"); 
     scanf ("%d", &choice); 

     switch (choice) 
     { 
      case 1: printf("Enter number you want to push: "); 
        scanf("%d",&num); 
        push(num); 
        break; 

      case 2: pop(); 
        break; 

      case 3: display(); 
        break; 

      case 4: return; 
     } 

     fflush (stdin); 
     printf ("Do you want to continue(Type 0 or 1)?\n"); 
     scanf ("%d", &option); 
    } 
} 



void display() 
{ 
    int i; 

    printf("\n\n"); 

    for(i=0;i< top ;i++) 
     printf("%d\n",contents[i]); 
} 


STACK OPERATION 
------------------------------------------ 
1 --> PUSH 
2 --> POP 
3 --> DISPLAY 
4 --> EXIT 
------------------------------------------ 
Enter your choice 
1 
Enter number you want to push: 100 
Do you want to continue(Type 0 or 1)? 
1 
------------------------------------------ 
1 --> PUSH 
2 --> POP 
3 --> DISPLAY 
4 --> EXIT 
------------------------------------------ 
Enter your choice 
1 
Enter number you want to push: 500 
Do you want to continue(Type 0 or 1)? 
1 
------------------------------------------ 
1 --> PUSH 
2 --> POP 
3 --> DISPLAY 
4 --> EXIT 
------------------------------------------ 
Enter your choice 
3 


100 
-12 
Do you want to continue(Type 0 or 1)? 
0 

프로세스 0 (0x0으로) 실행 시간을 돌려 보냈다.

요소를 올바르게 저장하지 않았거나 잘못 인쇄 한 것처럼 보입니다. 루프를 돌파하기 위해 최상위 변수를 사용하는 것이 잘못된 것입니까? 어떤 도움이라도 대단히 감사 할 것입니다.

+0

실제 출력/오류는 무엇입니까? 아무것도 잘못된 것으로 튀어 나오지 않습니다. –

+0

귀하의 컴파일러는 진단서를 발행해야합니다 : C11 draft standard n1570 * 6.8.6.4 return 문 1 표현식이있는 return 문은 반환 유형이 인 void 함수로 나타나면 안됩니다. 표현식이없는 return 문은 반환 유형이 void 인 함수에만 나타납니다. * 이것은 제약 조건입니다. – EOF

+0

내가 출력물을 출력했을 때 '100'과 '500'이 나에게 줬다. 대신 나는 항상 그 사이의 음수 값을 얻는다. – tadm123

답변

2

내 푸시 기능 만 char 얻는다 :

void push(char i)

char들 (128) 255 (unsigned char) 또는 -127 0에서 한정된다 (signed char = char - 그 기본값).

당신이 넣는 '500'은 따라서 (-126)으로 줄어들어 인쇄됩니다.

매개 변수를 int으로 설정해야 작동 할 수 있습니다.

+0

고마워, 이것이 이유였다. 이제 작동 중입니다. – tadm123