user-0 提问: 求助 C语言基础

回复:
user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <ctype.h>   #define STACK_INIT_SIZE 20 #define INCREMENT 10   typedef char ElemType;   typedef float ElemType1;   typedef struct{     ElemType *base;     ElemType *top;     int stackSize; }sqStack; typedef struct{     ElemType1 *base;     ElemType1 *top;     int stackSize; }sqStack1;   void InitStack(sqStack *s) {     s->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));     if(!s->base){         exit(0);     }       s->top=s->base;     s->stackSize = STACK_INIT_SIZE; } void InitStack1(sqStack1 *s) {     s->base=(ElemType1 *)malloc(STACK_INIT_SIZE*sizeof(ElemType1));     if(!s->base){         exit(0);     }       s->top=s->base;     s->stackSize=STACK_INIT_SIZE; }   void Push(sqStack *s,ElemType e) {     if(s->top-s->base>=s->stackSize){         s->base=(ElemType *)realloc(s->base,(s->stackSize+INCREMENT)*sizeof(ElemType));         if(!s->base){             exit(0);         }     }     *(s->top)=e;     s->top++; } void Push1(sqStack1 *s,ElemType1 e) {     if(s->top-s->base>=s->stackSize){         s->base=(ElemType1 *)realloc(s->base,(s->stackSize+INCREMENT)*sizeof(ElemType1));         if(!s->base){             exit(0);         }     }     *(s->top)=e;     s->top++; }   void Pop(sqStack *s,ElemType *e){     if(s->top==s->base){         return;     }     *e=*--(s->top); } void Pop1(sqStack1 *s,ElemType1 *e){     if(s->top==s->base){         return;     }     *e=*--(s->top); }   int StackLen(sqStack s){     return (s.top-s.base); } int StackLen1(sqStack1 s){     return (s.top-s.base); }   //实例：实现中缀表达式转换为后缀表达式 int main() {     ElemType c,e,str[INCREMENT];     ElemType1 p1,p2,result;     sqStack s,r,q,x;     sqStack1 p;     int i=0;       InitStack(&s);     InitStack(&r);     InitStack(&q);     InitStack(&x);     InitStack1(&p);       printf("请输入中缀表达式,以#结束：\n");     scanf("%c",&c);     while(c!='#'){         while((c>='0'&&c<='9')||c=='.'){    /*逻辑有问题*/             Push(&r,c);             scanf("%c",&c);             if(!((c>='0'&&c<='9')||c=='.')){                 Push(&r,'@');             }         }         if(c==')'){             Pop(&s,&e);             while(e!='('){                 Push(&r,e);                 Push(&r,'@');                 Pop(&s,&e);             }         }else if(c=='+'||c=='-'){             if(!StackLen(s)){                 Push(&s,c);             }else{                 do{                     Pop(&s,&e);                     if(e=='('){                         Push(&s,e);                     }else{                         Push(&r,e);                         Push(&r,'@');                     }                 }while(StackLen(s)&&e!='(');                 Push(&s,c);             }         }else if(c=='*'||c=='/'||c=='('){             Push(&s,c);         }else if(c=='#'){             break;         }else{             printf("Error!");         }         scanf("%c",&c);     }     while(StackLen(s)){         Pop(&s,&e);         Push(&r,e);         Push(&r,'@');     }       getchar();     Push(&q,'#');       while(StackLen(r)){         Pop(&r,&e);         Push(&x,e);         Push(&q,e);     }     printf("转换为后缀表达式的结果为：\n");     while(StackLen(x)){         Pop(&x,&e);         if(e!='@')             printf("%c",e);         else             printf(" ");     }       Pop(&q,&c);     while(StackLen(q)){         while(isdigit(c)||c=='.'){             str[i++]=c;             str[i]='\0';             if(i>=10){                 printf("Error!");             }             Pop(&q,&c);             if(c=='@'){                 p1=atof(str);                 Push1(&p,p1);                 i=0;                 break;             }         }         switch(c){             case '+':Pop1(&p,&p1);Pop1(&p,&p2);Push1(&p,p2+p1);break;             case '-':Pop1(&p,&p1);Pop1(&p,&p2);Push1(&p,p2-p1);break;             case '*':Pop1(&p,&p1);Pop1(&p,&p2);Push1(&p,p2*p1);break;             case '/':Pop1(&p,&p1);Pop1(&p,&p2);if(p1!=0){Push1(&p,p2/p1);}else{printf("Error!");}break;           }         Pop(&q,&c);     }     Pop1(&p,&result);     printf("\n计算得出的结果为：\n%f",result);       return 0; } 参考一下这个朋友的帖子；  http://bbs.csdn.net/topics/392292433

user: user-1
content: 建议自己先写写，遇到问题再提出来。或者在网上搜一些例子改一改

user: user-2
content: 引用最好能把程序写出来 并加一些注释我也这么想