HUST-奇偶校验

题目描述

输入一个字符串,然后对每个字符进行奇校验,最后输出校验后的二进制数(如'3’,输出:10110011)。

输入描述

输入包括一个字符串,字符串长度不超过100。

输出描述

可能有多组测试数据,对于每组数据,
对于字符串中的每一个字符,输出按题目进行奇偶校验后的数,每个字符校验的结果占一行。

程序代码

#include <iostream>
using namespace std;
void by(char c)
{
	int num = int(c);
	int a[8], i, count=0;
	for(i=0; i<8; i++)
	a[i] = 0;
	while(num)
	{
		i--;
		a[i] = num%2;
		if(num%2) count++;
		num /= 2;
	}
	if(count%2) a[0]=0;
	else a[0]=1;
	for(i=0; i<8; i++)
	cout << a[i];
}
int main()
{
	char c;
	while(cin >> c)
    {
       by(c);
	   cout << endl; 
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38196810/article/details/81185898