Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- int main()
- {
- int input[5];
- int binary[4];
- int graycode[4];
- int decimal;
- int po;
- printf("첫 번째 코드를 입력해주세요 (0=이진코드,1=그레이코드) : ");
- scanf_s("%d", &input[0]);
- printf("코드 4자 입력\n");
- for (int i = 1; i < 5; i++) {
- printf("input[%d]입력 : ", i);
- scanf_s("%d", &input[i]);
- }
- printf("입력된 코드 ");
- for (int i = 0; i < 5; i++) {
- printf("%d ", input[i]);
- }
- printf("\n");
- if (input[0] == 0)//맨앞이 0이면 이진코드->그레이코드
- {
- graycode[0] = input[1];
- for (int j = 0; j < 3; j++)
- {
- if (graycode[j] == input[j + 2])
- graycode[j + 1] = 0;
- else graycode[j + 1] = 1;
- }
- printf("변환된 그레이코드 = ");
- for (int i = 0; i < 4; i++) {
- printf("%d ", graycode[i]);
- }
- }
- else
- {
- binary[0] = input[1];
- for (int j = 0; j < 3; j++) {
- if (input[j + 1] == input[j + 2])
- binary[j + 1] = 0;
- else binary[j + 1] = 1;
- }
- printf("변환된 이진코드 = ");
- for (int i = 0; i < 4; i++) {
- printf("%d ", binary[i]);
- }
- decimal = 0;
- po = 0;
- for(int k = 0; k<3; k++)
- {
- po = pow(2, 3 - k);
- decimal = decimal+(binary[k] * po);
- }
- printf("\n2진수의 decimal값 = %d\n", decimal);
- }
- }
Add Comment
Please, Sign In to add comment