下一个空,回车
这个是数,下一个不是数且不是.回车
这个是符号(不是+,-,.)回车
这个是加减&&前一个非空,前一个为),或前一个为数,回车
四则运算表达式由运算数(必定包含数字,可能包含正或负符号、小数点)、运算符(包括+、-、*、/)以及小括号((和))组成,每个运算数、运算符和括号都是一个token(标记)。现在,对于给定的一个四则运算表达式,请把她的每个token切分出来。题目保证给定的表达式是正确的,不需要做有效性检查。
输入格式:
在一行中给出长度不超过40个字符的表达式,其中没有空格,仅由上文中token的字符组成
输出格式:
依次输出表达式中的tokens,每个token占一行。
输入样例:
32*((2-2)+5)/(-15)
输出样例:
32
*
(
(
2
2
)
+
5
)
/
(
-15
)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
System.out.printf("%c",ch);
if(i+1==s.length())
System.out.printf("%n");
else if(ch>='0'&&ch<='9'&&!(s.charAt(i+1)>='0'&&s.charAt(i+1)<='9')&&s.charAt(i+1)!='.')
System.out.printf("%n");
else if(ch=='*'||ch=='/'||ch=='('||ch==')')
System.out.printf("%n");
else if((ch=='+'||ch=='-')&&i-1!=-1){
if(s.charAt(i-1)==')'||s.charAt(i-1)>='0'&&s.charAt(i-1)<='9')
System.out.printf("%n");
}
}
}
}