一、前言
1.第四次题集是接着前面的第三次题集的迭代,知识点主要是继承的运用,正则表达式的运用同样少不了,相对于前面一次添加了对选题以及填空题,难度也相对于前一次加大。
2.第五次题集是新的题集迭代是有关电路的,知识点有抽象类,抽象方法的使用以及继承的使用,同样正则表达式;由于没有考虑电阻问题只有一个用电器,也是只有一条串联电路,难度比较小。
3.第六次是第五次的迭代,知识点有抽象类,抽象方法,继承,接口,正则表达式。相对于上一次添加了串联电路,同时考虑电阻,所以难度有所提升。
二、设计与分析
1.答题判题程序-4
从类图可以看出在上一次的基础上添加了Multiplequestion,Fillblankquestions两个类,分别是多选题类和填空题类,两者都继承了题目Problem类,这两个类都有一个构造方法,但是在多选题类中而外添加了一个方法用于拆分选项。代码如下:
String s[]=getStandardAnswer().split(" ");
public String[] getS()
{
return s;
}
所有源代码如下:
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
class Problem{
private int pnum;
private String content;
private String standardAnswer;
private int grade;
public int getGrade()
{
return grade;
}
public void setGrade(int grade)
{
this.grade=grade;
}
int getPnum()
{
return pnum;
}
void setPnum(int pnum)
{
this.pnum=pnum;
}
String getContent()
{
return content;
}
void setContent(String content)
{
this.content=content;
}
String getStandardAnswer()
{
return standardAnswer;
}
void setStandardAnswer(String standardAnswer)
{
this.standardAnswer=standardAnswer;
}
public Problem(){}
public Problem(int pnum,String content,String standardAnswer,int grade)
{
setPnum( pnum);
setContent(content);
setStandardAnswer(standardAnswer);
setGrade(grade);
}
}
class Multiplequestion extends Problem{
public Multiplequestion(int pnum, String content, String standardAnswer, int grade) {
super(pnum, content, standardAnswer, grade);
}
String s[]=getStandardAnswer().split(" ");
public String[] getS()
{
return s;
}
}
class Fillblankquestions extends Problem{
public Fillblankquestions(int pnum, String content, String standardAnswer, int grade) {
super(pnum, content, standardAnswer, grade);
}
}
class Paper{
private int nump;
private ArrayList
private ArrayList
private ArrayList
private int allgrade;
public int getNump()
{
return nump;
}
public void setNump(int nump)
{
this.nump=nump;
}
public int getallGrade()
{
return allgrade;
}
public void setallGrade(int allgrade)
{
this.allgrade=allgrade;
}
public ArrayList
{
return mquestionlist;
}
public ArrayList
{
return problemlist;
}
public ArrayList
{
return fquestionlist;
}
public void setproblemlist(int num,Problem problem)
{
problemlist.add(num-1,problem);
}
public int ejudge(int num,String answer)
{
for(int i=0;i<problemlist.size();i++)
{
if(numproblemlist.get(i).getPnum())
{
if(problemlist.get(i).getStandardAnswer().equals(answer))
return 1;
if(problemlist.get(i).getStandardAnswer().equals(answer)false)
return 0;
}
}
for(int i=0;i<mquestionlist.size();i++)
{
if(nummquestionlist.get(i).getPnum())
{
String t[]=answer.split(" ");
int tem[]=new int[t.length];
int falg=0;
int falg1=0;
for(int k=0;k<t.length;k++)
{
for(int p=0;p<mquestionlist.get(i).getS().length;p++)
{
if(t[k].equals(mquestionlist.get(i).getS()[p]))
tem[k]=1;
}
}
for(int k=0;k<t.length;k++)
{
if(tem[k]1)
falg++;
if(tem[k]0)
falg1++;
}
if(falg10&&falgmquestionlist.get(i).getS().length)
return 1;
else if(falg10&&falg<mquestionlist.get(i).getS().length)
return 2;
else
return 0;
}
}
for(int i=0;i<fquestionlist.size();i++)
{
if(num==fquestionlist.get(i).getPnum())
{
if(fquestionlist.get(i).getStandardAnswer().equals(answer))
return 1;
if(fquestionlist.get(i).getStandardAnswer().contains(answer))
return 2;
if(fquestionlist.get(i).getStandardAnswer().equals(answer)==false&&fquestionlist.get(i).getStandardAnswer().contains(answer)==false)
return 0;
}
}
return 3;
}
public void addmproblem(int num,Multiplequestion question)
{
mquestionlist.add(num-1,question);
}
public void addfproblem(int num,Fillblankquestions multiplequestion)
{
fquestionlist.add(num-1,multiplequestion);
}
public void addproblem(int num,Problem question)
{
problemlist.add(num-1,question);
}
public Problem getproblem(int num)
{
return problemlist.get(num);
}
public ArrayList<Problem> getProblemlist() {
return problemlist;
}
public void setProblemlist(ArrayList<Problem> problemlist) {
this.problemlist = problemlist;
}
public ArrayList<Multiplequestion> getMquestionlist() {
return mquestionlist;
}
public void setMquestionlist(ArrayList<Multiplequestion> mquestionlist) {
this.mquestionlist = mquestionlist;
}
public ArrayList<Fillblankquestions> getFquestionlist() {
return fquestionlist;
}
public void setFquestionlist(ArrayList<Fillblankquestions> fquestionlist) {
this.fquestionlist = fquestionlist;
}
}
class Answer {
private Paper paper;
private ArrayList
private ArrayList
private int answernum;
private String sid;
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public int getAnswernum()
{
return answernum;
}
public void setAnswernum(int answernum)
{
this.answernum=answernum;
}
Paper getPaper()
{
return paper;
}
void setPaper(Paper paper)
{
this.paper=paper;
}
public void ajudge(int num,int deleteproblem,int existproblem,int answernumber)
{
if(num<=answerlist.size())
{
if(existproblem==1&&num==answernumber)
judgement.add(num-1,"false");
if(deleteproblem!=0&&num==deleteproblem)
{
judgement.add(num-1,"false");
}
else if(getPaper().ejudge(num,answerlist.get(num-1))==1)
judgement.add(num-1,"true");
else if(getPaper().ejudge(num,answerlist.get(num-1))==2)
judgement.add(num-1,"partially correct");
else
judgement.add(num-1,"false");
}
else
judgement.add(num-1,"false");
}
public void addanswer(int num,String answer)
{
answerlist.add(num-1,answer);
}
public void printf1(int num,int deleteproblem,int existproblem,int answernumber)
{
for(int i=0;i<num;i++)
{
if(i<answerlist.size())
{
if(existproblem==1&&i==answernumber)
System.out.println("non-existent question~0");
else if(deleteproblem!=0&&i==deleteproblem-1&&existproblem!=1)
{
for(int j=0;j<paper.getProblemlist().size();j++)
{
if(i+1==paper.getProblemlist().get(j).getPnum())
{
if(paper.getproblem(j).getContent().equals(""))
System.out.println("non-existent question~0");
else
System.out.println("the question "+deleteproblem+" invalid~0");
}
}
for(int j=0;j<paper.getMquestionlist().size();j++)
{
if(i+1==paper.getMquestionlist().get(j).getPnum())
{
if(paper.getMquestionlist().get(j).getContent().equals(""))
System.out.println("non-existent question~0");
else
System.out.println("the question "+deleteproblem+" invalid~0");
}
}
for(int j=0;j<paper.getFquestionlist().size();j++)
{
if(i+1==paper.getFquestionlist().get(j).getPnum())
{
if(paper.getFquestionlist().get(j).getContent().equals(""))
System.out.println("non-existent question~0");
else
System.out.println("the question "+deleteproblem+" invalid~0");
}
}
}
else{
for(int j=0;j<paper.getProblemlist().size();j++)
{
if(i+1==paper.getProblemlist().get(j).getPnum())
{
if(paper.getproblem(j).getContent().equals(""))
System.out.println("non-existent question~0");
else
System.out.println(paper.getProblemlist().get(j).getContent()+"~"+answerlist.get(i)+"~"+judgement.get(i));
}
}
for(int j=0;j<paper.getMquestionlist().size();j++)
{
if(i+1==paper.getMquestionlist().get(j).getPnum())
{
if(paper.getMquestionlist().get(j).getContent().equals(""))
System.out.println("non-existent question~0");
else
System.out.println(paper.getMquestionlist().get(j).getContent()+"~"+answerlist.get(i)+"~"+judgement.get(i));
}
}
for(int j=0;j<paper.getFquestionlist().size();j++)
{
if(i+1==paper.getFquestionlist().get(j).getPnum())
{
if(paper.getFquestionlist().get(j).getContent().equals(""))
System.out.println("non-existent question~0");
else
System.out.println(paper.getFquestionlist().get(j).getContent()+"~"+answerlist.get(i)+"~"+judgement.get(i));
}
}
}
}
else if(existproblem==1&&i==answernumber)
System.out.println("non-existent question~0");
else
System.out.println("answer is null");
}
}
public void printf2(int num)
{
int a[]=new int[num];
int sum=0;
for(int j=0;j<num;j++)
{
if(judgement.get(j).equals("true"))
{
for(int i=0;i<paper.getProblemlist().size();i++)
{
if(j+1==paper.getProblemlist().get(i).getPnum())
a[j]=paper.getproblem(j).getGrade();
}
for(int i=0;i<paper.getMquestionlist().size();i++)
{
if(j+1==paper.getMquestionlist().get(i).getPnum())
{
a[j]=paper.getMquestionlist().get(i).getGrade();
}
}
for(int i=0;i<paper.getFquestionlist().size();i++)
{
if(j+1==paper.getFquestionlist().get(i).getPnum())
{
a[j]=paper.getFquestionlist().get(i).getGrade();
}
}
}
else if(judgement.get(j).equals("partially correct"))
{
for(int i=0;i<paper.getMquestionlist().size();i++)
{
if(j+1==paper.getMquestionlist().get(i).getPnum())
{
a[j]=paper.getMquestionlist().get(i).getGrade()/2;
}
}
for(int i=0;i<paper.getFquestionlist().size();i++)
{
if(j+1==paper.getFquestionlist().get(i).getPnum())
{
a[j]=paper.getFquestionlist().get(i).getGrade()/2;
}
}
}
else
a[j]=0;
sum=sum+a[j];
}
for(int i=0;i<num;i++)
{
if(i<num-1)
System.out.printf("%d ",a[i]);
else
System.out.printf("%d",a[i]);
}
System.out.println("~"+sum);
}
}
class Student{
private String sid;
private String name;
private ArrayList
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Answer> getAnswerlist() {
return answerlist;
}
public void setAnswerlist(ArrayList<Answer> answerlist) {
this.answerlist = answerlist;
}
public void setAnswer(Answer answer) {
answerlist.add(answer);
}
public Student(String sid,String name)
{
setSid(sid);
setName(name);
}
public void show(){
System.out.printf(sid+" "+name+": ");
}
}
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String sf[]=new String[100];
String t[]=new String[10];
int j=0;
int nump=0;
ArrayList
ArrayList
String regex1="#S:(\d+)\s(\d+)\s(#A:(.?)(\s+|$))";
Pattern pattern3 = Pattern.compile("#T:(\d+)\s+\d+-\d+");
Pattern pattern4 = Pattern.compile("(\d+)-(\d+)");
Pattern pattern2 = Pattern.compile(regex1);
Pattern pattern5 = Pattern.compile("#X:\d+\s[A-Za-z]+-\d*");
int k=0;
int psum=0;
for(j=0;;j++)
{
sf[j]=sc.nextLine();
Matcher matcher1= pattern3.matcher(sf[j]);
if(matcher1.find())
{
psum++;
}
if(sf[j].equals("end"))
break;
}
Paper p[]=new Paper[psum];
for(int i=0;i<psum;i++)
p[i]=new Paper();
int stulenth=0;
for(int i=0;;i++)
{
Matcher ma=pattern5.matcher(sf[i]);
if(ma.find())
{
String stulist[]=sf[i].split("-");
stulenth=stulist.length;
}
if(sf[i].equals("end"))
break;
}
Pattern pattern7=Pattern.compile("#D:N-(\d+)");
int deleteproblem=0;
int ds=-1;//删除信息的位置
for(int i=0;;i++)
{
if(sf[i].equals("end"))
break;
Matcher mi=pattern7.matcher(sf[i]);
if(mi.find())
{
ds=i;
deleteproblem=Integer.parseInt(mi.group(1));
}
}
Student student[]=new Student[stulenth];
Pattern pattern6 = Pattern.compile("#X:(\\d+)");
for(int i=0;;i++)
{
String sid="";
Matcher ma=pattern5.matcher(sf[i]);
if(ma.find())
{
String stulist[]=sf[i].split("-");
String stu1[]=stulist[0].split(" ");
Matcher ma2=pattern6.matcher(stu1[0]);
if(ma2.find()){
sid=ma2.group(1);
}
String name=stu1[1];
student[0]=new Student(sid,name);
for(j=1;j<stulenth;j++)
{
String stu2[]=stulist[j].split(" ");
String sid1=stu2[0];
String name1=stu2[1];
student[j]=new Student(sid1,name1);
}
}
if(sf[i].equals("end"))
break;
}
for(int i=0;;i++) {
Matcher matcher2= pattern3.matcher(sf[i]);
if(matcher2.find()){
String str[]=sf[i].split(" ");
int q=Integer.parseInt(matcher2.group(1).trim());
nump=str.length-1;
p[q-1].setNump(nump);
}
if(sf[i].equals("end"))
break;
}
for(j=0;;j++)
{
Matcher matcher1= pattern2.matcher(sf[j]);
if(matcher1.find())
{
answerpaper.add(k++,sf[j]);
}
if(sf[j].equals("end"))
break;
}
int size=answerpaper.size();
Answer answer[]=new Answer[size];
for(int i=0;i<size;i++)
answer[i]=new Answer();
Pattern pattern1 = Pattern.compile("A:(\\d+)-(.*)");
Pattern pa=Pattern.compile("S:(.+)");
int b=0;
int c=0;int answernumber[]=null;
for(int i=0;i<size;i++)
{
String s[]=answerpaper.get(i).split("#");
for(int l=0;l<s.length;l++)
t[l]=s[l].trim();
c=s.length-2;
answernumber=new int[c];
int l=0;
for(j=2;j<c+2;j++)
{
Matcher m= pattern1.matcher(t[j]);
if(m.find())
{
answernumber[l]=Integer.parseInt(m.group(1).trim());l++;
}
}
}
int questionNumber[]=new int[10];
String questionContent[]=new String[10];
String standardAnswer[]=new String[10];
int grade[]=new int[10];
int mquestionNumber[]=new int[10];
String mquestionContent[]=new String[10];
String mstandardAnswer[]=new String[10];
int mgrade[]=new int[10];
int fquestionNumber[]=new int[10];
String fquestionContent[]=new String[10];
String fstandardAnswer[]=new String[10];
int fgrade[]=new int[10];
int num[]=new int[nump];
int g=0;
String regex = "#N:(\\d+)\\s+#Q:(.+)\\s+#A:(.+)";
Pattern po=Pattern.compile("#N:(\\d+).*");
Pattern pattern = Pattern.compile(regex);
Pattern pm=Pattern.compile("#Z:(\\d+)\\s+#Q:(.+)\\s+#A:(.+)");
Pattern pf=Pattern.compile("#K:(\\d+)\\s+#Q:(.+)\\s+#A:(.+)");
Pattern po2=Pattern.compile("#Z:(\\d+).*");
Pattern po3=Pattern.compile("#K:(\\d+).*");
for(int i=0;;i++)
{
Matcher matcher= pattern.matcher(sf[i]);
Matcher mo=po.matcher(sf[i]);
if(mo.find()&&matcher.find()==false)
{
notproblem.add(sf[i]);
int x=Integer.parseInt(mo.group(1).trim());
questionNumber[g]=x;
questionContent[g]="";
standardAnswer[g]="";
g++;
}
if(sf[i].equals("end"))
break;
}
for(int i=0;;i++)
{
Matcher matcher= pm.matcher(sf[i]);
Matcher mo=po2.matcher(sf[i]);
if(mo.find()&&matcher.find()==false)
{
notproblem.add(sf[i]);
int x=Integer.parseInt(mo.group(1).trim());
mquestionNumber[g]=x;
mquestionContent[g]="";
mstandardAnswer[g]="";
g++;
}
if(sf[i].equals("end"))
break;
}
for(int i=0;;i++)
{
Matcher matcher= pf.matcher(sf[i]);
Matcher mo=po3.matcher(sf[i]);
if(mo.find()&&matcher.find()==false)
{
notproblem.add(sf[i]);
int x=Integer.parseInt(mo.group(1).trim());
fquestionNumber[g]=x;
fquestionContent[g]="";
fstandardAnswer[g]="";
g++;
}
if(sf[i].equals("end"))
break;
}
for(int i=0;;i++)
{
Matcher matcher= pattern.matcher(sf[i]);
if(matcher.find()){
int x=Integer.parseInt(matcher.group(1).trim());
questionNumber[g]=x;
questionContent[g]= matcher.group(2).trim();
standardAnswer[g]= matcher.group(3).trim();
g++;
}
if(sf[i].equals("end"))
break;
}
for(int i=0;;i++)
{
Matcher mm= pm.matcher(sf[i]);
if(mm.find()){
int x=Integer.parseInt(mm.group(1).trim());
mquestionNumber[g]=x;
mquestionContent[g]= mm.group(2).trim();
mstandardAnswer[g]= mm.group(3).trim();
g++;
}
if(sf[i].equals("end"))
break;
}
for(int i=0;;i++)
{
Matcher mf= pf.matcher(sf[i]);
if(mf.find()){
int x=Integer.parseInt(mf.group(1).trim());
fquestionNumber[g]=x;
fquestionContent[g]= mf.group(2).trim();
fstandardAnswer[g]= mf.group(3).trim();
g++;
}
if(sf[i].equals("end"))
break;
}
Pattern pu=Pattern.compile(".*T[^a-zA-Z].*");
ArrayList<String> gradepaper=new ArrayList<String>();
int fg=0;
int ss=0;//试卷位置
ArrayList<String> paperexsist=new ArrayList<String>();
for(int i=0;;i++) {
Matcher mu=pu.matcher(sf[i]);
if(mu.find())
{
gradepaper.add(sf[i]);
}
if(sf[i].equals("end"))
break;
}
for(int i=0;i<gradepaper.size();i++) {
Matcher matcher2= pattern3.matcher(gradepaper.get(i));
if(matcher2.find()==false)
{
fg=1;
paperexsist.add(gradepaper.get(i));
}
}
if(fg==0){
int existproblem=0;
for(int i=0;;i++) {
Matcher matcher2= pattern3.matcher(sf[i]);
Matcher mu=pu.matcher(sf[i]);
int sum=0;
if(matcher2.find()){
ss=i;
String str[]=sf[i].split(" ");
int y=Integer.parseInt(matcher2.group(1).trim());
for( j=1;j<str.length;j++)
{
Matcher matcher3= pattern4.matcher(str[j]);
if(matcher3.find())
{
num[j-1]=Integer.parseInt(matcher3.group(1).trim());
int index=findIndex(questionNumber,num[j-1]);
int index1=findIndex(mquestionNumber,num[j-1]);
int index2=findIndex(fquestionNumber,num[j-1]);
if(index!=-1)
{
if(ds!=-1&&ds<ss)
grade[9]=0;
else
grade[index]=Integer.parseInt(matcher3.group(2).trim());
sum=sum+grade[index];
}
else if(index1!=-1) {
if(ds!=-1&&ds<ss)
grade[9]=0;
else
mgrade[index1]=Integer.parseInt(matcher3.group(2).trim());
sum=sum+mgrade[index1];
}
else if(index2!=-1) {
if(ds!=-1&&ds<ss)
grade[9]=0;
else
fgrade[index2]=Integer.parseInt(matcher3.group(2).trim());
sum=sum+fgrade[index2];
}
else
{
grade[9]=0;
sum=sum+grade[9];
}
}
}
p[y-1].setallGrade(sum);
int a=0;
int x=0;
int f=0;
for(int u=0;u<str.length-1;u++)
{
int index1=-1;
int index=findIndex(questionNumber,num[u]);
int index2=findIndex(mquestionNumber,num[u]);
int index3=findIndex(fquestionNumber,num[u]);
if(u+1<=answernumber.length)
{
index1=findIndex(num,answernumber[u]);
}
if(index!=-1&&index1!=-1)
{
if(ds<ss&&questionNumber[index]==deleteproblem)
{
p[y-1].addproblem(a+1,new Problem(questionNumber[index],"",standardAnswer[index],grade[index]));
}
else{
p[y-1].addproblem(a+1,new Problem(questionNumber[index],questionContent[index],standardAnswer[index],grade[index]));
}
a++;
}
if(index2!=-1&&index1!=-1)
{
if(ds<ss&&mquestionNumber[index2]==deleteproblem)
{
p[y-1].addmproblem(x+1,new Multiplequestion(mquestionNumber[index2],"",mstandardAnswer[index2],mgrade[index2]));
}
else{
p[y-1].addmproblem(x+1,new Multiplequestion(mquestionNumber[index2],mquestionContent[index2],mstandardAnswer[index2],mgrade[index2]));
}
x++;
}
if(index3!=-1&&index1!=-1)
{
if(ds<ss&&fquestionNumber[index3]==deleteproblem)
{
p[y-1].addfproblem(f+1,new Fillblankquestions(fquestionNumber[index3],"",fstandardAnswer[index3],fgrade[index3]));
}
else{
p[y-1].addfproblem(f+1,new Fillblankquestions(fquestionNumber[index3],fquestionContent[index3],fstandardAnswer[index3],fgrade[index3]));
}
f++;
}
if(index==-1&&index1==-1)
{
p[y-1].addproblem(a+1,new Problem(2,"","",0));
a++;
break;
}
if(index2==-1&&index1==-1)
{
p[y-1].addmproblem(x+1,new Multiplequestion(2,"","",0));
x++;
break;
}
if(index3==-1&&index1==-1)
{
p[y-1].addfproblem(f+1,new Fillblankquestions(2,"","",0));
f++;
break;
}
if(index==-1&&index2==-1&&index3==-1)
{
if(str.length-1==answernumber.length)
existproblem=1;
}
}
}
if(sf[i].equals("end"))
break;
}
if(ds<ss)
{
deleteproblem=0;
}
String id="";
for(int i=0;i<size;i++)
{
String s[]=answerpaper.get(i).split("#");
for(int l=0;l<s.length;l++)
t[l]=s[l].trim();
c=s.length-2;
String lo[]=t[1].split(" ");
id=lo[1];
if(c==0)
{
String printfanswer[]=new String[nump];
Matcher o=pa.matcher(lo[0]);
if(o.find())
{
b=Integer.parseInt(o.group(1).trim());
}
answer[i].setAnswernum(b);
answer[i].setSid(id);
answernumber=new int[nump];
if(answer[i].getAnswernum()<=p.length)
answer[i].setPaper(p[b-1]);
else
answer[i].setPaper(p[p.length-1]);
for(j=0;j<nump;j++)
{
printfanswer[j]="";
}
int y=0;
for(int u=0;u<c;u++)
{
int index=-1;
for(int v=0;v<answer[i].getPaper().getproblemlist().size();v++)
{
if(answer[i].getPaper().getproblemlist().get(v).getPnum()==answernumber[u])
{
index=u;
}
}
for(int v=0;v<answer[i].getPaper().getmquestionlist().size();v++)
{
if(answer[i].getPaper().getmquestionlist().get(v).getPnum()==answernumber[u])
{
index=u;
}
}
for(int v=0;v<answer[i].getPaper().getfquestionlist().size();v++)
{
if(answer[i].getPaper().getfquestionlist().get(v).getPnum()==answernumber[u])
{
index=u;
}
}
if(index!=-1)
{
answer[i].addanswer(y+1,printfanswer[index]);
y++;
}
}
for(int z=0;z<stulenth;z++)
{
if(student[z].getSid().equals(lo[1]))
{
student[z].setAnswer(answer[i]);
}
}
}
else {
String printfanswer[]=new String[c];
Matcher o=pa.matcher(lo[0]);
if(o.find())
{
b=Integer.parseInt(o.group(1).trim());
}
answer[i].setAnswernum(b);
answer[i].setSid(id);
if(answer[i].getAnswernum()<=p.length)
answer[i].setPaper(p[b-1]);
else
answer[i].setPaper(p[p.length-1]);
for(j=2;j<c+2;j++)
{
Matcher m= pattern1.matcher(t[j]);
if(m.find())
{
printfanswer[j-2]=m.group(2).trim();
}
}
int y=0;
for(int u=0;u<c;u++)
{
int index=-1;
for(int v=0;v<answer[i].getPaper().getproblemlist().size();v++)
{
if(answer[i].getPaper().getproblemlist().get(v).getPnum()==answernumber[u])
{
index=answernumber[u]-1;
}
}
for(int v=0;v<answer[i].getPaper().getmquestionlist().size();v++)
{
if(answer[i].getPaper().getmquestionlist().get(v).getPnum()==answernumber[u])
{
index=answernumber[u]-1;
}
}
for(int v=0;v<answer[i].getPaper().getfquestionlist().size();v++)
{
if(answer[i].getPaper().getfquestionlist().get(v).getPnum()==answernumber[u])
{
index=answernumber[u]-1;
}
}
if(index!=-1)
{
answer[i].addanswer(y+1,printfanswer[index]);
y++;
}
}
for(int z=0;z<stulenth;z++)
{
if(student[z].getSid().equals(lo[1]))
{
student[z].setAnswer(answer[i]);
}
}
}
}
for(j=0;j<size;j++)
{
for(int i=0;i<answer[j].getPaper().getNump();i++)
{
if(i+1<=answernumber.length)
answer[j].ajudge(i+1,deleteproblem,existproblem,answernumber[i]);
else
answer[j].ajudge(i+1,deleteproblem,existproblem,0);
}
}
for(int i=1;i<=psum;i++)
{
if(notproblem.size()!=0)
{
for(String pro:notproblem)
System.out.println("wrong format:"+pro);
}
if(p[i-1].getallGrade()!=100)
System.out.println("alert: full score of test paper"+i+" is not 100 points");
}
int flag=0;
for(int l=0;l<student.length;l++)
{
int x=1;
for(int n=0;n<student[l].getAnswerlist().size();n++)
{
if(student[l].getAnswerlist().get(n).getAnswernum()>p.length)
System.out.println("The test paper number does not exist");
else {
if(student[l].getAnswerlist().get(n).getAnswernum()==x)
{
student[l].getAnswerlist().get(n).printf1(student[l].getAnswerlist().get(n).getPaper().getNump(),deleteproblem,existproblem,answernumber[n]);
for(j=0;j<student.length;j++)
{
if(student[j].getSid().equals(student[l].getAnswerlist().get(n).getSid()))
flag=1;
}
if(flag==1)
{
student[l].show();
student[l].getAnswerlist().get(n).printf2(student[l].getAnswerlist().get(n).getPaper().getNump());
}
else
System.out.println(id+" not found");
x++;
n=-1;
}
}
if(x-1==student[l].getAnswerlist().size())
{
break;
}
}
}
}
else
{
for(String pro:paperexsist)
{
System.out.println("wrong format:"+pro);
System.out.println("The test paper number does not exist");
}
}
}
public static int findIndex(int array[],int target){
for (int i=0;i<array.length;i++){
if (array[i]==target){
return i;
}
}
return -1;
}
}
从Main类中可以看出运用了大量的正则表达式和for循环,和大量的判断变量,这就导致在判断题目是否合法方面很难完成,这也是上次的题集留下的毛病很难更改 。总的来说代码的复用性任然很差,每一个新的点都极难更改,要改上很多。
2.家居强电电路模拟程序-1
2.1 从类图可以看出设计了一个Device抽象类,所有的设备都继承它,包括控制设备“Switch(开关)、BinningGovernor(分档调速器)、ContinuousGovernor(连续调速器)”,受控设备“Light(灯),Fan(风扇)”,而灯还分为fluorescent(日关灯)、Incandescent(白炽灯)两者都继承Light,风扇有Ceilinfan(吊扇)继承Fan,同时有一个受控设备类Controlleddevices,串联电路类Seriescircuit。串联电路类中包含一个ArrayList元素用于一条串联电路添加设备。
2.2 同时在每个设备类中都有一个show方法用于最后的输出信息。也有calculateOutputV方法用于计算输出电压,而每个受控设备类中都有一个calculateshow方法用于根据输出电压就算各自的属性。
2.3 Main类源代码如下:
public class Main {
public static void main(String[] args) {
Seriescircuit seriescircuits = new Seriescircuit();
ArrayList
Scanner sc = new Scanner(System.in);
String dl[] = new String[100];
Device device = null;
Pattern p1 = Pattern.compile("VCC\s(.\d)-(\d)");
Pattern p2 = Pattern.compile("(.\d)-(\d)\s(.\d)-(\d)");
Pattern p3 = Pattern.compile("(.\d)-(\d)\sGND");
Pattern p4 = Pattern.compile("#[A-Z]\d.");
Pattern p5 = Pattern.compile("#L\d:(\d+.?\d)");
Pattern p6 = Pattern.compile("#(K\d)");
Pattern p7 = Pattern.compile("#(F\d)\+");
Pattern p8 = Pattern.compile("#(F\d)-");
for (int i = 0;; i++) {
dl[i] = sc.nextLine();
Matcher m1 = p1.matcher(dl[i]);
Matcher m2 = p2.matcher(dl[i]);
Matcher m3 = p3.matcher(dl[i]);
Matcher m4 = p4.matcher(dl[i]);
if (dl[i].equals("end"))
break;
if (m1.find()) {
String id = m1.group(1);
if (id.charAt(0) == 'K')
device = new Switch(id, 220);
else if (id.charAt(0) == 'F')
device = new BinningGovernor(id, 220);
else if (id.charAt(0) == 'L')
device = new ContinuousGovernor(id, 220);
else if (id.charAt(0) == 'B')
device = new Incandescent(id, 0);
else if (id.charAt(0) == 'R')
device = new fluorescent(id, 0);
else
device = new Ceilinfan(id, 0);
seriescircuits.addDevice(device);
}
if (m2.find()) {
String id = m2.group(3);
if (id.charAt(0) == 'K')
device = new Switch(id, 220);
else if (id.charAt(0) == 'F')
device = new BinningGovernor(id, 220);
else if (id.charAt(0) == 'L')
device = new ContinuousGovernor(id, 220);
else if (id.charAt(0) == 'B')
device = new Incandescent(id, 0);
else if (id.charAt(0) == 'R')
device = new fluorescent(id, 0);
else
device = new Ceilinfan(id, 0);
seriescircuits.addDevice(device);
}
if (m3.find()) {
}
if (m4.find()) {
command.add(m4.group(0));
}
}
if (seriescircuits.getDevices().size() == 1) {
seriescircuits.getDevices().get(0).setOutputV(220);
seriescircuits.getDevices().get(0).calculateshow();
for (int i = 0; i < seriescircuits.getDevices().size(); i++) {
seriescircuits.getDevices().get(i).show();
}
} else {
if (command.size() != 0) {
Matcher m5 = p5.matcher(command.get(0));
Matcher m6 = p6.matcher(command.get(0));
Matcher m7 = p7.matcher(command.get(0));
Matcher m8 = p8.matcher(command.get(0));
if (m5.find()) {
((ContinuousGovernor) (seriescircuits.getDevices().get(0)))
.setGearparameter(Double.parseDouble(m5.group(1)));
seriescircuits.getDevices().get(0).calculateOutputV();
}
if (m6.find()) {
for (int i = 0; i < command.size(); i++) {
Matcher m11 = p6.matcher(command.get(i));
if (m11.find()) {
for (int j = 0; j < seriescircuits.getDevices().size(); j++) {
if (seriescircuits.getDevices().get(j).getId().equals(m11.group(1))) {
if (((Switch) (seriescircuits.getDevices().get(j))).getState() == 0)
((Switch) (seriescircuits.getDevices().get(j))).setState(1);
else
((Switch) (seriescircuits.getDevices().get(j))).setState(0);
seriescircuits.getDevices().get(j).calculateOutputV();
}
}
}
}
}
if (m7.find() || m8.find()) {
for (int i = 0; i < command.size(); i++) {
Matcher m9 = p7.matcher(command.get(i));
Matcher m10 = p8.matcher(command.get(i));
if (m9.find()) {
if (((BinningGovernor) (seriescircuits.getDevices().get(0))).getId().equals(m9.group(1)))
((BinningGovernor) (seriescircuits.getDevices().get(0))).addGear();
}
if (m10.find()) {
if (((BinningGovernor) (seriescircuits.getDevices().get(0))).getId().equals(m10.group(1)))
((BinningGovernor) (seriescircuits.getDevices().get(0))).subtractGear();
}
}
seriescircuits.getDevices().get(0).calculateOutputV();
}
}
if (seriescircuits.getDevices().size() > 2) {
int index = 0;
int flag = 0;
for (int i = 0; i < seriescircuits.getDevices().size(); i++) {
if (seriescircuits.getDevices().get(i) instanceof Controlleddevices)
index = i;
else {
if (((Switch) (seriescircuits.getDevices().get(i))).getState() == 0)
flag = 1;
}
}
if (flag == 0) {
seriescircuits.getDevices().get(index).setOutputV(220);
} else
seriescircuits.getDevices().get(index).setOutputV(0);
seriescircuits.getDevices().get(index).calculateshow();
for (int i = 0; i < seriescircuits.getDevices().size(); i++) {
if (i != index)
seriescircuits.getDevices().get(i).show();
}
seriescircuits.getDevices().get(index).show();
} else {
seriescircuits.getDevices().get(1).setOutputV(seriescircuits.getDevices().get(0).getOutputV());
seriescircuits.getDevices().get(1).calculateshow();
for (int i = 0; i < seriescircuits.getDevices().size(); i++) {
seriescircuits.getDevices().get(i).show();
}
}
}
}
}
2.3.1 首先是使用正则表达式对输入的语句进行判断,然后添加设备到串联电路的ArrayList中以及添加命令语句到command这一个ArrayList中,用于之后的判断。
2.3.2 接着就是对命令语句的遍历,然后根据命令对相应的设备进行输出电压计算,受控设备根据电压再进行属性计算。
2.3.3 最后就是输出所以设备的信息了,调用设备的show方法,进行输出。
3.家居强电电路模拟程序-2
3.1 从类图可以看出在家居强电电路模拟程序-2的基础上加上了FloorFan(落地扇)类,继承Fan类;Parallelcircuit(并联电路)类,继承Device类。同时Device类实现了Comparable接口,同时根据id重写了compaTo方法。
3.2 Parallelcircuit类中有个串联电路Seriescircuit类的ArrayList属性,用于对并联电路添加串联电路。
3.3 Main类源代码如下:
public class Main {
static ArrayList
public static void main(String[] args) {
ArrayList<String> command = new ArrayList<>();
ArrayList<Seriescircuit> seriescircuits = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String dl[] = new String[100];
Device device = null;
Pattern p4 = Pattern.compile("#(K|F|L)\\d([^\\[\\]])*");
Pattern p5 = Pattern.compile("#L\\d:(\\d+.?\\d*)");
Pattern p6 = Pattern.compile("#(K\\d)");
Pattern p7 = Pattern.compile("#(F\\d)\\+");
Pattern p8 = Pattern.compile("#(F\\d)-");
Pattern p9 = Pattern.compile("#T(\\d):\\[.*\\]");
Pattern p10 = Pattern.compile("#M(\\d):\\[.*\\]");
Pattern p11 = Pattern.compile("#(M\\d):\\[T(\\d)");
Pattern p12 = Pattern.compile("\\[*T(\\d)\\]*");
for (int i = 0;; i++) {
dl[i] = sc.nextLine();
if (dl[i].equals("end"))
break;
Matcher m12 = p9.matcher(dl[i]);
Matcher m13 = p10.matcher(dl[i]);
Matcher m4 = p4.matcher(dl[i]);
if (m12.find()) {
Seriescircuit seriescircuit = new Seriescircuit(Integer.parseInt(m12.group(1)));
Pattern pattern = Pattern.compile("\\[([^\\[\\]])*\\]");
Matcher matcher = pattern.matcher(dl[i]);
ArrayList<String> matches = new ArrayList<>();
while (matcher.find()) {
String match = matcher.group(0);
matches.add(match);
}
for (int j = 0; j < matches.size(); j++) {
adddevice(seriescircuit, matches.get(j));
}
seriescircuit.calculateallresistance();
seriescircuits.add(seriescircuit);
}
if (m13.find()) {
String s[] = dl[i].split(" ");
Parallelcircuit parallelcircuit = null;
for (int j = 0; j < s.length; j++) {
Matcher m1 = p11.matcher(s[j]);
Matcher m2 = p12.matcher(s[j]);
if (m1.find()) {
parallelcircuit = new Parallelcircuit(m1.group(1), 0);
for (int l = 0; l < seriescircuits.size(); l++) {
if (seriescircuits.get(l).getSnum() == Integer.parseInt(m1.group(2))) {
parallelcircuit.addseriescircuits(seriescircuits.get(l));
}
}
} else if (m2.find()) {
for (int l = 0; l < seriescircuits.size(); l++) {
if (seriescircuits.get(l).getSnum() == Integer.parseInt(m2.group(1))) {
parallelcircuit.addseriescircuits(seriescircuits.get(l));
}
}
} else {
}
}
parallelcircuit.calculateallresistance();
parallelcircuits.add(parallelcircuit);
}
if (m4.find()) {
command.add(m4.group(0));
}
}
int index = -1;// 找到主串联电路
for (int i = 0; i < seriescircuits.size(); i++) {
if (seriescircuits.get(i).getIsMain() == 1) {
index = i;
break;
}
}
int index1 = -1;// 找到主电路控制设备命令
int index3 = -1;// 判断主电路是否有开关
for (int k = 0; k < command.size(); k++) {
if (seriescircuits.get(index).getDevices().get(0) instanceof Controlleddevices) {
for (int j = 1; j < seriescircuits.get(index).getDevices().size(); j++) {
if (command.get(k).substring(1, 3).equals(seriescircuits.get(index).getDevices().get(j).getId())) {
Matcher m11 = p6.matcher(command.get(k));
if (m11.find()) {
if (seriescircuits.get(index).getDevices().get(j).getId().equals(m11.group(1))) {
index3 = 1;
if (((Switch) (seriescircuits.get(index).getDevices().get(j))).getState() == 0)
((Switch) (seriescircuits.get(index).getDevices().get(j))).setState(1);
else
((Switch) (seriescircuits.get(index).getDevices().get(j))).setState(0);
seriescircuits.get(index).getDevices().get(j).calculateOutputV();
if (((Switch) (seriescircuits.get(index).getDevices().get(j))).getState() == 1)
seriescircuits.get(index).setIsaccess(1);
else
seriescircuits.get(index).setIsaccess(0);
}
}
}
}
} else {
for (int m = 0; m < seriescircuits.get(index).getDevices().size(); m++) {
if (command.get(k).substring(1, 3).equals(seriescircuits.get(index).getDevices().get(m).getId())) {
index1 = k;
Matcher m5 = p5.matcher(command.get(k));
Matcher m6 = p6.matcher(command.get(k));
Matcher m7 = p7.matcher(command.get(k));
Matcher m8 = p8.matcher(command.get(k));
if (m5.find()) {
((ContinuousGovernor) (seriescircuits.get(index).getDevices().get(0)))
.setGearparameter(Double.parseDouble(m5.group(1)));
seriescircuits.get(index).getDevices().get(0).calculateOutputV();
}
if (m6.find()) {
for (int j = 0; j < seriescircuits.get(index).getDevices().size(); j++) {
if (seriescircuits.get(index).getDevices().get(j).getId().equals(m6.group(1))) {
if (((Switch) (seriescircuits.get(index).getDevices().get(j))).getState() == 0)
((Switch) (seriescircuits.get(index).getDevices().get(j))).setState(1);
else
((Switch) (seriescircuits.get(index).getDevices().get(j))).setState(0);
seriescircuits.get(index).getDevices().get(j).calculateOutputV();
}
}
}
if (m7.find() || m8.find()) {
Matcher m9 = p7.matcher(command.get(k));
Matcher m10 = p8.matcher(command.get(k));
if (m9.find()) {
if (((BinningGovernor) (seriescircuits.get(index).getDevices().get(0))).getId()
.equals(m9.group(1)))
((BinningGovernor) (seriescircuits.get(index).getDevices().get(0))).addGear();
}
if (m10.find()) {
if (((BinningGovernor) (seriescircuits.get(index).getDevices().get(0))).getId()
.equals(m10.group(1)))
((BinningGovernor) (seriescircuits.get(index).getDevices().get(0))).subtractGear();
}
seriescircuits.get(index).getDevices().get(0).calculateOutputV();
}
if (seriescircuits.get(index).getDevices().get(m).getOutputV() != 0) {
seriescircuits.get(index).setIsaccess(1);
} else
seriescircuits.get(index).setIsaccess(0);
}
}
}
}
if (seriescircuits.get(index).getDevices().get(0) instanceof Controlleddevices) {
seriescircuits.get(index).getDevices().get(0)
.setOutputV(seriescircuits.get(index).getDevices().get(0).getInputV());
if (index3 != 1)
seriescircuits.get(index).setIsaccess(1);
}
for (int k = 0; k < command.size(); k++) {
if (k != index1) {
Matcher m11 = p6.matcher(command.get(k));
if (m11.find()) {
if (parallelcircuits.size() != 0) {
for (int p = 0; p < parallelcircuits.get(0).getSeriescircuits().size(); p++) {
for (int j = 0; j < parallelcircuits.get(0).getSeriescircuits().get(p).getDevices()
.size(); j++) {
if (parallelcircuits.get(0).getSeriescircuits().get(p).getDevices().get(j).getId()
.equals(m11.group(1))) {
if (((Switch) (parallelcircuits.get(0).getSeriescircuits().get(p).getDevices()
.get(j))).getState() == 0)
((Switch) (parallelcircuits.get(0).getSeriescircuits().get(p).getDevices()
.get(j))).setState(1);
else
((Switch) (parallelcircuits.get(0).getSeriescircuits().get(p).getDevices()
.get(j))).setState(0);
if (((Switch) (parallelcircuits.get(0).getSeriescircuits().get(p).getDevices()
.get(j))).getState() == 1) {
parallelcircuits.get(0).getSeriescircuits().get(p).setIsaccess(1);
} else
parallelcircuits.get(0).getSeriescircuits().get(p).setIsaccess(0);
}
}
}
}
}
}
}
int index2 = 0;// 用于判断串联电路是否含有控制设备
int isaccess = 0;
double r = 0;
int index5=-1;
for (int i = 0; i < seriescircuits.get(index).getDevices().size(); i++) {
if (seriescircuits.get(index).getDevices().get(i) instanceof Switch) {
if (((Switch) (seriescircuits.get(index).getDevices().get(i))).getState() == 0)
index5=1;
}
}
if(index5==1)
seriescircuits.get(index).setIsaccess(0);
if (parallelcircuits.size() != 0) {
for (int p = 0; p < parallelcircuits.get(0).getSeriescircuits().size(); p++) {
index2 = 0;
for (int j = 0; j < parallelcircuits.get(0).getSeriescircuits().get(p).getDevices().size(); j++) {
if (parallelcircuits.get(0).getSeriescircuits().get(p).getDevices().get(j) instanceof Switch) {
index2 = 1;
}
}
if (index2 == 0) {
parallelcircuits.get(0).getSeriescircuits().get(p).setIsaccess(1);
}
}
parallelcircuits.get(0).calculateallresistance();
seriescircuits.get(index).calculateallresistance();
r = parallelcircuits.get(0).getAllresistance() / seriescircuits.get(index).getAllresistance();
if(index5==1&¶llelcircuits.size()!=0) {
seriescircuits.get(index).setIsaccess(0);
}
for (int i = 0; i < parallelcircuits.get(0).getSeriescircuits().size(); i++) {
if (parallelcircuits.get(0).getSeriescircuits().get(i).getIsaccess() == 1) {
if (index5==-1) {
seriescircuits.get(index).setIsaccess(1);
for (int j = 0; j < parallelcircuits.get(0).getSeriescircuits().get(i).getDevices()
.size(); j++) {
if (parallelcircuits.get(0).getSeriescircuits().get(i).getDevices()
.get(j) instanceof Controlleddevices) {
parallelcircuits.get(0).getSeriescircuits().get(i).getDevices().get(j)
.setOutputV(seriescircuits.get(index).getDevices().get(0).getOutputV() * r*(parallelcircuits.get(0).getSeriescircuits().get(i).getDevices().get(j).resistance/parallelcircuits.get(0).getSeriescircuits().get(i).getAllresistance()));
parallelcircuits.get(0).getSeriescircuits().get(i).getDevices().get(j).calculateshow();
}
}
}
}
}
} else {
seriescircuits.get(index).calculateallresistance();
}
if (seriescircuits.get(index).getIsaccess() == 1)
isaccess = 1;
else
isaccess = 0;
double v = seriescircuits.get(index).getDevices().get(0).getOutputV();
if (isaccess == 1) {
for (int i = 0; i < seriescircuits.get(index).getDevices().size(); i++) {
if (seriescircuits.get(index).getDevices().get(i) instanceof Controlleddevices) {
r = seriescircuits.get(index).getDevices().get(i).resistance
/ seriescircuits.get(index).getAllresistance();
seriescircuits.get(index).getDevices().get(i).setOutputV(v * r);
seriescircuits.get(index).getDevices().get(i).calculateshow();
}
}
} else {
for (int i = 0; i < seriescircuits.get(index).getDevices().size(); i++) {
if (seriescircuits.get(index).getDevices().get(i) instanceof Controlleddevices) {
seriescircuits.get(index).getDevices().get(i).setOutputV(0);
seriescircuits.get(index).getDevices().get(i).calculateshow();
}
}
}
ArrayList<Switch> switchs = new ArrayList<>();
ArrayList<BinningGovernor> binningGovernors = new ArrayList<>();
ArrayList<ContinuousGovernor> continuousGovernors = new ArrayList<>();
ArrayList<Incandescent> incandescents = new ArrayList<>();
ArrayList<fluorescent> fluorescents = new ArrayList<>();
ArrayList<Ceilinfan> ceilinfans = new ArrayList<>();
ArrayList<FloorFan> floorFans = new ArrayList<>();
for (int i = 0; i < seriescircuits.get(index).getDevices().size(); i++) {
if (seriescircuits.get(index).getDevices().get(i) instanceof Parallelcircuit) {
for (int j = 0; j < ((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i)))
.getSeriescircuits().size(); j++) {
for (int k = 0; k < ((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i)))
.getSeriescircuits().get(j).getDevices().size(); k++) {
if (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i))).getSeriescircuits()
.get(j).getDevices().get(k) instanceof Switch)
switchs.add((Switch) ((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i)))
.getSeriescircuits().get(j).getDevices().get(k));
else if (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i))).getSeriescircuits()
.get(j).getDevices().get(k) instanceof BinningGovernor)
binningGovernors.add((BinningGovernor) (((Parallelcircuit) (seriescircuits.get(index)
.getDevices().get(i))).getSeriescircuits().get(j).getDevices().get(k)));
else if (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i))).getSeriescircuits()
.get(j).getDevices().get(k) instanceof ContinuousGovernor)
continuousGovernors.add((ContinuousGovernor) (((Parallelcircuit) (seriescircuits.get(index)
.getDevices().get(i))).getSeriescircuits().get(j).getDevices().get(k)));
else if (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i))).getSeriescircuits()
.get(j).getDevices().get(k) instanceof Incandescent)
incandescents.add(
(Incandescent) (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i)))
.getSeriescircuits().get(j).getDevices().get(k)));
else if (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i))).getSeriescircuits()
.get(j).getDevices().get(k) instanceof fluorescent)
fluorescents.add(
(fluorescent) (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i)))
.getSeriescircuits().get(j).getDevices().get(k)));
else if (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i))).getSeriescircuits()
.get(j).getDevices().get(k) instanceof Ceilinfan)
ceilinfans.add(
(Ceilinfan) (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i)))
.getSeriescircuits().get(j).getDevices().get(k)));
else
floorFans
.add((FloorFan) (((Parallelcircuit) (seriescircuits.get(index).getDevices().get(i)))
.getSeriescircuits().get(j).getDevices().get(k)));
}
}
} else {
if (seriescircuits.get(index).getDevices().get(i) instanceof Switch)
switchs.add((Switch) (seriescircuits.get(index).getDevices().get(i)));
else if (seriescircuits.get(index).getDevices().get(i) instanceof BinningGovernor)
binningGovernors.add((BinningGovernor) (seriescircuits.get(index).getDevices().get(i)));
else if (seriescircuits.get(index).getDevices().get(i) instanceof ContinuousGovernor)
continuousGovernors.add((ContinuousGovernor) (seriescircuits.get(index).getDevices().get(i)));
else if (seriescircuits.get(index).getDevices().get(i) instanceof Incandescent)
incandescents.add((Incandescent) (seriescircuits.get(index).getDevices().get(i)));
else if (seriescircuits.get(index).getDevices().get(i) instanceof fluorescent)
fluorescents.add((fluorescent) (seriescircuits.get(index).getDevices().get(i)));
else if (seriescircuits.get(index).getDevices().get(i) instanceof Ceilinfan)
ceilinfans.add((Ceilinfan) (seriescircuits.get(index).getDevices().get(i)));
else
floorFans.add((FloorFan) (seriescircuits.get(index).getDevices().get(i)));
}
}
Collections.sort(switchs);
Collections.sort(binningGovernors);
Collections.sort(continuousGovernors);
Collections.sort(incandescents);
Collections.sort(fluorescents);
Collections.sort(ceilinfans);
Collections.sort(floorFans);
for (int i = 0; i < switchs.size(); i++) {
switchs.get(i).show();
}
for (int i = 0; i < binningGovernors.size(); i++) {
binningGovernors.get(i).show();
}
for (int i = 0; i < continuousGovernors.size(); i++) {
continuousGovernors.get(i).show();
}
for (int i = 0; i < incandescents.size(); i++) {
incandescents.get(i).show();
}
for (int i = 0; i < fluorescents.size(); i++) {
fluorescents.get(i).show();
}
for (int i = 0; i < ceilinfans.size(); i++) {
ceilinfans.get(i).show();
}
for (int i = 0; i < floorFans.size(); i++) {
floorFans.get(i).show();
}
}
static void adddevice(Seriescircuit seriescircuit, String str) {
Pattern p1 = Pattern.compile("VCC\\s(.\\d)-(\\d)");
Pattern p4 = Pattern.compile("VCC\\s(M\\d)-IN");
Pattern p2 = Pattern.compile("(.\\d)-(\\d)\\s(.\\d)-(\\d)");
Pattern p11 = Pattern.compile("IN\\s(.\\d)-(\\d)");
Pattern p12 = Pattern.compile("(M\\d)-OUT\\s(.\\d)-(1)");
Pattern p3 = Pattern.compile("(.\\d)-2\\s(M\\d)-IN");
Matcher m1 = p1.matcher(str);
Matcher m2 = p2.matcher(str);
Matcher m3 = p11.matcher(str);
Matcher m4 = p12.matcher(str);
Matcher m5 = p3.matcher(str);
Matcher m6 = p4.matcher(str);
Device device = null;
if (m1.find()) {
seriescircuit.setIsMain(1);
String id = m1.group(1);
if (id.charAt(0) == 'K')
device = new Switch(id, 220);
else if (id.charAt(0) == 'F')
device = new BinningGovernor(id, 220);
else if (id.charAt(0) == 'L')
device = new ContinuousGovernor(id, 220);
else if (id.charAt(0) == 'B')
device = new Incandescent(id, 220);
else if (id.charAt(0) == 'R')
device = new fluorescent(id, 220);
else if (id.charAt(0) == 'A')
device = new FloorFan(id, 220);
else
device = new Ceilinfan(id, 220);
seriescircuit.addDevice(device);
}
if (m2.find()) {
String id = m2.group(3);
if (id.charAt(0) == 'K')
device = new Switch(id, 220);
else if (id.charAt(0) == 'F')
device = new BinningGovernor(id, 220);
else if (id.charAt(0) == 'L')
device = new ContinuousGovernor(id, 220);
else if (id.charAt(0) == 'B')
device = new Incandescent(id, 0);
else if (id.charAt(0) == 'R')
device = new fluorescent(id, 0);
else if (id.charAt(0) == 'A')
device = new FloorFan(id, 0);
else
device = new Ceilinfan(id, 0);
seriescircuit.addDevice(device);
}
if (m3.find()) {
String id = m3.group(1);
if (id.charAt(0) == 'K')
device = new Switch(id, 0);
else if (id.charAt(0) == 'F')
device = new BinningGovernor(id, 220);
else if (id.charAt(0) == 'L')
device = new ContinuousGovernor(id, 220);
else if (id.charAt(0) == 'B')
device = new Incandescent(id, 0);
else if (id.charAt(0) == 'R')
device = new fluorescent(id, 0);
else if (id.charAt(0) == 'A')
device = new FloorFan(id, 0);
else
device = new Ceilinfan(id, 0);
seriescircuit.addDevice(device);
}
if (m5.find()) {
for (int i = 0; i < parallelcircuits.size(); i++) {
if (parallelcircuits.get(i).getId().equals(m5.group(2)))
device = parallelcircuits.get(i);
}
seriescircuit.addDevice(device);
}
if (m4.find()) {
String id = m4.group(2);
if (id.charAt(0) == 'K')
device = new Switch(id, 220);
else if (id.charAt(0) == 'F')
device = new BinningGovernor(id, 220);
else if (id.charAt(0) == 'L')
device = new ContinuousGovernor(id, 220);
else if (id.charAt(0) == 'B')
device = new Incandescent(id, 0);
else if (id.charAt(0) == 'R')
device = new fluorescent(id, 0);
else if (id.charAt(0) == 'A')
device = new FloorFan(id, 0);
else
device = new Ceilinfan(id, 0);
seriescircuit.addDevice(device);
}
if(m6.find()) {
seriescircuit.setIsMain(1);
for (int i = 0; i < parallelcircuits.size(); i++) {
if (parallelcircuits.get(i).getId().equals(m6.group(1)))
device = parallelcircuits.get(i);
device.setOutputV(220);
}
seriescircuit.addDevice(device);
}
}// 用于串联电路添加设备
}
3.3.1 由于这次的输入格式和上次的不太一样,因此首先根据输入的每一行前面的字母用正则表达式来进行识别存入串联电路,并联电路,命令。
3.3.2 可以看到定义了一个adddevice静态方法,用于串联电路添加设备。
3.3.3 下面就是循环command数组的同时循环主电路的设备进行计算电压以及属性。
3.3.4 最后就是show了,这里定义了七个ArrayList,分别对应每一个设备,然后将对应设备存入数组中,调用Collections.sort()方法对每个数组排序,最后按顺序show就OK了。
三、踩坑心得
- 答题判题程序-4
这次的多选题的设定,在起初的时候只是通过整体进行比较,比如答案是A B C D,而输入A C,将两者字符串进行比较,这样造成只有正确和错误,没有对一半的情况,导致很多点没有过,后面还是通过调试才发现了这里的错误,耽误了很多时间,这就是刚开始就没有考虑全面造成的。
2.家居强电电路模拟程序-1
这次中有个控制器是分档调速器,有个升档和降档的命令,起初没有考虑超过最高档位的因素,导致一直有几个点没过去,是通过测试了很多个测试点才发现的,也是刚开始没有考虑全面。
3.家居强电电路模拟程序-2
这次没有给测试点提示,也说明了考虑没全面的大问题,期中一个主要的考虑不全面就是忽视了并联电路的IN可以直接接在电源正极上,只考虑了样例给的情况就是接在调速器的后面,这里是在添加设备的正则表达式中少了一部分。
四、改进建议
1.答题判题程序-4
class Multiplequestion extends Problem{
public Multiplequestion(int pnum, String content, String standardAnswer, int grade) {
super(pnum, content, standardAnswer, grade);
}
String s[]=getStandardAnswer().split(" ");
public String[] getS()
{
return s;
}
}
这里在多选类中将标准答案全部拆开
for(int i=0;i<mquestionlist.size();i++)
{
if(nummquestionlist.get(i).getPnum())
{
String t[]=answer.split(" ");
int tem[]=new int[t.length];
int falg=0;
int falg1=0;
for(int k=0;k<t.length;k++)
{
for(int p=0;p<mquestionlist.get(i).getS().length;p++)
{
if(t[k].equals(mquestionlist.get(i).getS()[p]))
tem[k]=1;
}
}
for(int k=0;k<t.length;k++)
{
if(tem[k]1)
falg++;
if(tem[k]0)
falg1++;
}
if(falg10&&falgmquestionlist.get(i).getS().length)
return 1;
else if(falg10&&falg<mquestionlist.get(i).getS().length)
return 2;
else
return 0;
}
}
然后将输入的答案拆开同时进行比较每一个是否标准答案中含有,这样就解决了多选问题。
2.家居强电电路模拟程序-1
这里升档和降档进行了判断,就解决了超出设定档位的问题
3.家居强电电路模拟程序-2
Pattern p4 = Pattern.compile("VCC\s(M\d)-IN");
Matcher m6 = p4.matcher(str);
if(m6.find()) {
seriescircuit.setIsMain(1);
for (int i = 0; i < parallelcircuits.size(); i++) {
if (parallelcircuits.get(i).getId().equals(m6.group(1)))
device = parallelcircuits.get(i);
device.setOutputV(220);
}
seriescircuit.addDevice(device);
}
在原来的adddevice方法中加入了这些,解决了并联电路的IN接在电源的问题。
五、总结
1.答题判题程序-4由于在前面的三次设计中就没有设计的很好,这一次还是延续了上次的大部分,也是造成了很多问题,因此在每次题集的首次就应该设计的比较全面这样才方便以后得迭代。
2.从家居强电电路模拟程序-1开始类的设计较为全面以及抽象类的使用比较到位,比起答题判题程序的设计轻松了许多,也可以说是在前面的得到了锻炼,学到了很多。
3.总的来说每一次的题集都是在一步一步的提升,学习更多的类设计原则,简化程序等内容。