原版代码
首先,先来看下视频中何同学的这两段代码:
代码分析
首先呢,根据图片中的这两段代码,我猜测他可能是想获取数组中第0,1,5,6,10,51,56,58,64号下标的值。
了解到这个目的后,就可以根据这个需求进行代码编写了。
forceCon[whichKey - 1] = force
void littleFingerForce(void) { powerCon(1 | 2 | 6 | 7 | 11 | 52 | 57 | 58 | 65, 10); }
代码实现
- 这段代码是根据B站up主:内田补水彩 的视频实现的
- c++实现:可看B站up主:摸鱼摸摸鱼鱼
#include <stdio.h> #include <stdlib.h> #define u8 unsigned char #define forceConLength 128 //数组长度 u8 forceCon[forceConLength] = {0}; //SaiBoDingZhen数组 u8 forceCon_tmp[forceConLength] = {0}; //临时缓存数组 u8 indextable[forceConLength] = {0}; //索引数组 u8 r = 0; u8 q = 0, p = 0, cnt = 0; /* 从字符串中提取数组,并包含数字索引信息 函数返回值作为powerCon函数输入值 */ int string2nums(char *nums) { while(1) { while(nums[r] && (nums[r] < '0' || nums[r] > '9')) { r++; } if(nums[r]) { p = r; q = r + 1; indextable[cnt] = nums[r] - '0'; while(nums[q] >= '0' && nums[q] <= '9') { indextable[cnt] = 10 * indextable[cnt] + (nums[q] - '0'); q++; } r = q; cnt++; } else { break; } } return 0; } void powerCon(u8 whichKey, u8 force) { if(whichKey) forceCon[whichKey - 1] = force; else for(u8 i = 0; i < 68; i++) forceCon[i] = force; } #define powerCon(nums, force) powerCon(string2nums(#nums), force) void littleFingerForce(void) { powerCon(1 | 2 | 6 | 7 | 11 | 52 | 57 | 58 | 65, 10); } int main() { printf("I am SaiBoDingZhen!\n"); littleFingerForce(); /* 向索引位置赋值,其他位置为0 */ for(u8 k = 0; k < cnt; k++) { forceCon_tmp[indextable[k]] = forceCon[indextable[k]]; } /* 向原数组赋值,并打印输出 */ for(u8 j = 0; j < forceConLength; j++) { forceCon[j] = forceCon_tmp[j]; printf("%d -> %d\n", j, forceCon[j]); } system("pause"); }
代码执行结果
可以看出,数组的第0,1,5,6,10,51,56,58,64号位置输出结果为10,其他位置为0。