c++编写的算24程序

今天在大本营看到了一个人家写的24程序,感觉人家的思路非常清晰,于是自己也着手写了一个,顺便温习了一下标准c++。在我的程序中,使用了stringstream类做parse,后缀表达式穷举所有可行解,而用中缀表达式计算用户输入的表达式。因为比较懒,当用户算不出来时,按出来的答案是个后缀表达式。

 

===============================================

4.21更新

1.发现对后缀表达式的理解错了。修改了穷举算24的函数,穷举空间改为4!*4^3 * 5,其中3个操作符在长度为7的后缀表达式的可能位置为5种。

2.使用二叉树和堆栈实现了后缀表达式向中缀的转化,可以给出算24的答案

3.加入了接受用户输入的4个数,也可以让系统产生四个数。

功能上已经比较完整了,用户界面上还不是很好,不过这个已经不是很重要的问题的。这部分代码,如果要做

图形界面,应该是很好移过去的。

有关源代码如下:

Token.h :用于Parse,以及操作符,优先级的定义

Code:
  1. #ifndef TOKEN_H  
  2. #define TOKEN_H  
  3. enum Token_type {Numeric = 0,Op};  
  4. enum Operator{ADD_OPR = 0,MINUS_OPR,  
  5. MUL_OPR,DIV_OPR,  
  6. LEFT_BRA,RIGHT_BRA,  
  7. TER};  
  8. enum PRI{HIGHER = 0,LOWER,EQUAL,NO_POSSIBLE};  
  9. class Token  
  10. {  
  11. public:  
  12.     Token(){}  
  13.     Token(Token_type _type,double _x,Operator _op):type(_type),  
  14.         x(_x),op(_op){}  
  15.     Token_type type;  
  16.     double x;  
  17.     Operator op;  
  18. };  
  19. void Parse(string expression,vector<Token>& tokens);  
  20. bool isOp(char c);  
  21. #endif  

Token.cpp

Code:
  1. #include <string>  
  2. #include <vector>  
  3. #include <sstream>  
  4. using namespace std;  
  5. #include "Token.h"  
  6. extern int OpTypeNum;  
  7. char operators[7] = { '+','-','*','/','(',')','#'};  
  8.   
  9. bool isOp(char c,int &index)  
  10. {  
  11.     for (int i = 0;i < OpTypeNum;i++)  
  12.     {  
  13.         if (c == operators[i])  
  14.         {  
  15.             index = i;  
  16.             return true;  
  17.         }  
  18.     }  
  19.     return false;  
  20. }  
  21. void Parse(string expression,vector<Token>& tokens)  
  22. {  
  23.     stringstream ss (stringstream::in | stringstream::out);  
  24.     ss << expression;  
  25.     char c;  
  26.     int val,index;  
  27.     while (ss >> c)  
  28.     {  
  29.         if (isdigit(c))  
  30.         {  
  31.             ss.putback(c);  
  32.             ss >> val;  
  33.             tokens.push_back(Token(Numeric,val,Operator(0)));  
  34.         }else if (isOp(c,index))  
  35.             tokens.push_back(Token(Op,-1,Operator(index)));  
  36.     }  
  37. }  

ExpCalc.h  用堆栈实现的中缀和后缀表达式的计算,以及后缀转化为中缀表达式的功能

Code:
  1. #ifndef EXPCALC_H  
  2. #define EXPCALC_H  
  3. class tree_Node  
  4. {  
  5. public:  
  6.     tree_Node(){}  
  7.     ~tree_Node();  
  8.     void Print();  
  9.     tree_Node(tree_Node * _left,tree_Node * _right,Token _token):  
  10.       left(_left),right(_right),token(_token){}  
  11.     tree_Node * left;  
  12.     tree_Node * right;  
  13.     Token token;  
  14. };  
  15. class ExpCalc  
  16. {  
  17. public:  
  18.     void ShowInfixExp(vector<Token>& tokens);  
  19.   
  20.     bool PostfixCalc(vector<Token> & tokens,double & res);  
  21.     bool infixCalc(vector<Token>& tokens,double& res);  
  22.     bool Calc(double x1,double x2,Operator op,double & res);  
  23.     void Clear();  
  24. private:  
  25.     bool doWhenHigher(Operator op);  
  26.     bool doWhenLower(Operator op,Operator nxt_op);  
  27.     bool doWhenEqual();  
  28.   
  29.     stack<double> operands_stack;  
  30.     stack<Operator> operators_stack;  
  31. };  
  32. #endif  

 

ExpCalc.cpp

Code:
  1. #include <stack>  
  2. #include <vector>  
  3. #include <cmath>  
  4. #include <iostream>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值