user-0 提问: C语言栈运用

回复:
user: user-1
content: operator是c++的关键字，所以你不能定义这样名字的变量，你改下名称即可

user: user-2
content: 下面这条语句后面少了分号  int operand2=0 //后操作数  其次operator变量名换个名字，和关键字有重复

user: user-3
content: 扩展名 cpp 应该会按 c++ 编译。  扩展名 c 默认会按 C 编译。

user: user-4
content: #include<stdio.h>   #define MAXSIZE 10  typedef struct {     //char data[MAXSIZE];     int data[MAXSIZE];     int top; }SeqStack;  void Init_SeqStack(SeqStack *s) {     s->top=0; }  /*入栈操作*/ int Push_SeqStack(SeqStack *s, int x) {     if(s->top==MAXSIZE)         return 0;     s->data[s->top]=x;     s->top++;      return 1; }  int Pop_SeqStack(SeqStack *s, int *x) {     if(s->top == 0) {         return 0;//栈空不能出栈     } else {         s->top--;         *x = s->data[s->top];         return 1;     }  } int Empty_SeqStack(SeqStack *s) {     if(s->top == 0)         return 1;     else         return 0; }  int is_operator(int operator) {     switch (operator) {         case '+':         case '-':         case '*':         case '/':             return 1;//是运算符返回1             break;          default:             return 0;//不是运算符返回0     } }  int priority(int o) {     switch(o)     {         case '+':         case '-':             return 1;//加减运算符的优先级为1         case '*':         case '/':             return 2;//乘除运算符的优先级为2         default:             return 0;     } }  int two_result(int operator, int operator1, int operator2) {     switch(operator)     {         case '+':             return(operator2+operator1);         case '-':             return(operator2-operator1);         case '*':             return(operator2*operator1);         case '/':             return(operator2/operator1);     }      return 0; }  int main(void) {     SeqStack operand, operator, *s_operand, *s_operator;     char expression[50];//声明表达式字符数组     int position = 0;//表达式中元素位置     int op = 0;//运算符     int operand1=0;//前操作数     int operand2=0;//后操作数     int evaluate=0;//运算结果      s_operand = &operand;     s_operator = &operator;      Init_SeqStack(s_operand);     Init_SeqStack(s_operator);     gets(expression);      while(expression[position] != '\0' && expression[position] != '\n');     {         if(is_operator(expression[position]))         {             if(!Empty_SeqStack(s_operator))             {                 while(!Empty_SeqStack(s_operator) && priority(expression[position]) <= priority(s_operator->data[s_operator->top-1]))                 {                     Pop_SeqStack(s_operand, &operand1);                     Pop_SeqStack(s_operand,&operand2);                     Pop_SeqStack(s_operator,&op);                     Push_SeqStack(s_operand,two_result(op,operand1,operand2));                 }             }             Push_SeqStack(s_operator,expression[position]);         }         else             Push_SeqStack(s_operand,expression[position]-48);         position++;     }     while(!Empty_SeqStack(s_operator))     {         Pop_SeqStack(s_operator,&op);         Pop_SeqStack(s_operand,&operand1);         Pop_SeqStack(s_operand,&operand2);         Push_SeqStack(s_operand,two_result(op,operand1,operand2));     }     Pop_SeqStack(s_operand,&evaluate);     printf("%d",evaluate);     return 0; } 把源文件的后缀名改一下，改成.c试试； 另外，程序里有太多Pop-SeqStack或Push-SeqStack等等；这些函数的定义的名字都是以_下划线连接的，不是-连接的。 另外，类型不匹配，入栈，出栈的参数定义时使用char类型，调用时，传递的是int类型。这些都需要注意下。 程序没有运行，逻辑问题没有检查；