简体   繁体   English

C-尝试在循环中添加比上一行更多的空间

[英]C - Attempting to add one more space than the previous line in a loop

I want to add an additional space to the beginning of each new line. 我想在每个新行的开头添加一个额外的空间。 the current output is: 当前输出为:

$****
 $***
  $**
   $*
    $
     Press any key to continue . . .

What I want is: 我想要的是:

$****
 $***
  $**
   $*
    $
Press any key to continue . . .

I added the for-loop! 我添加了for循环! it looks almost perfect except the "press any key to continue..." message is tabbed. 它看起来几乎是完美的,除了“按任意键继续...”消息被标记。 How can this be fixed? 如何解决?

Code: 码:

for(r=1; r<=5; r++) {
  printf("$");
  for(c=1; c<=5;c++) {
    if(6-r<=c) {
      printf(" ");
    } else {
      printf("*");
    }
  }
  printf("\n");
  n++;
  for (f=0;n>f;f++)
    {
        printf(" ");
    }
}

In its simplest form: just print spaces, then the '$', then the asterisks. 最简单的形式:先打印空格,然后是“ $”,然后是星号。

int r, c;
for(r=0; r<5; r++) {
  for(c=0; c<r; c++){
    printf(" ");
  }
  printf("$");
  for(c=r+1; c<5;c++) {
      printf("*");
  }
  printf("\n");
  n++;//What is this here for?
}

You can pare that down with a great printf trick (already submitted by someone else, so I won't go into it here: 您可以使用很棒的printf技巧来减少它(已经由其他人提交了,所以我在这里不再赘述:

int r, c;
for(r=0; r<5; r++) {
  printf("%*s", r+1, "$");
  for(c=r+1; c<5;c++) {
      printf("*");
  }
  printf("\n");
  n++;//What is this here for?
}

But wait, there's more! 但是,等等,还有更多! using a period ( . ), you can force a string to truncate, so: 使用句点( . ),您可以强制截断字符串,因此:

int r;
for(r=1; r<6; r++) {
  printf("%*s%.*s\n", r, "$", 5-r, "*****");
}

Bam! 巴姆! Got the whole thing down to one statement! 将整个事情简化为一个陈述!

You can use the trick of variable left-padding when you write the $ : 您可以在编写$时使用变量左填充的技巧:

printf("%*s", r, "$");

The * in the string format field lets you specify a padding width. 字符串格式字段中的*允许您指定填充宽度。

Currently your n++; 当前您的n++; does nothing. 什么也没做。 To print(" ") n times you need to use something like a for loop. print(" ") n次,您需要使用for循环之类的东西。 One way of doing this: Print the amount of spaces, Then print the amount of stars you need which should be 4 - n. 一种方法是:打印空格数量,然后打印所需的星星数量,该数量应为4-n。

#include <stdio.h>

int main(int argc, char *argv[]) {
    int r;
    int c;
    int n = 0;
    for(r=1; r<=5; r++)
    {
        for (int i = 1; i<=n; i++) { //Print " " n number of times.
            printf(" ");
        }
        printf("$"); //Print dollar sign.
        for(c=0; c<(4-n);c++) //Print * 4-n times.
        {
            printf("*");
        }
        printf("\n");
        n++;

    }
}

Well you got the answer in your question itself, "how to say printf(" "); 'n' times" the easiest way to say printf 'n' times is to run it 'n' times, just like how you asked the program to print '*' (6-r) times! 好吧,您在问题本身中得到了答案:“如何说printf(“”);'n'次”说printf'n'次最简单的方法是运行它'n'次,就像您问程序打印'*'(6-r)次!

well here is a very simple code to help you get the output you wanted 好吧,这是一个非常简单的代码,可以帮助您获得所需的输出

#include<stdio.h>
void main()
{
    int r,c,n=0;

    for(r=1; r<=5; r++)
    {
        for(c=n;c>0;c--)
        {
            printf(" ");

        }
        printf("$");
        for(c=1; c<=5;c++)
        {
            if((6-r)<=c)
            {
                printf(" ");
            }
            else
            {
                printf("*");
            }
        }

        printf("\n");
        n++;

    }
}

If you want to check out the code just go to Codepad.org here you can run your code and see the output 如果您想检查代码,请访问Codepad.org,在这里您可以运行代码并查看输出

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

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