如何在不同坐标下打印出不同的字符串?

问题描述 投票:0回答:2

我必须制作一个表格,但我不想对其进行硬编码。

COORD point = { 30, 10 };
SetConsoleCursorPosition(hConsole, point);
printf("%s", a);
COORD point1 = { 30,12 };
SetConsoleCursorPosition(hConsole, point1);
printf("%s", b);
COORD point2 = { 30,14 };
SetConsoleCursorPosition(hConsole, point2);
printf("%s", c);
COORD point3 = { 30,16 };
SetConsoleCursorPosition(hConsole, point3);
printf("%s", d);
COORD point4 = { 30,18 };
SetConsoleCursorPosition(hConsole, point4);
printf("%s", e);
COORD point5 = { 50,10 };
SetConsoleCursorPosition(hConsole, point5);
printf("%s", f);
COORD point6 = { 50,12 };
SetConsoleCursorPosition(hConsole, point6);
printf("%s", g);
COORD point7 = { 50,14 };
SetConsoleCursorPosition(hConsole, point7);
printf("%s", h);
COORD point8 = { 50,16 };
SetConsoleCursorPosition(hConsole, point8);
printf("%s", k);
COORD point9 = { 50,18 };
SetConsoleCursorPosition(hConsole, point9);
printf("%d", l);

基本上我有这个,我很感兴趣是否可以通过 for 循环或类似的东西来实现它

c windows terminal console
2个回答
2
投票

将许多变量命名为

point1
point9
通常不是一个好主意,因为这不允许您在循环中使用它们。但是,如果您将这些变量放在一个数组中,那么您可以在循环中使用它们。

这同样适用于名为

a
l
的变量。

如果您使用数组和循环,您的代码可能会是这样的:

// The following array should contain the values of
// the variables "a" to "l"
char *strings[10];
// TODO: initialize the elements in the strings array
//       to the appropriate values

COORD points[10];

for ( int i = 0; i < 2; i++ )
{
    for ( int j = 0; j < 5; j++ )
    {
        points[i*5+j].X = 30 + i * 20;
        points[i*5+j].Y = 10 + j * 2;
        SetConsoleCursorPosition( hConsole, points[i*5+j] );
        printf( "%s", strings[i*5+j] );
    }
}

但是,在将坐标传递给

SetConsoleCursurPosition
后,您可能不需要记住它们。在这种情况下,您不需要坐标数组,因此代码可以简化为以下内容:

// The following array should contain the values of
// the variables "a" to "l"
char *strings[10];
// TODO: initialize the elements in the strings array
//       to the appropriate values

for ( int i = 0; i < 2; i++ )
{
    for ( int j = 0; j < 5; j++ )
    {
        COORD point;

        point.X = 30 + i * 20;
        point.Y = 10 + j * 2;
        SetConsoleCursorPosition( hConsole, point );
        printf( "%s", strings[i*5+j] );
    }
}

上面的代码假设您使用的

COORD
结构是来自 Windows API 的 this one

在评论部分,您指出变量

a
l
并不都具有相同的类型。您指出其中一个变量具有不同的类型。在这种情况下,情况会更复杂,最好在循环外处理不同类型的变量,或者在循环之前将该变量转换为字符串,以便可以对它们进行相同的处理方式。


1
投票

这有点棘手,因为并非每个元素都适合循环,但根据下面的代码,这是可行的。 棘手,因为在打印过程中,X 坐标从 30 跳到 50。我们在代码中考虑到了这一点,但它确实会破坏理想循环的线性。

此外,要打印的字符串被降级为二维数组。这 真的很好地帮助了循环的线性度,我实际上已经经常使用它了。所以,这不是“一次性”代码,但可以在很长一段时间内重复使用和实用。 这段代码是我今天编译和测试的。特色的

Output

是此代码刚才运行的控制台窗口的实际屏幕截图。

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <stdio.h>

HANDLE hConsole = NULL;

char *mssg[] = {
    "The 1st mssg",
    "The 2nd mssg",
    "The 3rd mssg",
    "The 4th mssg",
    "The 5th mssg",
    "The 6th mssg",
    "The 7th mssg",
    "The 8th mssg",
    "The 9th mssg",
    "The 10th mssg",
};


int main() 
{
    int iii;
    COORD point;
    BOOL updated = FALSE;

    /* Removed needless check for hConsole==NULL per comment 
    * from of Andreas Wenzel */
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    if(hConsole == INVALID_HANDLE_VALUE)
    {
        printf("STDOUT not available");
        return 0;
    }
    point.X = 30;
    point.Y = 10;
    for(iii=0; iii<10; iii++)
    {
        SetConsoleCursorPosition(hConsole, point);
        printf("%s", mssg[iii]);
        point.Y += 2;
        if( iii >= 4 && !updated )
        {
            point.X = 50;
            point.Y = 10;
            updated = TRUE;
        }
    }
    return 0;
}

输出:

The 1st mssg The 6th mssg The 2nd mssg The 7th mssg The 3rd mssg The 8th mssg The 4th mssg The 9th mssg The 5th mssg The 10th mssg

© www.soinside.com 2019 - 2024. All rights reserved.