[先说点出题背景]

这个题是为低年级同学、学C语言的同学准备的,因为,对这部分同学,这个题目编写起来略有一点复杂。如果是高年级、学过了正则表达式(Regular Expression)的同学或者学过了Java等OO语言的同学做这个题,应当发现这题比较简单吧。哦,对了,什么是tokenizer?请自行查询解决。反正在此处不应翻译成“令牌解析器”。

[正题]

四则运算表达式由运算数(必定包含数字,可能包含正或负符号小数点)、运算符(包括+-*/)以及小括号(())组成,每个运算数、运算符和括号都是一个token(标记)。现在,对于给定的一个四则运算表达式,请把她的每个token切分出来。题目保证给定的表达式是正确的,不需要做有效性检查。

输入格式:

在一行中给出长度不超过40个字符的表达式,其中没有空格,仅由上文中token的字符组成

输出格式:

依次输出表达式中的tokens,每个token占一行。

输入样例:

32*((2-2)+5)/(-15)

输出样例:

32
*
(
(
2
-
2
)
+
5
)
/
(
-15
)

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <map> using namespace std;
char a[55];
int main()
{
gets(a);
int len=strlen(a);
string s;
for(int i=0;i<len;i++)
{
if((a[i]>='0'&&a[i]<='9')||a[i]=='.')
{
s+=a[i];
}
else if(a[i]=='-')
{
if((a[i-1]>='0'&&a[i-1]<='9'&&i!=0)||a[i+1]=='(')
{
if(s!="")
{
cout<<s<<endl;
s="";
}
cout<<a[i]<<endl;
}
else
s+=a[i];
}
else if(a[i]=='+')
{
if(i==0)
s+=a[i];
else
{
if(s!="")
{
cout<<s<<endl;
s="";
}
cout<<a[i]<<endl; }
}
else if(a[i]=='*'||a[i]=='/'||a[i]=='('||a[i]==')')
{
if(s!="")
{
cout<<s<<endl;
s="";
}
cout<<a[i]<<endl;
} }
return 0; }

pta习题集 5-10 切分表达式——写个tokenizer吧的更多相关文章

  1. [刷题] PTA 7-62 切分表达式 写个tokenizer吧

    我的程序: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 50 4 char token[]= {'+','-',' ...

  2. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

  3. lambda表达式——写多线程

    JDK1.8 中Lambda 表达式的出现,基本可以取替原来的匿名类实现多线程的方式.下面列举常用的常用的三种情况. 一.普通开启异步线程   new Thread(() -> System.o ...

  4. 【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)

    [LeetCode Weekly Contest 29][2017/04/23] 第17周 Binary Tree Tilt (3) Array Partition I (6) Longest Lin ...

  5. 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)

    Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...

  6. 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

    Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...

  7. 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)

    Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...

  8. 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

    Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...

  9. 10. Lambda表达式.md

    为了简化匿名内部类的代码,具体定义: 例如将9.内部类中的匿名内部类例子: 原来代码: //Main.java public class Main { public static void main( ...

随机推荐

  1. ADSI Edit 工具

    最近在弄.net的活动目录用到了工具ADSI Edit,网上找了点资料,一来自己记录下,二来分享给大家: 下载的压缩包里存在两个文件adsiedit.dll和adsiedit.msc 1.将adsie ...

  2. e638. 向剪切板获取和粘贴图像

    // If an image is on the system clipboard, this method returns it; // otherwise it returns null. pub ...

  3. e1087. try/catch语句

    The try/catch statement encloses some code and is used to handle errors and exceptions that might oc ...

  4. fatal error: malformed or corrupted AST file: &#39;Unable to load module Darwin.pcm 问题解决

    xcode5 编译project.偶然碰到了以下的问题: fatal error: malformed or corrupted AST file: 'Unable to load module &q ...

  5. 使用C#压缩解压rar和zip格式文件

    为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...

  6. _variant_t和_bstr_t

    _variant_t和_bstr_t这两个类分别封装并管理VARIANT和BSTR这两种数据类型, VARIANT和BSTR这两种类型是COM中使用的数据类型. 实现_variant_t向int,St ...

  7. CSS定位背景图片 background-position

    网站的样式的时候经常会发现一种情况,就是在很多background属性里都调用同一张图片,来满足网页各个部分的使用.打开这种图片看一下,会发现这张图片上包含了很多小图片; 又如: 这些小图片就是整图分 ...

  8. 安装Phoenix时./sqlline.py执行报错File "./sqlline.py", line 27, in <module> import argparse ImportError: No module named argparse解决办法(图文详解)

    不多说,直接上干货! 前期博客 Apache版Phoenix的安装(图文详解) 问题现象 Traceback (most recent call last): File , in <module ...

  9. mybatise 动态sql

    1. <if><choose> 动态sql 相当 <if> Java if  满足多个条件  <choose> <when>    java ...

  10. [Scikit-learn] *Dynamic Bayesian Network - Partical Filter

    涉及的一些知识: 机器人的自我定位 Sequential Importance Sampling Ref: http://scipy-cookbook.readthedocs.io/items/Par ...