poj 1684 Lazy Math Instructor(字符串)
题目链接: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(字符串)的更多相关文章
- 数据结构——POJ 1686 Lazy Math Instructor 栈的应用
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...
- POJ 1686 Lazy Math Instructor(栈)
原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...
- Lazy Math Instructor
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3721 Accepted: 1290 Description A m ...
- UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)
因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号) sum += Cal( ...
- POJ - 2183 Bovine Math Geniuses
“模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数.注意到仔细看题,这种题一定要细心! POJ - 2183 Bovine Math Geniuses Ti ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】
题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...
- POJ 2389 Bull Math(水~Java -大数相乘)
题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...
随机推荐
- 搜索服务器xunsearch实现
安装方法: centos 6.6 64位 histroy: 12 cd /srv/ 13 wget http://www.xunsearch.com/download/xunsea ...
- 读取本地HTML的小说阅读器应用源码项目
该源码是一个不错的读取本地HTML的小说阅读器,读取本地HTML的小说阅读器,并且源码也比较简单的,非常适合我们的新手朋友拿来学习,有兴趣的朋友研究下. 源码下载: http://code.662p. ...
- linux命令学习01-mkdir
1.环境说明 centos6.7,2.6.32-573.el6.x86_64 2.man mkdir NAM mkdir - make directories SYNOPSI ...
- Linux----函数中变量的作用域、local关键字。
问题引出: 问题说明: shell的函数中如果变量的前面没有加local关键字.在全局作用域内存在这个变量名的情况下.函数中会直接使用这个变量 而不是自己创建一个.要想做到在函数创建一个变量也是可以的 ...
- J2SE知识点摘记(二十五)
Set 1.5.1 概述 Java 中的Set和正好和数学上直观的集(set)的概念是相同的.Set最大的特性就是不允许在其中存放的元素是重复的.根据这个特点,我们就可以使用Set 这个 ...
- delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)
delphi 的IDE快捷键与输入法切换键中突,以往的解决方法是下载一个ImeTool修改 windows 系统的快捷键 在 xp win7 都好使,但在win 10经常是修改完后,重启又失效了. 本 ...
- rsyslog start with
startswith Checks if the value is found exactly at the beginning of the property value. For example, ...
- python 类型转换函数
python提供了一些可将某个值从一种类型转换为另一种类型的内置函数. 1. int函数可以把任何可以转换为整型的值转换为整型.int可以将浮点数转换为整数,但不会做四舍五入操作,而是直接丢弃小数部分 ...
- Android 4.2启动代码分析(一)
Android系统启动过程分析 Android系统的框架架构图如下(来自网上): Linux内核启动之后----->就到Android的Init进程 ----->进而启动Android ...
- 从零开始Unity3D游戏开发【4 材质球和渲染纹理】
[创建材质球] 1.Project 面板下 create-Material 然后将材质球拖放到物体,物体的颜色便会和材质球相同: [渲染纹理 RenderTexture]