LinMao's Blog
学习科研记录与分享!

CSAPP实验拆炸弹(bomb)源码

#include 
#include 
#include 

#define input_max_size 1024 // 允许一次输入最大长度

void init_bomb();
void explode_bomb();
int read_line(char*);
void phase_1(char*);
void phase_2(char*);
void phase_3(char*);
void phase_4(char*);
void phase_5(char*);
void phase_6(char*);
void phase_7(char*);
int strings_not_equal(const char*,const char*);
void read_six_numbers(const char*, int*);
int func4(int);

int main() {
char* input = (char*)malloc(sizeof(char) * input_max_size);
init_bomb();

read_line(input);
phase_1(input);

read_line(input);
phase_2(input);

read_line(input);
phase_3(input);

read_line(input);
phase_4(input);

read_line(input);
phase_5(input);

read_line(input);
phase_6(input);

read_line(input);
phase_7(input);

return 0;
}

void init_bomb() {
printf("Bomb start, let's defuse the bomb.\n");
}

void explode_bomb() {
printf("You type wrong answer, Bomb!!!");
exit(0);
}

// 在标准输入输出中读取一行
int read_line(char* line)
{
int c;
int len = 0;
while( (c = getchar()) != EOF && len < input_max_size ){
line[len++] = c;
if('\n' == c)
break;
}

line[len] = '\0';
return len;
}

// 输入一个数,
// bomb1中值为188(0xb2)
// bomb2中值为198(0xb2)
// bomb3中值为278(0xb2)
// bomb4中值为288(0xb2)
void phase_1(char* input) {
int num;

int result = sscanf(input, "%d", &num);
if (result != 1) {
explode_bomb();
}

if (num != 188) {
explode_bomb();
}

printf("Well done, you have defused a bomb!\n");
}

// 输入两个数
// bomb1中这两个数的和为10
// bomb2中这两个数的和为20
// bomb3中这两个数的和为15
// bomb4中这两个数的和为30
void phase_2(char* input) {
int num1, num2;
int result = sscanf(input, "%d %d", &num1, &num2);
if (result != 2)
explode_bomb();
if ((num1+num2) != 10)
explode_bomb();

printf("You are right, how about next one?\n");
}

// 输入两个数
// bomb1,bomb2中前一个是后一个的两倍
// bomb3中前一个是后一个的4倍
// bomb4中前一个是后一个的8倍
void phase_3(char* input) {
int num1, num2;
int result = sscanf(input, "%d %d", &num1, &num2);
if (result != 2)
explode_bomb();
if ((num1>>1) != num2)
explode_bomb();
printf("Ok, you have defused three bombs!\n");

}

// bomb1中输入字符串hello world
// bomb2中输入字符串hello
// bomb3中输入字符串hello sdu
// bomb4中输入字符串jinan
void phase_4(char* input) {
const char *dst = "hello world\n";
int result = strings_not_equal(input, dst);
if (result != 0) {
explode_bomb();
}

printf("Wow, you have defused four bombs!\n");
}

// 输入6个数
// bomb1中6个数是公比为4的等比数列
// bomb2中6个数是公比为4的等比数列
// bomb3中6个数是公比为2的等比数列
// bomb4中6个数是公比为2的等比数列
void phase_5(char *input) {
int a[6];
read_six_numbers(input, a);

int *begin = &a[0];
int *end = &a[5];

for (; begin < end; ++begin) {
int pre_value = *begin << 2;
if (pre_value != *(begin+1))
explode_bomb();
}

printf("Well done, you have defused five bombs!\n");
}

// 输入两个数,第一个是n,第二个是n的阶乘sum
void phase_6(char* input) {
int num1, num2;

int result = sscanf(input, "%d %d", &num1, &num2);
if (result != 2) {
explode_bomb();
}

result = func4(num1);
if (result != num2) {
explode_bomb();
}
printf("come on, baby! One bombs left!\n");
}

// 读出的字符与ox0f相与得出数组的下标值,然后根据下标索引出字符串"sdu"
// s -> 0x09 { ')','9','I','Y','i','y' }
// d -> 0x0f { '/','?','O','_','o' }
// u -> 0x05 { '%','5','E','U','e','u'}
void phase_7(char* input) {
static int array[] = { 'l', 'i', 'n', 'm', 'o', 'u', 'i', 'y', 'o', 's', 'x', 't', 'a', 'b', 'h', 'd'};
char str[4];

int len = strlen(input);
if(len != 4)
explode_bomb();

int i;
for (i = 0; i < 3; ++i) {
str[i] = array[input[i] & 0x0f];
}
str[3] = '\0';

int result = strings_not_equal(str, "sdu");
if (result != 0) {
explode_bomb();
}
printf("Excellent!!! You have defused all bombs!!!\n");
printf("Please visit www.linmao.site for source code.\n");

}

// 字符串比较
int strings_not_equal(const char* input,const char* dst) {
int result;
size_t len1 = strlen(input);
size_t len2 = strlen(dst);

if (len1 != len2) {
result = 1;
} else if (input[0] == '\0') {
result = 0;
} else {
result = 1;
while (*input++ == *dst++) {
if (*input == '\0') {
result = 0;
break;
}
}
}
return result;
}

// 从readline中读取输入的六个数
void read_six_numbers(const char* input, int *a) {
int result = sscanf(input, "%d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
if (result != 6) {
explode_bomb();
}
}

// 递归求n的阶乘
int func4(int n) {
if (n == 0)
return 1;
return n*func4(n-1);
}
赞(0) 打赏
转载请注明出处:LinMao's Blog(林茂的博客) » CSAPP实验拆炸弹(bomb)源码

评论 抢沙发

静态归档版本,评论功能已关闭。
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

LinMao's Blog(林茂的博客)

了解更多联系我们

觉得文章有用就打赏一下作者吧~

支付宝扫一扫打赏

支付宝

微信扫一扫打赏

微信