编码应该运筹帷幄之中,决胜千里之外
正文
大家2022年好,我是bug菌~
上一次跟大家更新一篇文章:
<C語(yǔ)言switch中case僅僅只是一個(gè)標(biāo)簽>
然后就有粉絲來(lái)問(wèn)了文章里面的達(dá)夫設(shè)備代碼不是很好理解,感覺(jué)很怪異:
這讓就讓我很頭疼,文章中應(yīng)該已經(jīng)講得非常詳細(xì)了,相信大部分都理解了。當(dāng)然這個(gè)朋友可能對(duì)C語(yǔ)言不是很熟悉,也可以理解,都是一個(gè)熟練過(guò)程。
通常善于編程的人邏輯思維是非常敏捷的,因?yàn)槌3P枰芽胤浅4蟮倪壿嫶a,那么"運(yùn)籌帷幄之中,決勝千里之外"的能力肯定不能太弱,當(dāng)你看到當(dāng)前代碼就能夠知曉后續(xù)的程序運(yùn)行過(guò)程和走向,從而理順其對(duì)系統(tǒng)的影響,這才是一個(gè)程序員真正對(duì)程序的把控力。
后來(lái)想想既然是switch中的case是一個(gè)標(biāo)簽,那么是不是可以直接用goto來(lái)進(jìn)行模擬呢?于是就有了如下的代碼:
1#include?<stdio.h>2#include?<stdlib.h>34//用goto模擬switch5void?duff_Simulate_Copy(?int?*?to,?int*?from,?int?count)6{7????int?n?=?(count?+?7)/8;89????if(count%8?==?0)?goto?case_label0; 10????if(count%8?==?1)?goto?case_label1; 11????if(count%8?==?2)?goto?case_label2; 12????if(count%8?==?3)?goto?case_label3; 13????if(count%8?==?4)?goto?case_label4; 14????if(count%8?==?5)?goto?case_label5; 15????if(count%8?==?6)?goto?case_label6; 16????if(count%8?==?7)?goto?case_label7; 17 18????case_label0:????do{?*to++?=?*from++; 19????case_label1:????????*to++?=?*from++; 20????case_label2:????????*to++?=?*from++; 21????case_label3:????????*to++?=?*from++; 22????case_label4:????????*to++?=?*from++; 23????case_label5:????????*to++?=?*from++; 24????case_label6:????????*to++?=?*from++; 25????case_label7:????????*to++?=?*from++; 26????????????????????}while(?--n>0); 27} 28 29//用達(dá)夫設(shè)備 30void?duff_Copy(?int?*?to,?int*?from,?int?count) 31?{ 32????int?n?=?(count + 7)/8; 33 34????switch(?count%8?)? 35???{???? 36?????case?0:?do{?*to++?=?*from++; 37?????case?7:?????*to++?=?*from++; 38?????case?6:?????*to++?=?*from++; 39?????case?5:?????*to++?=?*from++; 40?????case?4:?????*to++?=?*from++; 41?????case?3:?????*to++?=?*from++; 42?????case?2:?????*to++?=?*from++; 43?????case?1:?????*to++?=?*from++; 44????????????????}while(--n?>?0); 45????} 46}? 47 48//常規(guī)數(shù)據(jù)拷貝 49void??MyCopy(?int?*?Dst,?int?*?Src,?int?count) 50{ 51????int?i?=?0; 52 53????for(i?=?0;i?!=?count;?++i)? 54????{ 55??????*Dst?++??=?*Src?++?; 56????}? 57} 58 59//測(cè)試應(yīng)用程序 60int?main(int?argc,?char?*argv[])?{ 61 62????int?Array1[100]?=?{0}; 63????int?Array2[100]?=?{0}; 64????long?i?=?0; 65 66????for(i?=?0?;i?<?100;i++) 67????{ 68????????Array1[i]?=?i; 69????} 70 71????duff_Simulate_Copy(Array2,Array1,100); 72 73????for(i?=?0?;i?<?100;i++) 74????{ 75????????printf("%d?",Array2[i]); 76????????if((i?+?1)%10?==?0) 77????????printf("\n"); 78????} 79 80????return?0; 81}相信goto語(yǔ)句改寫的達(dá)夫設(shè)備應(yīng)該可以理解了吧。
同時(shí)也從另一方面展示了goto也是大有用處的。
大家也可以關(guān)注一下我的視頻號(hào):"嵌入式工程師",捕獲更多炫酷的嵌入式技術(shù)精彩~
最后
? ? ??好了,今天就跟大家分享這么多了,如果你覺(jué)得有所收獲,一定記得點(diǎn)個(gè)贊~,
最后一個(gè)bug,bug菌唯一創(chuàng)作平臺(tái)~
推薦專輯??點(diǎn)擊藍(lán)色字體即可跳轉(zhuǎn)
???MCU進(jìn)階專輯?
???嵌入式C語(yǔ)言進(jìn)階專輯?
???“bug說(shuō)”專輯?
??專輯|Linux應(yīng)用程序編程大全
??專輯|學(xué)點(diǎn)網(wǎng)絡(luò)知識(shí)
??專輯|手撕C語(yǔ)言
??專輯|手撕C++語(yǔ)言
??專輯|經(jīng)驗(yàn)分享
??專輯|電能控制技術(shù)
??專輯 | 從單片機(jī)到Linux
總結(jié)
以上是生活随笔為你收集整理的编码应该运筹帷幄之中,决胜千里之外的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 老闪创业那些事儿(27)——员工定级标准
- 下一篇: 破解JS加密:url unicode加密