计算(calc.cpp)

【问题描述】

小明在你的帮助下,破密了Ferrari设的密码门,正要往前走,突然又出现了一个密码门,门上有一个算式,其中只有“(”,“)”,“0-9”,“+”,“-”,“*”,“/”,“^”求出的值就是密码。小明数学学得不好,还需你帮他的忙。(“/”用整数除法)

【输入】

输入文件calc.in共1行,为一个算式。

【输出】

输出文件calc.out共1行,就是密码。

【输入样例】calc.in

1+(3+2)*(7^2+6*9)/(2)

【输出样例】calc.out

258

【限制】

100%的数据满足:算式长度<=30 其中所有数据在2^31-1的范围内。

用栈来处理运算顺序。把数和符号存在不同的栈中。每次读到数后检查已经入栈的运算符号,能算的就先算完。

如读到'^',直接算出乘方结果压入栈中,而读到'*''/'要先检查下一个符号是不是'^',不是的话才能算结果,加减同理……

以下的代码有一个已知的小bug:不支持自动消除空格(懒得写)读入的时候要是有空格,程序会爆炸。

代码(未使用STL):

  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. int a[];//数栈
  7. int p,i;
  8. char sy[],c[];//符号栈 原算式
    9 void push(){
  9. sy[++p]=c[i];
  10. }
  11. void pp(){//弹栈运算
  12. switch(sy[p--]){
  13. case '+':a[p]+=a[p+];break;
  14. case '-':a[p]-=a[p+];break;
  15. case '*':a[p]*=a[p+];break;
  16. case '/':a[p]/=a[p+];break;
  17. case '^':a[p]=pow(a[p],a[p+]);break;
  18.  
  19. }
  20. return;
  21. }
  22. int can(){//优先级判断
  23. if((c[i]=='+'||c[i]=='-') && sy[p]!='(')return ;
  24. if((c[i]=='*'||c[i]=='/') && (sy[p]=='*' || sy[p]=='/'))return ;
  25. return ;
  26. }
  27. int main(){
  28. p=;i=;
  29. cin>>c;
  30. c[strlen(c)]=')';
  31. sy[p]='(';
  32. //
  33. printf("test");
  34. while(i<strlen(c)){
  35. while(c[i]=='('){//处理左括号
  36. push();
  37. i++;
  38. }
  39. int x=;
  40. while(c[i]>=''&& c[i]<=''){//处理数字
  41. x=x*+c[i++]-'';
  42. }
  43. a[p]=x;
  44. do{
  45. if(c[i]==')'){//处理右括号
  46. while(sy[p]!='(')pp();
  47. a[--p]=a[p+];
  48.  
  49. }
  50. else{
  51. while(can())pp();
  52. push();
  53. }
  54. i++;
  55. }while(i<strlen(c) && c[i-]==')');
  56.  
  57. }
  58. printf("%d",a[p]);
  59. return ;
  60. }

代码(STL):

STL真好用

  1. /**/
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<stack>
  6. #include<cstring>
  7. using namespace std;
  8. char c[];
  9.  
  10. stack<int>num;//数
  11. stack<char>sy;//符号
  12. int pow1(int a,int b){//自写乘方
  13. int i;
  14. for(i=;i<=b;i++){
  15. a*=a;
  16. }
  17. return a;
  18. }
  19. void mth(){//运算
  20. int a,b;
  21. char ch;
  22. b=num.top();
  23. num.pop();
  24. a=num.top();
  25. num.pop();
  26. ch=sy.top();
  27. sy.pop();
  28. switch(ch){
  29. case '+': num.push(a+b);break;
  30. case '-': num.push(a-b);break;
  31. case '*': num.push(a*b);break;
  32. case '/': num.push(a/b);break;
  33. case '^': num.push(pow1(a,b));break;
  34. default: break;
  35. }
  36. return;
  37. }
  38. int cmp(int ch){//优先级判断,没有严谨验证过,初步测试没有问题 //注释掉的那部分代码来自大神yhy,保证无误
  39. if(sy.empty() || sy.top()=='(')return ;
  40. if(ch=='+' || ch=='-')return ;
  41. if(sy.top()=='^')return ;
  42. if((ch=='*' || ch=='/') && (sy.top()=='*' ||sy.top()=='/'))return ;
  43. return ;
  44. /* if (sy.empty()||sy.top()=='(') return 0;
  45. if (sy.top()=='^') return 1;
  46. if (ch=='^') return 0;
  47. if (sy.top()=='*'||sy.top()=='/') return 1;
  48. if (ch=='*'||ch=='/') return 0;
  49. return 1;*/
  50. }
  51. int main(){
  52. gets(c);
  53. int len=strlen(c);
  54. c[len]=')';
  55. sy.push('(');
  56. int i;
  57. for(i=;i<=len;i++){
  58. if(c[i]=='('){
  59. sy.push('(');
  60. continue;
  61. }
  62. if(c[i]>='' && c[i]<='')
  63. {
  64. int x=;
  65. while(c[i]>='' && c[i]<=''){
  66. x=x*+c[i]-'';
  67. i++;
  68. }
  69. i--;
  70. num.push(x);
  71. continue;
  72. }
  73. if(c[i]==')'){
  74. while(sy.top()!='(')mth();
  75. sy.pop();
  76. continue;
  77. }
  78. while(cmp(c[i]))mth();
  79. sy.push(c[i]);
  80. }
  81. while(!sy.empty())mth();
  82. printf("%d ",num.top());
  83. return ;
  84. }

calc 多项式计算 (STL版和非STL版) -SilverN的更多相关文章

  1. [C++]竞赛模板·数据统计与IO(重定向版与非重定向版)

      /* 数据统计与IO 重定向版模板 描述:本机测试用文件数据流重定向,一旦提交到比赛就自动“删除”重定向语句 */ # define LOCAL #include<stdio.h> # ...

  2. java二叉树遍历——深度优先(DFS)与广度优先(BFS) 递归版与非递归版

    介绍 深度优先遍历:从根节点出发,沿着左子树方向进行纵向遍历,直到找到叶子节点为止.然后回溯到前一个节点,进行右子树节点的遍历,直到遍历完所有可达节点为止. 广度优先遍历:从根节点出发,在横向遍历二叉 ...

  3. Eclipse下创建Spring MVC web程序--非maven版

    首先, 安装eclipse和tomcat, 这里我下载的是tomcat9.0版本64位免安装的:地址https://tomcat.apache.org/download-90.cgi 免安装的如何启动 ...

  4. stl文件格式解析代码--java版

    代码是参考three.js中的stlLoader.js写的. 需要注意的地方,java中byte取值-128~127 package test_stl.test_entry; import java. ...

  5. 队列问题非STL解决方案

    队列问题非STL解决方案 常年使用STL解决队列问题,以至于严重生疏队列的根本原理... 直到今日 被老师被迫 使用算法原理解决问题,方才意识到我对队列一窍不通... ...直到 经过一系列的坑蒙拐骗 ...

  6. 标准非STL容器 : bitset

    1. 概念 什么是"标准非STL容器"?标准非STL容器是指"可以认为它们是容器,但是他们并不满足STL容器的所有要求".前文提到的容器适配器stack.que ...

  7. UOJ34 多项式乘法(非递归版)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. MATLAB学习笔记(六)——MATLAB数据分析与多项式计算

    (一)数据处理统计 一.最大值和最小值 1.求向量的最大值和最小值 y=max(X); %返回向量X的最大值存入y,如果X中含有复数则按模最大的存入y [y,I]=max(X);%返回向量X的最大值存 ...

  9. 泛型编程、STL的概念、STL模板思想及其六大组件的关系,以及泛型编程(GP)、STL、面向对象编程(OOP)、C++之间的关系

    2013-08-11 10:46:39 介绍STL模板的书,有两本比较经典: 一本是<Generic Programming and the STL>,中文翻译为<泛型编程与STL& ...

随机推荐

  1. 与众不同 windows phone (48) - 8.0 其它: C# 调用 C++

    [源码下载] 与众不同 windows phone (48) - 8.0 其它: C# 调用 C++ 作者:webabcd 介绍与众不同 windows phone 8.0 之 其它 C# 中调用 W ...

  2. csharp: Speech

    Speech SDK 5.1https://www.microsoft.com/en-us/download/details.aspx?id=10121 detects mobile devices ...

  3. Verilog学习笔记设计和验证篇(一)...............总线和流水线

    总线 总线是运算部件之间数据流通的公共通道.在硬线逻辑构成的运算电路中只要电路的规模允许可以比较自由的确定总线的位宽,从而大大的提高数据流通的速度.各个运算部件和数据寄存器组可以通过带有控制端的三态门 ...

  4. Hibernate之映射文件中索引及约束的使用

    1.添加索引: 在一对多的关系中,在多的一方会产生一个外键,这个外键没有自动 添加索引,当存在从一的一端产生对多的一端的查询时,有可能会在多的一端造成全表查询问题,数据量巨大时会产生严重的性能问题.可 ...

  5. Python语言规范及风格规范

    语言规范: http://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_ ...

  6. [Azure] Notification Hubs注册模式

    [Azure] Notification Hubs注册模式 关于Azure Notification Hubs的注册模式,可以参考下列连结的文件内容. Notification Hubs Featur ...

  7. Visual Studio添加dll程序集引用操作步骤

    Visual Studio 中添加引用的操作: 在“解决方案资源管理器”中,先右击项目图标,在弹出菜单选择“添加引用...” 然后在弹出的窗口中选择所要添加的选项,点击确定就可以了. 原文:http: ...

  8. jquery实现轮播

    HTML代码: <div class="ad"> <ul class="slider"> <li><img src=& ...

  9. Azure SQL Database 时间点还原(Point in Time Restore)功能

      微软中国TechNet 7 Oct 2014 9:17 PM Comments 0 Likes 原文地址:http://blogs.technet.com/b/azuretw/archive/20 ...

  10. [ html canvas putImageData ] canvas绘图属性 putImageData 属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...