波兰式计算器程序(C++)

b22bb5b7

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /**********************************
 2InBlock.gif题目:波兰式计算器
 3InBlock.gif/* 以下是几个标准的表达式:
 4InBlock.gif5 * 2 + -3
 5InBlock.gif5 * (2 + -3)
 6InBlock.gif5 + ((-4 * -5) + (((5 + (6 - 2)) * 7 + ((4 + 2) * (3 - 1))))
 7InBlock.gif与之等价的波兰表达式为
 8InBlock.gif+ * 5 2 -3
 9InBlock.gif* 5 + 2 -3
10InBlock.gif+ 5 + * -4 -5 + * + 5 - 6 2 7 * + 4 2 - 3 1
11InBlock.gif
12InBlock.gifAuthor:Linghucong
13InBlock.gifDate:2007-4-28
14ExpandedBlockEnd.gif***********************************/

15 None.gif#include  < math.h >
16 None.gif#include  < stdio.h >
17 None.gif#include  < string >
18 None.gif#include  < iostream >
19 None.gif using   namespace  std;
20 None.gif
21 ExpandedBlockStart.gifContractedBlock.gif void  Polan( string  test)   dot.gif {
22InBlock.gif    string ops[100];
23InBlock.gif    int opNum = -1;
24InBlock.gif    int i = 0;
25InBlock.gif    while (i < test.length())
26ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
27InBlock.gif        //处理空格
28ExpandedSubBlockStart.gifContractedSubBlock.gif        if (test[i] == ' 'dot.gif{
29InBlock.gif            i++;
30InBlock.gif            continue;
31ExpandedSubBlockEnd.gif        }

32InBlock.gif        //处理操作符
33ExpandedSubBlockStart.gifContractedSubBlock.gif        if (test[i] == '+' || test[i] == '*' || (test[i] == '-' && test[i + 1== ' ')) dot.gif{
34InBlock.gif            ops[++opNum] = test[i++];
35InBlock.gif            continue;
36ExpandedSubBlockEnd.gif        }

37InBlock.gif        //处理数字
38ExpandedSubBlockStart.gifContractedSubBlock.gif        for (int count =0; count < test.length() - i; count++dot.gif{
39ExpandedSubBlockStart.gifContractedSubBlock.gif            if (test[i + count] == ' 'dot.gif{
40InBlock.gif                break;
41ExpandedSubBlockEnd.gif            }

42ExpandedSubBlockEnd.gif        }

43InBlock.gif        string str = test.substr(i, count);
44InBlock.gif        i += count;
45InBlock.gif        ops[++opNum] = str;
46InBlock.gif
47InBlock.gif        int res;
48InBlock.gif        //算法:加入一个数字之后检查其前面一个值是不是数字,如果是,则计算,如果不是,继续往数组中添加值。
49ExpandedSubBlockStart.gifContractedSubBlock.gif        while (opNum > 1 && ops[opNum - 1!= "+" && ops[opNum - 1!= "*" && ops[opNum - 1!= "-"dot.gif{
50ExpandedSubBlockStart.gifContractedSubBlock.gif            if (ops[opNum - 2== "+"dot.gif{
51InBlock.gif                res = atoi(ops[opNum].c_str()) + atoi(ops[opNum - 1].c_str()) ;
52InBlock.gif                opNum -= 2;
53ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else if(ops[opNum - 2== "-"dot.gif{
54InBlock.gif                res = atoi(ops[opNum - 1].c_str()) - atoi(ops[opNum].c_str());
55InBlock.gif                opNum -= 2;
56ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else if(ops[opNum - 2== "*"dot.gif{
57InBlock.gif                res = atoi(ops[opNum].c_str()) * atoi(ops[opNum - 1].c_str());
58InBlock.gif                opNum -= 2;
59ExpandedSubBlockEnd.gif            }

60InBlock.gif
61InBlock.gif            _itoa(res, (char *)ops[opNum].c_str(), 10);
62ExpandedSubBlockStart.gifContractedSubBlock.gif            if (opNum == 0dot.gif{
63InBlock.gif                break;
64ExpandedSubBlockEnd.gif            }

65ExpandedSubBlockEnd.gif        }

66ExpandedSubBlockEnd.gif    }

67InBlock.gif
68InBlock.gif    cout << ops[0<< endl;
69ExpandedBlockEnd.gif}

70 None.gif

写的时候才发现原来这么一个小程序也是这么的不容易,费了好办天劲,总发现算法只能满足大部分的情况,后来索性放下程序,仔细研究算法规律,突然发现,其实比想象中的要简单许多。在网上找到的代码有好几百行,简直不是人看的呵呵。发现自己的编程能力真的有待提高:》

转载于:https://www.cnblogs.com/jiji262/archive/2007/04/29/732722.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值