C语言第二章部分习题代码

#include "TypeExpression.h"


TypeExpression::TypeExpression(void)
{
}


TypeExpression::~TypeExpression(void)
{
}

/*
* 十六进制转换十进制
*/
void htoi(char s[]);
void practice2_3();
void squeeze(char retrieval[], char cha[]);
void practice2_4();
int any(char s1[], char s2[]);
void practice2_5();
unsigned getbits(unsigned x,int p, int n);
void setbits(unsigned x, int p, int n, unsigned y);
void practice2_6();
void invert(unsigned x, int p, int n);
void practice2_7();
void rightrot(unsigned int x, int n);
void practice2_8();
int bitcount(unsigned x);
void main()
{
	practice2_8();
	printf("结束打印");
}

void htoi(char s[]){
	int totalNum = 0;
	int c;
	int position = 0;
	c = s[position];
	if (s[0] == '0' && s[1] == 'x' || s[1] == 'X') {
		while (c != '\0' && c > 0) {
			position ++;
			c = s[position];
		}
		position --;
		for (c = s[position]; position > 1; position --) {
			int num;
			// 小写转大写
			if (c >= 'a' && c <= 'z') {
				c -= 'a' - 'A';
			}
			// 大写A~F转换成10-15,数字字符转数字0-9
			if (c >= 'A' && c <= 'F') {
				num = c - 'A' + 10;
			} else {
				num = c - '0';
			}
			if (position > 2) {
				int exponent = position - 2; // 指数
				int exponentNum = 1; // 十六进制对应指数结果值
				for (; exponent > 0; exponent--)
				{
					exponentNum *= 16;
				}
				num *= exponentNum; // 计算当前位置对应的结果值
			}
			totalNum += num;
		}
		printf("\n十六进制转换十进制\t%6d", totalNum);
	} else {
		printf("\n字符串不符合十六进制格式");
	}
	
	printf("\n打印结束");
}

void practice2_3() {
	printf("录入合法的十六进制的字符串,将帮您转换成对应的十进制数\n");
	int c;
	char ch [500];
	int position = 0;
	while ((c = getchar()) != EOF && c != '\n' && c != '\t' && c != ' ') {
		ch[position] = c;
		position ++;
	}
	htoi(ch);
}

void squeeze(char retrieval[], char cha[]) {
	char retrievalRequest[200];
	int i = 0;
	int retrievalRequestIndex = 0;
	int c;
	while (i < 200 && (c = cha[i]) != '\0' && c >= 0)
	{
		bool isContainer = false;
		int j = 0;
		int rc;
		while ((rc = retrieval[j ++]) != '\0' && rc >= 0) {
			if(rc == c) {
				isContainer = true;
				break;
			}
		}
		if (!isContainer) {
			retrievalRequest[retrievalRequestIndex ++] = c;
		}
		i ++;
	}
	retrievalRequest[retrievalRequestIndex] = '\0';
	printf("\n需要去除敏感字符为:\t%s", retrieval);
	printf("\n去除后的结果\t%s", retrievalRequest);
}

char retrieval[6] = {'c', 'h', 'l', 'w', 'y', '\0'};
void practice2_4(){
	printf("您输入的字符串将进行敏感字符过滤\n");
	char scannCha[200];
	int c;
	int index = 0;
	while(index < 200 &&(c = getchar()) != EOF && c != '\n'){
		scannCha[index++] = c;
	}
	
	squeeze(retrieval, scannCha);
}

int any(char s1[], char s2[]){
	char c1;
	char c2;
	int i = 0;

	while ((c2 = s2[i]) != EOF && c2 != '\0')
	{
		int j = 0;
		while ((c1 = s1[j++]) != EOF && c1 != '\0')
		{
			if (c1 == c2)
				return i;
		}
		i++;
	}
	return -1;
}
#define CHAR_MAX 200
void practice2_5() {
	printf("请输入任意字符\n");
	char s1[CHAR_MAX];
	int c;
	int index = 0;
	while(index < CHAR_MAX && (c = getchar()) != EOF && c != '\n'){
		s1 [index++] = c;
	}
	s1[index] = '\0';
	printf("查找字符串:%s 任意字符在字符串:%s,中第一次出现的位置,如果不存在则返回 -1",s1, retrieval);
	int retrievalIndex = any(s1, retrieval);
	if (retrievalIndex > -1) {
		printf("\n第一次出现的字符为%s,出现的位置%3d", retrieval[retrievalIndex], retrievalIndex);
	} else {
		printf("\n没有相同的字符");
	}
	
}

unsigned getbits(unsigned x, int p, int n){
	int a = x >> (p - n);
	printf("\n对%d右移%d位 结果%d", x, (p -n), a);
	int b = ~(~0 << n);
	printf("\n用于按位与的值%d", b);
	int c = a & b;
	printf("\nrequest= %d", c);
	return c;
}

void setbits(unsigned x, int p ,int n, unsigned y) {
	unsigned int a = getbits(x, p, n);
	int b = y >> n << n;
	int request = b + a;
	printf("\n将%d中最右边的%d位置设置为%d结果 %d", y, n, a, request);
}

void practice2_6(){
	setbits(77, 4, 3, 1000);
}

void invert(unsigned x, int p, int n){
	printf("\n将%d中从%d位置开始向%d位置的(二进制)求反,其余位置不变", x, p, n);
	int interval = getbits(x, p, n);
	int rest = x & ~(~0 << (p - n));
	printf("\n剩余位数的值rest= %d", rest);
	int unInterval = ~ interval ^ (~0 << n);// 11111001 ^ 11111000 = 1
	printf("\n对区间内的值%d取反,与对~0向左移%d位的结果求按位异或结果unInterval= %d", interval, n, unInterval);
	int andRest = unInterval<< (p-n) | rest;
	printf("\n取反后的值与剩余的值按位或【|】andRest= %d", andRest);
	int value = x >> p << p | andRest;
	printf("\n对原始值先左后右移动 %d 位,再与andRest求按位或【|】打印结果值value= %d", p ,value);
}

void practice2_7(){
	invert(80, 4, 3);
}

void rightrot(unsigned int x, int n) {
	int x1 = x;
	int p;
	for (p = 0; x != 0; x>>=1)
	{
		p++;
	}
	printf("\n%d的二进制位数 %d", x ,p);
	n = n > p ? p :n;
	int move = x & ~(~0 << n);
	printf("\n%d右移%d位移出的值 = %d", x, n, move);
	int moveLeft = move << (p - n); // 待移动位置左移
	printf("\n%d左移%d位移出的值 = %d", move, p - n, moveLeft);
	int rest = x >> n;
	printf("\n%d右移%d位 = %d", x, n, rest);
	int request = rest | moveLeft;
	printf("\n%d右移%d位并补充到前面得到值 = %d", x, n, request);
}

void practice2_8() 
{
	rightrot(77, 4);
}

int bitcount(unsigned x)
{
	int b;
	for (b = 0; x != 0; x>>=1)
	{
		if(x > x & 01)
			b++;
	}
	return b;
}

猜你喜欢

转载自blog.csdn.net/ff_hh/article/details/83007835