如何根据用户输入的列数显示数字列表?(C)方案
我正在努力完成我的程序,在控制台中显示一系列令人高兴的数字。我只想显示前20行(取决于列值),然后程序将要求按enter键继续显示范围内的下20+个数字 这是如何根据用户输入的列数显示数字列表?(C)方案,c,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{
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;
}
我尝试了一些有效的方法,但是我对它们进行了注释,正如您在代码中看到的那样。我只是不知道如何继续完成它。请查看并给出一些想法,欢迎使用。它并不能真正回答您关于列的问题,但在终端上输出文本对于任何类型的科学工作都是非常有限的。相反,考虑输出到<代码> TSV 或<代码> CSV < /代码>,并用更复杂的工具绘制图形。
#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;
}
如果我们使用脚本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
,这可能比终端输出更漂亮
为什么不在控制台上按从右到左或从上到下的顺序列出数字?然后你可以把这些数字加载到eg graphviz中。@NeilEdelman怎么做?对不起,
gnuplot
。
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