首页 > 解决方案 > 如何根据用户输入的列数显示数字列表?(C程序)

问题描述

我正在尝试完成我的程序,在控制台中显示一系列快乐的数字。我只想显示前 20 行(取决于列值),然后程序将要求按 Enter 以继续显示该范围内的下 20 多个数字。

这是main代码:

int happynum(int num);
#define MAXCOLUM 4

int main()
{
int num, liminf, limsup, cont=0, tab=20, col, hnum;

   do{
        //DIGITANDO EL RANGO
      do{
        system("cls");
         printf("Ingrese l%cmite inferior: ",161);
         scanf("%d",&liminf);


         if ( liminf <= 0 )
         {
            printf("El valor debe ser mayor que cero.\n");
            system("pause");
         }

      }while ( liminf <= 0 );

      do{
            system("cls");
         printf("Ingrese l%cmite superior: ",161);
         scanf("%5d",&limsup);

         if ( limsup <= 0 )
         {
            printf("El valor debe ser mayor que cero.\n");
            system("pause");
         }

      }while ( limsup <= 0 );

      // VALIDANDO EL RANGO

      if ( liminf >= limsup )
      {
         printf("Rango incorrecto. Por favor ingr%cselo de nuevo.\n",130);
         getch();
      }

   }while ( liminf >= limsup);

     do{
            system("cls");
            printf("Enter the # of columns to show the happy numbers (max 4): \n");
            scanf("%d", &col);

            if (col > 4 || col < 0)
            {
                printf("Must be 4 columns max. \n");
            }
        }while (col > 4 || col < 0);

    system("cls");
    printf("happy numbers between range [%d,%d]: \n",liminf,limsup);

    for (num = liminf; num<=limsup; num++)
    {
        hnum = happynum(num);

       if (hnum== 1)
        {
        printf("%d F\n", num);
        cont++;
        }
            else {
                printf("%d \n", num);
                cont++;
            }
       /*if (cont < col)
       {
           printf("\t");
           cont++;
       } else {
        printf("\n");
        cont=1;
       }*/




                // Validating that shows the first 20 lines
                   /*  if (cont >= tab)
                    {

                    tab+=20;
                    }
                    */
    }

    return 0;
}

例如,如果用户想要 2 列中的快乐数字列表,输出将是这样的:

happy numbers between range [1,50]:

1 F  21
2    22
3    23 F
4    24
5    25 
6    26
7 F  27
8    28 F
9    29
10 F 30
11   31 F
12   32 F
13 F 33
14   34
15   35
16   36
17   37
18   38
19 F 39
20   40

Press enter to keep showing numbers...

happy numbers between range [1,50]:

41
42
43
44 F
45
46
47
48
49 F
50

我尝试了一些可行的方法,但正如您在代码中看到的那样,我对它们进行了评论。我只是不知道如何继续完成它。请检查一下并提出一些想法,欢迎他们。

标签: c

解决方案


它并没有真正回答您关于列的问题,但在终端上输出文本对于任何类型的科学工作来说都是非常有限的。相反,请考虑输出到tsvorcsv并使用更复杂的工具绘制图形。

#include <stdlib.h> /* EXIT bsearch strtol */
#include <stdio.h> /* [f]printf */
#include <limits.h> /* INT_MAX */
#include <errno.h> /* errno */

/* This seems hard, and it's not part of your question, so I've dowloaded
 a static list from <https://oeis.org/A007770/list>. */
static const unsigned happy10[] = {
    1,7,10,13,19,23,28,31,32,44,49,68,70,79,82,86,91,
    94,97,100,103,109,129,130,133,139,167,176,188,190,
    192,193,203,208,219,226,230,236,239,262,263,280,
    291,293,301,302,310,313,319,320,326,329,331,338
};
static const size_t happy10_size = sizeof happy10 / sizeof *happy10;

static int compare(const int *const a, const int *const b) {
    return (*a > *b) - (*b > *a);
}

static int compar(const void *a, const void *b) {
    return compare(a, b);
}

static int happy(unsigned num) {
    return bsearch(&num, happy10, happy10_size, sizeof *happy10, &compar)
        ? 1 : 0;
}

int main(int argc, char **argv) {
    char *end;
    unsigned liminf, limsup, num;
    long l;
    int success = EXIT_FAILURE;

    /* Get command line parameters. */
    if(argc != 3) goto catch;
    if((!(l = strtol(argv[1], &end, 0)) && errno) || *end != '\0') goto catch;
    if(l <= 0 || l > 338 || l > INT_MAX) { errno = ERANGE; goto catch; }
    liminf = l;
    if((!(l = strtol(argv[2], &end, 0)) && errno) || *end != '\0') goto catch;
    if(l <= liminf || l > 338 || l > INT_MAX) { errno = ERANGE; goto catch; }
    limsup = l;

    /* Output all 10-happy numbers in the range. */
    printf("# 10-happy numbers in the range [%u, %u]\n", liminf, limsup);
    for(num = liminf; num <= limsup; num++)
        if(happy(num) == 1) printf("%d\n", num);

    success = EXIT_SUCCESS;
    goto finally;

catch:
    if(errno) perror("happy");
    else fprintf(stderr, "usage: <inferior> <superior>\n");

finally:
    return success;
}

运行这个:

# 10-happy numbers in the range [1, 10]
1
7
10

如果我们使用gnuplot脚本happy.gnu

set term postscript eps enhanced
set output "happy.eps"
set grid
set boxwidth 0.5
set style fill solid
set style line 1 lc rgb "#0060ad"
set xlabel "n"
set ylabel "F_{2,10}(n)"
set autoscale xfixmin
set autoscale xfixmax
plot "happy.data" using 1 notitle with boxes ls 1;

然后做,

./a.out 1 100 > happy.data
gnuplot happy.gnu

它给出了happy.eps,这可能比终端输出更漂亮。

F_{2,10} 代表 [1, 100]


推荐阅读