Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- int len ( char string[] ) {
- int i = 0;
- while ( string[i] != '\0' ) {
- i++;
- }
- return i;
- }
- int pow ( int base, int exp ) {
- int powered = 1;
- if ( exp == 0 ) {
- return powered;
- }
- else {
- for (int i = 0; i < exp; i++) {
- powered *= base;
- }
- return powered;
- }
- }
- int str2int ( char string ) {
- int stringa2intero = string;
- return ( stringa2intero - 48 );
- }
- void str2chr ( std::string string, char output[]) {
- /*
- devo andare a creare un array di cardinalita' pari alla lenght della string in input + 1 (dato che ci sta '\0'
- quindi calcolo prima la lunghezza della mia stringa;
- */
- int lenStr = 0;
- while ( string[lenStr] != '\0' ) {
- lenStr++;
- }
- for ( int i = 0; i < lenStr; i++) {
- output[i] = string[i];
- }
- }
- int bin2dec( char number[]) {
- int lenght = len(number);
- int dec = 0;
- int base = 2;
- for ( int i = lenght - 1 ; i >= 0; i-- ) {
- dec += str2int(number[i])*pow(base, lenght - i - 1);
- }
- return dec;
- }
- int main() {
- // leggo dal file INPUT.txt la prima riga che mi stabilisce il numero di righe da leggere (in questo caso 5)
- std::fstream fileIn ( "INPUT.txt", std::ios::in );
- int lenFile = 0;
- std::string buffer;
- char bufferChr[100];
- fileIn >> lenFile; // prima riga == 5 => 5 righe da leggere
- std::cout << "*** RILEVATE " << lenFile << " RIGHE DA LEGGERE *** \n";
- getline(fileIn, buffer);
- for ( int i = 0; i < lenFile; i++) {
- std::cout << "i: " << i << "\n";
- std::cout << "bufferChr: " << bufferChr << "\n";
- str2chr(buffer, bufferChr);
- getline(fileIn, buffer);
- str2chr(buffer, bufferChr);
- std::cout << buffer << " ==> " << bin2dec(bufferChr) << "\n";
- // devo fare una funzione che mi restituisca una #avvay di char
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement