본문 바로가기
Network

[C Network Programming] Parity Code C코드

by TYB 2024. 3. 13.
반응형

#include<stdio.h>
#pragma warning(disable:4996)

int main() {
	char Cnum_1;
	char Cnum_2;
	int i, j;
	int result_1[20];
	int result_2[20];
	result_1[7] = 0;
	result_2[7] = 0;
	int count_a1 = 0, count_a2 = 0, count_b1 = 0, count_b2 = 0;
	printf("Cnum_1,Cnum_2입력: ");
	scanf("%c %c",&Cnum_1,&Cnum_2);
	printf("Cnum_1:%d\nCnum_2:%d\n", Cnum_1, Cnum_2);
	int num_1 = Cnum_1;
	int num_2 = Cnum_2;
	for (i = 0; num_1 > 0; i++) {
		result_1[i] = num_1 % 2;
		result_2[i] = num_2 % 2;
		num_1 = num_1 / 2;
		num_2 = num_2 / 2;
	}
	printf("");
	for (j = i ; j >= 0; j--) {
		printf("%d",result_2[j]);
		}
	printf(" ");
	for (j = i ; j >= 0; j--) {
		printf("%d", result_1[j]);
	}
	printf("\n");
	for (i = 7; i >= 0; i--) {
		if (i < 4) {
			if (result_1[i] == 1) count_a1++;	
		}
		if (i > 3) {
			if (result_1[i] == 1) count_a2++;
		}
	}
	for (i = 7; i >= 0; i--) {
		if (i < 4) {
			if (result_2[i] == 1) count_b1++;
		}
		if (i > 3) {
			if (result_2[i] == 1) count_b2++;
		}
	}
	
	for (j = 7; j >= 0; j--) {
		if (j < 4) {
			printf("%d", result_2[j]);
			if (j == 0) printf("(%d)", count_b2 % 2);
			
		}
		if (j > 3) {
			printf("%d", result_2[j]);
			if (j == 4) printf("(%d)", count_b1 % 2);
		}
	}
	for (j = 7; j >= 0; j--) {
		if (j < 4) {
			printf("%d", result_1[j]);
			if (j == 0) printf("(%d)", count_a2 % 2);
		}
		if (j > 3) {
			printf("%d", result_1[j]);
			if (j == 4) printf("(%d)", count_a1 % 2);
		}
	}


	return 0;
}

 

 

반응형