博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第五章编程题
阅读量:4984 次
发布时间:2019-06-12

本文共 3245 字,大约阅读时间需要 10 分钟。

 

1.

/***将标准输入复制到标准输出,将所有大写字母转换为小写字母。*/#include 
int main(void){ int ch; while(ch = getchar() != EOF) { if (ch >= 'A' && ch <='Z') ch = ch + 'a' - 'A'; putchar(ch); }}

 

2.

/*从标准输入读取字符,并把它们写入到标准输出中,非字母字符按**输入形式输出,字母字符加密,加密为字母表上距其13个位置的字母。*/#include 
int 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;}#include 
int main(void){ printf("%x\n", store_bit_filed(0xffff, 0x123, 13, 9)); return 0;}

转载于:https://www.cnblogs.com/frechei/archive/2013/03/16/2963982.html

你可能感兴趣的文章
GA来源分析
查看>>
常用统计指标
查看>>
iOS设置圆角矩形和阴影效果
查看>>
在博客园的第一篇文章,先简单自述一下吧
查看>>
深入了解 Dojo 的服务器推送技术
查看>>
hdu 4284 状态压缩
查看>>
逆向分析技术
查看>>
Latex
查看>>
SpringMVC处理JSON
查看>>
几何建模
查看>>
java crm 系统 进销存 springmvc SSM项目项目源码
查看>>
jQuery.extend 函数详解
查看>>
<jQuery> 一. jQuery简介及优点
查看>>
架构相关概念——学习笔记
查看>>
被称为“开发者神器”的GitHub,到底该怎么用?
查看>>
(坑集)Django环境配置
查看>>
利用padding-top/padding-bottom百分比,进行占位和高度自适应
查看>>
常用的监控系统资源的工具
查看>>
08ssm三大框架整合以前步骤
查看>>
R语言学习笔记之八
查看>>