c++ - 打印 ASCII 艺术钻石

标签 c++

这是我在学校编程类(class)的作业题,我有点迷路了,所以请帮忙。这是问题:

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;

最佳答案

我不会尝试给出完整的答案,因为它是家庭作业,但我的直接建议是尝试将任务分解为一些子任务,每个子任务都比较简单。例如,我会从一个小函数开始,它只打印出 行指定长度的星号和指定的理由。从事情的表面上看,您可能可以很容易地处理这些事情,并且有了它,剩下的就更简单了。

关于c++ - 打印 ASCII 艺术钻石,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1541487/

相关文章:

c++ - 将闭包作为参数传递给构造函数 C++

C++ 为两个 double 重载 operator%

c++ - 帮助删除用户帐户的功能

C++ 通过解构迭代 vector 对

c++ - 将 42010958 毫秒转换为小时 :Minutes:Seconds in Qt? 的最干净方法是什么

c++ - 静态成员的问题

c++ - 在左侧创建一个菜单

C++ 无法识别 DLL 中的命令

c++ - 涉及优化器的局部变量构建和销毁

c++ - 用户在“控制面板”->“区域和语言选项”中更改设置后修改主窗口的区域设置