是否可以更改此“开关”?首先,我虽然可以使用“ 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);
}
}
}
}
答案 0 :(得分:4)
首先,我想说这段代码的效率不高,主要是因为它在重复自身,而不是因为内存使用情况。
如何在这方面进行改进-您只能有一个for循环而根本没有开关箱。
如何?尝试将每种情况下的for循环表示为case值的函数,然后根据其他for循环体将其概括。例如:
请注意,对于每种情况:
Axiom Axiom3: forall A B: Prop, (~A -> ~B)-> ((~A -> B) -> A).
Lemma classical : forall A : Prop, A \/ ~ A.
Proof.
intros A.
apply (Axiom3 (A \/ ~A) (A \/ ~A)).
- trivial.
- intros H. exfalso.
assert (H' : ~ ~ A).
{ intros HA. apply H. right. trivial. }
apply H'. intros HA. apply H. left. trivial.
Qed.
(PageNumber-1)*8
n < PageNumber*8
子句中的第一行始终为if-then
以此类推。
继续归纳每个随Write_string("L ", (n-((PageNumber-1)*8), 0));
而变化的数字,然后最终将只有一个for循环而根本没有切换情况。