打印ASCII艺术钻石

6 c++

这是我在学校编程时遇到的一个家庭作业问题,我有点迷茫,所以请帮忙.这是一个问题:

编写一个应用程序,在屏幕上打印一个星号(*)菱形.钻石中的线条数由用户给出.例如,如果用户请求具有7行的菱形,则将显示以下内容.

这是我到目前为止:

{
  int nlines;
  int nsp;
  cout << "enter the number of lines (must be more then one)" << endl;
  cin >> nlines;
  while((nlines)<=0)
  {
    cout << "enter the number of lines (must be more then one)" << endl;
    cin >> nlines;
  }
  nsp=(nlines - 1)/2;
  for(int currspace = 1; currspace <= nsp; currspace++)
  {
    cout << " ";
  }
  cout << "*" << endl;
  for( int currentline = 0; currentline < nlines; currentline++)
  {
    for( int currentaster = 0; currentaster <= currentline; currentaster++)
      cout << "*";
    cout << endl;
  }
  return 0;
Run Code Online (Sandbox Code Playgroud)

Jer*_*fin 15

我不会试图给出一个完整的答案,因为它是家庭作业,但我的直接建议是尝试将任务分解为一些子任务,每个子任务都更简单一些.例如,我会与什么也没做,但打印出来的一个小功能启动一个指定长度的星号线与指定的理由.从事物的外观来看,你可以很容易地处理它,而你拥有它,其余的则相当简单.


Sma*_*ery 6

由于它的功课,我只是建议你可能想要采取的一些步骤,并让你有实际写作的乐趣:-)

所以要打印钻石,我认为它会是这样的(例如5行 - 行数必须是奇数)

  *
 ***
*****
 ***
  *
Run Code Online (Sandbox Code Playgroud)

我标记了角色的位置.所以你需要做的是弄清楚你需要打印多少空间,然后计算出多少个星号.在每一行之后,根据当前行号计算出你需要添加/减去多少空格,然后计算出你需要添加/减去多少个星号.我推荐的一些步骤:

  1. 在给定总行数和当前行号的情况下,编写一个计算要打印的空格数的函数
  2. 为星号数写一个类似的函数
  3. 使用这两个功能逐行打印出钻石.


the*_*rty 5

也许一个好的开始是你编写一个在屏幕上写入n个空格和m个星的函数.


pax*_*blo 5

这是我怎么做的.检查一些样品钻石(5,7和9行):

                         *
             *          ***
   *        ***        *****
  ***      *****      *******
 *****    *******    *********
  ***      *****      *******
   *        ***        *****
             *          ***
                         *
Run Code Online (Sandbox Code Playgroud)

第一行包括一些空间和一些星星.多少?星数总是一个,空格数取决于所需的行数:

Number of lines  |  Initial space count
-----------------+---------------------
       1         |            0
       2         |            0
       3         |            1
       4         |            1
       5         |            2
       6         |            2
       7         |            3
       8         |            3
Run Code Online (Sandbox Code Playgroud)

所以空格的初始数量是(#lines - 1) / 2, rounded down.

你会注意到的另一件事是,在每个后续行上,空格数减少一个,星数增加两个.

直到空间数为零的点为止,然后你开始将空间增加1并将星空减少2,直到恒星的数量再次为1.

唯一的另一种特殊情况是偶数行,你应该复制中间行.

查看您的代码,您非常接近,您只需调整循环语句相对于正在打印的内容的位置.换句话说,外部循环用于行,然后是空间的内部循环,接着是另一个用于星形的内部循环.

我用Python编写了一个测试程序(见下文),但你真的不应该理解Python语法(该程序比我原先认为的要大得多)从这个答案中得到一些用处,所以这里有一些简化的伪代码.在开始编写代码之前,您应该始终坐下来思考问题.这将有助于您培养将来能够帮助您的技能.

Main:
    Get numlines from user, check that greater than 0.
    Set numstars to 1.
    Set numspaces to int((numlines-1)/2)
    call Output (numspaces,numstars)
    while numspaces > 0:
        numspaces = numspaces - 1
        numstars = numstars + 2
        call Output (numspaces,numstars)
    if numlines is even:
        call Output (numspaces,numstars)
    while numstars > 0:
        numspaces = numspaces + 1
        numstars = numstars - 2
        call Output (numspaces,numstars)
    end.

Output(spaces,stars):
    for i = 1 to spaces:
        print " "
    for i = 1 to stars:
        print "*"
    print end-of-line
    return
Run Code Online (Sandbox Code Playgroud)

最后,这是我用来测试伪代码的Python代码(因为你需要C++而不是真正给你代码):

import sys

# Construct line based on number of spaces and stars.
def outLine (spc,str):
    # Start with empty line.
    line = ""

    # Add spaces.
    for i in range(0,spc):
        line = "%s "%(line)

    # Add stars.
    for i in range(0,str):
        line = "%s*"%(line)

    #Output line.
    print line

# Get number of lines from user and check.

numlines = input ("Enter number of lines: ")
if numlines < 1:
    print "Must be greater than zero"
    sys.exit(1);

# Calculate initial space and star count.
numspaces = int ((numlines-1)/2)
numstars = 1

# Output initial line.
outLine (numspaces,numstars)

# Output subsequent lines until middle reached.
while numspaces > 0:
    numspaces = numspaces - 1
    numstars = numstars + 2
    outLine (numspaces,numstars)

# Repeat middle if even number of lines desired.
if numlines % 2 == 0:
    outLine (numspaces,numstars)

# Output the bottom half of the diamond.
while numstars > 0:
    numspaces = numspaces + 1
    numstars = numstars - 2
    outLine (numspaces,numstars)
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

Enter number of lines: 15
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *
Run Code Online (Sandbox Code Playgroud)