zoukankan      html  css  js  c++  java
  • Hlg 表达式求值...cpp

    题意:

      给出一个表达式..包含'+' '-' '*' '/' '.' 以及空格..

      求出表达式的值..

     

    思路:

      栈的应用..

     

    Tips:

      ①. 指针引用那里..参数是字符串首地址不可以改变..

               而如果是指针..就可以改变..所以把字符串首地址作为参数传给change函数不可以..但是先传给函数calculate变成了指针再传给change就可以arr++了..

      ②. 运算符优先级可以用一个二维数组来判断..

     

    Code:

    View Code
      1 #include <stdio.h>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <algorithm>
      5 #include <ctype.h>
      6 #include <map>
      7 using namespace std;
      8 
      9 struct Node
     10 {
     11     char arr[1024];
     12     int st;
     13     int en;
     14 }node[1024];
     15 
     16 const int MAXN = 40;
     17 int com[250][250];
     18 
     19 void ini()
     20 {
     21     memset(com, 0xff, sizeof(com));
     22     com['+']['+'] = com['+']['-'] = com['+'][')'] = com['+']['#'] = 1;
     23     com['-']['-'] = com['-']['+'] = com['-'][')'] = com['-']['#'] = 1;
     24     com['*']['+'] = com['*']['-'] = com['*']['*'] = com['*']['/'] = com['*'][')'] = com['*']['#'] = 1;
     25     com['/']['+'] = com['/']['-'] = com['/']['*'] = com['/']['/'] = com['/'][')'] = com['/']['#'] = 1;
     26     com[')']['+'] = com[')']['-'] = com[')']['*'] = com[')']['/'] = com[')'][')'] = com[')']['#'] = 1;
     27     com['('][')'] = com['#']['#'] = 0;
     28 }
     29 
     30 double change(char *&arr)
     31 {
     32     double sum = 0;
     33     bool flag = false;
     34     int d = 0;
     35     while(isdigit(*arr) || *arr == '.') {
     36         if(*arr != '.') sum = sum * 10 + (*arr-'0');
     37         if(flag) d++;
     38         if(*arr == '.')
     39             flag = true;
     40         arr++;
     41     }
     42     if(flag)
     43     while(d--) {
     44         sum *= 0.1;
     45     }
     46     return sum;
     47 }
     48 
     49 double cal(double a, char o, double b)
     50 {
     51     if(o == '+') return a+b;
     52     else if(o == '-') return a-b;
     53     else if(o == '*') return a*b;
     54     else if(o == '/') return a/b;
     55 }
     56 
     57 bool isoptr(char c) {
     58     if(c == '(' || c == '+' || c == '-' || c == '*' || c == '/' || c == ')' || c == '#') return true;
     59     else return false;
     60 }
     61 
     62 double calculate(char *arr)
     63 {
     64     char optr[MAXN];
     65     double opnd[MAXN];
     66     char c;
     67     double a, b;
     68     int top1 = 0, top2 = 0;
     69     optr[top1++] = '#';
     70     while(*arr != '#' || optr[top1-1] != '#') {
     71         if(*arr == ' ')
     72             arr++;
     73         else if(!isoptr(*arr)) {
     74            opnd[top2++] = change(arr);
     75         } else {
     76             c = optr[top1-1];
     77             switch(com[c][*arr]) {
     78                 case -1:
     79                     optr[top1++] = *arr;
     80                     arr++;
     81                     break;
     82                 case 0:
     83                     --top1;
     84                     arr++;
     85                     break;
     86                 case 1:
     87                     c = optr[--top1];
     88                     b = opnd[--top2];
     89                     a = opnd[--top2];
     90                     opnd[top2++] = cal(a, c, b);
     91                     break;
     92             }
     93         }
     94     }
     95     return opnd[top2-1];
     96 }
     97 
     98 int main()
     99 {
    100     char arr[1024];
    101     ini();
    102     while(gets(arr)) {
    103         int len = strlen(arr);
    104       //  printf("%s\n", arr);
    105       arr[len] = '#';
    106         printf("%.2lf\n", calculate(arr));
    107     }
    108 
    109 }

    题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1572

  • 相关阅读:
    ObjectiveC语法快速参考
    IIS网站全部显示无权访问需要登录
    如何让自己的网站尽快收录绍兴114导航
    asp.net的运行原理
    WPF学习视频资料
    Asp.net MVC3 自定义HtmlHelper控件
    Unity3D中C#和JS的方法互相調用
    对于冒泡算法的思考,大牛可一笑而过~~
    2013年年前瞻望与计划
    使用vs2010编辑Unity脚本,配置方法
  • 原文地址:https://www.cnblogs.com/Griselda/p/2755874.html
Copyright © 2011-2022 走看看