程序代码:
#include <cstdio> int main( void ) { struct tree { int size; char values[27]; }; struct tree trees[27] = { 0 }; for( int c, level=-1; c=getchar(), c!=EOF && c!='\n'; ) { switch( c ) { case '(': ++level; break; case ')': --level; break; case ' ': break; case ',': break; default: trees[level].values[ trees[level].size++ ] = c; } } for( size_t level=0; level!=27 && trees[level].size!=0; ++level ) printf( "%.*s", trees[level].size, trees[level].values ); putchar( '\n' ); return 0; }