【问题标题】:Is there a way to end the code without a new line after a repeat commnad that contains a new line inside it?在包含新行的重复命令之后,有没有办法在没有新行的情况下结束代码?
【发布时间】:2021-02-13 06:15:15
【问题描述】:

所以我写了一个代码来计算两个矩阵的乘法。 该程序通过产生正确的结果来完成其工作。我在下面提供了大部分输出功能:

   for (i = 0; i < row; ++i) 
    {
      for (j = 0; j < column; ++j) 
        {
            printf("%d ", result[i][j]);
            if (j == column -1)
            printf("\n"); 
        }
    }

最后两行有助于正确表达结果,例如:

xxx xx
x -xx
x x

其中 x 代表整数。 如果没有新行,我将如何使程序结束?例如:

xxx xx
x -xx
x xPress any key to continue...

而不是我现在得到的:

xxx xx
x -xx
x x
Press any key to continue...

提前致谢。

【问题讨论】:

  • if (i != row -1 &amp;&amp; j == column -1)?

标签: c matrix newline matrix-multiplication end-of-line


【解决方案1】:

要在没有换行符的情况下结束,请在末尾停止打印换行符。

   for (i = 0; i < row; ++i) 
    {
      for (j = 0; j < column; ++j) 
        {
            printf("%d ", result[i][j]);
            if (i != row -1 && j == column -1)
            printf("\n"); 
        }
    }

要结束也没有空格,请在最后停止打印空格。

   for (i = 0; i < row; ++i) 
    {
      for (j = 0; j < column; ++j) 
        {
            printf(i == row -1 && j == column -1 ? "%d" : "%d ", result[i][j]);
            if (i != row -1 && j == column -1)
            printf("\n"); 
        }
    }

【讨论】:

  • 它仍然添加了一个空格。 (x Press..) 我需要 (xPress..)
  • @ΜάνοςΧαμηλάκης 你只写了新行,但我应该从例子中读到空间。已更新。
【解决方案2】:

如果您想在没有结尾 \n 的情况下结束输出,则不要在每行的末尾打印它。您可以在开头打印它(嗯,这将使您的输出始终在开头有一个空行)或者更好,行之间。例如:

char *sep = "";
while(whatever_you_like_to_test_to_continue_the_test) {

    /* the separator has been initialized to the empty string, so it will
     * print nothing the first time. */
    printf("%s", sep);

    /* now we change it, to whatever we like to use */
    sep = "\n";

    /* print the body of what you want to print */
    printf("....", ...);
}
/* when you reach here, the last part you wrote was the body, not the separator */

例如,在您的示例代码中,您可以用每行的一对括号、一个返回线来装饰矩阵的元素,并将所有内容放在另一对括号中,如下所示:

#include <stdio.h>
int main() {
        int rows = 10, i, cols = 6, j, number = 0;

        char *sep1 = "{";
        for (i = 0; i < rows; ++i) {
                printf("%s", sep1);
                sep1 = ",\n ";
                char *sep2 = "{";
                for (j = 0; j < cols; ++j) {
                        printf("%s", sep2);
                        sep2 = ",";
                        printf("%3d", number++);
                }
                /* this ends the row */
                printf("}");
        }
        printf("}"); /* the last bracket, and no newline */
} /* main */

将产生:

$ ./a.out
{{  0,  1,  2,  3,  4,  5},
 {  6,  7,  8,  9, 10, 11},
 { 12, 13, 14, 15, 16, 17},
 { 18, 19, 20, 21, 22, 23},
 { 24, 25, 26, 27, 28, 29},
 { 30, 31, 32, 33, 34, 35},
 { 36, 37, 38, 39, 40, 41},
 { 42, 43, 44, 45, 46, 47},
 { 48, 49, 50, 51, 52, 53},
 { 54, 55, 56, 57, 58, 59}}$ _

【讨论】:

    【解决方案3】:
    for (i = 0; i < row; ++i) 
    {
        if (i != 0)
            printf("\n");
        for (j = 0; j < column; ++j) 
        {
            if (j != 0)
                printf(" ");
            printf("%d", result[i][j]);
        }
    }
    

    在循环之外做一些事情,之前而不是之后。

    【讨论】:

      【解决方案4】:

      答案很简单,关键是以测试机器可接受的方式呈现输出,这意味着在每条新行结束后必须没有空格才能通过测试。我对理解其背后的逻辑有些麻烦,但最终经过一番搜索,上面的一些帮助和一天的反复试验,我想通了。

      for (i = 0; i < row; ++i)
          {
              for (j = 0; j < column; ++j)
              {
                  printf(j == column -1  ? "%d" : "%d ", result[i][j]);
                  if (i != row -1 && j == column -1)
                      printf("\n");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2014-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-24
        • 2022-06-10
        • 2023-04-09
        • 2016-10-05
        • 1970-01-01
        相关资源
        最近更新 更多