今天在看到一个笔试题,觉得蛮有意思的,
题目如下:
从事先写好的Input.txt文件中读取数, Input.txt 内容 A{13,2,1,20,30,50} B{1,2,34,5,6} C{2,3,12,23,14,11} 用户在键盘随意敲入...例如((A*B))+B-C,((C+B)*A)-B期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
开始的时候打算用栈来计算的,
但想了想,不会搞,还是来个笨方法吧。
废话不多说,贴代码。
代码写的很烂,汗了!
/***从事先写好的Input.txt文件中读取数,*Input.txt内容*A{13,2,1,20,30,50}*B{1,2,34,5,6}*C{2,3,12,23,14,11}*用户在键盘随意敲入...例如((A*B))+B-C,((C+B)*A)-B期中+,*,-,分别代表集合的并交差运算,控制台打印输出。 */ 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; /** *@authorbzwm* */ publicclassEditorString{ privateTypea=null; privateTypeb=null; privateTypec=null; privateTypet=null; /** *读入指定的文件**@parampath */ publicvoidreadFile(Stringpath){ BufferedReaderreader=null; try{ reader=newBufferedReader(newInputStreamReader( newFileInputStream(path))); Stringstr=null; while((str=reader.readLine())!=null){ if(str.substring(0,1).equals("A")){ a=newType(str); }elseif(str.substring(0,1).equals("B")){ b=newType(str); }elseif(str.substring(0,1).equals("C")){ c=newType(str); }elseif(str.substring(0,1).equals("T")){ t=newType(str); }else{ System.out.println("nosuchtype!"); return; }} }catch(Exceptione){ e.printStackTrace(); return; }} /** *处理并、交、差操作,显示结果**@paramrule */ publicvoiddisplayResult(Stringrule){ intstart=0; intend=0; while(rule.length()>2){ if(rule.contains("(")){ start=rule.lastIndexOf("("); end=start+4;rule=execute(start,end,rule); }else{ start=0;end=start+2;rule=executeNormal(start,end,rule);}}Listresult=t.getArray(); for(inti=0;i<result.size();i++) System.out.println(result.get(i)); } /** *处理并、交、差操作**@paramstart*@paramend*@paramrule*@returnrule */ privateStringexecute(intstart,intend,Stringrule){ intsize=rule.length(); Typeobj=typeFactory(rule.substring(start+1,start+2));Stringope=rule.substring(start+2,start+3);Typearg=typeFactory(rule.substring(end-1,end)); try{ t=execute(obj,arg,ope); }catch(Exceptione){ e.printStackTrace();} returnrule.substring(0,start)+"T"+rule.substring(end+1,size); } /** *当用户输入的rule没有括号的情况:处理并、交、差操作*@paramstart*@paramend*@paramrule*@returnrule */ privateStringexecuteNormal(intstart,intend,Stringrule){ intsize=rule.length(); Typeobj=typeFactory(rule.substring(start,start+1));Stringope=rule.substring(start+1,start+2);Typearg=typeFactory(rule.substring(end,end+1)); try{ t=execute(obj,arg,ope); }catch(Exceptione){ e.printStackTrace();} returnrule.substring(0,start)+"T"+rule.substring(end+1,size); } /** *根据ope的不同,调用不同的方法**@paramobj*@paramarg*@paramope*@return*@throwsSecurityException*@throwsNoSuchMethodException*@throwsIllegalArgumentException*@throwsIllegalAccessException*@throwsInvocationTargetException */ privateTypeexecute(Typeobj,Typearg,Stringope) 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}); } /** *读入用户输入的匹配规则如:((C+B)*A)-B**@return */ publicstaticStringreadInput(){ Stringret=null; BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); try{ ret=br.readLine(); }catch(IOExceptione){ e.printStackTrace(); returnnull; } returnret; } /** *构造工厂**@paramtype*@return */ privateTypetypeFactory(Stringtype){ if(type.equals("A")){ returnnewType(a.getArray()); }elseif(type.equals("B")){ returnnewType(b.getArray()); }elseif(type.equals("C")){ returnnewType(c.getArray()); }elseif(type.equals("T")){ returnnewType(t.getArray()); }else{ returnnull; }} /** *把如{13,2,1,20,30,50}的集合抽象成一个类,提供并、交、差操作**@authorbzwm* */ classType{ privateListarray=newArrayList(); publicType(Stringsrt){ this.array=createList(srt); } publicType(Listlist){ this.array.addAll(list); } publicListgetArray(){ returnthis.array; } /** *并操作**@paramarg*@return */ publicTypebing(Typearg){ //是否加入到集合中的标志 booleanadd=true; //取出传入的Type对象的List Listlist=arg.getArray(); //遍历传入的Type对象的List for(inti=0;i<list.size();i++){ add=true; //与array里的值一一进行比较,如果全都不等,则加入到原array中,否则不加入 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)); }} //返回新的Type对象 returnnewType(array); } /** *交操作**@paramarg*@return */ publicTypejiao(Typearg){ //是否加入到集合中的标志 booleanadd=false; //存放交集数据的List Listret=newArrayList(); //取出传入的Type对象的List Listlist=arg.getArray(); //遍历传入的Type对象的List for(inti=0;i<list.size();i++){ add=false; //与array里的值一一进行比较,如果有相等的,则加入到ret中,否则不加入 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)); }} //返回新的Type对象 returnnewType(ret); } /** *差操作**@paramarg*@return */ publicTypecha(Typearg){ //是否加入到集合中的标志 booleanadd=true; //存放交集数据的List Listlist=arg.getArray(); //遍历传入的Type对象的List for(inti=0;i<list.size();i++){ add=true; //与array里的值一一进行比较,如果有相等的,则从原array中将其删除,如果全都不等,则加入到原array中 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)); }} //返回新的Type对象 returnnewType(array); } /** *解析字符串,将数字加入到List中**@paramstr*@return */ privateListcreateList(Stringstr){ //将字符串解析成字符串数组A{13,2,1,20,30,50}-->newString[]{13,2,1,20,30,50} 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; }} /** *测试程序*@paramargs */ publicstaticvoidmain(Stringargs[]){ EditorStringes=newEditorString(); es.readFile("input.txt"); es.displayResult(readInput());//((C+B)*A)-B }}写完这个程序之后,该睡觉了,也没测试!
有问题了再说啊!