0
点赞
收藏
分享

微信扫一扫

我用c语言把何同学的代码跑起来了

@TOC

免责声明:仅供娱乐,只是展示这段代码在理论上是可行的。

原版代码

首先,先来看下视频中何同学的这两段代码:

powerCon

littleFingerForce

代码分析

首先呢,根据图片中的这两段代码,我猜测他可能是想获取数组中第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。
![](https://s2.51cto.com/images/blog/202208/17211243_62fce94b31d0522665.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

![在这里插入图片描述](https://s2.51cto.com/images/blog/202208/17211241_62fce949c8c618170.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
举报

相关推荐

0 条评论