Ç - 扫描用户输入到一个数组进行排序 [英] C - scan user inputs into an array to be sorted

查看:111
本文介绍了Ç - 扫描用户输入到一个数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我,我是一个C编程菜鸟。我需要做的是从标准输入取值,并将其存储在一个阵列中,将在后面进行排序的路线。

Forgive me, I'm a C programming rookie. What I need to do is take values from standard input and store them in an array which is to be sorted later on down the line.

条目的用户的方法是在一次一个行一个数(即输入一个号码,preSS输入,输入号码,preSS输入,等等。)。当用户完成输入数字,它们preSS ENTER 无需提供一个数字。

The method of entry for the user is one number on one line at a time (i.e. enter a number, press enter, enter number, press enter, etc..). When the user is done entering numbers, they press ENTER without providing a number.

我的code接受的价值观并将其存储如下。你可能会立刻看到这个问题,但我没有看到它。

My code for accepting the values and storing them is as follows. You'll probably see the issue immediately, but I'm not seeing it.

#include <stdio.h>
#define MAX 100

int main()
{
    int n, i, array[MAX];

    printf("Enter a list of integers\n");

    for(i = 0; i <= MAX; ++i){
        printf("> ");
        if (scanf("%d", &n) == -1)
            break;
        else
            scanf("%d", &n);
            array[i] = n;
    }

    printf("The array is %d", *array);
    return 0;
}

下图为程序应该如何运行。我有那种code不已,它似乎工作得很好。你的帮助是很大的AP preciated。

The picture below is how the program should run. I have the sort code already, and it seems to work quite well. Your help is greatly appreciated.

推荐答案

下面是更新previous答案上输入exit。

Here is the updated previous answer to exit on enter.

#include <stdio.h>
#define MAX 100

int main()
{
    int n, i, array[MAX];
    char num[MAX];
    int res;

    printf("Enter a list of integers [ctrl+d] to end\n");

    for(i = 0; i <= MAX; ++i){
        printf("> ");
    fgets(num, sizeof(num), stdin);
    res = sscanf(num, "%d", &n);
    if(res != 1)
     break;
    n = atoi(num);
        array[i] = n;
    }
    puts ("");

    int z;
    for (z = 0; z < i; z++)
        printf("The array is %d\n", array[z]);

    return 0;
}

这篇关于Ç - 扫描用户输入到一个数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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