2013-02-04 40 views
-4

如何使用atoi使pairCount產生這一行?C,unsigned int arg的二進制表示法

# ./paircounts 17 0

# ./paircounts 255 7

# ./paircounts 10 20 too many arguments!

int pairCounts(unsigned int n); 

int main(int argc, char *argv[]) { 
    const char testStr[] = "# pairs in base 2 of %u = %d, should be %d\n"; 
    printf (testStr, 0, pairCount (0), 0); 
    printf (testStr, 11, pairCount (11), 1); 
    printf (testStr, 2863377066u, pairCount (2863377066u), 2); 
    printf (testStr, 268435456, pairCount (268435456), 0); 
    printf (testStr, 4294705151u, pairCount (4294705151u), 29); 
    return 0; 
} 

int pairCounts(unsigned int n) { 
} 
+6

通常的問題:你有什麼試過? – Bharat

+0

我也會添加,請訪問[THIS](http://mattgemmell.com/2008/12/08/what-have-you-tried/)網站 –

+0

userz2038240,請停止破壞你的問題。通常,如果您想刪除其中一個問題,則可以使用它下面的刪除鏈接。不過,如果人們發佈了答案,你將無法做到這一點。如果您想擺脫已發佈的內容,請考慮使用上面的「標誌」鏈接向主持人尋求幫助。 –

回答

0

閱讀頁。我知道它的cplusplus.com,但它仍然適用於atoi的c版本。我不完全相信你正在試圖完成的任務,但我有兩個想法:

  1. 您試圖檢查的觀點的權利數量已經走在這種情況下你不需要的atoi,你只需要檢查ARGC正確的金額:

    if (argc != 3) throw some error; 
    

    記住ARGC包括節目名稱,以便它永遠是一個更那麼實際輸入參數。

  2. 您想使用atoi來做其他事情,在這種情況下閱讀atoi指南,因爲它非常易於使用。

如果你想要更多的說明,你需要改善你的問題。祝你好運。

1

爲什麼你會用atoi做任何事情?如果你想把一個字符串解析爲一個整數值,你應該使用strol。並且atoi甚至不適合生產輸出。要生成行:

# ./paircounts 17 0 

你會使用puts(或fputs,或printf,或fprintf,或可能fwrite,也許write,...),而不是atoi。例如:puts("# ./paircounts 17 0")。對於其他線路也是如此。