问一道有关Directory Listing的ACM题

Haruka Haruka 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
题目是这样的:
Given a tree of UNIX directories and file/directory sizes, you are supposed to list them as a tree with proper indention and sizes.

Input

The input consists of several test cases. Each case consists of several lines which represent the levels of the directory tree. The first line contains the root file/directory. If it is a directory, then its children will be listed in the second line, inside a pair of parentheses. Similarly, if any of its children is a directory, then the contents of that directory will be listed in the next line, inside a pair of parentheses. The format of a file/directory is:


name size or *name size

where name, the name of the file/directory, is a string of no more than 10 characters; size > 0 is the integer size of the file/directory; * means the name is a directory. It is guaranteed that name will not contain characters '(', ')', '[', ']', and '*'. There are no more than 10 levels for each case, and no more than 10 files/directories on each level.

Output

For each test case, list the tree in the format shown by the sample. Files/directories that are of depth d will have their names indented by 8d spaces. Do NOT print tabs to indent the output. The size of a directory D is the sum of the sizes of all the files/directories in D, plus its own size.


Sample Input

*/usr 1
(*mark 1 *alex 1)
(hw.c 3 *course 1) (hw.c 5)
(aa.txt 12)
*/usr 1
()


Sample Output

|_*/usr[24]
|_*mark[17]
| |_hw.c[3]
| |_*course[13]
| |_aa.txt[12]
|_*alex[6]
|_hw.c[5]
|_*/usr[1]
我编了一个程序,用多组数据测试了都没有错误,但是上传上去老是说WRONG ANSWER,请大家帮我找找是那种情况没考虑吧,小弟谢谢大家了!
以下是我的源程序:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define DIR 1
#define FIL 2
struct tree
{
char name[11];
int size;
int type;
struct tree *child;
struct tree *sibling;
};
struct t
{
struct tree *tre;
struct t *next;
};
int IsDigit(char c)
{
return ('0'<=c&&c<='9');
}
struct tree *CreatTree()
{
struct tree *temp=malloc(sizeof(struct tree));
if(temp==NULL)
{
printf("Out of memory\n");
exit(0);
}
temp->child=temp->sibling=NULL;
temp->size=0;
temp->type=FIL;
temp->name[0]=0;
return temp;
}
struct tree *Read(char *c)
{
char *name;
struct tree *branch=NULL;
while(*c==' ')
scanf("%c",c);
if(*c!=')'&&*c!=EOF&&*c!='\n')
{
branch=CreatTree();
name=branch->name;
if(*c=='*')
branch->type=DIR;
do{
*(name++)=*c;
scanf("%c",c);
}while(*c!=' ');
*name=0;
while(*c==' ')
scanf("%c",c);
while(IsDigit(*c))
{
branch->size=branch->size*10+*c-'0';
scanf("%c",c);
}
}
return branch;
}
void readin(struct tree *branch,char c)
{
while(c==' '||c=='('||c=='\n')
scanf("%c",&c);
branch->child=Read(&c);
branch=branch->child;
while(branch)
{
branch->sibling=Read(&c);
branch=branch->sibling;
}
}
void add(struct tree *head,char c)
{
struct tree *ptr=head;
for(;ptr;ptr=ptr->sibling)
{
if(ptr->type==DIR)
readin(ptr,c);
else ptr->child=NULL;
}
for(ptr=head;ptr;ptr=ptr->sibling)
if(ptr->type==DIR)
add(ptr->child,c);
}
void DisplayTree(struct tree *root,int depth,char *blank)
{
char blankptr[91];
if(depth)
putchar('\n');
if(root)
{
printf("%s",blank);
printf("_%s[%d]",root->name,root->size);
if((root->sibling==NULL&&root->type==DIR)||depth==0)
blank[8*depth]=' ';
strcat(blank," |");
root=root->child;
while(root)
{
DisplayTree(root,depth+1,blank);
blank[8*depth+9]=0;
root=root->sibling;
}
}
}
int calculatesize(struct tree *root)
{
struct tree *temp=root;
int size=0;
if(root==NULL)
return 0;
while(temp)
{
if(temp->type==DIR)
temp->size+=calculatesize(temp->child);
size+=temp->size;
temp=temp->sibling;
}
return size;
}
struct t *CreatT(struct tree *tr)
{
struct t *temp=malloc(sizeof(struct t));
temp->tre=tr;
temp->next=NULL;
return temp;
}
void Dir()
{
char c,*name;
struct tree *t;
struct t *root=CreatT(NULL),*rootptr=root;
c=getchar();
while(c!=EOF)
{
t=CreatTree();
rootptr->next=CreatT(t);
rootptr=rootptr->next;
t->type=DIR;
while(c==' '||c=='\n')
c=getchar();
name=t->name;
do{
*(name++)=c;
c=getchar();
}while(c!=' ');
*name=0;
while(c==' ')
c=getchar();
while(IsDigit(c))
{
t->size=t->size*10+c-'0';
c=getchar();
}
add(t,c);
calculatesize(t);
do{
c=getchar();
}
while(c==' '||c=='\n');
}
for(rootptr=root->next;rootptr;rootptr=rootptr->next)
{
char blank[91]={'|'};
if(rootptr!=root->next)
putchar('\n');
DisplayTree(rootptr->tre,0,blank);
}
putchar('\n');
}
int main()
{
Dir();
return 0;
}
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
Haruka
C语言 70,040 成员 243,237 内容