目录树Print a simple tree to standard output:

#include

#include

typedefstructstem_t*stem;

structstem_t{constchar*str;stem next;};

voidtree(introot,stem head)

{

staticconstchar*sdown="  |",*slast="  `",*snone="  ";

structstem_t col={0,0},*tail;

for(tail=head;tail;tail=tail->next){

printf("%s",tail->str);

if(!tail->next)break;

}

printf("--%d\n",root);

if(root<=1)return;

if(tail&&tail->str==slast)

tail->str=snone;

if(!tail)tail=head=&col;

elsetail->next=&col;

while(root){// make a tree by doing something random

intr=1+(rand()%root);

root-=r;

col.str=root?sdown:slast;

tree(r,head);

}

tail->next=0;

}

intmain(intc,char**v)

{

intn;

if(c<2||(n=atoi(v[1]))<0)n=8;

tree(n,0);

return0;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容