废话不说了,
文件:
A{1,2,3,4,5,6}
B{7,4,5,6,8}
C{2,3,12,14,4,11}
测试时输入到控制台的字符串为:
C+B-(A*(C-A))+B
结果:
2 3 12 14 4 11 7 8 1 5 6
自己算了一下,是正确的!
代码如下,注释也写的蛮多的:
-
- packagecom.lim.test;
- importjava.io.BufferedReader;
- importjava.io.FileInputStream;
- importjava.io.IOException;
- importjava.io.InputStreamReader;
- importjava.lang.reflect.InvocationTargetException;
- importjava.lang.reflect.Method;
- importjava.util.ArrayList;
- importjava.util.List;
- importjava.util.Stack;
-
-
-
publicclassEditorStringV2{
-
-
privateTypetypeA=null;
-
-
privateTypetypeB=null;
-
-
privateTypetypeC=null;
-
privateStackstack=newStack();
-
publicEditorStringV2(Stringpath){
- readFile(path);
- }
-
-
-
privatevoidreadFile(Stringpath){
-
BufferedReaderreader=null;
-
try{
-
reader=newBufferedReader(newInputStreamReader(
-
newFileInputStream(path)));
-
Stringstr=null;
-
-
while((str=reader.readLine())!=null){
-
-
if(str.substring(0,1).equals("A")){
-
typeA=newType(str);
- }
-
-
elseif(str.substring(0,1).equals("B")){
-
typeB=newType(str);
- }
-
-
elseif(str.substring(0,1).equals("C")){
-
typeC=newType(str);
-
}else{
-
System.out.println("nosuchtype!");
-
return;
- }
- }
-
}catch(Exceptione){
- e.printStackTrace();
-
return;
- }
- }
-
-
-
publicStackdisplayResult(StackorgStack)throwsSecurityException,
- IllegalArgumentException,NoSuchMethodException,
- IllegalAccessException,InvocationTargetException{
-
-
intleftBracket=0;
-
-
booleanhasOpe=false;
-
-
intlength=orgStack.size();
-
Objectobj=null;
-
if(length<3){
-
System.out.println("inputruleisillegal.");
-
returnnull;
-
}else{
-
for(inti=0;i<length;i++){
-
- obj=orgStack.pop();
-
-
if(isLeftBracket(obj)){
- leftBracket+=1;
- stack.push(obj);
-
continue;
- }
-
-
if(isOperator(obj)){
-
hasOpe=true;
- stack.push(obj);
-
continue;
- }
-
- stack.push(obj);
-
-
if(leftBracket>0&&isRightBracket(obj)){
-
- stack.pop();
-
- Typearg=(Type)stack.pop();
-
- Stringope=stack.pop().toString();
-
- TypeinvokeObj=(Type)stack.pop();
-
- TypetypeTmp=execute(invokeObj,ope,arg);
-
- stack.pop();
-
- leftBracket-=1;
-
- stack.push(typeTmp);
-
-
if(stack.size()>2){
-
StacktmpStack=newStack();
-
while(stack.size()>0){
- tmpStack.push(stack.pop());
- }
- stack=displayResult(tmpStack);
- }
-
continue;
- }
-
-
-
if(leftBracket==0&&hasOpe&&isType(obj)){
-
- Typearg=(Type)stack.pop();
-
- Stringope=stack.pop().toString();
-
- TypeinvokeObj=(Type)stack.pop();
-
- TypetypeTmp=execute(invokeObj,ope,arg);
-
- stack.push(typeTmp);
-
-
hasOpe=false;
-
-
if(stack.size()>2){
-
StacktmpStack=newStack();
-
while(stack.size()>0){
- tmpStack.push(stack.pop());
- }
- stack=displayResult(tmpStack);
- }
-
continue;
- }
- }
-
-
returnstack;
- }
- }
-
-
-
privatebooleanisType(Objecto){
-
returnoinstanceofType;
- }
-
-
-
privatebooleanisOperator(Objecto){
-
return!isType(o)&&((String)o).matches("[+//*-]");
- }
-
-
-
privatebooleanisLeftBracket(Objecto){
-
return!isType(o)&&((String)o).equals("(");
- }
-
-
-
privatebooleanisRightBracket(Objecto){
-
return!isType(o)&&((String)o).equals(")");
- }
-
-
-
privateTypeexecute(Typeobj,Stringope,Typearg)
- throwsSecurityException,NoSuchMethodException,
- IllegalArgumentException,IllegalAccessException,
- InvocationTargetException{
- Classc=obj.getClass();
-
Class[]args=newClass[1];
- args[0]=arg.getClass();
-
Methodm=null;
-
-
if(ope.equals("+")){
-
m=c.getMethod("bing",args);
- }
-
-
elseif(ope.equals("*")){
-
m=c.getMethod("jiao",args);
- }
-
-
elseif(ope.equals("-")){
-
m=c.getMethod("cha",args);
-
}else{
-
System.out.println("NoSuchMethod");
-
returnnull;
- }
-
return(Type)m.invoke(obj,newObject[]{arg});
- }
-
-
-
privateStackreadInput(){
-
Stackret=newStack();
-
Stringstr=null;
-
Stringo=null;
-
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
-
try{
- str=br.readLine();
-
}catch(IOExceptione){
- e.printStackTrace();
-
returnnull;
- }
-
for(inti=str.length();i>0;i--){
- o=str.substring(i-1,i);
-
-
if(o.matches("[ABC]")){
- ret.push(typeFactory(o));
-
continue;
- }
- ret.push(o);
- }
-
returnret;
- }
-
-
-
privateTypetypeFactory(Stringtype){
-
if(type.equals("A")){
-
returnnewType(typeA.getArray());
-
}elseif(type.equals("B")){
-
returnnewType(typeB.getArray());
-
}elseif(type.equals("C")){
-
returnnewType(typeC.getArray());
-
}else{
-
returnnull;
- }
- }
-
-
-
classType{
-
-
privateListarray=newArrayList();
-
publicType(Stringsrt){
-
this.array=createList(srt);
- }
-
publicType(Listlist){
-
this.array.addAll(list);
- }
-
publicListgetArray(){
-
returnthis.array;
- }
-
-
-
publicTypebing(Typearg){
-
-
booleanadd=true;
-
- Listlist=arg.getArray();
-
-
for(inti=0;i<list.size();i++){
-
add=true;
-
-
for(intj=0;j<array.size();j++){
-
if(((Integer)list.get(i)).intValue()==((Integer)array
-
.get(j)).intValue()){
-
add=false;
- }
- }
-
if(add){
-
array.add(list.get(i));
- }
- }
-
-
returnnewType(array);
- }
-
-
-
publicTypejiao(Typearg){
-
-
booleanadd=false;
-
-
Listret=newArrayList();
-
- Listlist=arg.getArray();
-
-
for(inti=0;i<list.size();i++){
-
add=false;
-
-
for(intj=0;j<array.size();j++){
-
if(((Integer)list.get(i)).intValue()==((Integer)array
-
.get(j)).intValue()){
-
add=true;
- }
- }
-
if(add){
-
ret.add(list.get(i));
- }
- }
-
-
returnnewType(ret);
- }
-
-
-
publicTypecha(Typearg){
-
-
booleanadd=true;
-
- Listlist=arg.getArray();
-
-
for(inti=0;i<list.size();i++){
-
add=true;
-
-
for(intj=0;j<array.size();j++){
-
if(((Integer)list.get(i)).intValue()==((Integer)array
-
.get(j)).intValue()){
-
add=false;
-
- array.remove(j);
- }
- }
-
if(add){
-
array.add(list.get(i));
- }
- }
-
-
returnnewType(array);
- }
-
-
-
privateListcreateList(Stringstr){
-
-
Strings[]=str.replaceAll(str.substring(0,1),"").replace("{",
-
"").replace("}","").split(",");
-
Listlist=newArrayList();
-
for(inti=0;i<s.length;i++){
-
list.add(newInteger(s[i]));
- }
-
returnlist;
- }
- }
-
-
-
publicstaticvoidmain(Stringargs[])throwsSecurityException,
- IllegalArgumentException,NoSuchMethodException,
- IllegalAccessException,InvocationTargetException{
-
EditorStringV2es=newEditorStringV2("input.txt");
- Stacks=es.readInput();
-
Stackresult=es.displayResult(s);
- Listlist=((Type)result.pop()).getArray();
-
System.out.println("操作运算后结果为:");
-
for(inti=0;i<list.size();i++)
-
System.out.print(list.get(i)+"");
- }
- }
分享到:
相关推荐
那么一段长度为N的字符串,按照存储的ASCII码可以表示为一串如下的字符串(不包含[]) "x .. x][x .. x y x .. x][x .. x", 其中 s,e表示当前正在分析的一段子串(0, 下标e-s = 期望的分割长度cut_size) ^ ^ ^ ^ ^ 0...
Hyperledger Fabric make: *** No rule to make target问题 最近一段时间,改Fabric代码,发现没法编译了!make总是报找不到target! ➜ fabric git:(master) ✗ make configtxgen make: *** No rule to make target ...
Rule34.xxx API包装器 这是一个简单的模块,用于简化异步访问rule34.xxx API的过程 为了帮助机器人开发人员,该包装器根本不使用requests 。 相反,它使用aiohttp。 为了帮助异步编码人员,提供了一个名为Sync的类,...
Design Rule 相关介绍本篇介绍的design rule只针对CMOS技术。画版图时需要按design rule的要求来操作,所以也就有我们常提到的DRC(design rule check),对设计规则的检测工作。Design rule根据工艺,工厂设备,...
虚拟控制台的切换可以通过按下Alt键和一个功能键来实现,通常使用F1-F6 。 例如,用户登录后,按一下“Alt+ F2”键,用户就可以看到上面出现的“login:”提示符,说明用户看到了第二个虚拟控制台。然后只需按“Alt+...
运行main.py / main.exe并按照屏幕上的说明进行操作 如果您遇到困难,请将鼠标悬停在每个字段上以了解其功能 享受你过多的色情 常问问题 我可以一次搜索多个标签吗? 是的,该程序将每个单词视为一个标签; 因此, ...
在日常工作中,过滤表单中的一些特殊的字符是很常见的功能,比如文本中要求输入单纯的数字,但用户有时会误输入一些多余的空格或其他字符混合的文本,这显然不符合输入要求。 下面一起来学习怎么样去除字符串左右...
从 http://www.rule34.xxx 下载标记图像,选择一组页面来抓取或下载,但一定要先阅读自述文件中的警告!
python中有关于skope-rule模块的ppt的补充说明,更为详细地介绍了函数以及相关原理,帮助ppt阅读
JAVA RULE
NULL 博文链接:https://bijian1013.iteye.com/blog/2119792
C++实现的表达式解析,本程序是利用强大的boost::spirit库实现的。这个东西实在是太强大了。 程序运行结果如下: -----------表达式解析--------- 已定义的函数有:PI,SIN,COS,TAN,,ABS,EXP,LOGN,POW,SQRT,FORMAT,...
使用digester配置rule.xml规则来解析自定义的xml文件
大学毕业设计 使用python基于opencv开发车牌识别系统,可以实现后台传输的图片识别 使用了两个相同结构的卷积神经网络 车牌识别系统可以分为两个部分, 第一个部分是车牌定位过滤部分; 第二个部分是字符识别部分; ...
主持人:安装: 克隆回购: git clone https://github.com/gabhm/rule34-scraper.git && cd rule34-scraper 安装dep: pip3 install -r requirements.txt如何使用: 注意:刮板将下载所有图像并将其移动到:/ images...
rule treerule tree
这是一个找到一篇关于权限设计的教程,写的很好,但是发现给出的代码,有些地方可能有笔误。贴出来供大家和我一起学习。 首先,设置三种要素:用户、群组、角色。 用户为登录用,对应到人。群组对应为用户的集合,是...
how2downloadrule34 一个下载所有rule34图像的程序您必须已经安装了NODE.JS 首先下载文件,然后将其解压缩到任意位置要吃午餐,请打开powershell,然后cd进入程序文件夹示例:“ cd V:/ dev / how2downloadrule34”...
Laravel开发-rule 用于Laravel的表达式验证规则生成器。
rule ppt 博文链接:https://cana.iteye.com/blog/184116