Advertisement
haunted_mind

ind1

Dec 8th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #pragma hdrstop
  2. #pragma argsused
  3.  
  4. #ifdef _WIN32
  5. #include <tchar.h>
  6. #else
  7. typedef char _TCHAR;
  8. #define _tmain main
  9. #endif
  10.  
  11. #include <stdio.h>
  12. #include <iostream>
  13. #include <conio.h>
  14. #include <string>
  15.  
  16. using namespace std;
  17.  
  18. void inputNumInBase12(char*Num12)
  19. {
  20. cout << "Enter 12-base num( it must be lesser or equal to 41A792678515120367), using digits and symbol 'A', 'B'."<<endl;
  21. cin.getline(Num12,19);
  22. cout << "u've entered "<< Num12;
  23. }
  24.  
  25. int conwert12DigitsTo10(char digit12)
  26. {
  27. if ((digit12!='A')&&(digit12!='B')) {
  28. return (digit12-'0');
  29. }
  30. else if (digit12!='A')
  31. {
  32. return 11;
  33. }
  34. else return 10;
  35. }
  36.  
  37. long long conwerBasefrom12To10(char*base12){
  38. int length = strlen(base12);
  39. long long res=0;
  40. for (int i = 0; i < length; i++)
  41. {
  42. res=res*12+conwert12DigitsTo10(*base12);
  43. // cout << "\ndigit12"<<i<<" = "<<conwert12DigitsTo10(*base12);
  44. //cout << "\nres"<<i<<" = "<<res;
  45. base12++;
  46. }
  47. return res;
  48. }
  49.  
  50. void conwertBase10To9(char * base9, long long base10)
  51. {
  52. int i= 0;
  53. do{
  54. base9[i]=char(base10%9+'0');
  55. base10/=9;
  56. i++;
  57. }while (base10);
  58. base9 = strrev(base9);
  59. }
  60.  
  61. int conwert12DigitIn10Nums()
  62. {
  63. char a = getch();
  64. if (int(a)==13)
  65. {
  66. return -1;
  67. }
  68. cout<<a;
  69. if (a=='A') {
  70. return 10;
  71. }
  72. if (a=='B') {
  73. return 11;
  74. }
  75. return (a-'0');
  76.  
  77. }
  78.  
  79. long long enterNumIn12BaseWithoutMassAndConwertItIn10BaseNum()
  80. {
  81. long long res = 0;
  82. cout << "Enter 12-base num( it must be lesser or equal to 41A792678515120367), using digits and symbol 'A', 'B'."<<endl;
  83. for (int i = 0; (i < 20); i++) {
  84. int digit = conwert12DigitIn10Nums();
  85. if (digit==-1) {
  86. break;
  87. }
  88. res=res*12+digit;
  89. }
  90. return res;
  91. }
  92.  
  93. void conwert10BaseTo9WithoutArray(long long base10)
  94. {
  95. int ans;
  96. int i = 0;
  97. cout << "In base9: ";
  98. long long ink = base10;
  99. do{
  100. ink/=9;
  101. i++;
  102. }while(ink!=0);
  103. for (int j =i ; j >0; j--) {
  104. long long incr = base10;
  105. for (int k = 0; k< j-1;k++ )
  106. {
  107. incr/=9;
  108. }
  109. cout <<char(incr%9+'0');
  110. }
  111.  
  112. }
  113. int _tmain(int argc, _TCHAR* argv[])
  114. {
  115. cout << "This program convert 12-base nums into 9-base nums"<<endl;
  116. char base12[19]={'\0'};
  117. inputNumInBase12(base12);
  118. long long base10=conwerBasefrom12To10(base12);
  119. cout<<"\nbase10 "<<base10;
  120. char base9[21]={'\0'};
  121. conwertBase10To9(base9, base10);
  122. cout << "\nbase9 : "<< base9;
  123. cout << "\n\nNow this program will solve the same problem without using arrays.";
  124. long long base10two = enterNumIn12BaseWithoutMassAndConwertItIn10BaseNum();
  125. cout << "\n\nbase10two "<< base10two;
  126. cout <<endl;
  127. conwert10BaseTo9WithoutArray(base10two);
  128. getch();
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement