2016-09-07 6 views
-4
#include<stdio.h> 
#include<string.h> 
#include <math.h> 
long long convertDecimalToBinary(int n); 

int main() { 
    int verify; 
    long long bip, dip; 
    char str1[100]; 
    printf("Enter dotted decimal ip address :\n"); 
    scanf("%s",str1); 
    verify = bin_verify(str1); 

     seperate(str1); 

    return 0; 

} 
int bin_verify(char str1[]) { 
    int i; 
    for(i = 0; i < strlen(str1); i++) { 
     if((str1[i] < 255) && (str1[i] > 0)) { 
      return 1; 
     } 
    } 
} 

// function to get first decimal no sepreted 

int seperate(char str1[]) { 
    int s1, s2, s3, s4; 
    int i, j, k = 0, m, cnt = 0, cnt1 = 0, pos = 0; 
    char a[4], str2[100]; 
    for(i = 0; i < strlen(str1); i++) { 
     pos = cnt; 
     if(str1[i] == '.') { 
      k = i; 
      pos = cnt; 
      for(j = 0; j < i; j++) { 
       a[j] = str1[k-cnt]; 
       cnt = cnt - 1; 
      } 
     break; 
     } 
     else { 
      cnt++; 
      //goto one; 
     } 

    } 
    for(m = 0; m <= pos; m++) { 
     str1++; 
    } 
    s1 = atoi(a); 
    s1 = convertDecimalToBinary(s1); 
    printf("Binary Format of IP :\n"); 
    printf("%d.",s1); 
    seperate2(str1); 
    return 0; 
} 




// function to get second decimal no sepreted 
int seperate2(char str1[]) { 
    int s1, s2, s3, s4; 
    int i, j, k = 0, m, cnt = 0, cnt1 = 0, pos = 0; 
    char a[4], str2[100]; 
    for(i = 0; i < strlen(str1); i++) { 
     pos = cnt; 
     if(str1[i] == '.') { 
      k = i; 
      pos = cnt; 
      for(j = 0; j < i; j++) { 
       a[j] = str1[k-cnt]; 
       cnt = cnt - 1; 
      } 
     break; 
     } 
     else { 
      cnt++; 
      //goto one; 
     } 

    } 
    for(m = 0; m <= pos; m++) { 
     str1++; 
    } 
    s2 = atoi(a); 
    s2 = convertDecimalToBinary(s2); 
    printf("%d.",s2); 
    seperate3(str1); 
    return 0; 
} 


// function to get third decimal no sepreted 

int seperate3(char str1[]) { 
    int s1, s2, s3, s4; 
    int i, j, k = 0, m, cnt = 0, cnt1 = 0, pos = 0; 
    char a[4], str2[100]; 
    for(i = 0; i < strlen(str1); i++) { 
     pos = cnt; 
     if(str1[i] == '.') { 
      k = i; 
      pos = cnt; 
      for(j = 0; j < i; j++) { 
       a[j] = str1[k-cnt]; 
       cnt = cnt - 1; 
      } 
     break; 
     } 
     else { 
      cnt++; 
      //goto one; 
     } 

    } 
    for(m = 0; m <= pos; m++) { 
     str1++; 
    } 
    s3 = atoi(a); 
    s3 = convertDecimalToBinary(s3); 
    printf("%d.",s3); 
    seperate4(str1); 
    return 0; 
} 

// function to get fourth decimal no sepreted 

int seperate4(char str1[]) { 
    int s1, s2, s3, s4; 
    int i, j, k = 0, m, cnt = 0, cnt1 = 0, pos = 0; 
    char a[4], str2[100]; 
    for(i = 0; i < strlen(str1); i++) { 
     pos = cnt; 
     if(str1[i] == '.') { 
      k = i; 
      pos = cnt; 
      for(j = 0; j < i; j++) { 
       a[j] = str1[k-cnt]; 
       cnt = cnt - 1; 
      } 
     break; 
     } 
     else { 
      cnt++; 


     } 

    } 

    for(m = 0; m <= pos; m++) { 
     str1++; 
    } 
    s4 = atoi(a); 
    s4 = convertDecimalToBinary(s4); 
    printf("%d\n",s4); 

    return 0; 
} 

//to convert decimal to binary 

long long convertDecimalToBinary(int n) 
{ 

//printf("%d", n); 
    long long binaryNumber = 0; 
    int remainder, i = 1,step=0; 

    while (n!=0) 
    { 
     remainder = n%2; 
     // printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, remainder, n/2); 

     n /= 2; 

     binaryNumber += remainder*i; 
     i *= 10; 
    } 
    return binaryNumber; 
} 

出力:なぜドット付き10進数の最後の数値がバイナリに変換されないのですか?

Enter dotted decimal ip address : 

192.15.7.4 
Binary Format of IP : 
11000000.1111.111.0 

私はそれは常に最後進数のバイナリとして0を返す二項にIPアドレスを変換したいが、 。 seperate4()機能を実行していないのはなぜですか?

+0

'inet_addr()'のようなライブラリ関数を使用していない理由は何ですか? – Barmar

+1

なぜ4つの関数が必要ですか?文字列を分割してループで処理するには 'strtok()'を使います。 – Barmar

+2

とは無関係の 'bin_verify'はナンセンスです。 – BLUEPIXY

答えて

1

あなたが間違ったことは既にコメントに記載されていますが、あなたはあまりにも複雑で、ほぼRube-Goldbergのようです。それは非常に簡単で、どんな複雑なトリックもなしに行うことができます。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

// ALL CHECKS OMMITTED! 

char *int8_to_bin(int n, char *buf) 
{ 
    int i = 8; 
    // we need an unsigned integer for the bit-juggling 
    // because of two's complement numbers. 
    // Not really needed here because the input is positive 
    unsigned int N; 
    // check for proper size 
    if (n < 0 || n > 255) { 
    return NULL; 
    } 
    // is safe now 
    N = (unsigned int) n; 
    // we work backwards here, least significant bit first 
    // but we want it least significant bit last. 
    while (i--) { 
    // make a (character) digit out of an integer 
    buf[i] = (char) (N & 0x1) + '0'; 
    // shift one bit to the right and 
    // drop the least significant bit by doing it 
    N >>= 1; 
    } 
    // return the pointer to the buffer we got (for easier use) 
    return buf; 
} 

int main(int argc, char **argv) 
{ 
    // keeps the intermediate integer 
    int addr; 
    // command-line argument, helper for strtol() and int8_to_bin() 
    char *s, *endptr, *cp; 
    // buffer for int8_to_bin() to work with 
    // initialize to all '\0'; 
    char buf[9] = { '\0' }; 
    // array holding the end-result 
    // four 8-character groups with three periods and one NUL 
    char binaddr[4 * 8 + 3 + 1]; 
    // iterator 
    int i; 

    if (argc < 2) { 
    fprintf(stderr, "Usage: %s dotted_ipv4\n", argv[0]); 
    exit(EXIT_FAILURE); 
    } 
    // set a pointer pointing to the first argument as a shortcut 
    s = argv[1]; 

    // the following can be done in a single loop, of course 

    // strtol() skips leading spaces and parses up to the first non-digit. 
    // endptr points to that point in the input where strtol() decided 
    // it to be the first non-digit 
    addr = (int) strtol(s, &endptr, 0); 

    // work on copy to check for NULL while keeping the content of buf 
    // (checking not done here) 
    cp = int8_to_bin(addr, buf); 
    // if(cp == NULL)... 

    // copy the result to the result-array 
    // cp has a NUL, is a proper C-string 
    strcpy(binaddr, cp); 

    // rinse and repeat three times 
    for (i = 1; i < 4; i++) { 
    // skip anything between number and period, 
    // (or use strchr() to do so) 
    while (*endptr != '.'){ 
     endptr++; 
    } 
    // skip the period itself 
    endptr++; 
    // add a period to the output 
    strcat(binaddr, "."); 
    // get next number 
    addr = (int) strtol(endptr, &endptr, 0); 
    cp = int8_to_bin(addr, buf); 
    // if(cp == NULL)... 
    strcat(binaddr, cp); 
    } 
    printf("INPUT: %s\n", s); 
    printf("OUTPUT: %s\n", binaddr); 

    exit(EXIT_SUCCESS); 
} 

私たちは、複雑な構文解析アルゴリズムを必要としない、strtol()が私たちのためにそれをしない、私たちは次の期間に自分自身を見つける必要があります。入出力のサイズは既知であるか、または制限内にあるかどうかを簡単にチェックすることができます。たとえば、退屈でエラーが発生しやすいメモリ割り当ては不要です。固定サイズのバッファとstrcat()を使用できます。

海軍だけでなくプログラミングでも保持される原則があります。KISS

関連する問題