gpt4 book ai didi

c - 使用通用案例进行开关改进

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:30 25 4
gpt4 key购买 nike

是否可以更改此“开关”?首先,我虽然可以使用“for”来代替,但我认为不再需要了。目的是使此代码更高效。有谁知道如何进行此改进以使代码更高效?我还在学习编程,我还没有那么有经验。我已经做了一些事情,但我不知道它是否缺少我将新代码放在旧代码下的东西。我认为可能有必要使用 if 而不是我创建的 for 或其他函数,因为我无法更改 'PageNumber' 的值,我只需要使用它。也许我不需要使用任何已经在“案例”中的东西。有人可以帮我解释一下吗?

switch (PageNumber) {

case 0x01:
m = 0;
for (n = 0; n < 8; n++) {
if (n < NumberOfSensor) {
if (n < 4) {
Write_string("L ", n, 0);
Write_int((n + 1), n, 1);
write_CustonCharacter(4, n, 3); //Write the indicator "->"


Write_int(Sensor_Count[n], n, 5);
} else if (n > 3) {
Write_string("L ", (n - 4), 10);
Write_int((n + 1), (n - 4), 11);
write_CustonCharacter(4, (n - 4), 13); //Write the indicator "->"
Write_int(Sensor_Count[n], (n - 4), 15);
}

}
}
break;


/*
* PAGE 2
*/
case 0x02:

m = 0;
for (n = 8; n < 16; n++) {
if (n < NumberOfSensor) {
if (n < 12) {
Write_string("L ", (n - 8), 0);
Write_int((n + 1), (n - 8), 1);
write_CustonCharacter(4, (n - 8), 3); //Write the indicator "->"
Write_int(Sensor_Count[n], (n - 8), 5);
} else if (n > 11) {
Write_string("L ", (n - 12), 10);
Write_int((n + 1), (n - 12), 11);
write_CustonCharacter(4, (n - 12), 13); //Write the indicator "->"
Write_int(Sensor_Count[n], (n - 12), 15);
}

}
}
break;


//...(This part is just repetition)

/*
* PAGE 8
*/
case 0x08:
m = 0;
for (n = 56; n < 65; n++) {
if (n < NumberOfSensor) {
if (n < 60) {
Write_string("L ", (n - 56), 0);
Write_int((n + 1), (n - 56), 1);
write_CustonCharacter(4, (n - 56), 3); //Write the indicator "->"
Write_int(Sensor_Count[n], (n - 56), 5);
} else if (n > 59) {
Write_string("L ", (n - 60), 10);
Write_int((n + 1), (n - 60), 11);
write_CustonCharacter(4, (n - 60), 13); //Write the indicator "->"
Write_int(Sensor_Count[n], (n - 60), 15);
}

}
}
break;
default:
break;
}
//==========================================================================
for (PageNumber = 1; PageNumber < 9; PageNumber++) { //actually I think this for is not necessary because it receives PageNumber and then it does what it needs to do.
m = 0; //but does it need something else?
for (n = (PageNumber * 8 - 8); n < (PageNumber * 8); n++) {
if (n < NumberOfSensor) {
if (n < (PageNumber * 8 - 4)) {
Write_string("L ", n - (PageNumber * 8 - 8), 0);
Write_int((n + 1), n - (PageNumber * 8 - 8), 1);
write_CustonCharacter(4, n - (PageNumber * 8 - 8), 3); //Write the indicator "->"
Write_int(Sensor_Count[n], n - (PageNumber * 8 - 8), 5);
} else if (n > (PageNumber * 8 - 5)) {
Write_string("L ", n - (PageNumber * 8 - 4), 10);
Write_int((n + 1), n - (PageNumber * 8 - 4), 11);
write_CustonCharacter(4, n - (PageNumber * 8 - 4), 13); //Write the indicator "->"
Write_int(Sensor_Count[n], n - (PageNumber * 8 - 4), 15);
}

}
}

}

最佳答案

首先,我要说这段代码效率不高主要是因为它在重复自己,而不是因为内存使用。

您如何在这方面改进它 - 您可以只有一个 for 循环而根本没有 switch-case。

如何?尝试将每个案例中的 for 循环表示为案例值的函数,然后根据其他 for 循环主体对其进行概括。例如:

请注意,对于每种情况:

  • for 循环的初始值总是(PageNumber-1)*8
  • for 循环条件总是 n < PageNumber*8
  • 第一行里面if-then子句总是 Write_string("L ", (n-((PageNumber-1)*8), 0));

等等。继续概括每个随 PageNumber 变化的数字。然后你最终将只有一个 for 循环,根本没有 switch-case。

关于c - 使用通用案例进行开关改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com