题目链接http://poj.org/problem?id=1686

思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit。

代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std; const int MAX_N = ;
const double factor = 1.1;
double number[MAX_N];
char str[MAX_N];
int len, pos; double Expression( );
double Term( );
double Factor( ); void InputExpression( )
{
int i = ;
pos = ;
string str_in;
getline(cin, str_in); for (i = , len = ; i < str_in.length( ); ++i, ++len)
str[len] = str_in[i];
str[len] = '\0';
} char GetCurrentToken( )
{
while (str[pos] == ' ' || str[pos] == '\t')
pos++;
return str[pos];
} char GetNextToken( )
{
++pos;
while (str[pos] == ' ' || str[pos] == '\t')
pos++;
if (pos == len)
return '\n';
else
return str[pos];
} double Expression( )
{
char ch;
double term1 = Term( ); while ((ch = GetCurrentToken( )) == '+' || (ch == '-')) {
GetNextToken( );
double term2 = Term( ); if (ch == '+')
term1 += term2;
else
term1 -= term2;
} return term1;
} double Term( )
{
char ch;
double factor_1 = Factor( ); while ((ch = GetCurrentToken( )) == '*') {
GetNextToken( );
double factor_2 = Factor( ); factor_1 *= factor_2;
}
return factor_1;
} double Factor( )
{
double value = 0.0;
char ch = GetCurrentToken( ); if (ch == '(') {
GetNextToken( );
value = Expression( );
GetNextToken( );
} else if (isdigit(ch)) {
value = ch - '';
GetNextToken( );
}
else if (ch == '\n')
value = -;
else {
if (number[ch - 'A'] == )
value = number[ch - 'A'] = (ch - 'A') * 1.1;
else
value = number[ch - 'A'];
GetNextToken( );
}
return value;
} int main( )
{
int case_time; scanf("%d\n", &case_time);
while (case_time--) {
InputExpression( );
double ans_1 = Expression( ); InputExpression( );
double ans_2 = Expression( ); if (abs(ans_1 - ans_2) < 1e-)
printf("YES\n");
else
printf("NO\n");
} return ;
}

poj 1684 Lazy Math Instructor(字符串)的更多相关文章

  1. 数据结构——POJ 1686 Lazy Math Instructor 栈的应用

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  2. POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑

    Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...

  3. POJ 1686 Lazy Math Instructor(栈)

    原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...

  4. Lazy Math Instructor

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3721   Accepted: 1290 Description A m ...

  5. UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)

    因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号)  sum += Cal( ...

  6. POJ - 2183 Bovine Math Geniuses

    “模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数.注意到仔细看题,这种题一定要细心! POJ - 2183 Bovine Math Geniuses Ti ...

  7. POJ 1743 Musical Theme (字符串HASH+二分)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15900   Accepted: 5494 De ...

  8. poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】

    题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...

  9. POJ 2389 Bull Math(水~Java -大数相乘)

    题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...

随机推荐

  1. Cookies与保持登录(新浪微博的简单模拟登录)

    Cookies与保持登录(新浪微博的简单登录) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino ...

  2. Web 前端知识点

  3. Android Studio git ignore

    # Built application files *.apk *.ap_ # Files for the Dalvik VM *.dex # Java class files *.class # G ...

  4. SVG image xlink:href 设置失败

    公司比较频繁的业务需求,需要在地图上面,标注地区的信息,考虑到兼容性问题,在实际开发中是通过raphael.js绘制地图信息,进行相关交互 产品部门同事辛苦的画SVG地图,可配置地图块与实际地区cod ...

  5. Flink资料(3)-- Flink一般架构和处理模型

    Flink一般架构和处理模型 本文翻译自General Architecture and Process Model ----------------------------------------- ...

  6. 请求(Request)的参数(Parameter)里包含特殊字符(#等)的正确处理方式

    遇到一个问题 在一个地址链接(URL)里使用 url?param1=val1&param2=val2 的方式传递参数,结果在获取参数值时发现不是当初设定的值. 具体案例 以特殊字符井号(#)为 ...

  7. 深入Android媒体存储服务(二):磁盘扫描流程

    简介: 本文是<深入Android媒体存储服务>系列第二篇,简要介绍媒体存储服务扫描文件的流程.文中介绍的是 Android 4.2. Android 有一套媒体存储服务,进程名是 and ...

  8. android开源框架和开源项目(转)

    特效: http://www.androidviews.net/ http://www.theultimateandroidlibrary.com/ 常用效果: 1. https://github.c ...

  9. linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客

    linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客 linux中读写锁的rwlock介绍 2013-02-26 13:59:35 分类: C/C++   http://yaro ...

  10. Eclipse无法识别(手机)设备的解决方案

    遇到问题 开始学习android一个多月了,用Eclipse开发,用android手机调试.之前一直好好的,突然Eclipse无法识别手机设备了.纠结了好久,找了各种解决方法,弄了一晚上终于解决问题了 ...