2012-11-07 66 views
0
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 

int myatoi(const char* string) { 
    int i = 0; 
    while (*string) { 
    i = (i << 3) + (i<<1) + (*string -'0'); 
    string++; 
    } 
    return i; 
} 

void decimal2binary(char *decimal, int *binary) { 
    decimal = malloc(sizeof(char) * 32); 
    long int dec = myatoi(decimal); 
    long int fraction; 
    long int remainder; 
    long int factor = 1; 
    long int fractionfactor = .1; 
    long int wholenum; 
    long int bin; 
    long int onechecker; 
    wholenum = (int) dec; 
    fraction = dec - wholenum; 

    while (wholenum != 0) { 
    remainder = wholenum % 2; // get remainder 
    bin = bin + remainder * factor; // store the binary as you get remainder 
    wholenum /= 2; // divide by 2 
    factor *= 10; // times by 10 so it goes to the next digit 
    } 
    long int binaryfrac = 0; 
    int i; 
    for (i = 0; i < 10; i++) { 
    fraction *= 2; // times by two first 
    onechecker = fraction; // onechecker is for checking if greater than one 
    binaryfrac += fractionfactor * onechecker; // store into binary as you go 
    if (onechecker == 1) { 
     fraction -= onechecker; // if greater than 1 subtract the 1 
    } 
    fractionfactor /= 10; 
    } 

    bin += binaryfrac; 
    *binary = bin; 
    free(decimal); 
} 

int main(int argc, char **argv) { 
    char *data; 
    data = malloc(sizeof(char) * 32); 
    int datai = 1; 
    if (argc != 4) { 
    printf("invalid number of arguments\n"); 
    return 1; 
    } 
    if (strcmp(argv[1], "-d")) { 
    if (strcmp(argv[3], "-b")) { 
     decimal2binary(argv[2], &datai); 
     printf("output is : %d" , datai); 
    } else { 
     printf("invalid parameter"); 
    } 
    } else { 
    printf("invalid parameter"); 
    } 
    free(data); 
    return 0; 
} 

在這個問題上,myatoi正常工作和decimal2binary算法是正確的,但我每次運行該代碼時它給我的輸出爲0。我不知道爲什麼。指針有問題嗎?我已經設置了可變數據的地址,但輸出仍然不變。十進制轉換爲二進制轉換不工作

./dec2bin "-d" "23" "-b" 
+2

調用這樣說:'./dec2bin -d 23 -b',而不是雙 - 引用 –

+0

shell將刪除引號,因此省略它們將不會有所作爲。 –

+0

好哇,我試過它,但我得到了109519692 – Nabmeister

回答

1

線:

long int fractionfactor = .1; 

將設置fractionfactor0因爲變量被定義爲一個整數。請嘗試使用floatdouble代替。

同樣,

long int dec = myatoi(decimal); 

存儲的整數值,所以wholenum是不必要的。


而不是

i = (i << 3) + (i<<1) + (*string -'0'); 

的代碼將更具可讀性爲

i = i * 10 + (*string - '0'); 

,並與今天的優化編譯器,這兩個版本可能會產生相同的目標代碼。一般來說,特別是當你的代碼不工作時,優先考慮優化的可讀性。


fraction *= 2; // times by two first 

評論這樣,簡單地把英文翻譯代碼,是不必要的,除非你使用的語言在一個不尋常的方式。您可以假設讀者熟悉該語言;相反,解釋你的推理更有幫助。

0
if(!strcmp(argv[3] , "-b")) 

if(!strcmp(argv[3] , "-d")) 

應該取消字符串比較函數的結果,以便繼續。否則它會打印無效的參數。因爲strcmp在字符串相等時返回'0'。

在「decimal2binary」功能要爲輸入參數「小數」在函數內部分配一個新的內存塊,

decimal = malloc(sizeof(char) * 32); 

這實際上覆蓋您輸入的參數數據。

0
void decimal2binary(char *decimal, int *binary) { 
    decimal = malloc(sizeof(char) * 32); 
    ... 
} 

上述代碼行分配一個新的內存塊到decimal,然後它將不再指向輸入數據。然後,線

long int dec = myatoi(decimal); 

新分配的存儲器中的(在隨機值)分配給dec

所以刪除行

decimal = malloc(sizeof(char) * 32); 

,你會得到正確的答案。

0

另一種編碼提示:不要寫成

if (strcmp(argv[1], "-d")) { 
    if (strcmp(argv[3], "-b")) { 
    decimal2binary(argv[2], &datai); 
    printf("output is : %d" , datai); 
    } else { 
    printf("invalid parameter"); 
    } 
} else { 
    printf("invalid parameter"); 
} 

你可以重構嵌套if塊,使他們更簡單,更容易理解。一般來說,早期檢查錯誤條件是一個好主意,可以將錯誤檢查與核心處理分開,並儘可能專門地解釋錯誤,以便用戶知道如何糾正錯誤。

如果你這樣做,也可能是更容易實現,無論原來的條件,應該被否定:

if (strcmp(argv[1], "-d") != 0) { 
    printf("Error: first parameter must be -d\n"); 
else if (strcmp(argv[3], "-b") != 0) { 
    printf("Error: third parameter must be -b\n"); 
} else { 
    decimal2binary(argv[2], &datai); 
    printf("Output is: %d\n" , datai); 
}