简体   繁体   English

如何在C语言的控制台上打印叉号?

[英]How do I print a cross to the console in C?

I've just started studying information technologies and I am currently stuck on a programming assignment. 我刚刚开始研究信息技术,目前正从事编程工作。

I have to write a code in C which displays a cross to the console, the size of the cross being determined by an initial input. 我必须用C编写一个代码,该代码在控制台上显示一个叉号,叉号由初始输入确定。

So the console output should look like this: 因此,控制台输出应如下所示:

size?: 5(user input)

xooox

oxoxo

ooxoo

oxoxo

xooox

(replace the os with blank space) (用空格替换操作系统)

I've now come as far as this: 我现在谈到的是:

#include <stdio.h>

int main(void)
{
    int n;

    printf("size?: ");
    scanf("%d",&n);

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if( (i==j) )
                printf("*");
            else 
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}  

But this only displays one diagonal of the cross, I'm thinking that the opposite diagonal can be created by another condition after the if however I am lost as to what that condition might be. 但是,这仅显示了十字架的一个对角线,我想如果在某个条件之后我不知所措之后,可以通过另一种条件来创建对角线。

You're definitely on the right track! 您绝对是在正确的轨道上! Don't give up. 不要放弃

The way to think about this is to think about the loop counters. 考虑这一点的方法是考虑循环计数器。 You've figured out one half of it. 您已经解决了一半。 If the row and column are the same, you need to output a * . 如果行和列相同,则需要输出* So what's the other condition? 那还有什么其他条件呢? Well, think about counting backwards. 好吧,请考虑倒数。 If the row is the same as the column counted backwards, we also want a * . 如果该行与向后计数的列相同,则我们还需要*

I don't want to do your homework for you, so I'll hold off on writing the code, but hopefully that gives you a hint as to what you need to do. 我不想为您做家庭作业,因此我将暂不编写代码,但希望可以为您提供有关您需要做什么的提示。

Should you need some help, you have to check the column from the other side as well: 如果需要帮助,还必须从另一侧检查该列:

#include <stdio.h>
int n;

int main(void) {
    printf("size?: ");
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if (i == j || i == n - j + 1) printf("*");
            else printf(" ");
        }
        printf("\n");
    }       
    return 0;
}  

Edit: There are suggestions to do i + j == n + 1 , but I believe i == n - j + 1 makes more sense, since: 编辑:有建议做i + j == n + 1 ,但我相信i == n - j + 1更有意义,因为:

  • i is your current row i是你当前的行
  • j is your current column j是您当前的列
  • n is the size of your square (max row / max column) n是正方形的大小(最大行/最大列)
  • i == n - j + 1 means draw * at max - current + 1 column i == n - j + 1表示max - current + 1 * max - current + 1 * max - current + 1

Replace if( (i==j) ) with if( (i==j)||(i+j)==n+1 ) , That is,: if( (i==j) )替换为if( (i==j)||(i+j)==n+1 ) ,即:

#include <stdio.h>

int main(void)
{
  int n;

  printf("size?: ");
  scanf("%d",&n);

  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<=n;j++)
     {
      if( (i==j)||(i+j)==n+1 )
       printf("*");
      else 
       printf(" ");
     }
     printf("\n");
  }



   return 0;
 } 

First thing's first, C compilers don't like 首先是C编译器不喜欢

for (int i = 0; i < n; i++)

They like 他们喜欢

int i = 0;
for (i = 0; i < n; i++)

But if you're using a C++ compiler, you probably won't get that problem. 但是,如果您使用的是C ++编译器,则可能不会遇到该问题。

Now; 现在; back to solving the problem at hand! 回到解决眼前的问题!

On the line: 在线上:

if( (i==j) )

With this conditional, you're plotting points at (1, 1), (2, 2), (3, 3) ... 在此条件下,您将在(1、1),(2、2),(3、3)...处绘制点

You want to also plot points at (1, n), (2, n - 1), (3, n - 2) ... 您还希望在(1,n),(2,n-1),(3,n-2)...处绘制点

So you need to add a second conditional to this if statement: 因此,您需要在此if语句中添加第二个条件:

if ( (i==j) || (i == (n - j) + 1 ) )

Then you can simplify this up a bit if you want... 然后,您可以根据需要简化此操作...

if ( (i==j) || (i == n - j + 1 ) )

And there you go! 然后你走了! It now prints a cross, like you described in your question. 现在,它会打印一个叉号,就像您在问题中所描述的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM