Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我需要一些帮助来创建特定的形状/图案_C++ - Fatal编程技术网

C++ 我需要一些帮助来创建特定的形状/图案

C++ 我需要一些帮助来创建特定的形状/图案,c++,C++,我正在尝试制作这个图案 但我似乎不懂它的窍门 我做的第一部分是我的代码 这是我目前的输出 输入行数:4 * *** ***** ******* ******* ***** *** * 这是我当前的输出,我只需要在它们之间添加中间部分。我需要一些帮助。感谢任何帮助。 我当前的输出与间距有点不符。您应该将代码拆分为函数。那么你的主要功能应该是这样的 int main() { const auto rows = []() {std::size_t r; std::

我正在尝试制作这个图案

但我似乎不懂它的窍门

我做的第一部分是我的代码

这是我目前的输出

输入行数:4

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

*******
 *****
  ***
   *
这是我当前的输出,我只需要在它们之间添加中间部分。我需要一些帮助。感谢任何帮助。
我当前的输出与间距有点不符。

您应该将代码拆分为函数。那么你的主要功能应该是这样的

int main() {
    const auto rows = []() {std::size_t r; std::cin >> r; return r;}();
    printHeader(rows);
    printDashes(rows);
    printMid(rows);
    printDashes(rows);
    printFooter(rows);
    return 0;
}
第一个金字塔可以用

void printStarsLine(const std::size_t row, const std::size_t rows) {
    const std::size_t stars = 1 + 2 * row;
    const std::size_t spaces = 2 * rows - row;

    std::cout << std::string(spaces, ' ')
              << std::string(stars, '*')
              << std::string(spaces, ' ')
              << '\n';
}

void printHeader(const std::size_t rows) {
    for (std::size_t i {0}; i < rows; ++i) {
        printStarsLine(i, rows);
    }
}
可以替换为

for (int i {0}; i < rows; ++i) {
    const int spaces = rows - 1 - i;
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    for (int j {0}; j < i + 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < 2 * rows - 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < i + 1; ++j) {
        std::cout << '*';
    }
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    std::cout << '\n';
}
for (int i {1}; i < rows; ++i) {
    const int spaces = i;
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    for (int j {0}; j < rows - i; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < 2 * rows - 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < rows - i; ++j) {
        std::cout << '*';
    }
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    std::cout << '\n';
}
for(int i{0};istd::cout@Ville Valtteri我的输出不止这些,但由于我的声誉,我无法发布图像。我很新。是字符串(空格“”)一个函数,它需要两个参数。谢谢你的帮助。我可以用一个for循环来做中间部分,而不是创建一个函数。我已经编程了2个星期,所以我仍然没有学习const Auto.感谢代码,我将尝试分析代码并找到中点循环。如果你对C++是新鲜的,你应该避免cStultL。IKE <代码> const char *b/COD>使用。cstring非常复杂,甚至教师使用它们来出错。<代码>:ST::String 是用文本处理C++的方法。当然,可以用相应的函数体替换每个函数调用,但我认为函数更容易理解。可以替换<代码> const Auto'
之间的区别
。在我的代码中,构造函数
std::string
是用两个参数调用的,第二个参数是char
'
,而不是const char数组
'
。因为中间部分的代码有点混乱。我仍然需要学习大小t。我将快速搜索它。感谢您的响应
void printDashes(const std::size_t rows) {
    const std::size_t dashes = 2 * rows + 1;

    std::cout << std::string(rows, ' ')
              << std::string(dashes, '-')
              << std::string(rows, ' ')
              << '\n';
}
void printMidLine(const std::size_t row, const std::size_t rows) {
    const std::size_t spaces = rows - 1 - row;

    std::cout << std::string(spaces, ' ')
              << std::string(row + 1, '*')
              << '|'
              << std::string(2 * rows - 1, '*')
              << '|'
              << std::string(row + 1, '*')
              << std::string(spaces, ' ')
              << '\n';
}

void printMid(const std::size_t rows) {
    for (std::size_t i {0}; i < rows; ++i) {
        printMidLine(i, rows);
    }
    for (std::size_t i {1}; i < rows; ++i) {
        printMidLine(rows - i - 1, rows);
    }
}
#include <cstddef>
#include <iostream>
#include <string>

void printStarsLine(const std::size_t row, const std::size_t rows) {
    const std::size_t stars = 1 + 2 * row;
    const std::size_t spaces = 2 * rows - row;

    std::cout << std::string(spaces, ' ')
              << std::string(stars, '*')
              << std::string(spaces, ' ')
              << '\n';
}

void printHeader(const std::size_t rows) {
    for (std::size_t i {0}; i < rows; ++i) {
        printStarsLine(i, rows);
    }
}

void printFooter(const std::size_t rows) {
    for (std::size_t i {rows}; i > 0; --i) {
        printStarsLine(i - 1, rows);
    }
}

void printDashes(const std::size_t rows) {
    const std::size_t dashes = 2 * rows + 1;

    std::cout << std::string(rows, ' ')
              << std::string(dashes, '-')
              << std::string(rows, ' ')
              << '\n';
}

void printMidLine(const std::size_t row, const std::size_t rows) {
    const std::size_t spaces = rows - 1 - row;

    std::cout << std::string(spaces, ' ')
              << std::string(row + 1, '*')
              << '|'
              << std::string(2 * rows - 1, '*')
              << '|'
              << std::string(row + 1, '*')
              << std::string(spaces, ' ')
              << '\n';
}

void printMid(const std::size_t rows) {
    for (std::size_t i {0}; i < rows; ++i) {
        printMidLine(i, rows);
    }
    for (std::size_t i {1}; i < rows; ++i) {
        printMidLine(rows - i - 1, rows);
    }
}

int main() {
    const auto rows = []() {std::size_t r; std::cin >> r; return r;}();
    printHeader(rows);
    printDashes(rows);
    printMid(rows);
    printDashes(rows);
    printFooter(rows);
    return 0;
}
printMid(rows);
for (int i {0}; i < rows; ++i) {
    const int spaces = rows - 1 - i;
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    for (int j {0}; j < i + 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < 2 * rows - 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < i + 1; ++j) {
        std::cout << '*';
    }
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    std::cout << '\n';
}
for (int i {1}; i < rows; ++i) {
    const int spaces = i;
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    for (int j {0}; j < rows - i; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < 2 * rows - 1; ++j) {
        std::cout << '*';
    }
    std::cout << '|';
    for (int j {0}; j < rows - i; ++j) {
        std::cout << '*';
    }
    for (int j {0}; j < spaces; ++j) {
        std::cout << ' ';
    }
    std::cout << '\n';
}