打印ASCII艺术钻石 [英] Print an ASCII art diamond

查看:173
本文介绍了打印ASCII艺术钻石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个家庭作业问题,我在我的程序设计课程在学校,我有点失落,所以请帮助。这是一个问题:

This is a homework question I got for my programming course at school, and I'm kind of lost, so please help. Here is the question:


编写一个应用程序

上打印一个星号(*)的钻石屏幕。
中的行数为钻石的行数由用户给出。作为
示例,如果用户请求具有7行的
钻石,将显示以下

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.

这里是我到目前为止:

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

这篇关于打印ASCII艺术钻石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆