鱼C论坛

 找回密码
 立即注册
查看: 4688|回复: 1

[学习笔记] 逆波兰表达式c++代码

[复制链接]
发表于 2018-3-30 16:09:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Sj中国智造 于 2018-3-31 08:43 编辑
  1. /*---------------------------------------------
  2.                 中缀表达式转后缀表达式
  3.                         逆波兰表达式
  4. ----------------------------------------------*/
  5. #include <iostream>
  6. #include <string>
  7. #include <sstream>
  8. #include <windows.h>

  9. #define MAX_SIZE 1024
  10. #define TRUE  1
  11. #define FALSE  0
  12. using namespace std;

  13. template <class T>
  14. class sqStack
  15. {
  16. public:
  17.         sqStack();//申请一块内存
  18.         ~sqStack();//释放申请的内存
  19.         int isEmpty();//判断栈是否为空
  20.         T push(T element);//压栈
  21.         T pop();//弹栈

  22. private:
  23.         T * top;//头指针
  24.         T *base;//底指针
  25. };

  26. template <class T>
  27. sqStack<T>::sqStack()
  28. {
  29.         this->base = this->top = new T[MAX_SIZE];
  30. }

  31. template <class T>
  32. sqStack<T>::~sqStack()
  33. {
  34.         delete[] this->base;
  35. }

  36. template<class T>
  37. int sqStack<T>::isEmpty()
  38. {
  39.         if (this->top == this->base)
  40.         {
  41.                 return TRUE;
  42.         }
  43.         else return FALSE;
  44. }

  45. template <class T>
  46. T sqStack<T>::push(T element)
  47. {
  48.         this->top++;
  49.         *(this->top) = element;
  50.         return *(this->top);
  51. }

  52. template <class T>
  53. T sqStack<T>::pop()
  54. {
  55.         if (!isEmpty())
  56.         {
  57.                 this->top--;
  58.                 return *(this->top + 1);
  59.         }
  60.         return NULL;
  61. }

  62. int main()
  63. {
  64.         sqStack<int> stack1;//储存数字的栈
  65.         sqStack<char>stack2;//储存操作符的栈
  66.         int num;//数字
  67.         int num1, num2;
  68.         char oper;//操作符
  69.         string input;//接受用户输入
  70.         string conversionResult;//转化的结果
  71.         int priority;//记录优先级

  72.         cout << "请输入表达式(以回车表示结束):";
  73.         getline(cin, input);//接收用户输入

  74.         istringstream in(input);
  75.         ostringstream out(conversionResult);

  76.         priority = 0;
  77.         while (1)
  78.         {
  79.                 while (in.peek() == ' ') in.ignore(MAX_SIZE, ' ');//跳过空格
  80.                 if (in.eof()) break;

  81.         if (in.peek() >= '0'&&in.peek() <= '9')
  82.                 {
  83.                         in >> num;
  84.                         out << num << ' ';
  85.                 }
  86.                 else
  87.                 {
  88.                         in >> oper;
  89.                         switch (oper)
  90.                         {
  91.                         case '+':
  92.                         case '-':
  93.                                 if (1 > priority) stack2.push(oper);
  94.                                 else
  95.                                 {
  96.                                         out << stack2.pop() << ' ';
  97.                                         stack2.push(oper);
  98.                                 }
  99.                                 priority = 1; break;

  100.                         case '*':
  101.                         case '/':
  102.                                 if (2 > priority) stack2.push(oper);
  103.                                 else
  104.                                 {
  105.                                         out << stack2.pop() << ' ';
  106.                                         stack2.push(oper);
  107.                                 }
  108.                                 priority = 2; break;

  109.                         case '(':
  110.                                 stack2.push(oper);
  111.                                 priority = 0; break;

  112.                         case ')':
  113.                                 for (oper = stack2.pop(); oper != '(' ; oper = stack2.pop())
  114.                                 {
  115.                                         out << oper << ' ';
  116.                                         if (stack2.isEmpty())
  117.                                         {
  118.                                                 std::cout << "括号不配对!" << endl;
  119.                                                 return -2;
  120.                                         }
  121.                                 }
  122.                                 break;

  123.                         default:
  124.                                 std::cout << "您输入的表达式格式有误或有非法字符!" << endl;
  125.                                 return -3;
  126.                         }
  127.                 }
  128.         }
  129.         while (!stack2.isEmpty()) out << stack2.pop() << ' ';
  130.         out << '#' << ends;

  131.         conversionResult = out.str();

  132.         in.clear();//清除eof()的设置
  133.         in.str(conversionResult);

  134.         while (in.peek() != '#')
  135.         {
  136.                 while (in.peek() == ' ') in.ignore(MAX_SIZE, ' ');
  137.                 if (in.peek() == '#')  break;

  138.                 if (in.peek() >= '0'&&in.peek() <= '9')
  139.                 {
  140.                         in >> num;
  141.                         stack1.push(num);//遇到数字就压栈
  142.                 }
  143.                 else
  144.                 {
  145.                         in >> oper;//读取操作符
  146.                         num1 = stack1.pop();
  147.                         num2 = stack1.pop();
  148.                         switch (oper)
  149.                         {
  150.                         case '+':
  151.                                 stack1.push(num1 + num2); break;
  152.                         case '-':
  153.                                 stack1.push(num2 - num1); break;
  154.                         case '/':
  155.                                 if (num1 == 0)
  156.                                 {
  157.                                         cout << "除数不能为零!" << endl;
  158.                                         return -4;
  159.                                 }
  160.                                 stack1.push(num2 / num1); break;
  161.                         case '*':
  162.                                 stack1.push(num1*num2); break;
  163.                         default:
  164.                                 cout << "程序出错!!"<<endl;
  165.                                 return -1;
  166.                         }
  167.                 }
  168.         }
  169.         cout << "运算好的结果是:" << stack1.pop() << endl<<endl;
  170.        
  171.         cout << "10s后程序自动关闭!";
  172.         Sleep(10000);
  173.         return 0;
  174. }
  175. //主程序返回0说明一切正常
  176. //返回-1说明程序计算出错
  177. //返回-2说明括号不匹配
  178. //返回-3说明输入表达式有误
  179. //返回-4说明除数为0
复制代码


看完小甲鱼的逆波兰表达式的程序,自己写了个c++实现代码,不足之处望批评指正!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-20 19:58:35 | 显示全部楼层
大佬牛逼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 23:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表