如何阅读数组为int的随机数 [英] how to read random number of int in array

查看:115
本文介绍了如何阅读数组为int的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读空格分开的整数到一个数组,当我preSS输入它应该停止在任何时间点看,如何实现循环对这一计划的请帮我解决这个问题。
我曾尝试以下code,但它简化版,work.and还怎么回来读。

i want to read space separated integer into an array and when i press enter it should stop reading at any point of time, how to implement loop for this program please help me to solve this. i have tried below code but it does't work.and also how to read back again.

#include<stdio.h>
int main()
{
    int arr[30];
    int j=0;
    while(1)
    {
        int d;
        scanf("%d",&d);
            arr[j++]=d;
        if(d=='\n')break;
    }
   return 0;
}

在此先感谢

推荐答案

您的问题是, scanf函数将所有空白(空格,制表符,换行符)会自动跳过,因为它查找下一个项目。你可以把它换行和其他空白区分由专门询问了换行改为:

Your problem is that scanf will automatically skip over all whitespace (spaces, tabs, newlines) as it looks for the next item. You can make it distinguish between newlines and other whitespace by specifically asking to read in a newline:

int main() {
    int arr[30]; // results array
    int cnt = 0; // number of results
    while (1) {
        // in the scanf format string below
        // %1[\n] asks for a 1-character string containing a newline
        char tmp[2]; // buffer for the newline
        int res = scanf("%d%1[\n]", &arr[cnt], tmp);
        if (res == 0) {
            // did not even get the integer
            // handle input error here
            break;
        }
        if (res == 1) {
            // got the integer, but no newline
            // go on to read the next integer
            ++cnt;
        }
        if (res == 2) {
            // got both the integer and newline
            // all done, drop out
            ++cnt;
            break;
        }
    }
    printf("got %d integers\n", cnt);
    return 0;
}

使用这种方法的问题是,它只能识别以下的整数换行符和将静默跳过只包含空白的线(并开始从下一行读取整数)。如果这是不能接受的,那么我认为最简单的方法是阅读整条生产线到缓冲区,并从该缓冲区解析整数:

The problem with this approach is that it only recognizes a newline following an integer and will silently skip a line that contains only whitespace (and start reading integers from the next line). If that is not acceptable, then I think the easiest solution is to read the whole line into a buffer and parse the integers from that buffer:

int main() {
    int arr[30]; // results array
    int cnt = 0; // number of results
    char buf[1000]; // buffer for the whole line
    if (fgets(buf, sizeof(buf), stdin) == NULL) {
        // handle input error here
    } else {
        int pos = 0; // current position in buffer
        // in the scanf format string below
        // %n asks for number of characters used by sscanf
        int num;
        while (sscanf(buf + pos, "%d%n", &arr[cnt], &num) == 1) {
            pos += num; // advance position in buffer
            cnt += 1; // advance position in results
        }
        // check here that all of the buffer has been used
        // that is, that there was nothing else but integers on the line
    }
    printf("got %d integers\n", cnt);
    return 0;
}

还要注意,上述解决方案会覆盖结果阵列时有上线超过30的整数。第二溶液还会留下一些输入线未读的,如果它是比装配到缓冲器更长。根据您的输入是从哪里来的,这两个可能是需要的是实际使用的code之前需要解决的问题。

Also note that both of the above solutions will overwrite the results array when there are more than 30 integers on the line. The second solution will also leave some of the input line unread if it is longer than what fits into the buffer. Depending on where your input is coming from, both of these may be problems that need to be fixed before the code is actually used.

这篇关于如何阅读数组为int的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆