开发者

Print an ASCII art diamond

开发者 https://www.devze.com 2022-12-08 16:22 出处:网络
This is a homewor开发者_如何学Pythonk question I got for my programming course at school, and I\'m kind of lost, so please help. Here is the question:

This is a homewor开发者_如何学Pythonk question I got for my programming course at school, and I'm kind of lost, so please help. Here is the question:

Write an application program that prints a diamond of asterisks (*) on the screen. The number of lines in the diamond is given by the user. As an example, if the user requested a diamond with 7 lines, the following would be displayed.

Here is what I have so far:

{
  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;


I won't try to give a complete answer since it's homework, but my immediate advice would be to try to break the task down into some sub-tasks, each of which is somewhat simpler. For example, I'd start with a small function that did nothing but print out one line of asterisks of a specified length with a specified justification. From the looks of things, you can probably handle that pretty easily, and one you have it, the rest is quite a bit more straightforward.


As it's homework, I'll just recommend some steps you might want to take, and let you have the fun of actually writing it :-)

So to print a diamond, I assume it'll be something like this (example with 5 lines - the number of lines must be odd)

  *
 ***
*****
 ***
  *

I marked the positions of the characters. So what you'll need to do is work out how many spaces you need to print, and then how many asterisks. After each line, work out how many spaces you have to add/subtract based on the current line number, and then work out how many asterists you have to add/subtract. Some steps I'd recommend:

  1. Write a function that calculates the number of spaces to print, given the total number of lines and the current line number
  2. Write a similar function for number of asterisks
  3. Use these two functions to print out the diamond, line by line.


Perhaps a good start would be for you to write a function that writes n spaces, and m stars on the screen.


Here's how I'd do it. Examine some sample diamonds (5, 7 and 9 lines):

                         *
             *          ***
   *        ***        *****
  ***      *****      *******
 *****    *******    *********
  ***      *****      *******
   *        ***        *****
             *          ***
                         *

The first line consists on some spaces and some stars. How many? Number of stars is always one, number of spaces depends on the number of lines desired:

Number of lines  |  Initial space count
-----------------+---------------------
       1         |            0
       2         |            0
       3         |            1
       4         |            1
       5         |            2
       6         |            2
       7         |            3
       8         |            3

So the initial number of spaces is (#lines - 1) / 2, rounded down.

The other thing you'll notice is that on each subsequent line, the number of spaces reduces by one and the number of stars increases by two.

That's up until the point where the number of spaces is zero, then you start increasing spaces by one and decreasing stars by two until the number of stars is once again one.

The only other special case is an even number of lines where you should duplicate the middle line.

Looking at your code, you're very close, you just have to adjust where the loop statements are in relation to what's being printed. In other words, the outer loop is for lines, then an inner loop for spaces followed by another inner loop for stars.

I wrote a test program in Python (see below) but you really shouldn't have to understand Python syntax (that program's a lot bigger than I originally thought it would be) to get some use from this answer so here's some simplified pseudo-code. You should always sit down and think out the problem before you start writing code. This will help you develop the skills that will do you well in the future.

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

Finally, here's the Python code I used to test the pseudo-code (not really giving you the code since you need 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)

and here's an example run:

Enter number of lines: 15
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *


Other people are suggesting that you design it "bottom up", by developing useful subroutines.

Another approach is to design a program "top-down": by writing the code in main first, assuming that any/all useful subroutines which would help to implement the solution already exist: because doing this will let you decide what subroutines you want to have.

For example:

int main()
{
  int nLinesTotal = getNumberOfLinesFromUser();
  int nLinesTopHalf = getHalfOf(nLinesTotal);
  //caution: using 1-based not 0-based index here
  for (int i = 1; i <= nLinesTopHalf; ++i)
  {
    int nAsterisksOnThisLine = calculateAsterisks(i);
    //following function takes nLinesTotal as a parameter because
    //that's necessary to calculate how wide the diamon is and
    //therefore how much leading whitespace there is on each line
    printAsterisks(nLinesTotal, nAsterisksOnThisLine);
  }
  ... etc for the middle line
  ... and for the bottom half of the diamond
}

Doing this shows that it would be useful to have the subroutines like the following:

  • getNumberOfLinesFromUser
  • getHalfOf
  • calculateAsterisks
  • printAsterisks


Lets take an example. Assume height of diamond box is 9. Following figure shows how to arrange '*****' in x-y coordinates.

alt text http://img527.imageshack.us/img527/7261/57257125.png

First we need to move origin to the centrer of diamond as follows.

alt text http://img515.imageshack.us/img515/7122/89173144.png

Find absolute values of all coordinates. Then the figure will be as follows.

alt text http://img73.imageshack.us/img73/9550/75806473.png

Above figure contains solution to problem.

Loop x and y from 0 to 8. 
Print '*' if (x+y) <= 4;  
else Print a space (' ');

Now take a general case. Then we have to

loop from 0 to (Height - 1)
Print '*' if (x+y) <= (Height/2);  
else Print a space (' ');

Program:

void main()
{
    int Height = 15;

    for( int y = 0; y < Height; y++ )
    {
        for( int x = 0; x < Height; x++ )
        {
            int X = abs( x - ( Height / 2 ) );
            int Y = abs( y - ( Height / 2 ) );

            if( ( X + Y ) <= ( Height / 2 ) )
                cout << '*';
            else
                cout << ' ';
        }
        cout << endl;
    }
}

Output:

       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *
0

精彩评论

暂无评论...
验证码 换一张
取 消