输出如下图形

*

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

*

这道题本来也不难。

CSDN的牛们也各有奇招。

但正好理解OSG的node遍历,因此就写了一个旁门左道的。 

#include <iostream>

class node
{
public:
    node(
void){;}
    
~node(void){;}
    
virtual void print(void= 0;
};

class group:public node
{
public:
    group(
void):_child(NULL){;}
    
~group(void){ delete _child;}
    
void print(void)
    {
        curdepth
++;
        
for (int j=0; j<curdepth; j++)
        {
            printf(
"*");
        }
        
if (_child)
        {
            printf(
"\n");
            _child
->print();
        }
        
else
        {
            printf(
"\n");
        }
        
if (_child)
        {
            
for (int j=0; j<curdepth; j++)
            {
                printf(
"*");
            }
            printf(
"\n");
        }
        curdepth
--;
    }
    node 
* _child;
    
static int curdepth;
};
int group::curdepth = 0;


int main(int argc, char** argv)
{
    
int asteriskmaxnum = 5;
    group 
* ptr = NULL;

    group 
* iter = ptr;
    
for (int i =0; i<asteriskmaxnum; i++)
    {
        
if (!iter)
        {
            ptr 
= new group;
            iter 
= ptr;
        }
        
else
        {
            iter
->_child = new group;
            iter 
= (group *)(iter->_child);
        }
    }

    ptr
->print();
    system(
"PAUSE");
    
return 0;


原文地址:https://www.cnblogs.com/mumuliang/p/2107661.html