2012-07-18 70 views
0

我參與了一些編碼競賽,幸運的是我的代碼也運行了。然而,我的解決方案並沒有預料到,因爲我對輸入模式有誤。如何根據輸入測試用例文件測試我的程序


問題涉及取入的整數作爲輸入,並執行一些操作並返回一個不同或相同的整數。我沒有與任何程序問題,我只是不知道如何代碼,以便採取投入這樣


Input 

The input will contain several test cases (not more than 10). 
Each test case is a single line with a number n, 0 <= n <= 1 000 000 000. 
It is the number given as input. 

Output 

For each test case output a single line, containing the integer returned. 

Example 

Input: 
12 
2 

Output: 
13 
2 

我的代碼是


#include <stdio.h> 

int functionReturningInteger(int n) 
{ 
// implementation 
........ 
return num; 
} 


int main(void) 
{ 

int number; 
//printf("Enter the number: "); 
scanf("%d",&number); 
printf(functionReturningInteger(number)); 
return 0; 

}


如何我應該知道,他們將有多少投入給(雖然他們確實提供了最大限制)。如果我使用數組來存儲這些大小等於最大限制的輸入,我該如何檢查c中整數數組的大小?


我會很感激任何人幫助一小段代碼。此外,如果我能夠對輸入測試文件進行測試並生成「output.txt」(輸出文件)。我已經有了所需的輸出文件「des.txt」。那我怎麼能匹配這兩個文件是否相同?

+0

你有一個錯誤在你的代碼'的printf( 「%d \ n」,functionReturningInteger(數字) );' – aisbaa 2012-07-18 07:33:33

+0

是的..其實我不得不定製代碼以避免發佈不必要的代碼。謝謝! – OneMoreError 2012-07-18 07:36:19

+0

@gcc賦值「不超過10」,因此您可以在這種特殊情況下安全地使用固定大小的數組。 – 2012-07-18 07:46:52

回答

0
#include <stdio.h> 
int scanned; 
while((scanned = scanf("%d", &number)) != EOF) { 
    printf("%d\n", functionReturningInteger(number)); 
} 

如果scanf檢測轉換成功之前輸入的結束,則返回EOF

對於其他問題,輸入輸出重定向,

$ ./your_prog <input.txt> output.txt 

,並比較

$ comp output.txt des.txt 
+0

或者,因爲OP在Windows上運行,所以'your_prog'而不是'。/ yourprog'。而'comp'而不是'diff'。 – 2012-07-18 07:42:11

+0

如果我希望用戶輸入5或6個數字(未知),然後在此之後生成輸出,該怎麼辦?那麼上述工作呢? – OneMoreError 2012-07-18 07:42:41

+0

@MrLister Aww,dang,錯過了標籤。 – 2012-07-18 07:43:21

0

您可以逐行閱讀,直到沒有任何內容從文件讀取。這樣你就不需要知道給出了多少輸入。

沒有默認的方法來跟蹤數組大小C.

要匹配文件,你可以在Linux \ Unix的操作系統使用diff

+0

我不應該寫一個文件打開並閱讀代碼!我需要像這樣編寫我的程序,然後在windows上使用命令prompmt或在Linux上使用終端命令來做同樣的事情。 – OneMoreError 2012-07-18 07:34:27

+0

stdin,stdout是類似於C中接口的文件。要從標準輸入中獲取數據,就像從文件中讀取數據;)。 – aisbaa 2012-07-18 07:37:20

相關問題