1.
/***将标准输入复制到标准输出,将所有大写字母转换为小写字母。*/#includeint main(void){ int ch; while(ch = getchar() != EOF) { if (ch >= 'A' && ch <='Z') ch = ch + 'a' - 'A'; putchar(ch); }}
2.
/*从标准输入读取字符,并把它们写入到标准输出中,非字母字符按**输入形式输出,字母字符加密,加密为字母表上距其13个位置的字母。*/#includeint main(void){ int ch; while(ch = getchar() != EOF) { if ((ch >= 'A' && ch <='M') || (ch >= 'a' && ch <= 'm')) ch = ch + 13; else if ((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z')) ch = ch - 13; putchar(ch); }}
3
/*my code*/unsigned int reverse_bits (unsigned int value){ int a = 8 * sizeof (unsigned int); unsigned int i, j = 1; unsigned int new_value = 0; for (; a > 0 ; a--) /* 一开始条件脑残地写成a <= 0*/ { new_value <<= 1; new_value |= value & j; value >>= 1; } return new_value;}/*guide's answer*/unsigned int reverse(unsigned int value){ unsigned int answer; unsigned int i; answer = 0; for (i = 1; i != 0; i <<= 1) { answer <<= 1; if (value & 1) answer |= 1; value >>= 1; } return answer;}int main (void){ unsigned int v = 0xfff; printf("%x\n%x\n", reverse_bits(v),reverse(v)); return 0;}
4.
/***guide中,求数组index以及位数bits用了函数,这样的话的确方便了,**每次需要时调用即可,虽然这里不必这样,但当相同的表达式用到很多**时就需要了,其中的思想应该学习。*/#include/*这两个头文件???啥用呢*/#include "bitarray.h"void set_bit(char bit_array[], unsigned bit_number){ unsigned int index, bits; index = bit_number / 8; bits = bit_number % 8; bit_array[index] |= (1 << bit_number);}void clear_bit(char bit_array[], unsigned bit_number){ unsigned int index, bits; index = bit_number / 8; bits = bit_number % 8; bit_array[index] &= ~(1 << bit_number);}/***guide中直接调用上面两个函数,简单多了好不好!o(╯□╰)o*/void assign_bit(char bit_array[], unsigned bit_number, int value){ unsigned int index, bits; index = bit_number / 8; bits = bit_number % 8; if (value) bit_array[index] |= (1 << bit_number); else bit_array[index] &= ~(1 << bit_number);}int test_bit(char bit_array[], unsigned bit_number){ unsigned int index, bits; index = bit_number / 8; bits = bit_number % 8; bit_array[index] &= (1 << bit_number); if (bit_array[index]) return 1; else return 0;}
5.
int store_bit_filed(int original_value,int value_to_store, unsigned starting_bit, unsigned ending_bit){ /*step 1*/ unsigned int c = starting_bit - ending_bit + 1; unsigned int mask = 1; unsigned int i; int new_value; for (i = 1; i < c; i++) { mask |= (mask << 1); } mask <<= ending_bit - 1; /*step 2*/ original_value = original_value & (~mask); /*step 3此处课后的提示有误!!*/ value_to_store <<= ending_bit -1; /*step 4*/ value_to_store &= mask; return value_to_store; /*step 5*/ new_value = value_to_store | original_value; //return new_value;}#includeint main(void){ printf("%x\n", store_bit_filed(0xffff, 0x123, 13, 9)); return 0;}