我有一个程序可以将大量整数输出到标准输出,每个整数排成一行。例如:
103
104
105
107
我需要编写另一个程序来读取该数组并填充数字不是前一个数字增量 1 的空间。数字之间唯一的区别是 2 (105,107),这使它更容易。
这是我执行该逻辑的代码:
printf("d",num1);
if ((num2-num1) != 1)
numbetween = num1 + 1;
printf("%d", numbetween);
printf("%d", num2);
else(
printf("%d",num2);
)
所以这个程序的输出现在是:
103
104
105
106
107
我的问题是阅读数字。我知道我可以执行 while (scanf("%hd", &num) != EOF)
一次读取所有行。但是要执行我想要的逻辑,我将需要一次读取两行并对它们进行计算,但我不知道该怎么做。
最佳答案
您始终可以只从文件中读取第一个和最后一个数字,然后打印其间的所有内容。
int main( void )
{
// get the first value in the file
int start;
if ( scanf( "%d", &start ) != 1 )
exit( 1 );
// get the last value in the file
int end = start;
while ( scanf( "%d", &end ) == 1 )
;
// print the list of numbers
for ( int i = start; i <= end; i++ )
printf( "%d\n", i );
}
关于c - 从标准输出一次扫描两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812778/