OOP课程第三次博客
1.前言
第七次和第八次题目集主要是模拟一个家居强电电路程序,重点考察了我们对数据结构的运用:需要使用hashmap,arraylist类来存储不同类型的设备及其相关信息,如设备标识符、编号、状态、引脚连接等。以及类和对象的设计:设计不同的类来表示各种设备(如开关、调速器、灯具、风扇、窗帘等),封装其属性和方法。设计串联电路类和并联电路类,处理电路的连接和信息传递。还有逻辑判断和条件语句:根据输入的控制设备调节信息,使用条件语句来改变设备的状态或档位。依据设备的连接关系和工作状态,进行复杂的逻辑判断来计算输出结果。还有一个数值计算和精度处理:按照题目要求,进行电压、电流、亮度、转速等数值的计算,注意使用 double 类型进行中间计算,并在输出时进行截尾处理。第八次题目集在保持原来输出的基础上还增加了对电势的计算,要求我们有一定得电路知识。题目难度于我而言较大。要迎难而上,再接再厉!
2.设计与分析
1.第七次pta作业
7-1 家居强电电路模拟程序-3
分数 100
作者 蔡轲
单位 南昌航空大学
智能家居是在当下家庭中越来越流行的一种配置方案,它通过物联网技术将家中的各种设备(如音视频设备、照明系统、窗帘控制、空调控制、安防系统、数字影院系统、影音服务器、影柜系统、网络家电等)连接到一起,提供家电控制、照明控制、电话远程控制、室内外遥控、防盗报警、环境监测、暖通控制、红外转发以及可编程定时控制等多种功能和手段。与普通家居相比,智能家居不仅具有传统的居住功能,兼备建筑、网络通信、信息家电、设备自动化,提供全方位的信息交互功能。请根据如下要去设计一个智能家居强电电路模拟系统。以下题目介绍中加粗的部分为本次迭代在“家居强电电路模拟程序-2”的基础上增加的功能要求。
1、控制设备
本题模拟的控制设备包括:开关、分档调速器、连续调速器、互斥开关。
开关:包括0和1两种状态。
开关有两个引脚,任意一个引脚都可以是输入引脚(接往电源端),而另一个则是输出引脚(接网接地端)。开关状态为0时,无论输入电位是多少,输出引脚电位为0。当开关状态为1时,输出引脚电位等于输入电位。
互斥开关:
互斥开关有3个引脚:1个是汇总引脚,另两个是分支引脚。
开关电路示意图如图1所示,左边是汇总引脚,编号为1;右边两个是分支引脚,右上的输出引脚为2,右下输出引脚为3。图中1、2、3引脚均可以是输入引脚,当1为输入引脚时,2、3引脚为输出引脚;1为输出引脚时,2、3引脚为输入引脚。
互斥开关只有两种状态:开关接往上面的2号引脚、接往下面的3号引脚。开关每次只能接通其中一个分支引脚,而另一个分支引脚处于断开状态。
互斥开关的默认状态为1、2引脚接通,1、3引脚断开。
图1中所示的互斥开关可以反过来接入电路,即汇总引脚接往接地端,两个分支引脚接往电源端。
图1 互斥开关
为避免短路,互斥开关设置了限流电阻,12引脚之间默认电阻为5,13引脚之间默认电阻为10。
分档调速器
按档位调整,常见的有3档、4档、5档调速器,档位值从0档-2(3/4)档变化。本次迭代模拟4档调速器,每个档位的输出电位分别为0、0.3、0.6、0.9倍的输入电压。
连续调速器
没有固定档位,按位置比例得到档位参数,数值范围在[0.00-1.00]之间,含两位小数。输出电位为档位参数乘以输入电压。
所有调速器都有两个引脚,一个固定的输入(引脚编号为1)、一个输出引脚(引脚编号为2)。当输入电位为0时,输出引脚输出的电位固定为0,不受各类开关调节的影响。
开关、调速器的初始状态/档位为0。
调速器的输入引脚编号为1,输出引脚编号为2。
除互斥开关外,其他控制设备的电阻为 0。
2、受控设备
本题模拟的受控设备包括:灯、风扇。两种设备都有两根引脚,通过两根引脚电压的电压差驱动设备工作。
灯有两种工作状态:亮、灭。在亮的状态下,有的灯会因引脚电位差的不同亮度会有区别。
风扇在接电后有两种工作状态:停止、转动。风扇的转速会因引脚间电位差的不同而有区别。
本次迭代模拟两种灯具。
白炽灯:
亮度在0~200lux(流明)之间。
电位差为0-9V时亮度为0,其他电位差按比例,电位差10V对应50ux,220V对应200lux,其他电位差与对应亮度值成正比。白炽灯超过220V。
日光灯:
亮度为180lux。
只有两种状态,电位差为0时,亮度为0,电位差不为0,亮度为180。
本次迭代模拟两种风扇。
吊扇:
工作电压区间为80V-150V,对应转速区间为80-360转/分钟。80V对应转速为80转/分钟,150V对应转速为360转/分钟,超过150V转速为360转/分钟(本次迭代暂不考虑电压超标的异常情况)。其他电压值与转速成正比,输入输出电位差小于80V时转速为0。
落地扇:
工作电压区间为 80V-150V,对应转速区间为 80-360 转/分钟;[80V,100V) 对应转速为 80 转/分钟;[100,120)V 对应转速为 160 转/分钟;[120,140)V 对应转速为 260 转/分钟;大于等于 140V 转速 为 360 转/分钟(本次迭代暂不考虑电压超标的异常情况)。
本次迭代模拟一种受控窗帘:
受控窗帘的电路符号为S,其最低工作电压为50V,电压达到或超过50V,窗帘即可正常工作,不考虑室外光照强度和室内空间大小等因素,窗帘受室内灯光的光照强度控制。
当电路中所有灯光的光照强度总和在[0,50)lux范围内,窗帘全开;
在[50,100)lux范围内,窗帘打开比例为0.8;
在[100,200)lux范围内,窗帘打开比例为0.6;
在[200,300)lux范围内,窗帘打开比例为0.4;
在[300,400)lux范围内,窗帘打开比例为0.2;
在400lux及以上范围内,窗帘关闭。
当电压低于50V,窗帘不工作,默认为全开状态。
如果电路中没有灯或者灯全部关闭,光照强度为0,窗帘处于全开状态。
受控设备电阻:白炽灯的电阻为 10,日光灯的电阻为 5,吊扇的电阻为 20,落地扇的电阻为 20,窗帘电阻为15。
3、输入信息
1)输入设备信息
分别用设备标识符K、F、L、B、R、D、A、H、S分别表示开关、分档调速器、连续调速器、白炽灯、日光灯、吊扇、落地扇、互斥开关、受控窗帘。
设备标识用标识符+编号表示,如K1、F3、L2等。
引脚格式:设备标识-引脚编号,例如:K1-1标识编号为1的开关的输入引脚。
开关、分档调速器、连续调速器的输入引脚编号为1,输出引脚编号为2。
受控设备的两个引脚编号分别为1、2。
互斥开关的引脚编号已经在互斥开关的介绍部分说明。
约束条件:
不同设备的编号可以相同。
同种设备的编号可以不连续。
设备信息不单独输入,包含在连接信息中。
2)输入连接信息
一条连接信息占一行,用[]表示一组连接在一起的设备引脚,引脚与引脚之间用英文空格" "分隔。
格式:"["+引脚号+" "+引脚号+"]"
例如:[K1-1 K3-2]表示K1的1引脚,K3的2引脚连接在一起。
约束条件:
不考虑调速器串联到其他调速器的情况。
考虑各类设备的并联接入。例如,K1 的输出接到 L2 的输入,L2 的输出再接其他设备属于串联接线。K1 的输出接到 L2 的输出,同时 K1 的输入接到 L2 的输入,这种情况属于并联。
本次迭代的连接信息不单独输入,包含在线路信息中。
3)输入控制设备调节信息
开关、互斥开关调节信息格式:
#+设备标识K+设备编号,例如:#K2,代表切换K2开关的状态。
#+设备标识H+设备编号,例如:#H2,代表切换H2互斥开关的状态。
分档调速器的调节信息格式:
#+设备标识F+设备编号+"+" 代表加一档,例如:#F3+,代表F3输出加一档。
#+设备标识F+设备编号+"-" 代表减一档,例如:#F1-,代表F1输出减一档。
连续调速器的调节信息格式:
#+设备标识L+设备编号+":" +数值 代表将连续调速器的档位设置到对应数值,例如:#L3:0.6,代表L3输出档位参数0.6。
4)电源接地标识:
VCC,电压220V,GND,电压0V。没有接线的引脚默认接地,电压为0V。
5)输入串联电路信息
一条串联电路占一行,串联电路由按从靠电源端到接地端顺序依次输入的 n 个连接 信息组成,连接信息之间用英文空格" "分隔。
串联电路信息格式:
"#T"+电路编号+":"+连接信息+" "+连接信息+...+" "+连接信息
例如:#T1:[IN K1-1] [K1-2 D2-1] [D2-2 OUT] 一个串联电路的第一个引脚是 IN,代表起始端,靠电源。最后一个引脚是 OUT,代表结尾端, 靠接地。
约束条件:
不同的串联电路信息编号不同。
输入的最后一条电路信息必定是总电路信息,总电路信息的起始引脚是 VCC,结束引脚是 GND。
连接信息中的引脚可能是一条串联或并联电路的 IN 或者 OUT。例如:
#T1:[IN K1-1] [K1-2 T2-IN] [T2-OUT OUT]
#T1:[IN K1-1] [K1-2 T2-IN] [T2-OUT M2-IN] [M2-OUT OUT]
6)输入并联电路信息
一条并联电路占一行,并联电路由其包含的几条串联电路组成,串联电路标识之间用英文空格" "分隔。
格式:
"#M"+电路编号+":"+”[”+串联电路信息+" "+....+" "+串联电路信息+”]”
例如:#M1:[T1 T2 T3]
该例声明了一个并联电路,由 T1、T2、T3 三条串联电路并联而成,三条串联电路的 IN 短 接在一起构成 M1 的 IN,三条串联电路的 OUT 短接在一起构成 M1 的 OUT。
约束条件:
本次迭代不考虑并联电路中包含并联电路的情况。
本题不考虑输入电压或电压差超过220V的情况。
输入信息以end为结束标志,忽略end之后的输入信息。
本题中的并联信息所包含的串联电路的信息都在并联信息之前输入,不考虑乱序输入的情况。
只要不因短路而造成无穷大的电流烧坏电路(如电路中的部分短接),都是合理情况,在测试点的考虑范围之内。会造成无穷大的电流的短路本次迭代不考虑。
本次迭代考虑多个并联电路串联在一起的情况。
本题考虑一条串联电路中包含其他串联电路的情况。例如:
#T3:[VCC K1-1] [K1-2 T2-IN] [T2-OUT K2-1] [K2-2 T1-IN] [T1-OUT GND]
本例中T1\T2两条串联电路T3的一个部分,本题考虑这种类型的输入。
4、输出信息:
按开关、分档调速器、连续调速器、白炽灯、日光灯、吊扇、互斥开关、受控窗帘的顺序依次输出所有设备的状态或参数。每个设备一行。同类设备按编号顺序从小到大输出。
输出格式:
@设备标识+设备编号+":" +设备参数值(控制开关的档位或状态、灯的亮度、风扇的转速,只输出值,不输出单位)
连续调速器的档位信息保留两位小数,即使小数为0,依然显示两位小数.00。
开关状态为0(打开)时显示turned on,状态为1(合上)时显示closed
如:
@K1:turned on
@B1:190
@L1:0.60
互斥开关显示1、2引脚的接通状态,接通时显示closed,断开时显示turned on。
如:
@H1:turned on
受控窗帘显示窗帘打开的百分比,如:
@S1:80%
5、家居电路模拟系列所有题目的默认规则:
1)当计算电压值等数值的过程中,最终结果出现小数时,用截尾规则去掉小数部分,只保留整数部分。为避免精度的误差,所有有可能出现小数的数值用double类型保存并计算,不要作下转型数据类型转换,例如电压、转速、亮度等,只有在最后输出时再把计算结果按截尾规则,舍弃尾数,保留整数输出。
2)所有连接信息按电路从电源到接地的顺序依次输入,不会出现错位的情况。电源VCC一定是第一个连接的第一项,接地GND一定是最后一个连接的后一项。
3)连接信息如果只包含两个引脚,靠电源端的引脚在前,靠接地端的在后。
4)调速器的输入端只会直连VCC,不会接其他设备。整个电路最多只有连接在电源上的一个调速器,且不包含在并联单路中。
6、家居电路模拟系列1-4题目后续迭代设计:
1)电路结构变化:
迭代1:只有一条线路,所有元件串联
迭代2:线路中包含一个并联电路
迭代3:线路中包含多个串联起来的并联电路
迭代4:并联电路之间可能出现包含关系
电路结构变化示意图见图1。
2)计算方式的变化
迭代1只包含1个受控元件,不用计算电流,之后的电路计算要包含电流、电阻等电路参数。
3)电路元件的变化
每次迭代会增加1-2个新的电路元件。

设计建议:
1、电路设备类:描述所有电路设备的公共特征。
2、受控设备类、控制设备类:对应受控、控制设备
3、串联电路类:一条由多个电路设备构成的串联电路,也看成是一个独立的电路设备
4、并联电路类:继承电路设备类,也看成是一个独立的电路设备
其他类以及类的属性、方法自行设计。

图2:建议设计类图
输入样例1:
在这里给出一组输入。例如:
#T1:[IN H1-1] [H1-2 D2-1] [D2-2 OUT]
#T2:[IN H1-1] [H1-3 D1-1] [D1-2 OUT]
#M1:[T1 T2]
#T4:[IN K3-1] [K3-2 B2-1] [B2-2 OUT]
#T5:[IN K1-1] [K1-2 B1-1] [B1-2 OUT]
#M2:[T4 T5]
#T3:[VCC K2-1] [K2-2 M1-IN] [M1-OUT M2-IN] [M2-OUT GND]
#K1
#K2
end
输出样例1:
在这里给出相应的输出。例如:
@K1:closed
@K2:closed
@K3:turned on
@B1:87
@B2:0
@D1:0
@D2:262
@H1:closed
输入样例2:
在这里给出一组输入。例如:
#T1:[IN D2-1] [D2-2 H1-2] [H1-1 OUT]
#T2:[IN D1-1] [D1-2 H1-3] [H1-1 OUT]
#M1:[T1 T2]
#T4:[IN K3-1] [K3-2 B2-1] [B2-2 OUT]
#T5:[IN K1-1] [K1-2 B1-1] [B1-2 OUT]
#M2:[T4 T5]
#T3:[VCC K2-1] [K2-2 M1-IN] [M1-OUT M2-IN] [M2-OUT GND]
#K1
#K2
end
输出样例2:
在这里给出相应的输出。例如:
@K1:closed
@K2:closed
@K3:turned on
@B1:87
@B2:0
@D1:0
@D2:262
@H1:closed
输入样例3:
在这里给出一组输入。例如:
#T1:[IN K1-1] [K1-2 B2-1] [B2-2 OUT]
#T2:[IN K2-1] [K2-2 R1-1] [R1-2 OUT]
#M1:[T1 T2]
#T3:[VCC K3-1] [K3-2 M1-IN] [M1-OUT S1-1] [S1-2 GND]
#K1
#K2
#K3
end
输出样例3:
在这里给出相应的输出。例如:
@K1:closed
@K2:closed
@K3:closed
@B2:71
@R1:180
@S1:40%
输入样例4:
在这里给出一组输入。例如:
#T1:[IN K2-1] [K2-2 D2-1] [D2-2 OUT]
#T2:[IN K3-1] [K3-2 D1-1] [D1-2 OUT]
#T3:[VCC K1-1] [K1-2 T1-IN] [T1-OUT T2-IN] [T2-OUT GND]
#K1
#K2
#K3
end
输出样例4:
在这里给出相应的输出。例如:
@K1:closed
@K2:closed
@K3:closed
@D1:200
@D2:200
输入样例5:
在这里给出一组输入。例如:
#T3:[VCC B2-1] [B2-2 K1-1] [K1-2 S1-1] [S1-2 H1-1] [H1-2 GND]
#K1
end
输出样例5:
在这里给出相应的输出。例如:
@K1:closed
@B2:95
@H1:closed
@S1:80%
代码长度限制
100 KB
时间限制
1000 ms
内存限制
64 MB
栈限制
8192 KB
我的部分代码:
import java.util.*;
class Device {
String type;
String id;
double outputVoltage;
public Device(String type, String id) {
this.type = type;
this.id = id;
this.outputVoltage = 0;
}
public String getStatus() {
return "";
}
public int fanhui(){
if(type.equals("K"))
return 9;
else if(type.equals("F"))
return 8;
else if(type.equals("L"))
return 7;
else if(type.equals("B"))
return 6;
else if(type.equals("R"))
return 5;
else if(type.equals("D"))
return 4;
else if(type.equals("A"))
return 3;
else if (type.equals("H")) {
return 2;
} else if (type.equals("S")) {
return 1;
} else
return 0;
}
public void adjust(String command) {
}
public void applyInputVoltage(double voltage) {
}
}
class Switch extends Device {
boolean isOn;
public Switch(String id) {
super("K", id);
this.isOn = false; // Initially off
}
public boolean isOn() {
return isOn;
}
@Override
public String getStatus() {
return "@K" + id + ":" + (isOn ? "closed" : "turned on");
}
@Override
public void adjust(String command) {
isOn = !isOn; // Toggle the switch
}
@Override
public void applyInputVoltage(double voltage) {
outputVoltage = isOn ? voltage : 0;
}
}
class MutexSwitch extends Device {
double R;
boolean isOn;
boolean isOn1;
public MutexSwitch(String id) {
super("H", id);
// Initially off
this.isOn = true;
this.isOn1=true;
}
@Override
public String getStatus() {
return "@H" + id + ":" +(isOn ? "closed" : "turned on");
}
public double getR() {
return R;
}
@Override
public void adjust(String command) {
isOn=!isOn;
}
public void adjust1(){
isOn1=!isOn1;
}
public void setR() {
this.R=(isOn ? 5 : 10);
}
}
class MultiSpeedController extends Device {
int speed;
public MultiSpeedController(String id) {
super("F", id);
this.speed = 0;
}
@Override
public String getStatus() {
return "@F" + id + ":" + speed;
}
@Override
public void adjust(String command) {
if (command.equals("+") && speed < 3) {
speed++;
} else if (command.equals("-") && speed > 0) {
speed--;
}
}
@Override
public void applyInputVoltage(double voltage) {
outputVoltage = speed * 0.3 * voltage;
}
}
class ContinuousController extends Device {
double setting;
public ContinuousController(String id) {
super("L", id);
this.setting = 0.00;
}
@Override
public String getStatus() {
return String.format("@L%s:%.2f", id, setting);
}
@Override
public void adjust(String command) {
setting = Double.parseDouble(command);
}
@Override
public void applyInputVoltage(double voltage) {
outputVoltage = setting * voltage;
}
}
class IncandescentLamp extends Device {
double R;
double lux;
public IncandescentLamp(String id) {
super("B", id);
this.R=10;
this.lux=0;
}
public double getR() {
return R;
}
public void setLux(double outputVoltage) {
double lux1=0;
if(outputVoltage<10){
lux1=0.0;
}
else if(outputVoltage>=10&&outputVoltage<220){
lux1=(5*outputVoltage)/7+43;
} else if (outputVoltage>=220) {
lux1=200.0;
}
this.lux=lux1;
}
public double getLux() {
return lux;
}
@Override
public String getStatus() {
return "@B" + id + ":" + (int) Math.floor(lux);
}
@Override
public void applyInputVoltage(double voltage) {
outputVoltage = voltage;
}
}
class FluorescentLamp extends Device {
double R;
double lux;
public FluorescentLamp(String id) {
super("R", id);
this.R=5;
this.lux=0;
}
public double getR() {
return R;
}
public void setLux(double outputVoltage) {
if(outputVoltage>0){
this.lux =180;
} else if (outputVoltage<=0) {
this.lux =0;
}
}
public double getLux() {
return lux;
}
@Override
public String getStatus() {
return "@R" + id + ":" + (int) Math.floor(lux);
}
@Override
public void applyInputVoltage(double voltage) {
outputVoltage = voltage;
}
}
class CeilingFan extends Device {
double R;
public CeilingFan(String id) {
super("D", id);
this.R=20;
}
public double getR() {
return R;
}
@Override
public String getStatus() {
int rpm = outputVoltage < 80 ? 0 : outputVoltage > 150 ? 360 : (int) Math.floor((outputVoltage-80)*4+80);
return "@D" + id + ":" + rpm;
}
@Override
public void applyInputVoltage(double voltage) {
outputVoltage = voltage;
}
}
class Fan extends Device{
double R;
public Fan(String id){
super("A",id);
this.R=20;
}
public double getR() {
return R;
}
@Override
public String getStatus() {
int rpm=0;
if(outputVoltage<80){
rpm=0;
} else if (outputVoltage>=80&&outputVoltage<=99) {
rpm=80;
} else if (outputVoltage>=100&&outputVoltage<=119) {
rpm=160;
} else if (outputVoltage>=120&&outputVoltage<=139) {
rpm=260;
} else if (outputVoltage>=140) {
rpm=360;
}
return "@A" + id + ":" + rpm;
}
@Override
public void applyInputVoltage(double voltage) {
outputVoltage=voltage;
}
}
class curtain extends Device{
double R;
String lux;
public curtain(String id){
super("S",id);
this.R=15;
this.lux=lux;
}
public double getR() {
return R;
}
public void setLux(double lux1) {
String ratio="";
if(outputVoltage<50){
ratio="100%";
} else if (outputVoltage>=50) {
if(lux1>=0&&lux1<50){
ratio="100%";
}
else if(lux1>=50&&lux1<100){
ratio="80%";
} else if (lux1>=100&&lux1<200) {
ratio="60%";
} else if (lux1>=200&&lux1<300) {
ratio="40%";
} else if (lux1>=300&&lux1<400) {
ratio="20%";
} else if (lux1>=400) {
ratio="0.0%";
}
}
this.lux=ratio;
}
public void applyInputVoltage(double voltage) {
this.outputVoltage=voltage;
}
@Override
public String getStatus() {
return "@S" + id + ":" + lux;
}
}
class Series{
double id;
boolean isOn;
double R;
double outputVoltage;
Map<String, Device> devices = new HashMap<>();
public Series(double id){
this.id=id;
this.R=0;
this.isOn=true;
this.outputVoltage=0;
}
public double getOutputVoltage() {
return outputVoltage;
}
public void setOutputVoltage(double outputVoltage) {
this.outputVoltage = outputVoltage;
}
public void setR(double r) {
R = r;
}
public void adjust(String command) {
isOn = false; // Toggle the switch
}
public boolean isOn() {
return isOn;
}
public double getR() {
return R;
}
public double getId() {
return id;
}
public Map<String, Device> getDevices() {
return devices;
}
public void addDevice(String key,Device device){
devices.put(key,device);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Map<String, String> connections = new HashMap<>();
ArrayList<String> change = new ArrayList<>();
Map<Double, Series> series = new HashMap<>();
Map<Double,ArrayList>Bl=new HashMap<>();
double R1=0;
for (Map.Entry<Double,ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
R1+=bl.get(bl.size()-2);
// System.out.println(entry.getKey()+"==="+R1);
}
// System.out.println(R1+" "+R2);
double voloatage3 = voloatage*R2/(R1+R2);
for (int l = 0; l < list.size(); l++) {
int count=0;
Series series4 = series.get(list.get(l));
double voloatage2=voloatage3*series4.getR()/R2;
for (Map.Entry<Double,ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
for (int i = 0; i < bl.size()-2; i++) {
Double s = bl.get(i);
if (s == series4.getId()) {
count++;
}
}
}
if (count == 0&&series4.isOn()==true) {
Map<String, Device> series2 = series4.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("B")) {
IncandescentLamp device =(IncandescentLamp) entry1.getValue();
// System.out.println(voloatage2 * 10 / series4.getR());
device.applyInputVoltage(voloatage2 * 10 / series4.getR());
device.setLux(voloatage2 * 10 / series4.getR());
} else if (did.startsWith("R")) {
FluorescentLamp device = (FluorescentLamp) entry1.getValue();
device.applyInputVoltage(voloatage2 * 5 / series4.getR());
device.setLux(voloatage2 * 5 / series4.getR());
} else if (did.startsWith("D")) {
Device device = entry1.getValue();
device.applyInputVoltage(voloatage2 * 20 / series4.getR());
} else if (did.startsWith("A")) {
Device device = entry1.getValue();
device.applyInputVoltage(voloatage2 * 20 / series4.getR());
}else if (did.startsWith("S")) {
Device device = entry1.getValue();
device.applyInputVoltage(voloatage2 * 15 / series4.getR());
}
}
}
}
// System.out.println(voloatage);
for (Map.Entry<Double,ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
// double voloatage1 = voloatage * bl.get(bl.size() - 2) / (R1 + R2);
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
for (int l = 0; l < list.size(); l++) {
Series series4 = series.get(list.get(l));
if (s == series4.getId() && series4.isOn() == true) {
Map<String, Device> series2 = series4.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("F")) {
Device device = entry1.getValue();
device.applyInputVoltage(voloatage);
voloatage = device.outputVoltage;
} else if (did.startsWith("L")) {
// System.out.println(123);
Device device = entry1.getValue();
device.applyInputVoltage(voloatage);
voloatage = device.outputVoltage;
}
}
}
}
}
}
for (Map.Entry<Double,ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
double voloatage1= voloatage*bl.get(bl.size()-2)/(R1+R2);
for (int i = 0; i < bl.size()-2; i++) {
Double s = bl.get(i);
for (int l = 0; l < list.size(); l++) {
Series series4 = series.get(list.get(l));
if (s == series4.getId()&& series4.isOn() == true) {
Map<String, Device> series2 = series4.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("B")) {
IncandescentLamp device =(IncandescentLamp) entry1.getValue();
// System.out.println(voloatage1 * 10 / series4.getR());
device.applyInputVoltage(voloatage1 * 10 / series4.getR());
device.setLux(voloatage1 * 10 / series4.getR());
} else if (did.startsWith("R")) {
FluorescentLamp device = (FluorescentLamp) entry1.getValue();
device.applyInputVoltage(voloatage1 * 5 / series4.getR());
device.setLux(voloatage1 * 5 / series4.getR());
} else if (did.startsWith("D")) {
Device device = entry1.getValue();
device.applyInputVoltage(voloatage1 * 20 / series4.getR());
} else if (did.startsWith("A")) {
Device device = entry1.getValue();
device.applyInputVoltage(voloatage1 * 20 / series4.getR());
}else if (did.startsWith("S")) {
Device device = entry1.getValue();
device.applyInputVoltage(voloatage1 * 15 / series4.getR());
}
}
}
}
}
}
// System.out.println(R1+"--"+R2);
double lux=0.0;
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if(did.startsWith("B")){
IncandescentLamp device =(IncandescentLamp) entry1.getValue();
lux+= device.getLux();
} else if (did.startsWith("R")) {
FluorescentLamp device = (FluorescentLamp) entry1.getValue();
lux+= device.getLux();
}
}
}
// System.out.println(lux);
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if(did.startsWith("S")){
curtain device=(curtain) entry1.getValue();
device.setLux(lux);
// System.out.println(device.lux+"---"+device.outputVoltage);
}
}
}
ArrayList<Device> arraylist6=new ArrayList<>();
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
arraylist6.add(entry1.getValue());
}
}
Comparator<Device> numberComparator = new Comparator<Device>()
{
public int compare(Device o1,Device o2){
int a=o1.fanhui();
int b=o2.fanhui();
String c=o1.id;
String d=o2.id;
if(a>b) return -1;
else if(a==b){
if(c.compareTo(d)>0)
return 1;
else
return -1;
}
else
return 1;
}
};
Collections.sort(arraylist6,numberComparator);
for(Device suixcs:arraylist6){
System.out.println(suixcs.getStatus());
}
}
}
分析:分析:在设计实现这个智能家居强电电路模拟系统时,可以按照以下步骤进行:
1.设计控制设备类,包括开关、分档调速器和连续调速器,定义属性和方法来模拟它们的工作状态和功能。
2.设计受控设备类,包括灯和风扇,定义属性和方法来模拟它们的工作状态和功能。
3.设计电路类,将控制设备和受控设备组合在一起,模拟整个智能家居强电电路的结构和工作过程。
4.实现输入信息的处理,包括设备信息、连接信息和控制设备调节信息的解析和处理。
5.模拟电路的工作状态,根据输入的信息进行电路模拟,包括控制设备的状态变化和受控设备的工作状态。
6.输出模拟结果,展示各个设备的工作状态和输出信息。
2.第八次pta作业
7-1 家居强电电路模拟程序-4
分数 100
作者 蔡轲
单位 南昌航空大学
智能家居是在当下家庭中越来越流行的一种配置方案,它通过物联网技术将家中的各种设备(如音视频设备、照明系统、窗帘控制、空调控制、安防系统、数字影院系统、影音服务器、影柜系统、网络家电等)连接到一起,提供家电控制、照明控制、电话远程控制、室内外遥控、防盗报警、环境监测、暖通控制、红外转发以及可编程定时控制等多种功能和手段。与普通家居相比,智能家居不仅具有传统的居住功能,兼备建筑、网络通信、信息家电、设备自动化,提供全方位的信息交互功能。请根据如下要去设计一个智能家居强电电路模拟系统。以下题目介绍中加粗的部分为本次迭代在“家居强电电路模拟程序-3”的基础上增加的功能要求。
1、控制设备
本题模拟的控制设备包括:开关、分档调速器、连续调速器、互斥开关。
开关:包括0和1两种状态。
开关有两个引脚,任意一个引脚都可以是输入引脚(接往电源端),而另一个则是输出引脚(接网接地端)。开关状态为0时,无论输入电位是多少,输出引脚电位为0。当开关状态为1时,输出引脚电位等于输入电位。
互斥开关:
互斥开关有3个引脚:1个是汇总引脚,另两个是分支引脚。
开关电路示意图如图1所示,左边是汇总引脚,编号为1;右边两个是分支引脚,右上的输出引脚为2,右下输出引脚为3。图中1、2、3引脚均可以是输入引脚,当1为输入引脚时,2、3引脚为输出引脚;1为输出引脚时,2、3引脚为输入引脚。
互斥开关只有两种状态:开关接往上面的2号引脚、接往下面的3号引脚。开关每次只能接通其中一个分支引脚,而另一个分支引脚处于断开状态。
互斥开关的默认状态为1、2引脚接通,1、3引脚断开。
图1中所示的互斥开关可以反过来接入电路,即汇总引脚接往接地端,两个分支引脚接往电源端。

图1 互斥开关
为避免短路,互斥开关设置了限流电阻,1、2引脚之间默认电阻为5,1、3引脚之间默认电阻为10。
分档调速器
按档位调整,常见的有3档、4档、5档调速器,档位值从0档-2(3/4)档变化。本次迭代模拟4档调速器,每个档位的输出电位分别为0、0.3、0.6、0.9倍的输入电压。
连续调速器
没有固定档位,按位置比例得到档位参数,数值范围在[0.00-1.00]之间,含两位小数。输出电位为档位参数乘以输入电压。
所有调速器都有两个引脚,一个固定的输入(引脚编号为1)、一个输出引脚(引脚编号为2)。当输入电位为0时,输出引脚输出的电位固定为0,不受各类开关调节的影响。
开关、调速器的初始状态/档位为0。
开关的两个引脚编号为1、2。
除互斥开关外,其他控制设备的电阻为 0。
2、受控设备
本题模拟的受控设备包括:灯、风扇。两种设备都有两根引脚,通过两根引脚电压的电压差驱动设备工作。
灯有两种工作状态:亮、灭。在亮的状态下,有的灯会因引脚电位差的不同亮度会有区别。
风扇在接电后有两种工作状态:停止、转动。风扇的转速会因引脚间电位差的不同而有区别。
本次迭代模拟两种灯具。
白炽灯:
亮度在0~200lux(流明)之间。
电位差为0-9V时亮度为0,其他电位差按比例,电位差10V对应50ux,220V对应200lux,其他电位差与对应亮度值成正比。白炽灯超过220V。
日光灯:
亮度为180lux。
只有两种状态,电位差为0时,亮度为0,电位差不为0,亮度为180。
本次迭代模拟两种风扇。
吊扇:
工作电压区间为80V-150V,对应转速区间为80-360转/分钟。80V对应转速为80转/分钟,150V对应转速为360转/分钟,超过150V转速为360转/分钟(本次迭代暂不考虑电压超标的异常情况)。其他电压值与转速成正比,输入输出电位差小于80V时转速为0。
落地扇:
工作电压区间为 80V-150V,对应转速区间为 80-360 转/分钟;[80V,100V) 对应转速为 80 转/分钟;[100,120)V 对应转速为 160 转/分钟;[120,140)V 对应转速为 260 转/分钟;大于等于 140V 转速 为 360 转/分钟(本次迭代暂不考虑电压超标的异常情况)。
本次迭代模拟一种受控窗帘:
受控串联的电路符号为S,其最低工作电压为50V,电压达到或超过50V,窗帘即可正常工作,不考虑室外光照强度和室内空间大小等因素,窗帘受室内灯光的光照强度控制。
当电路中所有灯光的光照强度总和在[0,50)lux范围内,窗帘全开;
在[50,100)lux范围内,窗帘打开比例为0.8;
在[100,200)lux范围内,窗帘打开比例为0.6;
在[200,300)lux范围内,窗帘打开比例为0.4;
在[300,400)lux范围内,窗帘打开比例为0.2;
在400lux及以上范围内,窗帘关闭。
当电压低于50V,窗帘不工作,默认为全开状态。
如果电路中没有灯或者灯全部关闭,光照强度为0,窗帘处于全开状态。
受控设备电阻:白炽灯的电阻为 10,日光灯的电阻为 5,吊扇的电阻为 20,落地扇的电阻为 20,窗帘电阻为15。
3、输入信息
1)输入设备信息
分别用设备标识符K、F、L、B、R、D、A、H、S、P分别表示开关、分档调速器、连续调速器、白炽灯、日光灯、吊扇、落地扇、互斥开关、受控窗帘、二极管(见第6部分说明)。
设备标识用标识符+编号表示,如K1、F3、L2等。
引脚格式:
设备标识-引脚编号,例如:K1-1标识编号为1的开关的1号引脚。
开关、分档调速器、连续调速器的两个引脚编号为1、2。
受控设备的两个引脚编号分别为1、2。
互斥开关的引脚编号已经在互斥开关的介绍部分说明。
约束条件:
不同设备的编号可以相同。
设备信息不单独输入,包含在连接信息中。
2)输入连接信息
一条连接信息占一行,用[]表示一组连接在一起的设备引脚,引脚与引脚之间用英文空格" "分隔。
格式:
"["+引脚号+" "+引脚号+"]"
例如:[K1-1 K3-2]表示K1的1号引脚,K3的2号引脚连接在一起。
约束条件:
不考虑调速器串联到其他调速器的情况。
考虑各类设备的并联接入。例如,K1 的输出接到 L2 的输入,L2 的输出再接其他设备属于串联接线。K1 的输出接到 L2 的输出,同时 K1 的输入接到 L2 的输入,这种情况属于并联。
连接信息不单独输入,包含在线路信息中。
3)输入控制设备调节信息
开关、互斥开关调节信息格式:
#+设备标识K+设备编号,例如:#K2,代表切换K2开关的状态。
#+设备标识H+设备编号,例如:#H2,代表切换H2互斥开关的状态。
分档调速器的调节信息格式:
#+设备标识F+设备编号+"+" 代表加一档,例如:#F3+,代表F3输出加一档。
#+设备标识F+设备编号+"-" 代表减一档,例如:#F1-,代表F1输出减一档。
连续调速器的调节信息格式:
#+设备标识L+设备编号+":" +数值 代表将连续调速器的档位设置到对应数值,例如:#L3:0.6,代表L3输出档位参数0.6。
4)电源接地标识:
VCC,电压220V,GND,电压0V。没有接线的引脚默认接地,电压为0V。
5)输入串联电路信息
一条串联电路占一行,串联电路信息由 n 个连接信息组成,连接信息按从靠电源端到接地端顺序依次输入,连接信息之间用英文空格" "分隔。
串联电路信息格式:
"#T"+电路编号+":"+连接信息+" "+连接信息+...+" "+连接信息
例如:/#T1:[IN K1-1] [K1-2 D2-1] [D2-2 OUT] 一个串联电路的第一个引脚是 IN,代表起始端,靠电源。最后一个引脚是 OUT,代表结尾端, 靠接地。
约束条件:
不同的串联电路信息编号不同。
输入的最后一条电路信息必定是总电路信息,总电路信息的起始引脚是 VCC,结束引脚是 GND。
连接信息中的引脚可能是一条串联或并联电路的 IN 或者 OUT。例如:
#T1:[IN K1-1] [K1-2 T2-IN] [T2-OUT OUT]
#T1:[IN K1-1] [K1-2 T2-IN] [T2-OUT M2-IN] [M2-OUT OUT]
6)输入并联电路信息
一条并联电路占一行,并联电路由其包含的几条串联电路组成,串联电路标识之间用英文空格" "分隔。
格式:
"#M"+电路编号+":"+”[”+串联电路信息+" "+....+" "+串联电路信息+”]”
例如:#M1:[T1 T2 T3]
该例声明了一个并联电路,由 T1、T2、T3 三条串联电路并联而成,三条串联电路的 IN 短接在一起构成 M1 的 IN,三条串联电路的 OUT 短接在一起构成 M1 的 OUT。
在本题中,并联电路M中的串联电路可以包含别的并联电路。
约束条件:
本题不考虑输入电压或电压差超过220V的情况。
输入信息以end为结束标志,忽略end之后的输入信息。
本题中的并联信息所包含的串联电路的信息都在并联信息之前输入,不考虑乱序输入的情况。
只要不因短路而造成无穷大的电流烧坏电路(如电路中的部分短接),都是合理情况。
本次迭代考虑多个并联电路串联在一起的情况。
本题考虑一条串联电路中包含其他串联电路和并联电路的情况。例如:
#T3:[VCC K1-1] [K1-2 T2-IN] [T2-OUT K2-1] [K2-2 T1-IN] [T1-OUT GND]
本例中T1\T2两条串联电路是T3的一个部分,本题考虑这种类型的输入。
4、输出信息:
按开关、分档调速器、连续调速器、白炽灯、日光灯、吊扇、互斥开关、受控窗帘、二极管(见第6部分说明)的顺序依次输出所有设备的状态或参数。每个设备一行。同类设备按编号顺序从小到大输出。
输出格式:
@设备标识+设备编号+":" +设备参数值(控制开关的档位或状态、灯的亮度、风扇的转速,只输出值,不输出单位)+" "+设备所有引脚的电压(按编号从小到大顺序依次输出,电压的小数部分直接丢弃,保留整数输出,电压之间用”-”分隔)
说明:
连续调速器的档位信息保留两位小数,即使小数为0,依然显示两位小数.00。
开关状态为0(打开)时显示turned on,状态为1(合上)时显示closed
如:
@K1:turned on 32-15
@B1:190 68-17
@L1:0.60 220-176
互斥开关按1、2引脚的接通状态显示,1,2接通-1,3断开时显示closed,1,3接通-1,2断开时显示turned on。
如:
@H1:turned on
受控窗帘显示窗帘打开的百分比,如:
@S1:80%
5、家居电路模拟系列所有题目的默认规则
1)当计算电压值等数值的过程中,最终结果出现小数时,用截尾规则去掉小数部分,只保留整数部分。为避免精度的误差,所有有可能出现小数的数值用double类型保存并计算,不要作下转型数据类型转换,例如电压、转速、亮度等,只有在最后输出时再把计算结果按截尾规则,舍弃尾数,保留整数输出。
2)所有连接信息按电路从靠电源端到靠接地端的顺序依次输入,不会出现错位的情况。VCC/IN一定是第一个连接的第一项,GND/OUT一定是最后一个连接的后一项。
3)连接信息如果只包含两个引脚,靠电源端的引脚在前,靠接地端的在后。
4)调速器的输入端只会直连VCC,不会接其他设备。整个电路最多只有连接在电源上的一个调速器,且不包含在并联单路中。
5)本系列题目中元件的管脚除了互斥开关的1引脚,其他所有引脚在电路中最多只出现一次。
6、本题新增内容:
1)增加管脚电压的显示
在输出每个电器的状态信息后,再依次输出该电器每个管脚的电压。(格式详见输出信息部分)
2)电流限制
电器在工作时,过大的电流会引起电器过热,从而烧坏电路。本次迭代,每个元器件都有最大电流的设置,当实时电流超过最大电流时,在该电器输出信息的最后加入提示“exceeding current limit error”,与前面的信息之间用英文空格分隔。
例如:@B1:190 68-17 exceeding current limit error
本题各类电器的最大限定电流如下:
开关20、分档调速器18、连续调速器18、白炽灯9、日光灯5、吊扇12、落地扇14、互斥开关20、受控窗帘12、二极管8。
3)短路检测
如果电路出现无穷大的电流造成短路,所有元器件信息不输出,仅输出提示“short circuit error”
4)并联电路中包含并联
本次迭代考虑并联电路中包含并联电路的情况,即构成并联电路的串联电路可以包含别的并联电路。例如如下输入的电路,并联电路M2的其中一条串联电路T4中包含了另一条并联电路M1:
#T1:[IN D2-1] [D2-2 H1-2] [H1-1 OUT]
#T2:[IN D1-1] [D1-2 H1-3] [H1-1 OUT]
#M1:[T1 T2]
#T4:[IN K3-1] [K3-2 M1-IN] [M1-OUT OUT]
#T5:[IN K1-1] [K1-2 B1-1] [B1-2 OUT]
/#M2:[T4 T5]
5)二极管
增加二极管元件,其电路特性为:正向导通,反向截止;其电器符号如图4所示,当电流从左至右流过时,二极管导通”conduction”,电阻为0;电流从右至左流动时,二极管截止”cutoff”,电阻无穷大,相当于开关打开。

图2 二极管符号
二极管的标识符为’P’,左侧管脚编号为1,右侧管脚编号为2。
二极管如果两端电压相等,没有电流流过,分以下两种情况输出:
1、如果两端电压为0,二极管的导通/截止状态由接入方向决定,1号引脚靠近电源则状态为导通,反之为截止。
2、如果两端电压不为0,二极管导通。
7、设计建议
本题包含以下电路图中的所有连接情况:

图3 电路示意图
1、电路设备类:描述所有电路设备的公共特征。
2、受控设备类、控制设备类:对应受控、控制设备
3、串联电路类:一条由多个电路设备构成的串联电路,也看成是一个独立的电路设备
4、并联电路类:继承电路设备类,也看成是一个独立的电路设备
其他类以及类的属性、方法自行设计。

图4:建议设计类图
输入样例1:
在这里给出一组输入。例如:
#T1:[IN P2-2] [P2-1 H1-2] [H1-1 OUT]
#T2:[IN D1-1] [D1-2 H1-3] [H1-1 OUT]
#M1:[T1 T2]
#T4:[IN K3-1] [K3-2 M1-IN] [M1-OUT OUT]
#T5:[IN K1-1] [K1-2 B1-1] [B1-2 OUT]
#M2:[T4 T5]
#T3:[VCC K2-1] [K2-2 M2-IN] [M2-OUT GND]
#K1
#K2
end
输出样例1:
在这里给出相应的输出。例如:
@K1:closed 220-220 exceeding current limit error
@K2:closed 220-220 exceeding current limit error
@K3:turned on 220-0
@B1:200 220-0 exceeding current limit error
@D1:0 0-0
@H1:closed 0-0-0
@P2:cutoff 0-0
输入样例2:
在这里给出一组输入。例如:
#T1:[IN P2-1] [P2-2 H1-2] [H1-1 OUT]
#T2:[IN D1-1] [D1-2 H1-3] [H1-1 OUT]
#M1:[T1 T2]
#T4:[IN K3-1] [K3-2 M1-IN] [M1-OUT OUT]
#T5:[IN K1-1] [K1-2 B1-1] [B1-2 OUT]
#M2:[T4 T5]
#T3:[VCC K2-1] [K2-2 M2-IN] [M2-OUT GND]
#K1
#K3
#K2
end
输出样例2:
在这里给出相应的输出。例如:
@K1:closed 220-220 exceeding current limit error
@K2:closed 220-220 exceeding current limit error
@K3:closed 220-220 exceeding current limit error
@B1:200 220-0 exceeding current limit error
@D1:0 220-220
@H1:closed 0-220-220 exceeding current limit error
@P2:conduction 220-220 exceeding current limit error
输入样例3:
在这里给出一组输入。例如:
#T1:[IN P2-2] [P2-1 H1-2] [H1-1 OUT]
#T2:[IN D1-1] [D1-2 H1-3] [H1-1 OUT]
#M1:[T1 T2]
#T4:[IN K3-1] [K3-2 M1-IN] [M1-OUT OUT]
#T5:[IN K1-1] [K1-2 B1-1] [B1-2 OUT]
#M2:[T4 T5]
#T3:[VCC K2-1] [K2-2 M2-IN] [M2-OUT GND]
#K1
#K3
#K2
end
输出样例3:
在这里给出相应的输出。例如:
@K1:closed 220-220 exceeding current limit error
@K2:closed 220-220 exceeding current limit error
@K3:closed 220-220
@B1:200 220-0 exceeding current limit error
@D1:0 220-220
@H1:closed 0-0-220
@P2:cutoff 0-220
输入样例4:
在这里给出一组输入。例如:
#T3:[VCC K2-1] [K2-2 GND]
#K2
end
输出样例4:
在这里给出相应的输出。例如:
short circuit error
输入样例5:
在这里给出一组输入。如果两端电压为0,二极管的导通/截止状态由接入方向决定,1号引脚靠近电源则状态为导通,反之截止。 例如:
#T3:[VCC K2-2] [K2-1 P1-1] [P1-2 K1-2] [K1-1 GND]
end
输出样例5:
在这里给出相应的输出。例如:
@K1:turned on 0-0
@K2:turned on 0-220
@P1:conduction 0-0
输入样例6:
在这里给出一组输入。如果两端电压为0,二极管的导通/截止状态由接入方向决定,1号引脚靠近电源则状态为导通,反之截止。 例如:
#T3:[VCC K2-2] [K2-1 P1-2] [P1-1 K1-2] [K1-1 GND]
end
输出样例6:
####### 。例如:
@K1:turned on 0-0
@K2:turned on 0-220
@P1:cutoff 0-0
代码长度限制
100 KB
时间限制
1500 ms
内存限制
64 MB
栈限制
8192 KB
我的部分代码:
import java.util.*;
class Device {
String type;
String id;
double oV;
double current;
int v1;
int v2;
boolean isOn4;
public Device(String type, String id) {
this.type = type;
this.id = id;
this.current=0;
this.oV = 0;
this.v1=0;
this.v2=0;
this.isOn4=true;
}
public String getStatus() {
return "";
}
public void adjust4(){
isOn4=!isOn4;
}
public int fanhui(){
if(type.equals("K"))
return 11;
else if(type.equals("F"))
return 10;
else if(type.equals("L"))
return 9;
else if(type.equals("B"))
return 8;
else if(type.equals("R"))
return 7;
else if(type.equals("D"))
return 6;
else if(type.equals("A"))
return 5;
else if (type.equals("H")) {
return 4;
} else if (type.equals("S")) {
return 3;
}
else if (type.equals("P")) {
return 2;
} else if (type.equals("M")) {
return 1;
} else
return 0;
}
public void adjust(String command) {
}
public void setCurrent() {
}
public void setVolotage(double v1,double v2) {
this.v1 =v1>0?(int)Math.floor(v1):0;
this.v2=v1>0?(int)Math.floor(v2):0;
if(this.v1<0){
this.v1=0;
}
if(this.v2<0){
this.v2=0;
}
}
public void applyInputVoltage(double voltage) {
oV = voltage;
}
}
class D extends Device{
boolean isOn;
double R;
public D(String id) {
super("P", id);
this.isOn = true; // Initially off
this.R=0;
}
@Override
public void adjust(String command) {
isOn = !isOn; // Toggle the S
}
public void setR() {
this.R=isOn? 0:99999999;
}
public void setCurrent(double current) {
this.current=current;
}
@Override
public String getStatus() {
return "@P" + id + ":" + (isOn ? "conduction" : "cutoff")+(isOn?" "+v1+"-"+v2:" "+v2+"-"+v1)+(current>8?" exceeding current limit error":"");
}
}
class S extends Device {
boolean isOn;
public S(String id) {
super("K", id);
this.isOn = false;
}
public boolean isOn() {
return isOn;
}
public void setCurrent(double current) {
this.current=current;
}
@Override
public String getStatus() {
return "@K" + id + ":" + (isOn ? "closed" : "turned on")+(isOn4?" "+v1+"-"+v2:" "+v2+"-"+v1)+(current>20?" exceeding current limit error":"");
}
@Override
public void adjust(String command) {
isOn = !isOn;
}
@Override
public void applyInputVoltage(double voltage) {
oV = isOn ? voltage : 0;
}
}
class MS extends Device {
double R;
boolean isOn;
boolean isOn1;
boolean isOn2;
int volotage3;
public MS(String id) {
super("H", id);
// Initially off
this.isOn = true;
this.isOn1=true;
this.isOn2=true;
}
@Override
public String getStatus() {
if(this.isOn1==true) {
return "@H" + id + ":" + (isOn ? "closed" : "turned on") + " " + (isOn2 ? v1 : 0) + "-" + v2 + "-" + volotage3 + (current > 20 ? " exceeding current limit error" : "");
}
return "null";
}
public void setCurrent(double current) {
this.current=current;
}
public double getR() {
return R;
}
public void setVolotage(double v1,double v2,double volotage3) {
this.v1 =v1>=0?(int)Math.floor(v1):0;
this.v2=v2>=0?(int)Math.floor(v2):0;
this.volotage3=volotage3>=0?(int)Math.floor(volotage3):0;
}
@Override
public void adjust(String command) {
isOn=!isOn;
}
public void adjust1(){
isOn1=!isOn1;
}
public void adjust2(){
isOn2=!isOn2;
}
public void setR() {
this.R=(isOn ? 5 : 10);
}
}
class Mu extends Device {
int speed;
public Mu(String id) {
super("F", id);
this.speed = 0;
}
public void setCurrent(double current) {
this.current=current;
}
@Override
public String getStatus() {
return "@F" + id + ":" + speed+(isOn4?" "+v1+"-"+v2:" "+v2+"-"+v1)+(current>18?" exceeding current limit error":"");
}
@Override
public void adjust(String command) {
if (command.equals("+") && speed < 3) {
speed++;
} else if (command.equals("-") && speed > 0) {
speed--;
}
}
@Override
public void applyInputVoltage(double voltage) {
oV = speed * 0.3 * voltage;
}
}
public static void Process(ArrayList<String> change, LinkedHashMap<Double, Series> series, Map<Double,ArrayList>Bl,int flag,String s1,double flag3) {
ArrayList<String> arraylist2 = new ArrayList<>();
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
arraylist2.add(entry1.getKey());
}
}
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (int i = 0; i < change.size(); i++) {
String did = change.get(i).trim();
if (did.startsWith("K")) {
String deviceID = did;
Device device = series2.get(deviceID);
if (device != null) {
device.adjust("1");
}
} else if (did.startsWith("H")) {
String deviceID = did;
MS device = (MS) series2.get(deviceID);
if (device != null) {
device.adjust1();
device.adjust("1");
}
} else if (did.startsWith("F")) {
String deviceID = did.substring(0, did.length() - 1);
String command = did.substring(did.length() - 1);
Device device = series2.get(deviceID);
if (device != null) {
device.adjust(command);
}
} else if (did.startsWith("L")) {
String parts[] = did.split(":");
String deviceID = parts[0];
String command = parts[1];
Device device = series2.get(deviceID);
if (device != null) {
device.adjust(command);
}
}
}
}
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
double R = 0;
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("K")) {
S device = (S) entry1.getValue();
if (device.isOn() == false) {
series1.adjust("1");
}
} else if (did.startsWith("B")) {
I device = (I) entry1.getValue();
R += device.getR();
} else if (did.startsWith("R")) {
F device = (F) entry1.getValue();
R += device.getR();
} else if (did.startsWith("D")) {
Ce device = (Ce) entry1.getValue();
R += device.getR();
} else if (did.startsWith("A")) {
Fan device = (Fan) entry1.getValue();
R += device.getR();
} else if (did.startsWith("H")) {
MS device = (MS) entry1.getValue();
if (device.isOn1 == false)
series1.adjust("1");
device.setR();
R += device.getR();
} else if (did.startsWith("S")) {
curtain device = (curtain) entry1.getValue();
R += device.getR();
} else if (did.startsWith("P")) {
D device = (D) entry1.getValue();
device.setR();
R += device.R;
}
}
series1.setR(R);
}
Set<Double> set1 = series.keySet();
ArrayList<Double> list1 = new ArrayList<>(set1);
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
double R1 = 0.0;
ArrayList<Double> bl = entry.getValue();
for (int i = 0; i < bl.size(); i++) {
Double s = bl.get(i);
for (int l = 0; l < list1.size(); l++) {
Series series4 = series.get(list1.get(l));
if (s == series4.getId() && series4.isOn) {
if (R1 == 0) {
R1 = series4.getR();
} else if (R1 > 0) {
R1 = R1 * series4.getR() / (R1 + series4.getR());
}
}
}
}
for (int i = 0; i < bl.size(); i++) {
Double s = bl.get(i);
for (int l = 0; l < list1.size(); l++) {
Series series4 = series.get(list1.get(l));
if (s == series4.getId() && series4.R == 0 && series4.isOn == true) {
R1 = 0.0;
}
}
}
bl.set(bl.size() - 2, R1);
}
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("M")) {
int flag6=0;
Parallel device = (Parallel) entry1.getValue();
for (Map.Entry<Double, ArrayList> entry2 : Bl.entrySet()) {
if (Double.parseDouble(device.id) == entry2.getKey()) {
ArrayList<Double> bl = entry2.getValue();
device.setR(bl.get(bl.size() - 2));
bl.set(bl.size() - 1, -1.0);
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
for (Map.Entry<Double, Series> entry3 : series.entrySet()) {
Series series4 = entry3.getValue();
if (s == series4.getId()) {
if(series4.isOn==false){
flag6++;
}
device.addSeries(s, series4);
}
}
}
if(flag6== bl.size()-2){
device.setR(0);
}
}
}
}
}
}
for (Map.Entry<Double, ArrayList> entry2 : Bl.entrySet()) {
ArrayList<Double> bl = entry2.getValue();
if (bl.get(bl.size() - 1) == -1.0) {
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
Iterator<Map.Entry<Double, Series>> iterator = series.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Double, Series> entry1 = iterator.next();
if (Double.compare(s, entry1.getKey()) == 0) {
iterator.remove();
// System.out.println("11");
}
}
}
}
}
Iterator<Map.Entry<Double, ArrayList>> iterator = Bl.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Double, ArrayList> entry1 = iterator.next();
ArrayList<Double> bl = entry1.getValue();
if (bl.get(bl.size() - 1) == -1.0) {
// System.out.println("11");
iterator.remove();
}
}
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
double R = 0;
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("K")) {
S device = (S) entry1.getValue();
if (!device.isOn()) {
series1.adjust("1");
}
} else if (did.startsWith("B")) {
I device = (I) entry1.getValue();
R += device.getR();
} else if (did.startsWith("R")) {
F device = (F) entry1.getValue();
R += device.getR();
} else if (did.startsWith("D")) {
Ce device = (Ce) entry1.getValue();
R += device.getR();
} else if (did.startsWith("A")) {
Fan device = (Fan) entry1.getValue();
R += device.getR();
} else if (did.startsWith("H")) {
MS device = (MS) entry1.getValue();
device.setR();
R += device.getR();
} else if (did.startsWith("S")) {
curtain device = (curtain) entry1.getValue();
R += device.getR();
} else if (did.startsWith("M")) {
Parallel device = (Parallel) entry1.getValue();
R += device.getR();
} else if (did.startsWith("P")) {
D device = (D) entry1.getValue();
device.setR();
R += device.R;
}
}
series1.setR(R);
}
Set<Double> set = series.keySet();
ArrayList<Double> list = new ArrayList<>(set);
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
double R1 = 0.0;
ArrayList<Double> bl = entry.getValue();
for (int i = 0; i < bl.size(); i++) {
Double s = bl.get(i);
for (int l = 0; l < list.size(); l++) {
Series series4 = series.get(list.get(l));
if (s == series4.getId() && series4.isOn) {
if (R1 == 0) {
R1 = series4.getR();
} else if (R1 > 0) {
R1 = R1 * series4.getR() / (R1 + series4.getR());
}
}
}
}
for (int i = 0; i < bl.size(); i++) {
Double s = bl.get(i);
for (int l = 0; l < list.size(); l++) {
Series series4 = series.get(list.get(l));
if (s == series4.getId() && series4.R == 0 && series4.isOn) {
R1 = 0.00001;
}
}
}
bl.set(bl.size() - 2, R1);
}
double R2 = 0.0;
double v = 220.01;
double R1 = 0;
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
R1 += bl.get(bl.size() - 2);
}
for (int l = 0; l < list.size(); l++) {
int count = 0;
Series series4 = series.get(list.get(l));
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
if (s == series4.getId()) {
count++;
}
}
}
if (count == 0 && series4.isOn) {
R2 += series4.getR();
}
}
for (int j = 0; j < list.size(); j++) {
int count1 = 0;
Series series4 = series.get(list.get(j));
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
if (s == series4.getId()) {
count1++;
}
}
}
if (count1 == 0 && series4.isOn == false) {
v = 0;
}
}
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
int flag6 = 0;
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
for (int l = 0; l < list.size(); l++) {
Series series4 = series.get(list.get(l));
if (s == series4.getId() && series4.isOn() == false) {
flag6++;
}
}
if (flag6 == bl.size() - 2) {
v = 0;
}
}
}
for (int j = 0; j < list.size(); j++) {
int count1 = 0;
Series series4 = series.get(list.get(j));
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
if (s == series4.getId()) {
count1++;
}
}
}
if (count1 == 0 && series4.isOn) {
Map<String, Device> series2 = series4.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("K")) {
Device device = entry1.getValue();
device.applyInputVoltage(v);
v = device.oV;
}
}
}
}
for (int j = 0; j < list.size(); j++) {
Series series4 = series.get(list.get(j));
int count2 = 0;
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
if (s == series4.getId()) {
count2++;
}
}
}
if (count2 == 0 && series4.isOn) {
Map<String, Device> series2 = series4.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("F")) {
Device device = entry1.getValue();
device.applyInputVoltage(v);
v = device.oV;
} else if (did.startsWith("L")) {
Device device = entry1.getValue();
device.applyInputVoltage(v);
v = device.oV;
}
}
}
}
double I = v / (R1 + R2);
if (I > 9999999) {
System.out.println("short circuit error");
return;
}
double v3 = v * R2 / (R1 + R2);
for (int l = 0; l < list.size(); l++) {
int count = 0;
Series series4 = series.get(list.get(l));
double v2 = v3 * series4.getR() / R2;
double I1 = v2 / series4.getR();
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
if (s == series4.getId()) {
count++;
}
}
}
if (count == 0 && series4.isOn()) {
Map<String, Device> series2 = series4.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("F")) {
Mu device = (Mu) entry1.getValue();
device.setCurrent(I);
} else if (did.startsWith("L")) {
Co device = (Co) entry1.getValue();
device.setCurrent(I);
} else if (did.startsWith("K")) {
S device = (S) entry1.getValue();
device.setCurrent(I);
} else if (did.startsWith("B")) {
I device = (I) entry1.getValue();
device.applyInputVoltage(v2 * 10 / series4.getR());
device.setLux(v2 * 10 / series4.getR());
} else if (did.startsWith("R")) {
F device = (F) entry1.getValue();
device.applyInputVoltage(v2 * 5 / series4.getR());
device.setLux(v2 * 5 / series4.getR());
} else if (did.startsWith("D")) {
Device device = entry1.getValue();
device.applyInputVoltage(v2 * 20 / series4.getR());
} else if (did.startsWith("A")) {
Device device = entry1.getValue();
device.applyInputVoltage(v2 * 20 / series4.getR());
} else if (did.startsWith("S")) {
Device device = entry1.getValue();
device.applyInputVoltage(v2 * 15 / series4.getR());
} else if (did.startsWith("P")) {
D device = (D) entry1.getValue();
device.setCurrent(I1);
device.applyInputVoltage(v2 * device.R / series4.getR());
} else if (did.startsWith("H")) {
MS device1 = (MS) entry1.getValue();
device1.setCurrent(I1);
device1.applyInputVoltage(v2 * device1.R / series4.getR());
}
}
}
}
for (Map.Entry<Double, ArrayList> entry : Bl.entrySet()) {
ArrayList<Double> bl = entry.getValue();
double v1 = v * bl.get(bl.size() - 2) / (R1 + R2);
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
for (int l = 0; l < list.size(); l++) {
Series series4 = series.get(list.get(l));
double I2 = v1 / series4.getR();
if (s == series4.getId() && series4.isOn()) {
Map<String, Device> series2 = series4.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("B")) {
I device = (I) entry1.getValue();
device.applyInputVoltage(v1 * 10 / series4.getR());
device.setLux(v1 * 10 / series4.getR());
} else if (did.startsWith("R")) {
F device = (F) entry1.getValue();
device.applyInputVoltage(v1 * 5 / series4.getR());
device.setLux(v1 * 5 / series4.getR());
} else if (did.startsWith("D")) {
Device device = entry1.getValue();
device.applyInputVoltage(v1 * 20 / series4.getR());
} else if (did.startsWith("A")) {
Device device = entry1.getValue();
device.applyInputVoltage(v1 * 20 / series4.getR());
} else if (did.startsWith("S")) {
Device device = entry1.getValue();
device.applyInputVoltage(v1 * 15 / series4.getR());
} else if (did.startsWith("K")) {
S device1 = (S) entry1.getValue();
device1.setCurrent(I2);
} else if (did.startsWith("H")) {
MS device = (MS) entry1.getValue();
device.setCurrent(I2);
device.applyInputVoltage(v1 * device.R / series4.getR());
} else if (did.startsWith("P")) {
D device1 = (D) entry1.getValue();
device1.setCurrent(I2);
device1.applyInputVoltage(v1 * device1.R / series4.getR());
} else if (did.startsWith("M")) {
Parallel device = (Parallel) entry1.getValue();
double v4 = v1 * device.getR() / series4.getR();
device.applyInputVoltage(v1 * device.getR() / series4.getR());
Map<Double, Series> series5 = device.getSeries();
for (Map.Entry<Double, Series> entry2 : series5.entrySet()) {
Series series6 = entry2.getValue();
Map<String, Device> series7 = series6.getDevices();
double I3 = v4 / series6.getR();
if (series6.isOn()) {
for (Map.Entry<String, Device> entry3 : series7.entrySet()) {
String did1 = entry3.getKey();
if (did1.startsWith("B")) {
I device1 = (I) entry3.getValue();
device1.applyInputVoltage(v4 * 10 / series6.getR());
device1.setLux(v4 * 10 / series6.getR());
} else if (did1.startsWith("R")) {
F device1 = (F) entry3.getValue();
device1.applyInputVoltage(v4 * 5 / series6.getR());
device1.setLux(v4 * 5 / series6.getR());
} else if (did1.startsWith("D")) {
Device device1 = entry3.getValue();
device1.applyInputVoltage(v4 * 20 / series6.getR());
} else if (did1.startsWith("A")) {
Device device1 = entry3.getValue();
device1.applyInputVoltage(v4 * 20 / series6.getR());
} else if (did1.startsWith("S")) {
Device device1 = entry3.getValue();
device1.applyInputVoltage(v4 * 15 / series6.getR());
} else if (did1.startsWith("P")) {
D device1 = (D) entry3.getValue();
device1.setCurrent(I3);
device1.applyInputVoltage(v4 * device1.R / series6.getR());
} else if (did1.startsWith("K")) {
S device1 = (S) entry3.getValue();
device1.setCurrent(I3);
} else if (did1.startsWith("H")) {
MS device1 = (MS) entry3.getValue();
device1.setCurrent(I3);
device1.applyInputVoltage(v4 * device1.R / series6.getR());
} else if (did1.startsWith("M")) {
Parallel device1 = (Parallel) entry3.getValue();
double v5 = v4 * device1.getR() / series6.getR();
device1.applyInputVoltage(v5);
Map<Double, Series> series8 = device1.getSeries();
for (Map.Entry<Double, Series> entry4 : series8.entrySet()) {
Series series9 = entry4.getValue();
Map<String, Device> series10 = series9.getDevices();
double I4 = v5 / series9.getR();
if (series9.isOn()) {
for (Map.Entry<String, Device> entry5 : series10.entrySet()) {
String did2 = entry5.getKey();
if (did2.startsWith("B")) {
I device2 = (I) entry5.getValue();
device2.applyInputVoltage(v5 * 10 / series9.getR());
device2.setLux(v5 * 10 / series9.getR());
} else if (did2.startsWith("R")) {
F device2 = (F) entry5.getValue();
device2.applyInputVoltage(v5 * 5 / series9.getR());
device2.setLux(v5 * 5 / series9.getR());
} else if (did2.startsWith("D")) {
Device device2 = entry5.getValue();
device2.applyInputVoltage(v5 * 20 / series9.getR());
} else if (did2.startsWith("A")) {
Device device2 = entry5.getValue();
device2.applyInputVoltage(v5 * 20 / series9.getR());
} else if (did2.startsWith("S")) {
Device device2 = entry5.getValue();
device2.applyInputVoltage(v5 * 15 / series9.getR());
} else if (did2.startsWith("P")) {
D device2 = (D) entry5.getValue();
device2.setCurrent(I4);
device1.applyInputVoltage(v4 * device1.R / series6.getR());
} else if (did2.startsWith("K")) {
S device2 = (S) entry5.getValue();
device2.setCurrent(I4);
} else if (did2.startsWith("H")) {
MS device2 = (MS) entry5.getValue();
device2.setCurrent(I4);
device2.applyInputVoltage(v5 * device1.R / series9.getR());
}
}
}
}
}
}
}
}
}
}
}
}
}
}
double lux = 0.0;
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("B")) {
I device = (I) entry1.getValue();
lux += device.getLux();
} else if (did.startsWith("R")) {
F device = (F) entry1.getValue();
lux += device.getLux();
} else if (did.startsWith("M")) {
Parallel device = (Parallel) entry1.getValue();
Map<Double, Series> series5 = device.getSeries();
for (Map.Entry<Double, Series> entry2 : series5.entrySet()) {
Series series6 = entry2.getValue();
Map<String, Device> series7 = series6.getDevices();
for (Map.Entry<String, Device> entry3 : series7.entrySet()) {
String did1 = entry3.getKey();
if (did1.startsWith("B")) {
I device1 = (I) entry3.getValue();
lux += device1.getLux();
} else if (did1.startsWith("R")) {
F device1 = (F) entry3.getValue();
lux += device1.getLux();
}
else if (did1.startsWith("M")) {
Parallel device1 = (Parallel) entry3.getValue();
Map<Double, Series> series8 = device1.getSeries();
for (Map.Entry<Double, Series> entry4 : series8.entrySet()) {
Series series9 = entry4.getValue();
Map<String, Device> series10 = series9.getDevices();
if (series9.isOn()) {
for (Map.Entry<String, Device> entry5 : series10.entrySet()) {
String did2 = entry5.getKey();
if (did2.startsWith("B")) {
I device2 = (I) entry5.getValue();
lux += device2.getLux();
} else if (did1.startsWith("R")) {
F device2 = (F) entry5.getValue();
lux += device2.getLux();
}
}
}
}
}
}
}
}
}
}
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("S")) {
curtain device = (curtain) entry1.getValue();
device.setLux(lux);
} else if (did.startsWith("M")) {
Parallel device = (Parallel) entry1.getValue();
Map<Double, Series> series5 = device.getSeries();
for (Map.Entry<Double, Series> entry2 : series5.entrySet()) {
Series series6 = entry2.getValue();
Map<String, Device> series7 = series6.getDevices();
if (series6.isOn()) {
for (Map.Entry<String, Device> entry3 : series7.entrySet()) {
String did1 = entry3.getKey();
if (did1.startsWith("S")) {
curtain device1 = (curtain) entry3.getValue();
device1.setLux(lux);
} else if (did1.startsWith("M")) {
Parallel device1 = (Parallel) entry3.getValue();
Map<Double, Series> series8 = device1.getSeries();
for (Map.Entry<Double, Series> entry4 : series8.entrySet()) {
Series series9 = entry4.getValue();
Map<String, Device> series10 = series9.getDevices();
if (series9.isOn()) {
for (Map.Entry<String, Device> entry5 : series10.entrySet()) {
String did2 = entry5.getKey();
if (did2.startsWith("S")) {
curtain device2 = (curtain) entry5.getValue();
device2.setLux(lux);
}
}
}
}
}
}
}
}
}
}
}
Map<String, Double> map1 = new HashMap<>();
Map<String, Double> map2 = new HashMap<>();
Map<String, Double> map3 = new HashMap<>();
for (Map.Entry<Double, Series> entry : series.entrySet()) {
if (entry.getKey() == flag3) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
String parts1[] = s1.substring(4).split(" ");
for (int j = 1; j < parts1.length; j += 2) {
String parts2[] = parts1[j].split("-");
String a = parts2[0];
int fla=0;
if (a.startsWith("M")) {
// for (Map.Entry<Double, Series> entry8 : series.entrySet()) {
// Series series11 = entry8.getValue();
// Map<String, Device> series12 = series11.getDevices();
// if(series12.get(a)==null){
// System.out.println("11");
// fla++;
// }
// }
double v10=0;
for (Map.Entry<Double, ArrayList> entry1 : Bl.entrySet()) {
if (Double.parseDouble(a.substring(1)) == entry1.getKey()) {
//System.out.println("6");
ArrayList<Double> bl = entry1.getValue();
double v5 = v * bl.get(bl.size() - 2) / (R1 + R2);
v10=v5;
for (int i = 0; i < bl.size() - 2; i++) {
Double s = bl.get(i);
for (int l = 0; l < list.size(); l++) {
Series series4 = series.get(list.get(l));
if (s == series4.getId()) {
Map<String, Device> series3 = series4.getDevices();
double v4 = v;
for (Map.Entry<String, Device> entry2 : series3.entrySet()) {
String did = entry2.getKey();
if (did.startsWith("K")) {
S device = (S) series3.get(did);
// v4 = v4;
double v1 = v4;
if (!device.isOn ) {
v4 = 0;
}
double v2 = v4;
device.setVolotage(v1, v2);
} else if (did.startsWith("M")) {
// System.out.println("666");
Parallel device = (Parallel) series3.get(did);
double v0 = v4 * device.getR() / series4.getR();
Map<Double, Series> series5 = device.getSeries();
for (Map.Entry<Double, Series> entry4 : series5.entrySet()) {
Series series6 = entry4.getValue();
Map<String, Device> series7 = series6.getDevices();
double v7 = v4;
for (Map.Entry<String, Device> entry3 : series7.entrySet()) {
String did1 = entry3.getKey();
if (did1.startsWith("K")) {
S device1 = (S) series7.get(did1);
double v1 = v7;
if (!device1.isOn) {
v7 = 0;
}
double v2 = v7;
device1.setVolotage(v1, v2);
} else if (did1.startsWith("M")) {
Parallel device1 = (Parallel) series7.get(did);
double v8 = v7 * device1.getR() / series6.getR();
Map<Double, Series> series8 = device1.getSeries();
for (Map.Entry<Double, Series> entry5 : series8.entrySet()) {
Series series9 = entry5.getValue();
Map<String, Device> series10 = series9.getDevices();
double v9 = v7;
for (Map.Entry<String, Device> entry6 : series10.entrySet()) {
String did2 = entry6.getKey();
if (did2.startsWith("K")) {
S device2 = (S) series10.get(did2);
double v1 = v9;
if (!device2.isOn) {
v9 = 0;
}
double v2 = v9;
device2.setVolotage(v1, v2);
}
else if (did2.startsWith("P")) {
D device2 = (D) series10.get(did2);
double v1 = v9;
v9 = v9 - device2.oV;
double v2 = v9;
device2.setVolotage(v1, v2);
} else if (did2.startsWith("A")) {
Fan device2 = (Fan) series10.get(did2);
double v1 = v9;
v9 = v9 - device2.oV;
double v2 = v9;
device2.setVolotage(v1, v2);
} else if (did1.startsWith("D")) {
Ce device2 = (Ce) series10.get(did2);
double v1 = v9;
v9 = v9 - device2.oV;
double v2 = v9;
device2.setVolotage(v1, v2);
} else if (did2.startsWith("S")) {
curtain device2 = (curtain) series10.get(did2);
double v1 = v9;
v7 = v9 - device2.oV;
double v2 = v9;
device2.setVolotage(v1, v2);
} else if (did1.startsWith("B")) {
I device2 = (I) series10.get(did2);
double v1 = v9;
v9 = v9 - device2.oV;
double v2 = v9;
device2.setVolotage(v1, v2);
} else if (did2.startsWith("R")) {
F device2 = (F) series10.get(did2);
double v1 = v9;
v9 = v9 - device2.oV;
double v2 = v9;
device2.setVolotage(v1, v2);
} else if (did2.startsWith("H")) {
MS device2 = (MS) series10.get(did2);
if (device2.isOn2) {
double v1 = v9;
map1.put(did2, v1);
if (device2.isOn && device2.isOn1) {
v9 = v9 - device2.oV;
double v2 = v9;
map2.put(did2, v2);
} else if (device2.isOn && !device2.isOn1) {
v9 = 0;
double v2 = v9;
map3.put(did2, v2);
} else if (!device2.isOn && device2.isOn1) {
v9 = v9 - device2.oV;
double v2 = v9;
map3.put(did2, v2);
} else if (!device2.isOn && !device2.isOn1) {
v9 = 0;
double v2 = v9;
map2.put(did2, v2);
}
} else if (!device2.isOn2) {
double v1 = v9;
map1.put(did2, 0.0);
if (device2.isOn && device2.isOn1) {
v9 = v9 - device2.oV;
map2.put(did2, v1);
} else if (device2.isOn && !device2.isOn1) {
v9 = 0.0;
map3.put(did2, v1);
} else if (!device2.isOn && device2.isOn1) {
v9 = v9 - device2.oV;
map3.put(did2, v1);
} else if (!device2.isOn && !device2.isOn1) {
v9 = 0.0;
map2.put(did2, v1);
}
}
}
}
}
v7=v7-v8;
} else if (did1.startsWith("P")) {
D device1 = (D) series7.get(did1);
double v1 = v7;
v7 = v7 - device1.oV;
double v2 = v7;
device1.setVolotage(v1, v2);
} else if (did1.startsWith("A")) {
Fan device1 = (Fan) series7.get(did1);
double v1 = v7;
v7 = v7 - device1.oV;
double v2 = v7;
device1.setVolotage(v1, v2);
} else if (did1.startsWith("D")) {
Ce device1 = (Ce) series7.get(did1);
double v1 = v7;
v7 = v7 - device1.oV;
double v2 = v7;
device1.setVolotage(v1, v2);
} else if (did1.startsWith("S")) {
curtain device1 = (curtain) series7.get(did1);
double v1 = v7;
v7 = v7 - device1.oV;
double v2 = v7;
device1.setVolotage(v1, v2);
} else if (did1.startsWith("B")) {
I device1 = (I) series7.get(did1);
double v1 = v7;
v7 = v7 - device1.oV;
double v2 = v7;
device1.setVolotage(v1, v2);
} else if (did1.startsWith("R")) {
F device1 = (F) series7.get(did1);
double v1 = v7;
v7 = v7 - device1.oV;
double v2 = v7;
device1.setVolotage(v1, v2);
} else if (did1.startsWith("H")) {
MS device1 = (MS) series7.get(did1);
if (device1.isOn2) {
double v1 = v7;
map1.put(did1, v1);
if (device1.isOn && device1.isOn1) {
v7 = v7 - device1.oV;
double v2 = v7;
map2.put(did1, v2);
} else if (device1.isOn && !device1.isOn1) {
v7 = 0;
double v2 = v7;
map3.put(did1, v2);
} else if (!device1.isOn && device1.isOn1) {
v7 = v7 - device1.oV;
double v2 = v7;
map3.put(did1, v2);
} else if (!device1.isOn && !device1.isOn1) {
v7 = 0;
double v2 = v7;
map2.put(did1, v2);
}
} else if (!device1.isOn2) {
double v1 = v7;
map1.put(did1, 0.0);
if (device1.isOn && device1.isOn1) {
v7 = v7 - device1.oV;
map2.put(did1, v1);
//System.out.println("1");
} else if (device1.isOn && !device1.isOn1) {
v7 = 0.0;
map3.put(did1, v1);
// System.out.println("2");
} else if (!device1.isOn && device1.isOn1) {
v7 = v7 - device1.oV;
map3.put(did1, v1);
//System.out.println("3");
} else if (!device1.isOn && !device1.isOn1) {
v7 = 0.0;
map2.put(did1, v1);
//System.out.println("4");
}
}
}
}
}
v4 = v4 - v0;
} else if (did.startsWith("P")) {
D device = (D) series3.get(did);
double v1 = v4;
v4 = v4 - device.oV;
double v2 = v4;
device.setVolotage(v1, v2);
} else if (did.startsWith("A")) {
Fan device = (Fan) series3.get(did);
double v1 = v4;
v4 = v4 - device.oV;
double v2 = v4;
device.setVolotage(v1, v2);
} else if (did.startsWith("D")) {
Ce device = (Ce) series3.get(did);
double v1 = v4;
v4 = v4 - device.oV;
double v2 = v4;
device.setVolotage(v1, v2);
} else if (did.startsWith("S")) {
curtain device = (curtain) series3.get(did);
double v1 = v4;
v4 = v4 - device.oV;
double v2 = v4;
device.setVolotage(v1, v2);
} else if (did.startsWith("F")) {
Mu device = (Mu) series3.get(did);
double v1 = v4;
v4 = v4 - device.oV;
double v2 = v4;
device.setVolotage(v1, v2);
} else if (did.startsWith("L")) {
Co device = (Co) series3.get(did);
double v1 = v4;
v4 = v4 - device.oV;
double v2 = v4;
device.setVolotage(v1, v2);
} else if (did.startsWith("B")) {
I device = (I) series3.get(did);
double v1 = v4;
v4 = v4 - device.oV;
double v2 = v4;
device.setVolotage(v1, v2);
} else if (did.startsWith("R")) {
F device = (F) series3.get(did);
double v1 = v4;
v4 = v4 - device.oV;
double v2 = v4;
device.setVolotage(v1, v2);
}
else if (did.startsWith("H")) {
MS device1 = (MS) series3.get(did);
if (device1.isOn2) {
double v1 = v4;
if (device1.isOn && device1.isOn1) {
v4 = v4 - device1.oV;
double v2 = v4;
device1.setVolotage(v1,v2,0);
} else if (device1.isOn && !device1.isOn1) {
v4 = 0;
device1.setVolotage(v1,0,0);
} else if (!device1.isOn && device1.isOn1) {
v4 = v4 - device1.oV;
double v2 = v4;
device1.setVolotage(v1,0,v2);
} else if (!device1.isOn && !device1.isOn1) {
v4 = 0;
device1.setVolotage(v1,0,0);
}
} else if (!device1.isOn2 ) {
double v1 = v4;
if (device1.isOn && device1.isOn1) {
v4 = v4 - device1.oV;
double v2 = v4;
device1.setVolotage(v2, v1, 0);
} else if (device1.isOn && !device1.isOn1) {
v4 = 0;
device1.setVolotage(0, v1, 0);
} else if (!device1.isOn && device1.isOn1) {
v4 = v4 - device1.oV;
double v2 = v4;
device1.setVolotage(v2, 0, v1);
} else if (!device1.isOn && !device1.isOn1) {
v4 = 0;
device1.setVolotage(0, 0, v1);
}
}
}
}
}
}
}
}
}
v = v - v10;
} else if (a.startsWith("K")) {
S device = (S) series2.get(a);
double v1 = v;
if (!device.isOn) {
v = 0;
}
double v2 = v;
device.setVolotage(v1, v2);
} else if (a.startsWith("P")) {
D device = (D) series2.get(a);
double v1 = v;
v = v - device.oV;
double v2 = v;
device.setVolotage(v1, v2);
} else if (a.startsWith("A")) {
Fan device = (Fan) series2.get(a);
double v1 = v;
v = v - device.oV;
double v2 = v;
device.setVolotage(v1, v2);
} else if (a.startsWith("D")) {
Ce device = (Ce) series2.get(a);
double v1 = v;
v = v - device.oV;
double v2 = v;
device.setVolotage(v1, v2);
} else if (a.startsWith("S")) {
// System.out.println("2");
curtain device = (curtain) series2.get(a);
double v1 = v;
v = v - device.oV;
double v2 = v;
device.setVolotage(v1, v2);
} else if (a.startsWith("F")) {
Mu device = (Mu) series2.get(a);
double v1 = v;
v = v - device.oV;
double v2 = v;
device.setVolotage(v1, v2);
} else if (a.startsWith("L")) {
Co device = (Co) series2.get(a);
double v1 = v;
v = v - device.oV;
double v2 = v;
device.setVolotage(v1, v2);
} else if (a.startsWith("B")) {
// System.out.println("3");
I device = (I) series2.get(a);
double v1 = v;
v = v - device.oV;
double v2 = v;
device.setVolotage(v1, v2);
} else if (a.startsWith("R")) {
F device = (F) series2.get(a);
double v1 = v;
v = v - device.oV;
double v2 = v;
device.setVolotage(v1, v2);
}
}
}
}
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did = entry1.getKey();
if (did.startsWith("M")) {
Parallel device = (Parallel) entry1.getValue();
Map<Double, Series> series5 = device.getSeries();
for (Map.Entry<Double, Series> entry2 : series5.entrySet()) {
Series series6 = entry2.getValue();
Map<String, Device> series7 = series6.getDevices();
for (Map.Entry<String, Device> entry3 : series7.entrySet()) {
String did1 = entry3.getKey();
if (did1.startsWith("H")) {
MS device1 = (MS) series7.get(did1);
device1.setVolotage(map1.get(did1),map2.get(did1),map3.get(did1));
}
}
}
}
}
}
ArrayList<Device> arraylist6=new ArrayList<>();
for (Map.Entry<Double, Series> entry : series.entrySet()) {
Series series1 = entry.getValue();
Map<String, Device> series2 = series1.getDevices();
for (Map.Entry<String, Device> entry1 : series2.entrySet()) {
String did= entry1.getKey();
if(did.startsWith("M")){
Parallel device = (Parallel) entry1.getValue();
Map<Double, Series> series5 = device.getSeries();
for (Map.Entry<Double, Series> entry2 : series5.entrySet()) {
Series series6 = entry2.getValue();
Map<String, Device> series7 = series6.getDevices();
for (Map.Entry<String, Device> entry3 : series7.entrySet()) {
arraylist6.add(entry3.getValue());
}
}
}
else
arraylist6.add(entry1.getValue());
}
}
Comparator<Device> numberComparator = new Comparator<Device>()
{
public int compare(Device o1,Device o2){
int a=o1.fanhui();
int b=o2.fanhui();
String c=o1.id;
String d=o2.id;
if(a>b) return -1;
else if(a==b){
if(c.compareTo(d)>0)
return 1;
else
return -1;
}
else
return 1;
}
};
Collections.sort(arraylist6,numberComparator);
for(Device suixcs:arraylist6){
if(!suixcs.getStatus().equals("null"))
System.out.println(suixcs.getStatus());
}
}
}
分析:题目集八在题目集七上新增功能与重点:
1.管脚电压显示:要求在输出设备状态后,显示每个设备管脚的电压。
2.电流限制:为每种电器设置了最大电流限制,超过时给出提示。
3.短路检测:若出现短路情况,输出特定提示,而非设备信息。
4.并联电路嵌套:考虑了并联电路中包含并联电路的复杂情况。
5.二极管元件:引入二极管,具有正向导通、反向截止的特性,需根据电流方向和电压情况判断其状态。
解题思路:
1.数据结构选择:
继续使用合适的数据结构存储设备、电路连接等信息。
需要额外的结构来记录管脚电压和电流信息。
2.类设计优化:
完善已有的设备类,添加处理电流限制、管脚电压计算等方法。
新增二极管类,实现其特殊的导通和截止逻辑。
3.计算与逻辑处理:
准确计算电路中的电流、电压等参数。
进行短路检测和电流限制判断。
根据电流方向和电压处理二极管的状态。
3.踩坑心得
1.电流和电压计算错误:由于涉及多种设备和复杂的电路结构,在计算电流和电压时,因为公式使用错误或计算顺序不当导致结果出错。
2.短路判断失误:准确判断电路是否短路具有一定难度,如果判断逻辑不完善,会遗漏短路情况或者误判为短路。
3.电流限制处理不当:每种设备的最大电流限制不同,在计算电流并与限制值比较时,出现判断错误,导致没有正确给出电流超限提示。
4.二极管状态判断错误:二极管的导通和截止状态取决于电流方向和电压情况,判断条件较为复杂,容易出错。
5.忽视题目默认规则:没有按照题目中关于数据类型、计算精度、连接顺序等默认规则进行处理。
6.忽视管脚复用限制:题目中规定了元件的管脚除了互斥开关的 1 引脚,其他所有引脚在电路中最多只出现一次,如果没有严格遵守,会导致计算错误。
4.改进建议
1.针对电流和电压计算错误:对电流和电压的计算公式进行仔细审查和验证,确保其准确性。
2.针对短路判断失误:完善短路判断的逻辑,考虑更多可能导致短路的情况。
3.针对电流限制处理不当:在电流计算后立即进行电流限制检查,并确保提示信息准确输出。
4.针对二极管状态判断错误:对二极管的导通和截止条件进行再次梳理,确保判断逻辑的严密性。
5.针对忽视题目默认规则:在关键代码部分添加注释,提醒遵循题目规则。
6.针对忽视管脚复用限制:在连接建立和设备状态更新时,增加管脚复用的检查逻辑。
5.总结
面向对象编程(OOP):在设计和实现家居强电电路模拟程序时,需要使用面向对象的思想,设计合适的类和对象来表示电路、串联电路、并联电路等概念,以及它们之间的关系。
在Java学习的过程中,要注重理论和实践相结合,不断挑战自己,解决问题和提高编程能力。通过不断的学习和实践,逐步掌握Java编程的技能,不断提高自身的能力。
虽然题目集对我来说很难,但学习就如逆水行舟,在一次次的改错和调试中,真的学到了很多。加油,再接再厉!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现