|  | 
 
| 
本帖最后由 小休比 于 2018-5-11 23:49 编辑
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 #include<stdio.h>
 #include<stdlib.h>
 #include<ctype.h>
 #define N 10
 typedef double ElemType;
 typedef struct Linklist
 {
 ElemType data;
 struct Linklist *next;
 }*Node;
 typedef struct Plinklist
 {
 Node top;
 int stracksize;
 }*Strack;
 /*进栈操作*/
 void Push(Strack L,ElemType e)
 {
 Node p=(Node)malloc(sizeof(struct Linklist));                //为什么用栈操作可以输出正确的结果,用栈链就输出不了,我栈链创建哪里错了,求指正!
 p->data=e;
 p->next=L->top;
 L->top=p;
 L->stracksize++;
 
 }
 /*出栈操作*/
 int Pop(Strack L,ElemType *e)
 {
 Node p;
 if(L->stracksize)
 {
 return -1;
 }
 *e=L->top->data;
 p=L->top;
 L->top=p->next;
 free(p);
 L->stracksize--;
 
 
 }
 int main()
 {
 Strack L;
 L->top=(Node)malloc(sizeof(struct Linklist));
 L->top->next=NULL;
 char c;
 double e,d;
 char str[N];
 int i;
 printf("按照逆波兰表达式输入数据,以#作为结束标志\n");
 scanf("%c",&c);
 while(c!='#')
 {
 while(isdigit(c)||c=='.')//isdigit判断字符是否为数字
 {
 str[i++]=c;
 str[i]='\0';
 if(i>N)
 {
 printf("输入数字过大");
 return -1;
 }
 scanf("%c",&c);
 if(c==' ')
 {
 d=atof(str);
 Push(L,d);
 i=0;
 break;
 }
 }
 switch(c)
 {
 case '+':
 Pop(L,&e);
 Pop(L,&d);
 Push(L,e+d);break;
 case'-':
 Pop(L,&e);
 Pop(L,&d);
 Push(L,e-d);break;
 case'*':
 Pop(L,&e);
 Pop(L,&d);
 Push(L,e*d);break;
 case'/':
 Pop(L,&e);
 Pop(L,&d);
 if(d!=0)
 Push(L,e/d);
 }
 scanf("%c",&c);
 }
 Pop(L,&e);
 printf("%lf",e);
 return 0;
 }
 | 
 |