Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 按下enter键时停止获取用户输入(C语言)_Arrays_C_Scanf - Fatal编程技术网

Arrays 按下enter键时停止获取用户输入(C语言)

Arrays 按下enter键时停止获取用户输入(C语言),arrays,c,scanf,Arrays,C,Scanf,我对编程非常陌生,我遇到了一个问题,我应该写一个C程序,让用户输入一些数字,对输入的元素进行排序,并找到它的中间值。一旦用户按下enter键,它应该停止获取输入。 这是我的代码和idk出错的地方(顺便说一句,很抱歉问这么简单的问题) #包括 #包括 int main() { int n; int i=0; int j,k,m,num; int-arr[20]; while(i如果您想在空行上停止,您不能使用scanf。原因是scanf(“%d”,…)在等待用户输入数字时跳过用户输入的所有空白字符

我对编程非常陌生,我遇到了一个问题,我应该写一个C程序,让用户输入一些数字,对输入的元素进行排序,并找到它的中间值。一旦用户按下enter键,它应该停止获取输入。 这是我的代码和idk出错的地方(顺便说一句,很抱歉问这么简单的问题)

#包括
#包括
int main()
{
int n;
int i=0;
int j,k,m,num;
int-arr[20];

while(i如果您想在空行上停止,您不能使用
scanf
。原因是
scanf(“%d”,…)
在等待用户输入数字时跳过用户输入的所有空白字符。此处为“空白”包含新行字符
'\n'
。因此用户不能通过按Enter键返回
scanf
-只有文件结尾(linux上为Ctrl+D)或伪输入(非数字)才会返回
scanf

<>你必须用代码< fs> <代码> >代码> sSCANF替换<代码> SCAFF <代码> >
while (i < 20)
{
    int num;
    char str[15];

    printf("Enter a number: ");
    if (!fgets(str, sizeof str, stdin))
        break;
    if (sscanf(str, "%d", &num) != 1)
        break;
    arr[i] = num;
    i++;
}
while(i<20)
{
int-num;
char-str[15];
printf(“输入一个数字:”);
如果(!fgets(str,sizeof str,stdin))
打破
如果(sscanf(str,“%d”,&num)!=1)
打破
arr[i]=num;
i++;
}
它使用
fgets
输入一行输入,并使用
sscanf
解析该行。如果该行中没有任何内容,
sscanf
将失败(;任何其他失败值)

注:

  • 如果输入太长,它将被分成两行,这将让用户感到惊讶
  • 如果用户在该行中输入多个数字,多余的数字将被忽略
  • fgets
    的返回值的条件也会在文件末尾终止用户输入;如果您通过从文件重定向来向程序提供输入,这是很好的

可以逐个字符处理输入,直到存储二十个整数或找到换行符。
读取数字时,将其累积为一个值,检查是否溢出。
读取空格或其他字符后,将值存储在数组中

#include <stdio.h>
#include <ctype.h>
#include <limits.h>

int main ( void) {
    int ch = 0;
    int value = 0;
    int sign = 1;
    int digits = 0;
    int arr[20] = { 0};
    int n = 0;
    int i = 0;
    int j = 0;
    int k = 0;

    while ( n < 20) {
        while ( EOF != ( ch = fgetc ( stdin))) {//read a character
            if ( isdigit ( ( unsigned char)ch)) {
                ch -= '0';//character to int as '1' to 1
                digits = 1;
                if ( 1 == sign) {
                    if ( value < ( INT_MAX / 10) - ch) {
                        value *= 10;
                        value += ch;
                    }
                    else {
                        printf ( "overflow! reset to + and zero\n");
                        value = 0;
                        sign = 1;
                        digits = 0;
                    }
                }
                if ( -1 == sign) {
                    if ( value > ( INT_MIN / 10) + ch) {
                        value *= 10;
                        value -= ch;
                    }
                    else {
                        printf ( "overflow! reset to + and zero\n");
                        value = 0;
                        sign = 1;
                        digits = 0;
                    }
                }
            }
            else if ( '-' == ch || '+' == ch) {
                if ( digits) {
                    printf ( "invalid sign! reset to + and zero\n");
                    value = 0;
                    sign = 1;
                    digits = 0;
                }
                else {
                    sign = ( '-' == ch) ? -1 : 1;
                }
            }
            else if ( digits) {
                arr[n] = value;
                value = 0;//reset to zero
                sign = 1;//reset to +
                ++n;
                digits = 0;
            }
            if ( '\n' == ch) {
                break;
            }
        }
        if ( '\n' == ch || EOF == ch) {
            break;
        }
    }

    for ( i = 0; i < n; i++) {
        for ( j = i + 1; j < n; j++) {
            if ( arr[i] < arr[j]) {
                k = arr[i];
                arr[i] = arr[j];
                arr[j] = k;
            }
        }
    }

    for ( i = 0; i < n; i++) {
        printf ( "arr[%d] = %d\n", i, arr[i]);
    }

    if ( n % 2 != 0) {
        printf ( "The median is %d\n", arr[n/2]);
    }
    else {
        printf ( "The median is %.2f\n", ( arr[( n - 1) / 2] + arr[n / 2] ) /2.0);
    }

    return 0;
}

使用
scanf(“%d”,&num);
可以将
123
456
这样的字符串作为整数读取。但是,如果(num=='\n',则尝试将其作为字符使用中断;
。只有输入数字10,条件
num='\n'
才会为真。这是否回答了您的问题?通过使用
fgets()
输入字符串。空字符串(除换行符外)终止程序,否则使用
sscanf()
“else printf”处理它(“mediun是%.2f”,arr[(n-1)/2]+arr[n/2]/2.0);“在这一行,您可能希望在求和的周围使用括号
#include <stdio.h>
#include <ctype.h>
#include <limits.h>

int main ( void) {
    int ch = 0;
    int value = 0;
    int sign = 1;
    int digits = 0;
    int arr[20] = { 0};
    int n = 0;
    int i = 0;
    int j = 0;
    int k = 0;

    while ( n < 20) {
        while ( EOF != ( ch = fgetc ( stdin))) {//read a character
            if ( isdigit ( ( unsigned char)ch)) {
                ch -= '0';//character to int as '1' to 1
                digits = 1;
                if ( 1 == sign) {
                    if ( value < ( INT_MAX / 10) - ch) {
                        value *= 10;
                        value += ch;
                    }
                    else {
                        printf ( "overflow! reset to + and zero\n");
                        value = 0;
                        sign = 1;
                        digits = 0;
                    }
                }
                if ( -1 == sign) {
                    if ( value > ( INT_MIN / 10) + ch) {
                        value *= 10;
                        value -= ch;
                    }
                    else {
                        printf ( "overflow! reset to + and zero\n");
                        value = 0;
                        sign = 1;
                        digits = 0;
                    }
                }
            }
            else if ( '-' == ch || '+' == ch) {
                if ( digits) {
                    printf ( "invalid sign! reset to + and zero\n");
                    value = 0;
                    sign = 1;
                    digits = 0;
                }
                else {
                    sign = ( '-' == ch) ? -1 : 1;
                }
            }
            else if ( digits) {
                arr[n] = value;
                value = 0;//reset to zero
                sign = 1;//reset to +
                ++n;
                digits = 0;
            }
            if ( '\n' == ch) {
                break;
            }
        }
        if ( '\n' == ch || EOF == ch) {
            break;
        }
    }

    for ( i = 0; i < n; i++) {
        for ( j = i + 1; j < n; j++) {
            if ( arr[i] < arr[j]) {
                k = arr[i];
                arr[i] = arr[j];
                arr[j] = k;
            }
        }
    }

    for ( i = 0; i < n; i++) {
        printf ( "arr[%d] = %d\n", i, arr[i]);
    }

    if ( n % 2 != 0) {
        printf ( "The median is %d\n", arr[n/2]);
    }
    else {
        printf ( "The median is %.2f\n", ( arr[( n - 1) / 2] + arr[n / 2] ) /2.0);
    }

    return 0;
}
9 1 82   0    3
arr[0] = 82
arr[1] = 9
arr[2] = 3
arr[3] = 1
arr[4] = 0
The median is 3