郁闷的C小加(一)

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

我们熟悉的表达式如a+b、a+b*(c+d)等都属于中缀表达式。中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2。同理,后缀表达式就是操作符在两个操作数之后:num1 num2 operand。ACM队的“C小加”正在郁闷怎样把一个中缀表达式转换为后缀表达式,现在请你设计一个程序,帮助C小加把中缀表达式转换成后缀表达式。为简化问题,操作数均为个位数,操作符只有+-*/ 和小括号。

 
输入
第一行输入T,表示有T组测试数据(T<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个表达式。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。并且输入数据不会出现不匹配现象。
输出
每组输出都单独成行,输出转换的后缀表达式。
样例输入
  1. 2
  2. 1+2
  3. (1+2)*3+4*5
样例输出
  1. 12+
  2. 12+3*45*+

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stack>
  4. #include<queue>
  5. using namespace std;
  6.  
  7. char str[];
  8. int f(char a)
  9. {
  10. switch(a)
  11. {
  12. case '-' :
  13. case '+' : return ; break;
  14. case '/' :
  15. case '*' : return ; break;
  16. case '(' : return ; break;
  17. default :return -;
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. int T;
  24. stack<char>sc;
  25. queue<char>e;
  26. scanf("%d",&T);
  27. while(T--)
  28. {
  29. while(!e.empty())
  30. e.pop();
  31. while(!sc.empty())
  32. sc.pop();
  33. scanf("%s",str);
  34. int len=strlen(str);
  35. sc.push('#');
  36. for(int i=;i<len;++i)
  37. {
  38. if(str[i]>=''&&str[i]<='')
  39. {
  40. e.push(str[i]);
  41. }
  42. else if(str[i]=='(')
  43. {
  44. sc.push(str[i]);
  45. }
  46. else if(str[i]==')')
  47. {
  48. while(sc.top()!='(')
  49. {
  50. e.push(sc.top());
  51. sc.pop();
  52. }
  53. sc.pop();
  54. }
  55. else if(str[i]=='*'||str[i]=='/'||str[i]=='+'||str[i]=='-')
  56. {
  57. char s=sc.top();
  58. while(f(str[i])<=f(s))
  59. {
  60. e.push(s);
  61. sc.pop();
  62. s=sc.top();
  63. }
  64. sc.push(str[i]);
  65. }
  66. }
  67. while(!sc.empty())
  68. {
  69. e.push(sc.top());
  70. sc.pop();
  71. }
  72. while(e.front()!='#')
  73. {
  74. printf("%c",e.front());
  75. e.pop();
  76. }
  77. printf("\n");
  78.  
  79. }
  80. return ;
  81. }

467 代码:添加空格

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stack>
  4. #include<queue>
  5. using namespace std;
  6.  
  7. char str[];
  8. int f(char a)
  9. {
  10. switch(a)
  11. {
  12. case '-' :
  13. case '+' : return ; break;
  14. case '/' :
  15. case '*' : return ; break;
  16. case '(' : return ; break;
  17. default :return -;
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. int T;
  24. stack<char>sc;
  25. queue<char>e;
  26. scanf("%d",&T);
  27. while(T--)
  28. {
  29. while(!e.empty())
  30. e.pop();
  31. while(!sc.empty())
  32. sc.pop();
  33. scanf("%s",str);
  34. int len=strlen(str);
  35. sc.push('#');
  36. for(int i=;i<len-;++i)
  37. {
  38. if(str[i]>=''&&str[i]<='')
  39. {
  40. if(!e.empty())
  41. e.push(' ');
  42. while(str[i]>=''&&str[i]<=''||str[i]=='.')
  43. {
  44. e.push(str[i]);
  45. i++;
  46. }
  47. i--;
  48. }
  49. else if(str[i]=='(')
  50. {
  51. sc.push(str[i]);
  52. }
  53. else if(str[i]==')')
  54. {
  55. while(sc.top()!='(')
  56. {
  57. e.push(' ');
  58. e.push(sc.top());
  59. sc.pop();
  60. }
  61. sc.pop();
  62. }
  63. else if(str[i]=='*'||str[i]=='/'||str[i]=='+'||str[i]=='-')
  64. {
  65. char s=sc.top();
  66. while(f(str[i])<=f(s))
  67. {
  68. e.push(' ');
  69. e.push(s);
  70. sc.pop();
  71. s=sc.top();
  72. }
  73. sc.push(str[i]);
  74. }
  75. }
  76. while(!sc.empty())
  77. {
  78. e.push(' ');
  79. e.push(sc.top());
  80. sc.pop();
  81. }
  82. while(e.front()!='#')
  83. {
  84. printf("%c",e.front());
  85. e.pop();
  86. }
  87. printf("=\n");
  88.  
  89. }
  90. return ;
  91. }

nyoj-257 郁闷的C小加(一) 前缀表达式变后缀的更多相关文章

  1. NYOJ 257 郁闷的C小加(一) (字符串处理)

    题目链接 描述 我们熟悉的表达式如a+b.a+b(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符 ...

  2. NYOJ 257 郁闷的C小加(一)

    郁闷的C小加(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描写叙述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说) ...

  3. nyoj 257 郁闷的C小加(一)(栈、队列)

    郁闷的C小加(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...

  4. NYOJ 409 郁闷的C小加(三) (字符串处理)

    题目链接 描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考"郁闷的C小加(一)"),C小加很高兴.但C小加是个爱思考的人,他又想通过这种方法计算一个表达式的值 ...

  5. NYOJ 267 郁闷的C小加(二) (字符串处理)

    题目链接 描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考"郁闷的C小加(一)"),C小加很高兴.但C小加是个爱思考的人,他又想通过这种方法计算一个表达式的值 ...

  6. nyoj 409——郁闷的C小加(三)——————【中缀式化前缀后缀并求值】

    郁闷的C小加(三) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很 ...

  7. NYOJ-267 郁闷的C小加(二)

    郁闷的C小加(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很 ...

  8. 郁闷的C小加(一)(后缀表达式)

    郁闷的C小加(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...

  9. NYOJ--257--郁闷的C小加(一)(中缀表达式变后缀表达式 )

    郁闷的C小加(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...

随机推荐

  1. Oracle体系结构知识点的运用

    体系结构方面的优化问题: 设数据库很大,访问量非常高,共享池很小:这样共享池里面就无法存储很多解析过得sql语句,导致很多硬解析,这样数据库就非常缓慢.这个时候要加大共享池.如果是自动管理,就加大SG ...

  2. VS2010调试入门指南

    1 导言 在软件开发周期中,测试和修正缺陷(defect,defect与bug的区别:Bug是缺陷的一种表现形式,而一个缺陷是可以引起多种Bug的)的时间远多于写代码的时间.通常,debug是指发现缺 ...

  3. Large-Scale Deployment of SharePoint Team Services

    http://technet.microsoft.com/en-us/library/cc723713.aspx

  4. C++中的lambda表达式

    1.基本形式: [捕获列表](参数列表){函数体};     其中捕获列表和函数体不能省略但是捕获列表可以为空,也就是说最简单的lambda表达式是:  []{}; 2.lambda表达式又叫匿名函数 ...

  5. C# abstract function VS virtual function?

    An abstract function has to be overridden while a virtual function may be overridden. Virtual functi ...

  6. WPF 资源

    https://wpftoolkit.codeplex.com/documentation http://www.codeproject.com/Articles/563862/Multi-Selec ...

  7. mysql之sql语句导入与导出讲解

    导出SQL:mysqldump -u root -p 数据库名 [表名1 表名2] > 输出地址其中表名可选 本机测试实例:

  8. Network Saboteur(Rand版)

    poj2531:http://poj.org/problem?id=2531 题意:给你一个图,图中点之间会有边权,现在问题是把图分成两部分,使得两部分之间边权之和最大.题解:随机算法 #includ ...

  9. Namespace, string, vector and array

    1. Headers should not include using declaration Code inside headers ordinarily should not include us ...

  10. 2014多校第一场J题 || HDU 4870 Rating(DP || 高斯消元)

    题目链接 题意 :小女孩注册了两个比赛的帐号,初始分值都为0,每做一次比赛如果排名在前两百名,rating涨50,否则降100,告诉你她每次比赛在前两百名的概率p,如果她每次做题都用两个账号中分数低的 ...