c - 通过函数的模式

标签 c

问题是:

我做了什么:

我分别使用了 2 1 0 代替了 + - ' '。这道题必须通过函数来​​完成。
但是,这些与给出的输出不匹配。我哪里出错了?

最佳答案

我们初学者应该互相帮助。:)

给你。该代码是用 C++ 编写的。所以你需要研究它并自己用 C 重写它。

#include <iostream>

int main() 
{
    while ( true )
    {
        std::cout << "Input the number of elements : ";

        unsigned int n;

        if ( not ( std::cin >> n ) or ( n == 0 ) ) break;

        std::cout << "Input the size of each element : ";

        unsigned int m;

        if ( not ( std::cin >> m ) or ( m == 0 ) ) break;

        std::cout << '\n';

        for ( unsigned int i = 0; i < m + 2; i++  )
        {
            char c1 = i == 0 || i == m + 1 ? '+' : '|';
            char c2 = i == 0 || i == m + 1 ? '-' : ' ';

            for ( unsigned int j = 0; j < n + m * ( n - 1 ); j++ )
            {
                std::cout << ( j % (m + 1) == 0 ? c1 : c2 );  
            }
            std::cout << '\n';
        }

        std::cout << std::endl;
    }

    return 0;
}

程序输出可能如下所示

Input the number of elements : 10
Input the size of each element : 3

+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+

Input the number of elements : 0

关于c - 通过函数的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41663575/

相关文章:

用于 mpeg-ts 的具有恒定时间延迟的 c 循环缓冲区

c - 查找最长的重复字符串及其在给定字符串中重复的次数

c - 从文件中读取随机输入

c - GDB 不允许我读取 argv 内存段

c - gcc 栈内存分配

c++ - 什么时候数组充当c中的指针?

c - 在 C 中处理 stdin 上的输入

c - 尽管 0 长度缓冲区和编译器警告,scanf() 仍然可以工作。到底是怎么回事?

c - 在 C 中将主文件拆分为模块

c - 构建 C 应用程序?