Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<conio.h>
  4.  
  5. int SymbolCodeToInt(char c, int b)
  6. {
  7. if (b <= 10 && c >= '0' && c <= '0' + b - 1) // cifra in baza b
  8. {
  9. c -= '0'; // se obtine cifra
  10. }
  11. else if (b <= 16 && c >= 'A' && c <= 'A' + b - 1) // cifra hexazecimala scrisa cu litera mare
  12. {
  13. c += 10 - 'A';
  14. }
  15. else if (b <= 16 && c >= 'a' && c <= 'a' + b - 1) // cifra hexazecimala scrisa cu litera mica
  16. {
  17. c += 10 - 'a';
  18. }
  19. return c;
  20. }
  21.  
  22. unsigned long long StrToInt(const char string[], int nS, int b)
  23. {
  24. int j;
  25. unsigned long long I = 0;
  26.  
  27. for (j = 0; j < nS; ++j) // parcurgem fiecare caracter din sirul de intrare
  28. {
  29. // transformam caracterul in valoare, depinzand de pozitia in codul ASCII
  30. // apoi folosim formula din laborator
  31. I = I * b + SymbolCodeToInt(string[j], b);
  32. }
  33.  
  34. return I;
  35. }
  36.  
  37. char IntToSymbolCode(int i, int b)
  38. {
  39. if (i <= 9 && i >= 0) // cifra in baza 10
  40. {
  41. return (char)i + '0'; // se obtine cifra
  42. }
  43. else if (i >= 10) // cifra hexazecimala
  44. {
  45. return (char)i - 10 + 'A';
  46. }
  47. return '?';
  48. }
  49.  
  50. void Reverse(char S[], int nS) // Little Endian => Big Endian
  51. {
  52. int j = 0;
  53. int k = nS - 1;
  54. char aux;
  55. while (j < k)
  56. {
  57. aux = S[j];
  58. S[j] = S[k];
  59. S[k] = aux;
  60. j++;
  61. k--;
  62. }
  63. }
  64.  
  65. int IntToStr(unsigned long long I, int b, char S[])
  66. {
  67. int nS = 0; // numarul de caractere ale sirului format
  68. do {
  69. S[nS++] = IntToSymbolCode((int)(I % b), b);
  70. I /= b;
  71. } while (I != 0);
  72.  
  73. S[nS] = '\0';
  74. Reverse(S, nS);
  75. return nS;
  76. }
  77.  
  78. void Converteste(const char numar[], size_t nrCifre, int bazaInitiala, int bazaFinala, char rezultat[])
  79. {
  80. IntToStr(StrToInt(numar, (int)nrCifre, bazaInitiala), bazaFinala, rezultat);
  81. }
  82.  
  83. int main() {
  84. char numar[100];
  85. char rezultat[100];
  86. int bI, bF;
  87.  
  88. printf("Introduceti numarul ca sir de caractere: ");
  89. fgets(numar, 99, stdin);
  90. numar[strlen(numar) - 1] = '\0';
  91.  
  92. printf("Introduceti baza de numeratie in care este numarul %s: ", numar);
  93. scanf_s("%d", &bI);
  94.  
  95. printf("Introduceti baza de numeratie in care sa fie convertit numarul %s: ", numar);
  96. scanf_s("%d", &bF);
  97.  
  98. Converteste(numar, strlen(numar), bI, bF, rezultat);
  99. printf("Numarul %s din baza %d in baza %d este: %s\n", numar, bI, bF, rezultat);
  100.  
  101. Converteste(rezultat, strlen(rezultat), bF, bI, numar);
  102. printf("Numarul %s convertit inapoi, din baza %d in baza %d este: %s\n", rezultat, bF, bI, numar);
  103. _getch();
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement