Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4.  
  5.  
  6. int len ( char string[] ) {
  7.  
  8. int i = 0;
  9. while ( string[i] != '\0' ) {
  10.  
  11. i++;
  12.  
  13. }
  14.  
  15. return i;
  16.  
  17. }
  18.  
  19. int pow ( int base, int exp ) {
  20.  
  21. int powered = 1;
  22. if ( exp == 0 ) {
  23.  
  24. return powered;
  25.  
  26. }
  27.  
  28. else {
  29.  
  30. for (int i = 0; i < exp; i++) {
  31. powered *= base;
  32. }
  33. return powered;
  34. }
  35. }
  36.  
  37. int str2int ( char string ) {
  38.  
  39. int stringa2intero = string;
  40. return ( stringa2intero - 48 );
  41. }
  42.  
  43. void str2chr ( std::string string, char output[]) {
  44.  
  45. /*
  46.  
  47. devo andare a creare un array di cardinalita' pari alla lenght della string in input + 1 (dato che ci sta '\0'
  48. quindi calcolo prima la lunghezza della mia stringa;
  49. */
  50.  
  51. int lenStr = 0;
  52. while ( string[lenStr] != '\0' ) {
  53.  
  54. lenStr++;
  55.  
  56. }
  57.  
  58. for ( int i = 0; i < lenStr; i++) {
  59.  
  60. output[i] = string[i];
  61.  
  62. }
  63.  
  64. }
  65.  
  66. int bin2dec( char number[]) {
  67.  
  68. int lenght = len(number);
  69. int dec = 0;
  70. int base = 2;
  71. for ( int i = lenght - 1 ; i >= 0; i-- ) {
  72.  
  73. dec += str2int(number[i])*pow(base, lenght - i - 1);
  74.  
  75. }
  76. return dec;
  77. }
  78.  
  79. int main() {
  80.  
  81. // leggo dal file INPUT.txt la prima riga che mi stabilisce il numero di righe da leggere (in questo caso 5)
  82. std::fstream fileIn ( "INPUT.txt", std::ios::in );
  83. int lenFile = 0;
  84. std::string buffer;
  85. char bufferChr[100];
  86. fileIn >> lenFile; // prima riga == 5 => 5 righe da leggere
  87. std::cout << "*** RILEVATE " << lenFile << " RIGHE DA LEGGERE *** \n";
  88.  
  89. getline(fileIn, buffer);
  90.  
  91. for ( int i = 0; i < lenFile; i++) {
  92. std::cout << "i: " << i << "\n";
  93. std::cout << "bufferChr: " << bufferChr << "\n";
  94. str2chr(buffer, bufferChr);
  95.  
  96. getline(fileIn, buffer);
  97. str2chr(buffer, bufferChr);
  98. std::cout << buffer << " ==> " << bin2dec(bufferChr) << "\n";
  99. // devo fare una funzione che mi restituisca una #avvay di char
  100.  
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement