Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cstdlib>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6.  
  7. /* kommentaarid */
  8. /* kt6 yl3 */
  9. /* programm "Kahendsüsteem" */
  10. /* koostaja Killu-Smilla Palk */
  11.  
  12. int convertBinaryToDecimal(int n)
  13. {
  14. int decimalNumber = 0, i = 0, remainder;
  15. while (n!=0)
  16. {
  17. remainder = n%10;
  18. n /= 10;
  19. decimalNumber += remainder*pow(2,i);
  20. ++i;
  21. }
  22. return decimalNumber;
  23. }
  24.  
  25. int convertDecimalToBinary(int n)
  26. {
  27. int binaryNumber = 0;
  28. int remainder, i = 1, step = 1;
  29.  
  30. while (n!=0)
  31. {
  32. remainder = n%2;
  33. n /= 2;
  34. binaryNumber += remainder*i;
  35. i *= 10;
  36. }
  37. return binaryNumber;
  38. }
  39.  
  40. main()
  41. {
  42.  
  43.  
  44.  
  45. while (true)
  46. {
  47.  
  48. char qvestioone[1];
  49.  
  50. printf("Kas soovite teisendust kahendsusteemist kumnendsusteemi (vastake 'd') \nvoi teisendust kumnendsusteemist kahendsusteemi (vastake 'b')? \nKui soovite valjuda, vastake v.\n");
  51. scanf("%s", &qvestioone);
  52.  
  53.  
  54. if (strcmp(qvestioone, "v") == 0)
  55. {
  56. break;
  57. }
  58.  
  59. else if (strcmp(qvestioone, "d") == 0)
  60. {
  61. int A;
  62. printf("Sisestage arv:\n");
  63. scanf("%d", &A);
  64. int vastus = convertBinaryToDecimal(A);
  65. printf("%d\n\n", vastus);
  66. }
  67.  
  68. else if (strcmp(qvestioone, "b") == 0)
  69. {
  70. int A;
  71. printf("Sisestage arv:\n");
  72. scanf("%d", &A);
  73. int vastus = convertDecimalToBinary(A);
  74. printf("%d\n\n", vastus);
  75. }
  76.  
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement