2016-09-06 13 views
0

Ich bin ein Anfänger in C und habe versucht, ein Stack-Programm zu schreiben, das die Elemente darin ziehen/schieben und anzeigen kann. Ich hatte einige Probleme, auf die ich nicht näher eingehen kann. Hier ist mein Code so weit:Stacks on C (Einfaches Programm)

#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 

Prozess ergab 0 (0x0) Ausführungszeit: 11,474 s Drücken Sie eine beliebige Taste, um fortzufahren.

Scheint, es speichert die Elemente nicht richtig oder vielleicht Es ist falsch gedruckt. Vielleicht ist es falsch, die obere Variable zu verwenden, um durch die Schleifen zu laufen? Jede Hilfe würde sehr geschätzt werden.

+0

Was ist Ihre tatsächliche Ausgabe/Fehler? Nichts springt heraus als falsch. –

+0

Ihr Compiler wurde verpflichtet, eine Diagnose zu erstellen: C11-Draft-Standard n1570: * 6.8.6.4 Die return-Anweisung 1 Eine return-Anweisung mit einem Ausdruck soll nicht in einer Funktion erscheinen, deren Rückgabetyp ungültig ist. Eine return-Anweisung ohne einen Ausdruck darf nur in einer Funktion erscheinen, deren Rückgabetyp ungültig ist. * Beachten Sie, dass dies eine Einschränkung ist. – EOF

+0

Wenn ich die Liste ausgedruckt habe, sollte mir die Ausgabe '100' und '500' gegeben haben, was ich in den Stapel geschoben habe. Stattdessen bekomme ich immer diese negativen Werte dazwischen. – tadm123

Antwort

2

Ihre Push-Funktion wird nur ein char:

void push(char i)

char s von 0 bis 255 (unsigned char) oder -127 bis 128 begrenzt sind (signed char = char - das ist die Standardeinstellung).

Die von Ihnen eingegebene '500' wird also reduziert (mod 256) auf -12 - und das wird gedruckt.

Sie müssen den Parameter int machen, und es sollte funktionieren.

+0

Vielen Dank, das war der Grund. Es funktioniert jetzt. – tadm123