2012-07-18 68 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

相关问题