Guest User

Untitled

a guest
Oct 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4.  
  5. int main()
  6.  
  7. {
  8. int input[5];
  9. int binary[4];
  10. int graycode[4];
  11. int decimal;
  12. int po;
  13. printf("첫 번째 코드를 입력해주세요 (0=이진코드,1=그레이코드) : ");
  14. scanf_s("%d", &input[0]);
  15.  
  16. printf("코드 4자 입력\n");
  17. for (int i = 1; i < 5; i++) {
  18. printf("input[%d]입력 : ", i);
  19. scanf_s("%d", &input[i]);
  20.  
  21. }
  22.  
  23. printf("입력된 코드 ");
  24. for (int i = 0; i < 5; i++) {
  25. printf("%d ", input[i]);
  26. }
  27. printf("\n");
  28.  
  29. if (input[0] == 0)//맨앞이 0이면 이진코드->그레이코드
  30. {
  31. graycode[0] = input[1];
  32. for (int j = 0; j < 3; j++)
  33. {
  34. if (graycode[j] == input[j + 2])
  35. graycode[j + 1] = 0;
  36. else graycode[j + 1] = 1;
  37. }
  38. printf("변환된 그레이코드 = ");
  39. for (int i = 0; i < 4; i++) {
  40. printf("%d ", graycode[i]);
  41. }
  42. }
  43.  
  44.  
  45.  
  46. else
  47. {
  48. binary[0] = input[1];
  49. for (int j = 0; j < 3; j++) {
  50. if (input[j + 1] == input[j + 2])
  51. binary[j + 1] = 0;
  52. else binary[j + 1] = 1;
  53. }
  54. printf("변환된 이진코드 = ");
  55. for (int i = 0; i < 4; i++) {
  56. printf("%d ", binary[i]);
  57. }
  58. decimal = 0;
  59. po = 0;
  60. for(int k = 0; k<3; k++)
  61. {
  62. po = pow(2, 3 - k);
  63. decimal = decimal+(binary[k] * po);
  64.  
  65. }
  66. printf("\n2진수의 decimal값 = %d\n", decimal);
  67. }
  68.  
  69.  
  70. }
Add Comment
Please, Sign In to add comment