oop-third blog-pear
1.前言
1.一些碎碎念
这两次次作业在我看来都是比较具有挑战性的,以题促学,为了写出这两道道题目,我们需要去学习更多java中独特的语法和基本结构,同时这也加深了我对java语言中的基础知识的理解和掌握,做得到了从“会听”到“会用”的进步。令我受益匪浅,也掌握了一些对字符串的基本处理操作,这是我以前学c语言时未曾深入研究学习到的。
2.分析
1.第七次题目集
本次题目集新增了多条并联电路或者串联电路在主电路中的情况,同时出现了多条电路串并混连的情况,新增控制设备互斥开关,此处需要新增互斥开关对两条电路处理逻辑,且互斥开关存在电阻,计算时需要考虑。多条电路连接在一起,需要思考如何计算每一条电路的电流具体到每一个电器的分压,需要重构电路的逻辑,新增了。
2.第八次题目集
相比题目集七,此次题目集难度又是一次大提升,新增电器二极管,处理远比上次的窗帘困难,且这次电路迭代又更新了电路复杂程度,出现了并联电路中嵌套并联电路的情况,这无疑使题目的难度上升了几个等级,逻辑变得更加复杂
2.设计与分析
1.家居强电路三
类图
复杂度图表:
Complexity metrics | ���� | 29 6�� 2024 16:12:04 CST | ||
---|---|---|---|---|
Method | CogC | ev(G) | iv(G) | v(G) |
AFan.AFan() | 0 | 1 | 1 | 1 |
AFan.AFan(double, double, String, double, double) | 0 | 1 | 1 | 1 |
AFan.show() | 0 | 1 | 1 | 1 |
AFan.speed() | 5 | 1 | 1 | 5 |
DL.DL() | 0 | 1 | 1 | 1 |
DL.allR() | 9 | 1 | 1 | 7 |
DL.calU() | 4 | 1 | 1 | 4 |
DL.isOPen() | 8 | 1 | 1 | 5 |
DL2.DL2() | 0 | 1 | 1 | 1 |
DL2.allR() | 1 | 1 | 1 | 2 |
DL2.calR() | 5 | 1 | 2 | 5 |
DL2.calU() | 3 | 1 | 2 | 3 |
Main.main(String[]) | 169 | 15 | 64 | 73 |
continuousController.continuousController() | 0 | 1 | 1 | 1 |
continuousController.continuousController(double, double, String, double) | 0 | 1 | 1 | 1 |
continuousController.getOutput() | 0 | 1 | 1 | 1 |
continuousController.show() | 0 | 1 | 1 | 1 |
controlDevice.controlDevice() | 0 | 1 | 1 | 1 |
controlDevice.controlDevice(double, double, String) | 0 | 1 | 1 | 1 |
controlledDevice.controlledDevice() | 0 | 1 | 1 | 1 |
controlledDevice.controlledDevice(double, double, String, double, double) | 0 | 1 | 1 | 1 |
curtain.calOpen(double) | 9 | 1 | 1 | 7 |
curtain.curtain() | 0 | 1 | 1 | 1 |
curtain.curtain(double, double, String, double, double, double) | 0 | 1 | 1 | 1 |
curtain.show() | 0 | 1 | 1 | 1 |
electricalEquipment.electricalEquipment() | 0 | 1 | 1 | 1 |
electricalEquipment.electricalEquipment(double, double) | 0 | 1 | 1 | 1 |
fan.calculateSpeed() | 3 | 1 | 1 | 3 |
fan.fan() | 0 | 1 | 1 | 1 |
fan.fan(double, double, String, double, double) | 0 | 1 | 1 | 1 |
fan.show() | 0 | 1 | 1 | 1 |
fluorescentLamp.calculateBrightness() | 2 | 1 | 1 | 2 |
fluorescentLamp.fluorescentLamp() | 0 | 1 | 1 | 1 |
fluorescentLamp.fluorescentLamp(double, double, String, double, double) | 0 | 1 | 1 | 1 |
fluorescentLamp.show() | 0 | 1 | 1 | 1 |
incandescentLamp.calculateBrightness() | 3 | 1 | 1 | 3 |
incandescentLamp.incandescentLamp() | 0 | 1 | 1 | 1 |
incandescentLamp.incandescentLamp(double, double, String, double, double) | 0 | 1 | 1 | 1 |
incandescentLamp.show() | 0 | 1 | 1 | 1 |
stepController.getOutput() | 4 | 1 | 1 | 5 |
stepController.show() | 0 | 1 | 1 | 1 |
stepController.stepController() | 0 | 1 | 1 | 1 |
stepController.stepController(double, double, String, int) | 0 | 1 | 1 | 1 |
switchOn.getOutput() | 2 | 1 | 1 | 2 |
switchOn.show() | 2 | 1 | 3 | 3 |
switchOn.switchOn() | 0 | 1 | 1 | 1 |
switchOn.switchOn(double, double, String, int) | 0 | 1 | 1 | 1 |
switchOn1.calR() | 2 | 1 | 1 | 3 |
switchOn1.show() | 2 | 1 | 2 | 2 |
switchOn1.switchOn1() | 0 | 1 | 1 | 1 |
switchOn1.switchOn1(double, double, String, int, double) | 0 | 1 | 1 | 1 |
Class | OCavg | OCmax | WMC | |
AFan | 2.00 | 5 | 8 | |
DL | 4.25 | 7 | 17 | |
DL2 | 2.50 | 4 | 10 | |
Main | 73.00 | 73 | 73 | |
continuousController | 1.00 | 1 | 4 | |
controlDevice | 1.00 | 1 | 2 | |
controlledDevice | 1.00 | 1 | 2 | |
curtain | 2.50 | 7 | 10 | |
electricalEquipment | 1.00 | 1 | 2 | |
fan | 1.50 | 3 | 6 | |
fluorescentLamp | 1.25 | 2 | 5 | |
incandescentLamp | 1.50 | 3 | 6 | |
stepController | 2.00 | 5 | 8 | |
switchOn | 1.75 | 3 | 7 | |
switchOn1 | 1.75 | 3 | 7 | |
Package | v(G)avg | v(G)tot | ||
3.29 | 168 | |||
Module | v(G)avg | v(G)tot | ||
xianlu | 3.29 | 168 | ||
Project | v(G)avg | v(G)tot | ||
project | 3.29 | 168 |
部分源码
for(String test:id){
if(test.charAt(0)=='K'){
switchOn k=new switchOn(0,0,test,-1);
dl.electricalEquipments.add(k);
equipments.add(k);
switchOns.add(k);
} else if (test.charAt(0)=='F') {
stepController s=new stepController(0,0,test,0);
dl.electricalEquipments.add(s);
equipments.add(s);
steplist.add(s);
} else if (test.charAt(0)=='L') {
continuousController c=new continuousController(0,0,test,0);
dl.electricalEquipments.add(c);
equipments.add(c);
controllerlist.add(c);
} else if (test.charAt(0)=='B') {
incandescentLamp i=new incandescentLamp(0,0,test,0,10);
dl.electricalEquipments.add(i);
equipments.add(i);
incandescentLamps.add(i);
} else if (test.charAt(0)=='R') {
fluorescentLamp i=new fluorescentLamp(0,0,test,0,5);
dl.electricalEquipments.add(i);
equipments.add(i);
fluorescentLamps.add(i);
} else if (test.charAt(0)=='D') {
fan f=new fan(0,0,test,0,20);
dl.electricalEquipments.add(f);
equipments.add(f);
fans.add(f);
}else if(test.charAt(0)=='A'){
AFan a=new AFan(0,0,test,0,20);
dl.electricalEquipments.add(a);
equipments.add(a);
aFans.add(a);
}else if (test.charAt(0)=='H'){
int flag=0;
for(switchOn1 i:switchOn1s){
if(i.id.equals(test)){
flag=1;
dl.pin1=3;
dl.electricalEquipments.add(i);
//System.out.println("接口3");
//互斥开关出现过一次,电阻统一记录的5欧,后面计算要加上
break;
}
}
if(flag==0)
{
switchOn1 s1=new switchOn1(0,0,test,2,5);
dl.pin1=2;
dl.electricalEquipments.add(s1);
equipments.add(s1);
switchOn1s.add(s1);
//System.out.println("互斥开关第一次出现");
}
} else if (test.charAt(0)=='S') {
curtain cur=new curtain(0,0,test,0,15,1);
dl.electricalEquipments.add(cur);
equipments.add(cur);
curtains.add(cur);
}else if(test.charAt(0)=='M'){
for(electricalEquipment test1:DL2s){
if(test1 instanceof DL2){
if(((DL2) test1).id.equals(test)){
dl.electricalEquipments.add(test1);
equipments.add(test1);
//System.out.println("并联电路添加到主电路");
}
}
}
}
}
dl.allR();//计算一遍电阻
} else if (line.charAt(1)=='M') {
DL2 dl2=new DL2();
dl2.id=line.substring(1,3);
electricalEquipments.add(dl2);
DL2s.add(dl2);
line=line.substring(4);
Pattern pattern1 = Pattern.compile("([A-Z][0-9])");
Matcher matcher1 = pattern1.matcher(line);
Set<String> id=new HashSet<>();
while (matcher1.find()){
id.add(matcher1.group(1));
}
int count=0;
for(String test:id){
for(electricalEquipment test1:electricalEquipments){
if (test1 instanceof DL){
if(((DL) test1).id.equals(test)){
dl2.DLs.add((DL) test1);
//System.out.println("该条串联线路电阻"+((DL) test1).r);
count++;
}
}
}
}
//System.out.println("找到串联电路的条数"+count);
if(count== id.size()){
//System.out.println("并联成功");
dl2.allR();
//System.out.println("并联电路的电阻"+dl2.r);
}
}else if(line.charAt(1)=='K'){
line=line.substring(1);
for(switchOn test:switchOns){
if(line.equals(test.id)){
test.state=-1* test.state;
//System.out.println("改变了开关状态");
}
}
} else if (line.charAt(1)=='H') {
line=line.substring(1);
for(switchOn1 test:switchOn1s){
if(line.equals(test.id)){
if(test.state==2){
test.state=3;
//System.out.println("互斥开关接3了");
}else if(test.state==3){
test.state=2;
//System.out.println("互斥开关接2了");
}
}
}
} else if (line.charAt(1)=='F') {
//System.out.println("进入了加减档");
line=line.substring(1);
//System.out.println(line);
for(stepController test:steplist){
if(line.substring(0,2).equals(test.id)){
line=line.substring(2);
if(line.equals("+")){
test.step++;
//System.out.println("加档了");
} else if (line.equals("-")) {
test.step--;
//System.out.println("减档了");
}
}
}
} else if (line.charAt(1)=='L') {
line=line.substring(1);
for (continuousController test:controllerlist){
if (line.substring(0,2).equals(test.id)){
line=line.substring(3);
test.step=Double.parseDouble(line);
//System.out.println("调档了"+test.step);
}
}
}
}
分析:
优点:
- 模块化:代码将不同的电气设备抽象为不同的类,有助于模块化设计和代码重用。
- 易于理解:代码结构相对清晰,对于熟悉Java编程和电路原理的人来说,可以比较容易地理解其工作原理。
- 灵活性:程序可以通过读取不同的输入来模拟不同的电路配置,具有一定的灵活性
dl.electricalEquipments
和equipments
被所有类型的对象添加,这可能意味着这些列表用于一些通用的操作或展示,提高了代码的复用性。
缺点:
- 缺乏错误处理:代码没有对可能出现的错误(如输入格式错误、设备ID不存在等)进行处理,这可能导致程序在遇到错误输入时崩溃或产生不可预知的行为。
- 可读性较差:代码中存在大量的缩进和注释,这可能会影响代码的可读性。此外,变量命名和函数命名也不够清晰,可能会给理解代码带来困难。
- 效率问题:由于较为仓促代码中使用了大量的循环和条件判断,可能会影响程序的执行效率。特别是在处理大量电气设备时,可能会导致性能问题,应该优化数据结构使其变得易读
- 于所有对象都被添加到
electricalEquipments
和equipments
这样的通用列表中,这可能导致在后续处理这些列表时需要进行额外的类型检查和转换,降低了类型安全性。
2.家居强电路四
类图
Complexity metrics | ���� | 29 6�� 2024 16:12:04 CST | ||
---|---|---|---|---|
Method | CogC | ev(G) | iv(G) | v(G) |
AFan.AFan() | 0 | 7 | 2 | 2 |
AFan.AFan(double, double, String, double, double) | 0 | 1 | 1 | 1 |
AFan.show() | 0 | 1 | 1 | 1 |
AFan.speed() | 10 | 1 | 5 | 5 |
DL.DL() | 0 | 1 | 1 | 1 |
DL.allR() | 9 | 1 | 1 | 7 |
DL.calU() | 4 | 1 | 1 | 4 |
DL.isOPen() | 8 | 1 | 1 | 5 |
DL2.DL2() | 0 | 1 | 1 | 1 |
DL2.allR() | 1 | 1 | 1 | 2 |
DL2.calR() | 5 | 1 | 2 | 5 |
DL2.calU() | 3 | 1 | 2 | 3 |
Main.main(String[]) | 169 | 15 | 64 | 73 |
continuousController.continuousController() | 0 | 1 | 1 | 1 |
continuousController.continuousController(double, double, String, double) | 0 | 1 | 1 | 1 |
continuousController.getOutput() | 0 | 1 | 1 | 1 |
continuousController.show() | 0 | 1 | 1 | 1 |
controlDevice.controlDevice() | 0 | 1 | 1 | 1 |
controlDevice.controlDevice(double, double, String) | 0 | 1 | 1 | 1 |
controlledDevice.controlledDevice() | 0 | 1 | 1 | 1 |
controlledDevice.controlledDevice(double, double, String, double, double) | 0 | 1 | 1 | 1 |
curtain.calOpen(double) | 9 | 1 | 1 | 7 |
curtain.curtain() | 0 | 1 | 1 | 1 |
curtain.curtain(double, double, String, double, double, double) | 0 | 1 | 1 | 1 |
curtain.show() | 0 | 1 | 1 | 1 |
electricalEquipment.electricalEquipment() | 0 | 1 | 1 | 1 |
electricalEquipment.electricalEquipment(double, double) | 0 | 1 | 1 | 1 |
fan.calculateSpeed() | 3 | 1 | 1 | 3 |
fan.fan() | 0 | 1 | 1 | 1 |
fan.fan(double, double, String, double, double) | 0 | 1 | 1 | 1 |
fan.show() | 0 | 1 | 1 | 1 |
fluorescentLamp.calculateBrightness() | 2 | 1 | 1 | 2 |
fluorescentLamp.fluorescentLamp() | 0 | 1 | 1 | 1 |
fluorescentLamp.fluorescentLamp(double, double, String, double, double) | 0 | 1 | 1 | 1 |
fluorescentLamp.show() | 0 | 1 | 1 | 1 |
incandescentLamp.calculateBrightness() | 3 | 1 | 1 | 3 |
incandescentLamp.incandescentLamp() | 0 | 1 | 1 | 1 |
incandescentLamp.incandescentLamp(double, double, String, double, double) | 0 | 1 | 1 | 1 |
incandescentLamp.show() | 0 | 1 | 1 | 1 |
stepController.getOutput() | 4 | 1 | 1 | 5 |
stepController.show() | 0 | 1 | 1 | 1 |
stepController.stepController() | 0 | 1 | 1 | 1 |
stepController.stepController(double, double, String, int) | 0 | 1 | 1 | 1 |
switchOn.getOutput() | 2 | 1 | 1 | 2 |
switchOn.show() | 2 | 1 | 3 | 3 |
switchOn.switchOn() | 0 | 1 | 1 | 1 |
switchOn.switchOn(double, double, String, int) | 0 | 1 | 1 | 1 |
switchOn1.calR() | 2 | 1 | 1 | 3 |
switchOn1.show() | 2 | 1 | 2 | 2 |
switchOn1.switchOn1() | 0 | 1 | 1 | 1 |
switchOn1.switchOn1(double, double, String, int, double) | 0 | 1 | 1 | 1 |
Class | OCavg | OCmax | WMC | |
AFan | 2.00 | 5 | 8 | |
DL | 4.25 | 7 | 17 | |
DL2 | 2.50 | 4 | 10 | |
Main | 73.00 | 73 | 73 | |
continuousController | 1.00 | 1 | 4 | |
controlDevice | 1.00 | 1 | 2 | |
controlledDevice | 1.00 | 1 | 2 | |
curtain | 2.50 | 7 | 10 | |
electricalEquipment | 1.00 | 1 | 2 | |
fan | 1.50 | 3 | 6 | |
fluorescentLamp | 1.25 | 2 | 5 | |
incandescentLamp | 1.50 | 3 | 6 | |
stepController | 2.00 | 5 | 8 | |
switchOn | 1.75 | 3 | 7 | |
switchOn1 | 1.75 | 3 | 7 | |
Package | v(G)avg | v(G)tot | ||
9.0 | 168 | |||
Module | v(G)avg | v(G)tot | ||
xianlu | 3.29 | 168 | ||
Project | v(G)avg | v(G)tot | ||
project | 3.29 | 168 |
部分源码:
class DL extends electricalEquipment{
String id;
double U;//分压
double r;//分阻
int open;
ArrayList<electricalEquipment> electricalEquipments;
public DL() {
electricalEquipments=new ArrayList<>();
open=0;
}
public void allR(){
for(electricalEquipment test:electricalEquipments){
if(test instanceof controlledDevice){
this.r+=((controlledDevice) test).r;
}else if(test instanceof switchOn1){
if(this.pin1==2){
this.r+= 5;
} else if (this.pin1==3) {
this.r+=10;
}
}else if(test instanceof DL2){
this.r+= ((DL2) test).r;
}
}
}
public void isOPen(){
for(electricalEquipment test:electricalEquipments){
if(test instanceof switchOn){
open= ((switchOn) test).state;
}else if(test instanceof switchOn1){
if(((switchOn1) test).state==pin1){
open=1;
}
else {
open=-1;
}
}
}
}
public void calU(){
for (electricalEquipment test:this.electricalEquipments){
if(test instanceof controlledDevice){
((controlledDevice) test).u=(((controlledDevice) test).r/this.r)*this.U;
//System.out.println(((controlledDevice) test).id+"分压"+((controlledDevice) test).u);
} else if (test instanceof DL2) {
((DL2) test).U= (((DL2) test).r/this.r)*this.U;
}
}
}
}
class DL2 extends electricalEquipment{
String id;
double U;//分压
double r;//分阻
List<DL>DLs=new ArrayList<>();
public DL2() {
DLs=new ArrayList<>();
}
public void allR(){
double total=0;
for(DL test:DLs){
total+=1/test.r;
}
this.r=1/total;
}
public void calR(){
int count=0;
for(DL test:DLs){
if(test.open==1){
count++;
this.r=test.r;
}
}
if(count==2||count==0){
this.allR();
}
}
public void calU(){
for(DL test:this.DLs){
if(test.open==1){
test.U=this.U;
}
//System.out.println(test.id+"分压:"+test.U);
test.calU();
}
}
for(electricalEquipment test:electricalEquipments){
if(test instanceof DL){
((DL) test).isOPen();
if(((DL) test).open==1){
//System.out.println(((DL) test).id+"开着的");
} else if (((DL) test).open==-1) {
//System.out.println(((DL) test).id+"关着的");
}
}
}
//根据开关情况得到并联电路的电阻
for(electricalEquipment test:DL2s){
if(test instanceof DL2){
((DL2) test).calR();
//System.out.println(((DL2) test).id+"电阻:"+((DL2) test).r);
}
}
//计算分压
DL test1=(DL)(electricalEquipments.get(electricalEquipments.size()-1));
//调节器部分
if(test1.electricalEquipments.get(0) instanceof continuousController){
test1.U=220*((continuousController) test1.electricalEquipments.get(0)).step;
} else if (test1.electricalEquipments.get(0) instanceof stepController) {
test1.U=220*((stepController) test1.electricalEquipments.get(0)).step;
}else {
test1.U=220;
}
//主电路开关部分
if(test1.open==-1)
{
test1.U=0;
}
test1.r=0;
test1.allR();
test1.calU();
//System.out.println(test1.id+" "+test1.r);
for(electricalEquipment test:test1.electricalEquipments){
if(test instanceof controlledDevice){
//System.out.println(((controlledDevice) test).id+"分压:"+((controlledDevice) test).u);
} else if (test instanceof DL2) {
//System.out.println(((DL2) test).id+"分压:"+((DL2) test).U);
((DL2) test).calU();
}
}
double bright=0;
switchOns.sort((o1, o2) -> {
int num1 = Integer.parseInt(o1.id.substring(1));
int num2 = Integer.parseInt(o2.id.substring(1));
return Integer.compare(num1, num2);
});
if(!switchOns.isEmpty()){
for(switchOn test:switchOns){
test.show();
}
}
steplist.sort((o1, o2) -> {
int num1 = Integer.parseInt(o1.id.substring(1));
int num2 = Integer.parseInt(o2.id.substring(1));
return Integer.compare(num1, num2);
});
if(!steplist.isEmpty()){
for(stepController test:steplist){
test.show();
}
}
controllerlist.sort((o1, o2) -> {
int num1 = Integer.parseInt(o1.id.substring(1));
int num2 = Integer.parseInt(o2.id.substring(1));
return Integer.compare(num1, num2);
});
if(!controllerlist.isEmpty()){
for (continuousController test:controllerlist){
test.show();
}
}
incandescentLamps.sort((o1, o2) -> {
int num1 = Integer.parseInt(o1.id.substring(1));
int num2 = Integer.parseInt(o2.id.substring(1));
return Integer.compare(num1, num2);
});
if(!incandescentLamps.isEmpty()){
for(incandescentLamp test:incandescentLamps){
test.calculateBrightness();
test.show();
bright+= test.brightness;
}
}
fluorescentLamps.sort((o1, o2) -> {
int num1 = Integer.parseInt(o1.id.substring(1));
int num2 = Integer.parseInt(o2.id.substring(1));
return Integer.compare(num1, num2);
});
if(!fluorescentLamps.isEmpty()){
for (fluorescentLamp test:fluorescentLamps){
test.calculateBrightness();
test.show();
bright+= test.brightness;
}
}
fans.sort((o1, o2) -> {
int num1 = Integer.parseInt(o1.id.substring(1));
int num2 = Integer.parseInt(o2.id.substring(1));
return Integer.compare(num1, num2);
});
if(!fans.isEmpty()){
for (fan test:fans){
test.calculateSpeed();
test.show();
}
}
switchOn1s.sort((o1, o2) -> {
int num1 = Integer.parseInt(o1.id.substring(1));
int num2 = Integer.parseInt(o2.id.substring(1));
return Integer.compare(num1, num2);
});
if(!switchOn1s.isEmpty()){
for(switchOn1 test:switchOn1s){
test.show();
}
}
curtains.sort((o1, o2) -> {
int num1 = Integer.parseInt(o1.id.substring(1));
int num2 = Integer.parseInt(o2.id.substring(1));
return Integer.compare(num1, num2);
});
if(!curtains.isEmpty()){
for(curtain test:curtains){
test.calOpen(bright);
test.show();
}
}
分析:
优点:
- 结构清晰:代码按照功能划分,首先处理开关状态,然后计算并联电路的电阻,接着计算分压,最后输出每个设备的状态。
- 排序和显示:设备列表在显示之前进行了排序,使得输出更加有序。
- 功能分离:不同的设备类型有不同的显示方法,这有助于代码的模块化和重用。
缺点:
- 缺乏注释:代码中没有足够的注释来解释每个部分的目的是什么,这可能会给其他开发者理解代码带来困难。
- 魔法数:代码中出现了魔法数,如
220
,这些数字没有解释其含义,可能会导致未来的维护困难。 - 错误处理:代码中没有明显的错误处理逻辑,例如,如果设备列表为空,直接进行操作可能会导致空指针异常。
- 代码重复:排序和显示的逻辑在每个设备类型中都重复了一次,这可能会导致代码冗余,不利于维护。
- 可读性:代码中有大量的缩进和冗长的代码行,这可能会影响代码的可读性。
- 硬编码的假设:代码中可能存在一些硬编码的假设,例如,对设备ID的格式和内容的假设。这些假设可能会限制代码的灵活性,使其难以适应不同的输入数据。
- 可扩展性差:代码的结构可能不够灵活,难以添加新的设备类型或修改现有设备的行为。例如,如果需要添加一个新的设备类型,可能需要修改多个地方,包括排序和显示逻辑。
- 性能问题:对于大型电路模拟,代码中多次遍历设备列表可能会成为性能瓶颈。特别是在计算分压和电阻时,可能需要更高效的算法和数据结构。
4.改进建议
可以改进的地方很多,我的代码仍然不够完善。
-
命名规范:
使用一致的命名规范来提高代码的可读性,例如驼峰命名法或下划线分隔法,选择描述性的变量名和方法名,避免使用缩写或含义不明的名称。
-
性能优化:
分析代码的性能瓶颈,特别是在遍历大型列表或计算复杂度高的操作时,考虑使用更高效的数据结构,如平衡数或跳表,来提高排序和搜索的性能,例如考虑使用哈希表进行数据结构,一些电路数据的存储处理上的优化
以下四点可以改善
- 改进排序和显示逻辑:
- 使用哈希表来存储设备ID和设备对象的映射,然后使用排序集合(如
TreeSet
)来保持设备的顺序。 - 当需要显示设备时,可以直接迭代排序集合,而不需要先对列表进行排序。
- 使用哈希表来存储设备ID和设备对象的映射,然后使用排序集合(如
- 优化电路计算:
- 对于电路中的电阻和分压计算,可以考虑使用图论中的算法,如深度优先搜索(DFS)或广度优先搜索(BFS)来遍历电路图。
- 使用图数据结构来表示电路,其中节点代表电气设备,边代表连接和电阻。
- 使用优先队列(堆):
- 如果需要按照特定的顺序(如设备ID)来处理设备,可以使用优先队列(如
PriorityQueue
)来保持设备的顺序,并在处理时直接弹出队列中的元素。
- 如果需要按照特定的顺序(如设备ID)来处理设备,可以使用优先队列(如
- 避免重复计算:
- 对于电阻和分压的计算,可以添加缓存机制,以避免重复计算已经计算过的值。
- 使用哈希表来存储已计算过的结果,当需要计算某个电路元件的电阻或分压时,首先检查哈希表
哈希表(Hash Table)是一种数据结构,它提供了快速的插入、删除和查找操作。哈希表通过使用哈希函数来将键映射到表中的位置,以实现这些快速操作。在处理大规模数据集时,哈希表是一种非常有效的数据结构。哈希表的优点是它的快速操作,但也有一些缺点,如:
- 哈希表的性能依赖于哈希函数的质量。
- 对于小数据集,哈希表的空间开销可能比其他数据结构大。
- 哈希表的迭代顺序是不确定的,如果要保持插入顺序,可能需要额外的数据结构(如Java中的
- 改进排序和显示逻辑:
-
代码风格:
遵循一致的代码风格指南,包括缩进、括号使用和空格,避免过长的代码行,将其分解为更短的语句。
5.总结
这两次的PTA作业使我受益匪浅,让我意识到自己仍停留在简单的if-else结构上,而没有深入学习和掌握新的算法和数据结构。我意识到,仅仅依赖简单的逻辑判断是无法应对复杂问题的,需要掌握更高级的算法和数据结构,才能更有效地解决问题。同时,我也发现自己的代码风格不定,这增加了阅读的困难程度,使我难以复现和理解自己的代码。
此外,对于电路与电路之间的关系,我没有处理好,简单地将它们视为一种电器来进行操作,实际上这大大增加了处理的难度。电路之间的关系错综复杂,需要我们更深入地理解电路原理,才能更准确地模拟电路的行为。因此,在未来的学习和实践中,我需要加强对算法和数据结构的学习,同时注意代码风格的统一和规范。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?