2015-01-27 3 views
-5

Для моего класса я должен перевести код, который был предоставлен нам, и перевести его с Java на C. Это моя первая встреча с C. Итак, я завершил это. После компиляции я получаю около 6 ошибок, говорящих, что Stack не объявлен. И стек используется в коде. Может кто-то помочь указать, где я ошибся.«Стек» Необъявленная ошибка. Java to C

// $Id: crpn.c,v 1.28 2014-04-08 15:23:19-07 - - $ 
// NAME 
#include <assert.h> 
#include <libgen.h> 
#include <stdio.h> 
#include <stdlib.h> 

int exit_status = EXIT_SUCCESS; 
#define EMPTY (-1) 
#define SIZE 16 

struct stack{ 
    int top; 
    double numbers[SIZE]; 
}; 

void bad_operator (const char *oper) { 
    fflush (NULL); 
    fprintf (stderr, "oper=\"%s\"\n", oper); 
    fflush (NULL); 
    exit_status = EXIT_FAILURE; 
} 

void push (struct stack *the_stack, double number) { 
    if(stack->top >= SIZE - 1){ 
     printf ("%s: stack overflown%n", number); 
    }else{ 
     stack->numbers[++stack->top] = number; 
    } 
} 

void do_binop (struct stack *the_stack, char oper) { 
    if (stack->top < 1) { 
     printf ("'%s': stack undeflow", oper); 
    }else{ 
     double right = stack->numbers[stack->top--]; 
     double left = stack->numbers[stack->top--]; 
     switch (oper) { 
     case '+': push (stack, left + right); break; 
     case '-': push (stack, left - right); break; 
     case '*': push (stack, left * right); break; 
     case '/': push (stack, left/right); break; 
     } 
    } 
} 

void do_print (struct stack *the_stack) { 
    if (stack->top == EMPTY) { 
     printf ("stack is empty%n"); 
    }else { 
     for (int pos = 0; pos <= stack->top; ++pos) { 
      printf ("%s%n", stack->numbers[pos]); 
     } 
    } 
} 

void do_clear (struct stack *the_stack) { 
    stack->top = EMPTY; 
} 

void do_operator (struct stack *the_stack, const char *oper) { 
    switch (oper->charAt(0)) { 
     case '+': do_binop (stack, '+'); break; 
     case '-': do_binop (stack, '-'); break; 
     case '*': do_binop (stack, '*'); break; 
     case '/': do_binop (stack, '/'); break; 
     case ';': do_binop (stack);  break; 
     case '@': do_clear (stack);  break; 
     default : bad_operator (oper); break; 
    } 
} 
int main (int argc, char **argv) { 
    if (argc != 1) { 
     fprintf (stderr, "Usage: %s\n", basename (argv[0])); 
     fflush (NULL); 
     exit (EXIT_FAILURE); 
    } 
    struct stack the_stack; 
    the_stack.top = EMPTY; 
    char buffer[1024]; 
    for (;;) { 
     int scanrc = scanf ("%1023s", buffer); 
     if (scanrc == EOF) break; 
     assert (scanrc == 1); 
     if (buffer[0] == '#') { 
     scanrc = scanf ("%1023[^\n]", buffer); 
     continue; 
     } 
     char *endptr; 
     double number = strtod (buffer, &endptr); 
     if (*endptr == '\0') { 
     push (&the_stack, number); 
     }else if (buffer[1] != '\0') { 
     bad_operator (buffer); 
     }else { 
     do_operator (&the_stack, buffer); 
     } 
    } 
    return exit_status; 
} 

Сообщение об ошибке:

crpn.c: In function ‘push’: 
crpn.c:25:7: error: ‘stack’ undeclared (first use in this function) 
    if(stack->top >= SIZE - 1){ 
    ^
crpn.c:25:7: note: each undeclared identifier is reported only once for each function it appears in 
crpn.c:26:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘double’ [-Wformat=] 
     printf ("%s: stack overflown%n", number); 
    ^
crpn.c:26:7: warning: format ‘%n’ expects a matching ‘int *’ argument [-Wformat=] 
crpn.c:24:26: warning: unused parameter ‘the_stack’ [-Wunused-parameter] 
void push (struct stack *the_stack, double number) { 
         ^
crpn.c: In function ‘do_binop’: 
crpn.c:33:8: error: ‘stack’ undeclared (first use in this function) 
    if (stack->top < 1) { 
     ^
crpn.c:34:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] 
     printf ("'%s': stack undeflow", oper); 
    ^
crpn.c:32:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter] 
void do_binop (struct stack *the_stack, char oper) { 
          ^
crpn.c: In function ‘do_print’: 
crpn.c:48:8: error: ‘stack’ undeclared (first use in this function) 
    if (stack->top == EMPTY) { 
     ^
crpn.c:49:7: warning: format ‘%n’ expects a matching ‘int *’ argument [-Wformat=] 
     printf ("stack is empty%n"); 
    ^
crpn.c:47:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter] 
void do_print (struct stack *the_stack) { 
          ^
crpn.c: In function ‘do_clear’: 
crpn.c:58:4: error: ‘stack’ undeclared (first use in this function) 
    stack->top = EMPTY; 
    ^
crpn.c:57:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter] 
void do_clear (struct stack *the_stack) { 
          ^
crpn.c: In function ‘do_operator’: 
crpn.c:62:16: error: request for member ‘charAt’ in something not a structure or union 
    switch (oper->charAt(0)) { 
       ^
crpn.c:63:27: error: ‘stack’ undeclared (first use in this function) 
     case '+': do_binop (stack, '+'); break; 
         ^
crpn.c:67:7: error: too few arguments to function ‘do_binop’ 
     case ';': do_binop (stack);  break; 
    ^
crpn.c:32:6: note: declared here 
void do_binop (struct stack *the_stack, char oper) { 
    ^
crpn.c:61:33: warning: unused parameter ‘the_stack’ [-Wunused-parameter] 
void do_operator (struct stack *the_stack, const char *oper) { 

ответ

1

'стек' Необъявленная Ошибка

stack тип, но ваш указатель the_stack

void push (struct stack *the_stack, double number) 

поэтому вам нужно использовать the_stack вместо stack в таких заявлениях

if(stack->top >= SIZE - 1){ 
1

переменная объявлена ​​как the_stack:

void push (struct stack *the_stack, double number) { 

Но вы обращаетесь к нему, как stack:

if(stack->top >= SIZE - 1){ 

Там не является переменной stack в вашем коде, поэтому компилятор жалуется на неизвестные идентификаторы. struct stack является базой тип переменной the_stack (полный тип struct stack *).

Смежные вопросы