在C中绘制Unicode框:嵌套为循环的2个内部框,在击中测试条件时不会退出

发布于 2025-01-25 11:18:08 字数 2723 浏览 3 评论 0原文

我正在尝试从Unicode Chars绘制一个盒子,问题是,在达到26次迭代的条件下,内部循环似乎没有退出,这使输出拼凑成一条线并分为一半。

我尝试用2替换循环计数器的初始值(因为它似乎是由于0%2 == 0导致第一个决策条件被跳过,但无济于事。)

我也尝试过替换带有0并添加一些printf测试的空格,但似乎主要问题是我无法将此程序分解为模块,我不知道该如何执行它,给出了功能可以做的唯一基本动作就是打印一个character depending on the condition.

Should I try to generalize the edge cases and pass loop counters and the code of printable chars to a function, testing them on each iteration?

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

wchar_t edgeLU = 0x250c;
wchar_t vert = 0x2502;
wchar_t leftMid = 0x251c;
wchar_t edgeLB = 0x2514;
wchar_t botMid = 0x2534;
wchar_t horiz = 0x2500;
wchar_t edgeRB = 0x2518;
wchar_t rightMid = 0x2524;
wchar_t edgeRU = 0x2510;
wchar_t upMid = 0x252c;
wchar_t cross = 0x253c;

int i, j;

int main() {
  setlocale(LC_CTYPE, "");
  for (i = 0; i < 27; i++) {
    for (j = 0; j < 27; j++) {
        if (i == 0) {
            if (j == 0) {
                wprintf(L"%lc", edgeLU);
            }
            else if (j%2 == 1) {
                wprintf(L"%lc", horiz);
            }
            else if (j%2 == 0 && !(j == 0 || j == 26)) {
                wprintf(L"%lc", upMid);
            }
            else {
                wprintf(L"%lc", edgeRU);
            }
        }
        else if (i%2 == 1) {
            if (j%2 == 0) {
                wprintf(L"%lc", vert);
            }
            else {
                printf(" ");
            }
        }
        else if (i%2 == 0 && !(i == 0 || i == 26)) { 
            if (j == 0) {
                wprintf(L"%lc", leftMid);
            }
            else if (j%2 == 1) {
                wprintf(L"%lc", horiz);
            }
            else if (j%2 == 0 && !(j == 0 || j == 26)) {
                wprintf(L"%lc", cross);
            }
            else {
                wprintf(L"%lc", rightMid);
            }
        }
        else if (i == 26) {
            if (j == 0) {
               wprintf(L"%lc", edgeLB);
            }
            else if (j%2 == 1) {
                wprintf(L"%lc", horiz);
            }
            else if (j%2 == 0 && !(j == 0 || j == 26)) {
                wprintf(L"%lc", botMid);
            }
            else {
                wprintf(L"%lc", edgeRB);
            }
        }
    }
    printf("\n");
  }
  return 0;
}

To avoid confusion, ┤ ┴ ├ ┬ are denoted as rightMid, botMid, leftMid and upMid respectively.

这就是输出,该程序首先从(i%2 == 1)条件和新线打印出空间,以某种偶然的方式终止内部循环后。

I'm trying to draw a box from Unicode chars and the problem is that the inner for loop doesn't seem to exit after hitting the condition of reaching 26 iterations, which leaves the output scrambled in one line and split in half.

I've tried replacing the initial values of loop counters with 2 (since it appears as if the first decision condition is skipped due to 0%2 == 0 producing an error, but to no avail.)

I've also tried replacing the spaces with 0 and add some printf tests, but it seems like the main issue is with my inability to decompose this program into modules and I have no idea how it can be performed, giving that the only basic action the function can do is print a character depending on the condition.

Should I try to generalize the edge cases and pass loop counters and the code of printable chars to a function, testing them on each iteration?

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

wchar_t edgeLU = 0x250c;
wchar_t vert = 0x2502;
wchar_t leftMid = 0x251c;
wchar_t edgeLB = 0x2514;
wchar_t botMid = 0x2534;
wchar_t horiz = 0x2500;
wchar_t edgeRB = 0x2518;
wchar_t rightMid = 0x2524;
wchar_t edgeRU = 0x2510;
wchar_t upMid = 0x252c;
wchar_t cross = 0x253c;

int i, j;

int main() {
  setlocale(LC_CTYPE, "");
  for (i = 0; i < 27; i++) {
    for (j = 0; j < 27; j++) {
        if (i == 0) {
            if (j == 0) {
                wprintf(L"%lc", edgeLU);
            }
            else if (j%2 == 1) {
                wprintf(L"%lc", horiz);
            }
            else if (j%2 == 0 && !(j == 0 || j == 26)) {
                wprintf(L"%lc", upMid);
            }
            else {
                wprintf(L"%lc", edgeRU);
            }
        }
        else if (i%2 == 1) {
            if (j%2 == 0) {
                wprintf(L"%lc", vert);
            }
            else {
                printf(" ");
            }
        }
        else if (i%2 == 0 && !(i == 0 || i == 26)) { 
            if (j == 0) {
                wprintf(L"%lc", leftMid);
            }
            else if (j%2 == 1) {
                wprintf(L"%lc", horiz);
            }
            else if (j%2 == 0 && !(j == 0 || j == 26)) {
                wprintf(L"%lc", cross);
            }
            else {
                wprintf(L"%lc", rightMid);
            }
        }
        else if (i == 26) {
            if (j == 0) {
               wprintf(L"%lc", edgeLB);
            }
            else if (j%2 == 1) {
                wprintf(L"%lc", horiz);
            }
            else if (j%2 == 0 && !(j == 0 || j == 26)) {
                wprintf(L"%lc", botMid);
            }
            else {
                wprintf(L"%lc", edgeRB);
            }
        }
    }
    printf("\n");
  }
  return 0;
}

To avoid confusion, ┤ ┴ ├ ┬ are denoted as rightMid, botMid, leftMid and upMid respectively.

That is the output, the program first prints spaces from the (i%2 == 1) condition and newlines, following termination of the inner loop in some haphazard fashion.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬心 2025-02-01 11:18:08

在不确定的行为中,将宽和狭窄的字符输出与同一文件混合在一起。用相应的wprintf替换printf调用。

In is undefined behaviour to mix wide and narrow character output to the same file. Replace printf calls with corresponding wprintf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文