• hdu 1237 简单计算器 (栈的简单应用)


    感觉自己很菜,一个栈的应用,写了好久,还是代码能力弱啊~

    刚开始直接用的<stack>开了两个栈,结果在判空时出现问题,改用自己写的栈,还是WA,一怒之下上网查了一下,原来是我理解有问题,汗~~

    我们在算加减乘除的时候,总是先算乘除,再算加减,还有一点就是,再算加减的时候也要从左往右算,否则就WA了。

    自己写的代码,WA了:

     1 #include<stdio.h>
    2 #include<stdlib.h>
    3 #include<string.h>
    4 int jud(char a,char b)
    5 {
    6 if((a=='+'||a=='-')&&(b=='+'||b=='-'))
    7 return 0;
    8 else if((a=='+'||a=='-')&&(b=='*'||b=='/'))
    9 return 0;
    10 else if((a=='*'||a=='/')&&(b=='+'||b=='-'))
    11 return 1;
    12 else
    13 return 0;
    14 }
    15 double cal(double a,double b,char m)
    16 {
    17 if(m=='+')
    18 return (a+b);
    19 else if(m=='-')
    20 return (a-b);
    21 else if(m=='*')
    22 return (a*b);
    23 else return (a/b);
    24 }
    25 int main()
    26 {
    27 char str[205],p[205];
    28 double q[205],s;
    29 int i,j,m;
    30 int top1,top2;
    31 while(gets(str))
    32 {
    33 if(str[0]=='0'&&strlen(str)==1)
    34 break;
    35 top1=top2=0;
    36 int len=strlen(str);
    37 for(i=0;i<len;i++)
    38 {
    39 if(str[i]>='0'&&str[i]<='9')
    40 {
    41 m=0;
    42 while(str[i]!=' '&&i<len)
    43 {
    44 m=m*10+str[i]-'0';
    45 i++;
    46 }
    47 q[top1++]=m;
    48 }
    49 else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
    50 {
    51 if(top2==0)
    52 {
    53 p[top2++]=str[i];
    54 }
    55 else
    56 {
    57 if(jud(str[i],p[top2-1])==1)
    58 {
    59 p[top2++]=str[i];
    60 }
    61 else
    62 {
    63 s=cal(q[top1-2],q[top1-1],p[top2-1]);
    64 top1=top1-2;top2=top2-1;
    65 q[top1++]=s;p[top2++]=str[i];
    66 }
    67 }
    68 }
    69 }
    70 while(top1>=2&&top2>=1)
    71 {
    72 s=cal(q[top1-2],q[top1-1],p[top2-1]);
    73 top1=top1-2;top2=top2-1;
    74 q[top1++]=s;
    75 }
    76 printf("%.2lf\n",q[0]);
    77 }
    78 return 0;
    79 }

    参考网上的代码,AC:

     1 #include<stdio.h>
    2 #include<stdlib.h>
    3 #include<string.h>
    4 int jud(char a,char b)
    5 {
    6 if((a=='+'||a=='-')&&(b=='+'||b=='-'))
    7 return 1;
    8 else if((a=='+'||a=='-')&&(b=='*'||b=='/'))
    9 return 1;
    10 else if((a=='*'||a=='/')&&(b=='+'||b=='-'))
    11 return 0;
    12 else
    13 return 1;
    14 }
    15 double cal(double a,double b,char m)
    16 {
    17 if(m=='+')
    18 return (a+b);
    19 else if(m=='-')
    20 return (a-b);
    21 else if(m=='*')
    22 return (a*b);
    23 else return (a/b);
    24 }
    25 int main()
    26 {
    27 char str[205],p[205];
    28 double q[205],s;
    29 int i,m;
    30 int top1,top2;
    31 while(gets(str))
    32 {
    33 if(str[0]=='0'&&strlen(str)==1)
    34 break;
    35 top1=top2=0;
    36 int len=strlen(str);
    37 for(i=0;i<len;i++)
    38 {
    39 if(str[i]>='0'&&str[i]<='9')
    40 {
    41 m=0;
    42 while(str[i]!=' '&&i<len)
    43 {
    44 m=m*10+str[i]-'0';
    45 i++;
    46 }
    47 q[top1++]=m;
    48 }
    49 else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
    50 {
    51 if(top2==0)
    52 {
    53 p[top2++]=str[i];
    54 }
    55 else
    56 {
    57 while((jud(str[i],p[top2-1]))&&top2>=1&&i<len)
    58 {
    59 s=cal(q[top1-2],q[top1-1],p[top2-1]);
    60 top2--;top1--;
    61 q[top1-1]=s;
    62 }
    63 p[top2++]=str[i];
    64 }
    65 }
    66 }
    67 while(top2>=1)
    68 {
    69 s=cal(q[top1-2],q[top1-1],p[top2-1]);
    70 top1--;top2--;
    71 q[top1-1]=s;
    72 }
    73 printf("%.2lf\n",q[0]);
    74 }
    75 return 0;
    76 }




  • 相关阅读:
    金蝶用户组权限语句
    金蝶物料更改保质期控制状态
    cisco+pppoe拨号配置教程
    FormFile上传文件代码(修改)
    试用django 和 rgraph 简单记录
    一闪而过的远程桌面
    用 python matplotlib 画图 简单记录
    去除UTL_FILE导致的ORA12012 ORA04068 ORA04063 ORA06508
    python mysqldb模块安装
    canvas绘制圆形
  • 原文地址:https://www.cnblogs.com/misty1/p/2293418.html
Copyright © 2020-2023  润新知