lpf@lpf:~$ cat -n stack.c
1 #define maxlen 1000
2 #define empty -1
3 #define full (maxlen-1)
4 #include<stdio.h>
5 typedef enum boolean {false,true} boolean;
6 typedef struct stack{
7 char s[maxlen];
8 int top;
9 } stack;
10 void reset(stack *stk)
11 {
12 stk->top=empty;
13 }
14 void push(char c,stack *stk)
15 {
16 stk->top++;
17 stk->s[stk->top]=c;
18 }
19 void pop(stack *stk)
20 {
21 return(stk->s[stk->top--]);
22 }
23 void top(const stack *stk)
24 {
25 return(stk->s[stk->top]);
26 }
27 boolean empty(const stack *stk)
28 {
29 return((boolean)(stk->top==empty));
30 }
31 boolean full(const stack *stk)
32 {
33 return((boolean)(stk->top==full));
34 }
35 int main(void)
36 {
37 char str[]="my name is laura";
38 int i;
39 stack s;
40 reset(&s);
41 printf("in the string: %s\n",str);
42 for(i=0;str[i]!='\0';++i)
43 if(!full(&s))
44 push(str[[i],&s);
45 printf("from the stack: ");
46 while(!empty(&s))
47 putchar(pop(&s));
48 putchar('\n');
49 return 0;
50 }
lpf@lpf:~$ gcc stack.c
stack.c: In function ‘pop’:
stack.c:21: warning: ‘return’ with a value, in function returning void
stack.c: In function ‘top’:
stack.c:25: warning: ‘return’ with a value, in function returning void
stack.c: At top level:
stack.c:27: error: expected identifier or ‘(’ before ‘-’ token
stack.c:31: error: expected identifier or ‘(’ before numeric constant
stack.c: In function ‘main’:
stack.c:43: error: called object ‘999’ is not a function
stack.c:44: error: expected expression before ‘[’ token
stack.c:44: error: array subscript is not an integer
stack.c:44: error: too few arguments to function ‘push’
stack.c:46: error: called object ‘1’ is not a function
stack.c:47: error: invalid use of void expression
不知道为什么运行不了...我在它提示出错的地方找不到错误 - -!