7-8次PTA总结

后两次PTA总结

首先来看看第七次:

第七次相比于之前,添加了互斥开关元器件而且引入了并联互串等等接法,按照我之前的设计,作出改动不算太难,我之前的递归已经可以按照顺序储存所以的元器件到一起去了,主要还是归功于将串并联电路继承自元器件的方式十分有效,这样就能够将串并联电路当作元器件一起处理,再按照递归储存下来就可以了

新增加的变化:

加入互斥开关类,并在电子元件工厂构造添加创建内容,在输入的时候给不同支路互斥开关不同的状态,通电的时候再根据互斥开关的状态决定是否导通某条路即可,其他代码都没变,总体来说改动不大类图和之前基本一致

主要是第八次

第八次相比之前要求我们输出每个引脚的电压,而我之前一直是根据递归根据元器件的电阻分压的,这无疑加大了很多要求

我一直在想如何不动原来代码的基础上去实现最新的功能,刚开始想set电压,后来发现情况实在是太多了,于是我想到了一个方法
给断开的开关,反向的二极管设置一个很大的电阻,多大呢,有1e9大,而那些接通的开关等等原本电阻为零,后面改成了一个很小的电阻,这样一来就可以遍历设置电压差的时候顺便计算每个用电器管脚的电压,缺点是会有一点点精度问题(差一点得满分也是因为这个)
看看类图


源码:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
interface CircuitComponent{
    double getR();
    void calculateOutputVoltage(double inputVoltage);
}
abstract class Circuit_Component implements CircuitComponent{
    double r;
    double rH_Cut;
    String name;
    Bank bank;
    int hway;
    int hfront;
    double inputVoltage;
    double outputVoltage;
    double Voltagedifference;
    double current;
    double limiteCurrent;
    double h1_V;
    double middle;

    public double getLimiteCurrent() {
        return limiteCurrent;
    }

    public void setCurrent(double current){
        this.current = current;
    }
    public double getCurrent(){
        return current;
    }
    public void setHfront(int hfront) {
        this.hfront = hfront;
    }
    public int getHfront() {
        return hfront;
    }
    public void setVoltagedifference(double voltagedifference) {
        Voltagedifference = voltagedifference;
    }
    public double getVoltagedifference() {
        return Voltagedifference;
    }
    public void setInputVoltage(double inputVoltage) {
        this.inputVoltage = inputVoltage;
    }

    public void setOutputVoltage(double outputVoltage) {
        this.outputVoltage = outputVoltage;
    }
    public double getOutputVoltage() {
        return outputVoltage;
    }
    public double getInputVoltage(){
        return inputVoltage;
    }
    public Circuit_Component(String name) {
        this.name=name;
    }
    public void regulation(String regulation) {}
    public double getValue() {
        return 0;
    }
    public void setBank(Bank bank) {
        this.bank=bank;
    }
    public abstract void  calculateOutputVoltage(double inputVoltage);
    public  void operate(double voltage) {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setTotalLux(T t){}
    public double  getR() {
        return this.r;
    }
    public void setR(double r) {
        this.r=r;
    }
    public  int getHway(){return this.hway;}
    public  void setHway(int hway){
        this.hway=hway;
    }
    public double getrH_Cut(){
        return this.rH_Cut;
    }
    public void setrH_Cut(double r){
        this.rH_Cut=r;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null||getClass() != obj.getClass()) {
            return false;
        }
        Circuit_Component other = (Circuit_Component) obj;
        return name != null ? name.equals(other.name):other.name == null;
    }
    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

    public double getH1_V() {
        return h1_V;
    }

    public void setH1_V(double h1_V) {
        this.h1_V = h1_V;
    }

    public double getMiddle() {
        return middle;
    }

    public void setMiddle(double middle) {
        this.middle = middle;
    }
}
class PowerSupply extends Circuit_Component{
    public PowerSupply(String name) {
        super(name);
        r=0;
    }
    private static final int INPUT_VOLTAGE = 220;
    @Override
    public void calculateOutputVoltage(double inputVoltage) {
        setInputVoltage(INPUT_VOLTAGE);
        setOutputVoltage(INPUT_VOLTAGE);
    }
}
class Ground extends Circuit_Component {
    public Ground(String name) {
        super(name);
    }
    @Override
    public void calculateOutputVoltage(double inputVoltage) {
        setInputVoltage(0);
    }
}
class Switch extends Circuit_Component{
    private boolean status=false;
    public Switch(String component_Input) {
        super(component_Input);
        this.r=0.000000001;
        this.limiteCurrent=20;
    }
    @Override
    public void regulation(String regulation) {
        this.status=!status;
    }
    @Override
    public  double getValue(){
        if (status)return 1;
        else return 0;
    }
    @Override
    public void calculateOutputVoltage(double inputVoltage) {
        setInputVoltage(inputVoltage);
        if(this.status){
            setOutputVoltage(inputVoltage);
        }
        else {
            setOutputVoltage(0);
        }
    }
}
class P extends Circuit_Component{
    private boolean status=false;
    public P(String component_Input) {
        super(component_Input);
        this.r=0.000000001;
        this.limiteCurrent=10;
    }
    public void setR(){
        if(this.hway==1){
            this.r=0.000000001;
        }else{
            this.r=1e9;
        }
    }
    @Override
    public double getR(){
        setR();
        return this.r;
    }
    @Override
    public void calculateOutputVoltage(double inputVoltage) {}
}
class H extends Circuit_Component {
    private boolean status=false;
    private int hway=2;
    public H(String component_Input) {
        super(component_Input);
        this.r=0.00000001;
        this.rH_Cut=1e9;
        this.limiteCurrent=20;
    }
    public void setRH(){
        if(!status){
            this.r=5;
        }else{
            this.r=10;
        }
    }
    public  double getValue(){
        if (status)return 1;
        else return 0;
    }
    @Override
    public  int getHway(){
        return  this.hway;
    }
    @Override
    public  void setHway(int hway){
        this.hway=hway;
    }
    public void regulation(String regulation) {
        this.status=!status;
    }
    @Override
    public double  getR() {
        setRH();
        if(hway==2&& status){
            return 1e9;
        }else if(hway==3&& !status){
            return 1e9;
        }
        if(!status)return 5;
        else return 10;
    }
    @Override
    public void calculateOutputVoltage(double inputVoltage) {
        setInputVoltage(inputVoltage);
    }
}
abstract class Governor  extends Circuit_Component{
    public Governor(String name) {
        super(name);
        r=0;
    }

    @Override
    public abstract void  calculateOutputVoltage(double inputVoltage);
}
class F extends Governor {
    private int gear = 0;
    public F(String component_Input) {
        super(component_Input);
        this.limiteCurrent=18;
    }
    @Override
    public double getValue() {
        return gear;
    }
    @Override
    public void calculateOutputVoltage(double inputVoltage) {
        double changeVoltage = 0;
        switch (gear) {
            case 0:
                changeVoltage = 0;break;
            case 1:
                changeVoltage = inputVoltage * 0.3;break;
            case 2:
                changeVoltage = inputVoltage * 0.6;break;
            case 3:
                changeVoltage = inputVoltage * 0.9;break;
            default:break;
        }
        setOutputVoltage(changeVoltage);
    }
    @Override
    public void regulation(String regulation) {
        if ("+".equals(regulation)) {
            gear++;
            if(gear>=3)gear=3;
        } else if ("-".equals(regulation)) {
            gear--;
            if(gear<=0)gear=0;
        }
    }
}
class L extends Governor{
    private double smoothGear;
    public L(String component_Input) {
        super(component_Input);
        this.smoothGear=0;
        this.limiteCurrent=18;
    }
    @Override
    public double getValue() {
        return smoothGear;
    }
    public void setGear(double gear) {
        this.smoothGear=gear;
    }
    @Override
    public void regulation(String regulation) {
        this.smoothGear=Double.valueOf(regulation);
    }
    @Override
    public void calculateOutputVoltage(double inputVoltage) {
        setOutputVoltage(inputVoltage*smoothGear);
    }

}
abstract class ElectricalAppliances  extends Circuit_Component{
    public ElectricalAppliances(String name) {
        super(name);
    }
    int id;
    @Override
    public  void  calculateOutputVoltage(double inputVoltage){
        setInputVoltage(inputVoltage);
        setOutputVoltage(inputVoltage-getVoltagedifference());
    }
    public abstract void operate(double voltage);
}
class B extends ElectricalAppliances{
    private double brightness;
    public B(String name) {
        super(name);
        this.r=10;
        this.limiteCurrent=9;
    }
    public void flash(double voltage) {
        if (voltage < 9) {
            this.brightness=0;
        } else if (voltage >= 219) {
            this.brightness=200;
        } else {
            double brightness = 50 + (voltage - 10) * ((200.0 - 50) / (220 - 10));
            this.brightness=brightness;
        }
    }
    @Override
    public void operate(double voltage) {
        setVoltagedifference(voltage);
        flash(voltage);
    }
    @Override
    public double getValue() {
        return this.brightness;
    }

}
class R extends ElectricalAppliances{
    private double light;
    public R(String component_Input) {
        super(component_Input);
        this.r=5;
        this.limiteCurrent=5;
    }
    public void flash(double voltage) {
        if (voltage>0.0001) this.light=180;
        else this.light=0;
    }
    @Override
    public void operate(double voltage) {
        setVoltagedifference(voltage);
        flash(voltage);
    }
    @Override
    public double getValue() {
        return this.light;
    }

}
class D extends ElectricalAppliances{
    private double speed;
    public D(String component_Input) {
        super(component_Input);
        this.r=20;
        this.limiteCurrent=12;
    }
    public  void calculateSpeed(double voltage) {
        double speed = 0;
        if (voltage < 79) {
            speed = 0;
        } else if (voltage >= 149) {
            speed = 360;
        } else {
            speed = 80 + (voltage - 80) * ((360 - 80) / (150 - 80));
        }
        this.speed=speed;
    }

    @Override
    public void operate(double voltage) {
        setVoltagedifference(voltage);
        calculateSpeed(voltage);
    }
    @Override
    public double getValue() {
        return this.speed;
    }

}
class A extends ElectricalAppliances{
    private double speed;

    public A(String name) {
        super(name);
        this.r=20;
        this.limiteCurrent=14;
    }
    public void calculateSpeed(double voltage) {
        if (voltage >= 80 && voltage < 100) {
            this.speed = 80;
        } else if (voltage >= 100 && voltage < 120) {
            this.speed = 160;
        } else if (voltage >= 120 && voltage < 140) {
            this.speed = 260;
        } else if (voltage >= 140) {
            this.speed = 360;
        }
    }
    @Override
    public void operate(double voltage) {
        setVoltagedifference(voltage);
        calculateSpeed(voltage);
    }
    @Override
    public double getValue(){
        return this.speed;
    }
}
class S extends ElectricalAppliances{
    private double curtainPosition=1.0;
    private double totalLux=0;
    public  Bank bank;
    public S(String name) {
        super(name);
        this.r = 15;
        this.limiteCurrent=12;
    }
    public void setBank(Bank bank){
        this.bank=bank;
    }
    @Override
    public void setTotalLux(T t) {
        for(Circuit_Component component:t.getConcatenationList()){
            if(component.getName().startsWith("R")||component.getName().startsWith("B")){
                totalLux+=component.getValue();
            }else if(component.getName().startsWith("M")){
                for(T t1:bank.lookforM(component.getName()).getMList()){
                    setTotalLux(t1);
                }
            } else if (component.getName().startsWith("T")) {
                setTotalLux((T)component);
            }
        }
    }
    @Override
    public void operate(double voltage) {
        setVoltagedifference(voltage);
        if(voltage<0.0001){
            curtainPosition = 0;
        }else if (voltage < 50&&voltage>0.0001) {
            curtainPosition = 1.0;
        }else {
            if (totalLux < 50) {
                curtainPosition = 1.0;
            } else if (totalLux < 100) {
                curtainPosition = 0.8;
            } else if (totalLux < 200) {
                curtainPosition = 0.6;
            } else if (totalLux < 300) {
                curtainPosition = 0.4;
            } else if (totalLux < 400) {
                curtainPosition = 0.2;
            } else {
                curtainPosition = 0.0;
            }
        }
    }
    @Override
    public double getValue(){
        return this.curtainPosition;
    }
}
class T extends Circuit_Component{
    private String name;
    private List<Circuit_Component> concatenationList;
    private Bank bank;
    private double Hstatus = 0;
    public T(Bank bank) {
        super("null");
        this.bank = bank;
        concatenationList=new ArrayList<>();
    }
    public List<Circuit_Component> getConcatenationList() {
        return concatenationList;
    }

    @Override
    public void calculateOutputVoltage(double inputVoltage) {}

    public String getName() {
        return name;
    }
    public double getR(){
        double r = 0;
        for(Circuit_Component circuitComponent:concatenationList){
            if((this.getHstatus()==2&&circuitComponent.getName().startsWith("R")&&circuitComponent.getValue()==1)||(this.getHstatus()==3&&circuitComponent.getName().startsWith("R")&&circuitComponent.getValue()==0)){
                r+=1e9;
            }else{
                r+=circuitComponent.getR();
            }
        }
        return r;
    }
    public double getHstatus(){
        return Hstatus;
    }
    public void dataInput(String input) {
        String regex = "[a-zA-Z]+\\d+|VCC|GND";;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        HashSet<String> set = new HashSet<>();
        boolean ifBegin=true;
        while (matcher.find()) {
            String match = matcher.group();
            if( !set.contains(match) ) {
                if (match.startsWith("T")) {
                    if (ifBegin) {
                        this.name = match;
                        ifBegin = false;
                    } else {
                        concatenationList.add(bank.lookforT(match));
                        set.add(match);
                    }
                } else {
                    if (match.startsWith("M")) {
                        concatenationList.add(bank.lookforM(match));
                        set.add(match);
                    } else {
                        concatenationList.add(ComponentItemFactory.createComponent(match));
                        set.add(match);
                    }
                }
            }
        }
        boolean firstMatchProcessed = false;
        String regexH = "H\\d+-\\d+";
        Pattern patternH = Pattern.compile(regexH);
        Matcher matcherH = patternH.matcher(input);
        HashSet<String> setH = new HashSet<>();
        while (matcherH.find()) {
            String match = matcherH.group();
            String[] parts = match.split("-");
            if(parts[1].equals("2")){
                bank.looker(parts[0],this).setHway(2);
                this.Hstatus=2;
            }
            else if(parts[1].equals("3")){
                bank.looker(parts[0],this).setHway(3);
                this.Hstatus=3;
            }
            if(!firstMatchProcessed){
                if(parts[1].equals("1")){
                    bank.looker(parts[0],this).setHfront(1);
                }
                else if(parts[1].equals("2")||parts[1].equals("3")){
                    bank.looker(parts[0],this).setHfront(2);
                }
                firstMatchProcessed=true;
            }
        }
        String regexAll = "[A-GI-Z]\\d+-\\d+";
        Pattern patternAll = Pattern.compile(regexAll);
        Matcher matcherAll = patternAll.matcher(input);
        HashSet<String> setAll = new HashSet<>();
        while (matcherAll.find()) {
            if(!setAll.contains(matcherAll.group().split("-")[0])) {
                String match = matcherAll.group();
                String[] parts = match.split("-");
                if(parts[1].equals("1")){
                    bank.looker(parts[0],this).setHway(1);
                    setAll.add(parts[0]);
                }
                else if(parts[1].equals("2")){
                    bank.looker(parts[0],this).setHway(2);
                    setAll.add(parts[0]);
                }
            }

        }
    }
}
class M extends Circuit_Component{
    private ArrayList<T> mList= new ArrayList<T>();
    private Bank bank;
    public M(Bank bank) {
        super("null");
        this.bank = bank;
    }
    public ArrayList<T> getMList(){
        return this.mList;
    }
    @Override
    public void calculateOutputVoltage(double inputVoltage) {}
    public void setData(String input){
        String regex = "[a-zA-Z]\\d+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        //   HashSet<String> set = new HashSet<>();
        while (matcher.find()) {
            String match = matcher.group();
            if(match.startsWith("M")){
                this.name=match;
            }else {
                mList.add(bank.lookforT(match));
                //  set.add(match);
            }
        }
    }

    @Override
    public double getR() {
        double r = 0;
        boolean ifCount=true;
        for (T t : mList) {
            ifCount=true;
            if(ifCount) {
                double resistance = t.getR();
                r += 1 / resistance;
            }
        }
        return 1 / r;
    }
}
class Bank {
    private List<T> tList;
    private List<M> mList;
    private Operate operate;
    public Bank() {
        tList=new ArrayList<>();
        mList=new ArrayList<>();
        operate=new Operate(this);
    }
    public List<T> getTList(){
        return this.tList;
    }
    public void setData(Scanner sc,Operate operate){
        String input= sc.nextLine();
        while (!input.startsWith("end")){
            if (input.startsWith("#T")||input.startsWith("#M")){
                if(input.startsWith("#T")) {
                    T t = new T(this);
                    t.dataInput(input);
                    tList.add(t);
                } else if (input.startsWith("#M")) {
                    M m=new M(this);
                    m.setData(input);
                    mList.add(m);
                }
            }else{
                operate.operate(input);
            }
            input= sc.nextLine();
        }
    }
    public Circuit_Component looker(String name, T t){
        for(Circuit_Component circuitComponent: t.getConcatenationList()){
            String names= circuitComponent.getName();
            if(names.startsWith("M")){
                for(T t1:lookforM(names).getMList()){
                    if (looker(name,t1)==null)continue;
                    else return looker(name,t1);
                }
            }else if(names.startsWith("T")){
                if (looker(name,(T)circuitComponent)==null)continue;
                else return looker(name,(T)circuitComponent);
            }else if(name.equals(names)){
                return circuitComponent;
            }
        }
        return null;
    }
    public void operatorH(String name, T t){
        for(Circuit_Component circuitComponent: t.getConcatenationList()){
            String names= circuitComponent.getName();
            if(names.startsWith("M")){
                for(T t1:lookforM(names).getMList()){
                    operatorH(name,t1);
                }
            }else if(names.startsWith("T")){
                operatorH(name,(T)circuitComponent);
            }else if(name.equals(names)){
                circuitComponent.regulation("");
            }
        }
    }
    public List<Circuit_Component> sortArrayOutput(T t, List<Circuit_Component> list){
        for(Circuit_Component circuitComponent:t.getConcatenationList()){
            String name=circuitComponent.getName();
            if(!name.equals("VCC")&&!name.equals("GND")){
                if (name.startsWith("M")){
                    for(T t1:lookforM(name).getMList()){
                        sortArrayOutput(t1,list);
                    }
                }else if(name.startsWith("T")){
                    sortArrayOutput((T)circuitComponent,list);
                }else{
                    list.add(circuitComponent);
                }
            }
        }
        return list;
    }
    public T lookforT(String input){
        for(T t:tList){
            if(t.getName().equals(input)){
                return t;
            }
        }
        return null;
    }
    public M lookforM(String input){
        for(M m:mList){
            if(m.getName().equals(input)){
                return m;
            }
        }
        return null;
    }
}
class Operate{
    private Bank bank;
    public Operate(Bank bank){
        this.bank=bank;
    }
    public  void operate(String input) {
        int index = input.indexOf('#');
        if(input.startsWith("#K")){
            if(bank.looker(input.replace("#", ""),bank.getTList().get(bank.getTList().size()-1))!=null){
                bank.looker(input.replace("#", ""),bank.getTList().get(bank.getTList().size()-1)).regulation("");
            }
        } else if (input.startsWith("#H")) {
            bank.operatorH(input.replace("#", ""),bank.getTList().get(bank.getTList().size()-1));
        } else if (input.startsWith("#L")) {
            int startIndex = input.indexOf("#") + 1;
            int colonIndex = input.indexOf(":");
            String l1 = input.substring(startIndex, colonIndex);
            String value = input.substring(colonIndex + 1);
            bank.looker(l1,bank.getTList().get(bank.getTList().size()-1)).regulation(value);
        } else if (input.startsWith("#F")) {
            Pattern pattern = Pattern.compile("#(F\\d+)([\\-+])");
            Matcher matcher = pattern.matcher(input);
            if (matcher.find()) {
                String fNumber = matcher.group(1);
                String operators = matcher.group(2);
                bank.looker(fNumber,bank.getTList().get(bank.getTList().size()-1)).regulation(operators);
            }
        }
    }
}
class ComponentComparator implements Comparator<Circuit_Component> {
    List<String> order = Arrays.asList("K", "F", "L", "B", "R", "D", "A", "H", "S","P");

    @Override
    public int compare(Circuit_Component o1, Circuit_Component o2) {
        String name1 = o1.getName();
        String name2 = o2.getName();

        String prefix1 = name1.substring(0, 1);
        String prefix2 = name2.substring(0, 1);

        int index1 = order.indexOf(prefix1);
        int index2 = order.indexOf(prefix2);

        if (index1 != index2) {
            return Integer.compare(index1, index2);
        } else {
            String number1 = name1.substring(1);
            String number2 = name2.substring(1);
            return number1.compareTo(number2);
        }
    }
}
class ComponentVoltage {
    public ComponentVoltage() {
    }
}
class ComponentVoltage2 extends ComponentVoltage {
    private double voltagePin1;
    private double voltagePin2;

    public ComponentVoltage2(double voltagePin1, double voltagePin2) {
        super();
        this.voltagePin1 = voltagePin1;
        this.voltagePin2 = voltagePin2;
    }

    public double getVoltagePin1() {
        return voltagePin1;
    }

    public double getVoltagePin2() {
        return voltagePin2;
    }

    @Override
    public String toString() {
        return "Pin1: " + voltagePin1 + ", Pin2: " + voltagePin2;
    }
}
class ComponentVoltage3 extends ComponentVoltage {
    private double voltagePin1;
    private double voltagePin2;
    private double voltagePin3;

    public ComponentVoltage3(double voltagePin1, double voltagePin2,double voltagePin3) {
        super();
        this.voltagePin1 = voltagePin1;
        this.voltagePin2 = voltagePin2;
        this.voltagePin3 = voltagePin3;
    }

    public double getVoltagePin1() {
        return voltagePin1;
    }

    public double getVoltagePin2() {
        return voltagePin2;
    }
    public double getVoltagePin3() {
        return voltagePin3;
    }
    @Override
    public String toString() {
        return "Pin1: " + voltagePin1 + ", Pin2: " + voltagePin2;
    }
}
class HomeElectricalSystem {
    private static HomeElectricalSystem instance;
    private Bank bank;
    private Operate operate;
    private  List<Circuit_Component> list;
    private double volatilebefore=220;
    private double middle=0;
    private double S_V;
    private T tLast;
    private double h1_V=1;
    Map<Circuit_Component, ComponentVoltage> voltageMap = new HashMap<>();
    private HomeElectricalSystem() {
        this.bank=new Bank();
        this.operate=new Operate(bank);
        this.list=new ArrayList<>();
    }
    public static synchronized HomeElectricalSystem getInstance(){
        if(instance==null) {
            instance=new HomeElectricalSystem();
        }
        return instance;
    }
    public void setRAll(T t) {
        for (Circuit_Component circuitComponent : t.getConcatenationList()) {
            String name = circuitComponent.getName();
            if (name.startsWith("K")) {
                if (circuitComponent.getValue() == 0) {
                    circuitComponent.setR(1e9);
                }
            } else if (name.startsWith("H")) {
                if (circuitComponent.getValue() == 0 && circuitComponent.getHway() == 3) {
                }
                if (circuitComponent.getValue() == 1 && circuitComponent.getHway() == 2) {
                    break;
                }
            } else if (name.startsWith("M")) {
                for (T t2 : bank.lookforM(name).getMList()) {
                    setRAll(t2);
                }
            } else if (name.startsWith("T")) {
                setRAll((T)circuitComponent);
            }
        }
    }
    public void operateR(T t) {
        for (Circuit_Component circuitComponent : t.getConcatenationList()) {
            String name = circuitComponent.getName();
            if (name.startsWith("S")) {
                circuitComponent.setTotalLux(tLast);
                circuitComponent.operate(circuitComponent.getVoltagedifference());
            } else if (name.startsWith("M")) {
                for (T t2 : bank.lookforM(name).getMList()) {
                    operateR(t2);
                }
            } else if (name.startsWith("T")) {
                operateR((T)circuitComponent);
            }
        }
    }
    public Circuit_Component getComponentByName(String desiredName) {
        for (Circuit_Component component : voltageMap.keySet()) {
            if (component.getName().equals(desiredName)) {
                return component;
            }
        }
        return null;
    }
    public boolean setElectric(double V,T t,boolean ifOperate,double inputVoltage,double output_Voltage, Map<Circuit_Component, ComponentVoltage> voltageMap){
        boolean ifCount=true;
        if (ifCount&&ifOperate) {
            double currentVoltage = inputVoltage;
            for (Circuit_Component circuitComponent : t.getConcatenationList()) {
                if(circuitComponent.getName().startsWith("P11")){
                    int a = 1;
                }
                String name = circuitComponent.getName();
                double tR=t.getR();
                double circuitR= circuitComponent.getR();
                double componentVoltageDrop = 0;
                if(circuitR>10000000){
                    componentVoltageDrop = currentVoltage-output_Voltage;
                }else{
                    componentVoltageDrop = circuitR * V / t.getR();
                }
                if(!circuitComponent.getName().startsWith("H")){
                        circuitComponent.setCurrent(V/t.getR());
                }
                double outputVoltage = currentVoltage - componentVoltageDrop;

                if (name.startsWith("M")) {
                    for (T t1 : bank.lookforM(name).getMList()) {
                        double Vs=bank.lookforM(name).getR()*V/t.getR();
                        setElectric(Vs, t1,true,currentVoltage,outputVoltage, voltageMap);
                    }
                } else if(name.startsWith("T")){
                    double Vs=bank.lookforT(name).getR()*V/t.getR();
                    setElectric(Vs, (T)circuitComponent,true,currentVoltage,outputVoltage, voltageMap);
                }else {
                    if(circuitComponent.getName().startsWith("S")){
                        circuitComponent.setBank(bank);
                        circuitComponent.setVoltagedifference(circuitComponent.getR()*V/t.getR());
                    }else{
                        circuitComponent.operate(circuitComponent.getR()*V/t.getR());
                    }
                }
                if(!circuitComponent.getName().startsWith("L")&&!circuitComponent.getName().startsWith("F")){
                    if(circuitComponent.getName().startsWith("H")){
                        if(circuitComponent.getHfront()==1){
                            if(voltageMap.containsKey(circuitComponent)){
                                circuitComponent=getComponentByName(circuitComponent.getName());
                            }
                            if(t.getHstatus()==2){
                                if(circuitComponent.getValue()==0){
                                    circuitComponent.setCurrent(V/t.getR());
                                }
                                voltageMap.put(circuitComponent, new ComponentVoltage3(currentVoltage+0.00001, outputVoltage+0.00001,circuitComponent.getMiddle()));
                                circuitComponent.setMiddle(outputVoltage+0.00001);
                            }else if(t.getHstatus()==3){
                                if(circuitComponent.getValue()==1){
                                    circuitComponent.setCurrent(V/t.getR());
                                }
                                voltageMap.put(circuitComponent, new ComponentVoltage3(currentVoltage+0.00001,circuitComponent.getMiddle(), outputVoltage+0.00001));
                                circuitComponent.setMiddle(outputVoltage+0.00001);
                            }
                        }else{
                            if(voltageMap.containsKey(circuitComponent)){
                                circuitComponent=getComponentByName(circuitComponent.getName());
                            }
                            if(t.getHstatus()==2){
                                if(circuitComponent.getValue()==0){
                                    circuitComponent.setCurrent(V/t.getR());
                                }
                                if (outputVoltage<0.00001){
                                    voltageMap.put(circuitComponent, new ComponentVoltage3(circuitComponent.getH1_V(),currentVoltage+0.00001, circuitComponent.getMiddle()));
                                    circuitComponent.setMiddle(currentVoltage+0.00001);
                                }else{
                                    voltageMap.put(circuitComponent, new ComponentVoltage3(outputVoltage+0.00001,currentVoltage+0.00001, circuitComponent.getMiddle()));
                                    circuitComponent.setMiddle(currentVoltage+0.00001);
                                    circuitComponent.setH1_V(outputVoltage+0.00001);
                                }
                            }else if(t.getHstatus()==3){
                                if(circuitComponent.getValue()==1){
                                    circuitComponent.setCurrent(V/t.getR());
                                }
                                if(outputVoltage<0.00001){
                                    voltageMap.put(circuitComponent, new ComponentVoltage3(circuitComponent.getH1_V(),circuitComponent.getMiddle(),currentVoltage+0.00001));
                                    circuitComponent.setMiddle(currentVoltage+0.00001);
                                }else{
                                    voltageMap.put(circuitComponent, new ComponentVoltage3(outputVoltage+0.00001,circuitComponent.getMiddle(),currentVoltage+0.00001));
                                    circuitComponent.setMiddle(currentVoltage+0.00001);
                                    circuitComponent.setH1_V(outputVoltage+0.00001);
                                }
                            }
                        }
                    }else{
                        if(circuitComponent.getHway()==1){
                            voltageMap.put(circuitComponent, new ComponentVoltage2(currentVoltage+0.00001, outputVoltage+0.00001));
                        }else{
                            voltageMap.put(circuitComponent, new ComponentVoltage2(outputVoltage+0.00001, currentVoltage+0.00001));
                        }
                    }
                }
                currentVoltage = outputVoltage;
            }
        }
        return ifCount;
    }
    public void setPin_V(T t){
        for(Circuit_Component circuitComponent: t.getConcatenationList()){
            String name = circuitComponent.getName();
            if(name.startsWith("K")){
                if(circuitComponent.getValue()==0){
                    circuitComponent.setInputVoltage(t.getInputVoltage());
                    circuitComponent.setOutputVoltage(t.getOutputVoltage());
                }
            }
        }
    }
    public void showOut(List<Circuit_Component> list){
        List<Circuit_Component> lists=new ArrayList<>();
        if(tLast.getR()<0.001){
            System.out.println("short circuit error");
        }else{
            for(Circuit_Component components:list){
                ComponentVoltage2 componentVoltage = null;
                ComponentVoltage3 componentVoltageH = null;
                if(!components.getName().startsWith("H")){
                    componentVoltage = (ComponentVoltage2) voltageMap.get(components);
                }else{
                    componentVoltageH = (ComponentVoltage3) voltageMap.get(components);
                }
                if(components.getName().startsWith("K")&&components.getValue()==1) {
                    if(components.getCurrent()>components.getLimiteCurrent()){
                        System.out.println("@"+components.getName()+":closed "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2()+" exceeding current limit error");
                    }else{
                        System.out.println("@"+components.getName()+":closed "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2());
                    }
                }else if(components.getName().startsWith("K")&&components.getValue()==0) {
                    if(components.getCurrent()>components.getLimiteCurrent()){
                        System.out.println("@"+components.getName()+":turned on "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2()+" exceeding current limit error");
                    }else{
                        System.out.println("@"+components.getName()+":turned on "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2());
                    }
                }else if(components.getName().startsWith("F")){
                    if(components.getCurrent()>components.getLimiteCurrent()){
                        System.out.println("@"+components.getName()+":"+(int)Math.round(components.getValue())+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2()+" exceeding current limit error");
                    }else{
                        System.out.println("@"+components.getName()+":"+(int)Math.round(components.getValue())+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2());
                    }
                }else if(components.getName().startsWith("L")){
                    if(components.getCurrent()>components.getLimiteCurrent()){
                        System.out.println("@"+components.getName()+":"+ String.format("%.2f",components.getValue())+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2()+" exceeding current limit error");
                    }else{
                        System.out.println("@"+components.getName()+":"+ String.format("%.2f",components.getValue())+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2());
                    }
                }else if(components.getName().startsWith("H")&&components.getValue()==1) {
                    if(!ifExitamongList(lists, components.getName())) {
                        if(components.getCurrent()>components.getLimiteCurrent()){
                            System.out.println("@" + components.getName() + ":turned on "+(int)componentVoltageH.getVoltagePin1()+"-"+(int)componentVoltageH.getVoltagePin2()+"-"+(int)componentVoltageH.getVoltagePin3()+" exceeding current limit error");
                        }else{
                            System.out.println("@" + components.getName() + ":turned on "+(int)componentVoltageH.getVoltagePin1()+"-"+(int)componentVoltageH.getVoltagePin2()+"-"+(int)componentVoltageH.getVoltagePin3());
                        }
                    }
                }else if(components.getName().startsWith("H")&&components.getValue()==0) {
                    if(!ifExitamongList(lists, components.getName())) {
                        if(components.getCurrent()>components.getLimiteCurrent()){
                            System.out.println("@" + components.getName() + ":closed "+(int)componentVoltageH.getVoltagePin1()+"-"+(int)componentVoltageH.getVoltagePin2()+"-"+(int)componentVoltageH.getVoltagePin3()+" exceeding current limit error");
                        }else{
                            System.out.println("@" + components.getName() + ":closed "+(int)componentVoltageH.getVoltagePin1()+"-"+(int)componentVoltageH.getVoltagePin2()+"-"+(int)componentVoltageH.getVoltagePin3());
                        }
                    }
                }else if(components.getName().startsWith("S")) {
                    if(components.getCurrent()>components.getLimiteCurrent()){
                        System.out.println("@" + components.getName() +":"+(int)(components.getValue()*100)+"%"+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2()+" exceeding current limit error");
                    }else{
                        System.out.println("@" + components.getName() +":"+(int)(components.getValue()*100)+"%"+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2());
                    }
                }else  if(components.getName().startsWith("P")) {
                    if(((int)componentVoltage.getVoltagePin1()==0&&(int)componentVoltage.getVoltagePin2()==0)||((int)componentVoltage.getVoltagePin2()-(int)componentVoltage.getVoltagePin1()!=0)){
                        if(components.getHway()==2){
                            if(components.getCurrent()>components.getLimiteCurrent()){
                                System.out.println("@" + components.getName() +":"+"cutoff"+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2()+" exceeding current limit error");
                            }else{
                                System.out.println("@" + components.getName() +":"+"cutoff"+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2());
                            }
                        }else{
                            if(components.getCurrent()>components.getLimiteCurrent()){
                                System.out.println("@" + components.getName() +":"+"conduction"+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2()+" exceeding current limit error");
                            }else{
                                System.out.println("@" + components.getName() +":"+"conduction"+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2());
                            }
                        }
                    }else if((int)componentVoltage.getVoltagePin1()==(int)componentVoltage.getVoltagePin2()){
                        if(components.getCurrent()>components.getLimiteCurrent()){
                            System.out.println("@" + components.getName() +":"+"conduction"+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2()+" exceeding current limit error");
                        }else{
                            System.out.println("@" + components.getName() +":"+"conduction"+" "+(int)componentVoltage.getVoltagePin1()+"-"+(int)componentVoltage.getVoltagePin2());
                        }
                    }
                }else if(components.getName().startsWith("B")){
                    if (components.getCurrent() > components.getLimiteCurrent()) {
                        System.out.println("@" + components.getName() + ":" + (int) components.getValue() + " " + (int) componentVoltage.getVoltagePin1() + "-" + (int) componentVoltage.getVoltagePin2() + " exceeding current limit error");
                    } else {
                        System.out.println("@" + components.getName() + ":" + (int)  components.getValue() + " " + (int) componentVoltage.getVoltagePin1() + "-" + (int) componentVoltage.getVoltagePin2());
                    }
                }else if(components.getName().startsWith("D")){
                    if (components.getCurrent() > components.getLimiteCurrent()) {
                        System.out.println("@" + components.getName() + ":" + (int) Math.round(components.getValue()-0.1) + " " + (int) componentVoltage.getVoltagePin1() + "-" + (int) componentVoltage.getVoltagePin2() + " exceeding current limit error");
                    } else {
                        System.out.println("@" + components.getName() + ":" + (int) Math.round(components.getValue()-0.1) + " " + (int) componentVoltage.getVoltagePin1() + "-" + (int) componentVoltage.getVoltagePin2());
                    }
                }else{
                    if (components.getCurrent() > components.getLimiteCurrent()) {
                        System.out.println("@" + components.getName() + ":" + (int) Math.round(components.getValue()) + " " + (int) componentVoltage.getVoltagePin1() + "-" + (int) componentVoltage.getVoltagePin2() + " exceeding current limit error");
                    } else {
                        System.out.println("@" + components.getName() + ":" + (int)Math.round(components.getValue()) + " " + (int) componentVoltage.getVoltagePin1() + "-" + (int) componentVoltage.getVoltagePin2());
                    }
                }
                lists.add(components);
            }
        }
    }
    public boolean ifExitamongList(List<Circuit_Component> list,String name){
        for(Circuit_Component components:list){
            if(components.getName().equals(name))return true;
        }
        return false;
    }
    public void TakeIn(Scanner scanner) {
        bank.setData(scanner,operate);
        double Vinput=220;
        if(bank.getTList().get(bank.getTList().size()-1).getConcatenationList().get(1).getName().startsWith("F")||bank.getTList().get(bank.getTList().size()-1).getConcatenationList().get(1).getName().startsWith("L")){
            bank.getTList().get(bank.getTList().size()-1).getConcatenationList().get(1).calculateOutputVoltage(220);
            Vinput=bank.getTList().get(bank.getTList().size()-1).getConcatenationList().get(1).getOutputVoltage();
        }
        bank.getTList().get(bank.getTList().size()-1).setInputVoltage(220);
        setRAll(bank.getTList().get(bank.getTList().size()-1));
        if (Vinput!=0){
            Circuit_Component component=bank.getTList().get(bank.getTList().size()-1).getConcatenationList().get(1);
            voltageMap.put(component, new ComponentVoltage2(220,bank.getTList().get(bank.getTList().size()-1).getConcatenationList().get(1).getOutputVoltage()));
            setElectric(Vinput,bank.getTList().get(bank.getTList().size()-1),true,Vinput,0,voltageMap);
        }
        tLast=bank.getTList().get(bank.getTList().size()-1);
        operateR(tLast);
        list=bank.sortArrayOutput(bank.getTList().get(bank.getTList().size()-1),list);
        Collections.sort(list, new ComponentComparator());
        showOut(list);
    }
}
class ComponentItemFactory{
    public static Circuit_Component createComponent(String component_Input) {
        if(component_Input.startsWith("VCC")) {
            return new PowerSupply(component_Input);
        } else if (component_Input.startsWith("GND")) {
            return new Ground("GND");
        } else if(component_Input.startsWith("K")) {
            return new Switch(component_Input);
        }else if(component_Input.startsWith("H")) {
            return new H(component_Input);
        }else if(component_Input.startsWith("F")) {
            return new F(component_Input);
        }else if(component_Input.startsWith("L")) {
            return new L(component_Input);
        }else if(component_Input.startsWith("B")) {
            return new B(component_Input);
        }else if(component_Input.startsWith("R")) {
            return new R(component_Input);
        }else if(component_Input.startsWith("D")) {
            return new D(component_Input);
        }else  if(component_Input.startsWith("S")) {
            return new S(component_Input);
        }else if(component_Input.startsWith("A")){
            return new A(component_Input);
        } else if (component_Input.startsWith("P")) {
            return new P(component_Input);
        }
        return null;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HomeElectricalSystem system=HomeElectricalSystem.getInstance();
        system.TakeIn(scanner);
    }
}

因为本学期最后一次了嘛,发出来看看也可以,(学弟们应该用的不是同套题目集吧)

热门相关:纣临   绝世赘婿   邪王追妻99次:娘子,等我   帝少夜宠:小甜妻,乖!   我拍戏不在乎票房