zoukankan      html  css  js  c++  java
  • C

    题目:

    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。 

    Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。 
    Output对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。 
    Sample Input

    1 + 2
    4 + 2 * 5 - 7 / 11
    0

    Sample Output

    3.00
    13.36

    解法1:
    边读取数据边判断左边的符号,乘除直接计算,加法储存到下一位,减法加负号储存到下一位;
     1 #include <iostream>
     2 #include <stdio.h>
     3 
     4 using namespace std;
     5 
     6 
     7 
     8 int main()
     9 {
    10     char fu;
    11     double temp;
    12     double sum;
    13     while(1)
    14     {
    15         sum = 0;
    16         double a[200];
    17         int i = 0;
    18         cin>>temp;
    19         fu = getchar();
    20         if(temp == 0&&fu == '
    ') break;
    21         else if (fu == '
    ') printf("%.2lf
    ",temp);
    22         else
    23         {
    24             a[0] = temp;
    25             while(cin>>fu>>temp)
    26             {
    27                 if( fu == '*')
    28                     a[i] *= temp;
    29                 if( fu == '/')
    30                     a[i] /= temp;
    31                 if( fu == '+')
    32                     a[++i] = temp;
    33                 if( fu == '-')
    34                     a[++i] = -temp;
    35                 if( getchar() == '
    ')
    36                     break;
    37              }
    38 
    39              for(int t = 0;t <= i;t ++)
    40                 sum += a[t];
    41             printf("%.2lf
    ",sum);
    42 
    43         }
    44     }
    45 
    46     return 0;
    47 }

     

  • 相关阅读:
    vss
    JavaScript中的5种事件使用方式解说
    loadrunner
    NET体系结构图
    eclipse Java Build Path
    httpModules 与 httpHandlers
    redhat linux5 安装配置 JDK1.6+Tomcat6+Apache2.2.x+jk_mod1.2
    如何在线使用MSDN
    petshop
    ADO.NET Entity Framework 使用数据定义语言(实体框架)
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7209206.html
Copyright © 2011-2022 走看看