繁体   English   中英

如何在C中读取2行整数输入?

[英]How to read 2 lines of integer input in C?

我正在为我的算法课程做一个项目,输入时遇到很多麻烦。 我正在尝试读取这样的输入:

6 0 2 3 1 3
5 9 2 1 3

整数将需要转到

int num1; // num1 = 6
int num2; // num2 = 5
int array1[100]; // array1 = {0, 2, 3, 1, 3, 0, 0, ...}
int array2[100]; // array2 = {9, 2, 1, 3, 0, 0, ...}

输入来自文件格式的标准输入。 因此,在终端中运行程序将如下所示:

cat input.txt | ./a.out

其中input.txt包含两行整数。

到目前为止,这是我的错误尝试:

while(scanf("%d%c", &temp, &ch) > 1){
    if (ch != '\n'){
        one[count] = temp;
    } 
    else if (ch == '\n'){
        count = 0;
        two[count] = temp;

    }
    one[count] = temp;
    count++;
    if (ch != ' ')
    {
        printf("Invalid input. Please do int + space.\n");
        return -1;
    }
    if ((temp >= 100) || (temp <= -100))
    {
        printf("Input is too big, must be between -100, 100.\n");
        return -1;
    }
    if (one[0] < 1){
        printf("Input for n cannot be smaller than one!");
        return -1;
    }
}

我认为主要的问题是我不确定如何处理多行输入。 一行输入对我来说很好,但多行是让我绊倒的原因。

您可以使用getline函数获取整个输入行,然后遍历该行,并使用strtol函数一次扫描一个数字。

从问题的示例中,我假设您希望两个数组中的所有剩余条目均为零,因此不要忘记将它们归零(手动或使用memset函数)。

并且也不要忘记free()缓冲区getline给您。

看下面的代码。 也许对您有帮助。

通常,如果您知道要输入多少个数字,则可以使用scanf("%d", ...)来逐一读取数字scanf("%d", ...)并在达到预期数量时使用fflush()清除缓冲区中的其他任何数字。 此示例假定前两个数字是每行的相应长度。 输入看起来像:

// example input:
// 4
// 3
// 1 2 3 4
// 5 6 7
    int main()
    {
        int it;
        int it1 = 0;
        int it2 = 0;
        int line1[100];
        int line2[100];

        scanf("%d", &it1); // amount of line 1 numbers
        scanf("%d", &it2); // amount of line 2 numbers

        it = 0;
        do
        {
            scanf("%d", &line1[it]);
        } while (++it < it1);

        fflush(stdin); // clear input buffer

        it = 0;
        do
        {
            scanf("%d", &line2[it]);
        } while (++it < it2);

        return 0;
    }

实际上,我最终使用了scanf,这是下面的工作代码。 阅读这些评论并参考K&R确实有帮助

#include <stdio.h>
#include <string.h>

#define ARRAY_SIZE  100

void shiftArrayBackByOne(int a[]){
    for(int i = 1; i <= ARRAY_SIZE; i++){
        a[i - 1] = a[i];
    }
}

void printArray(int a[], int n){
    for(int i = 0; i < n; i++){
        printf("%d ", a[i]);
    }
    putchar('\n');
}

int main(){
    int isLineTwo = 0;
    int countOne = 0;
    int countTwo = 0;
    int inputNum;
    int num1;
    int num2;
    int array1[ARRAY_SIZE];
    int array2[ARRAY_SIZE];

    char ch;

    while(scanf("%d%c", &inputNum, &ch) > 0){

        //Puts the input into different arrays depeding
        //on value of isLineTwo
        if (isLineTwo){
            array2[countOne] = inputNum;
            countOne++;
        } else {
            array1[countTwo] = inputNum;
            countTwo++;
        }

        //Increment isLineTwo if ch is a 'newline'
        if (ch == '\n')
        {
            isLineTwo++;
        }

        //Check if user inputs more than 2 lines
        if (isLineTwo > 1){
            printf("Hey, no more than 2 input lines!\n");
        }

    }
    printArray(array1, countOne);
    printArray(array2, countTwo);

    num1 = array1[0];
    num2 = array2[0];

    shiftArrayBackByOne(array1);
    shiftArrayBackByOne(array2);

    printf("num1 = %d\n", num1);
    printf("num2 = %d\n", num2);

    printArray(array1, countOne);
    printArray(array2, countTwo);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM