返回信息流#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
struct node{
char name[20];
bool type;//false file, true dir
struct node * child;
struct node * brotherHead;
struct node * brotherTail;
};
struct index{
char name[20];
struct node * pos;
struct index * next;
};
class filesystem
{
private:
struct index *indexDir;
struct index *indexFile;
struct node *nodeHead;
struct index *indexDirTail;
struct index *indexFileTail;
public:
filesystem();
void createfile(char newName[], char targetName[]);
void createdir(char newName[], char targetName[]);
void listfile(char targetName[]);
void listdir(char targetName[]);
};
filesystem::filesystem()
{
indexFile = NULL;
nodeHead = (struct node*)malloc(sizeof(struct node));
strcpy(nodeHead->name, "root");
nodeHead->type = true;
nodeHead->child = NULL;
nodeHead->brotherHead = NULL;
nodeHead->brotherTail = nodeHead->brotherHead;
indexDir = (struct index*)malloc(sizeof(struct index));
strcpy(indexDir->name, "root");
indexDir->next = NULL;
indexDir->pos = nodeHead;
indexDirTail = indexDir;
indexFileTail = indexFile;
}
void filesystem::createfile(char newName[], char targetName[])
{
struct index * current = indexDir;
struct node * parent = NULL;
while(current != NULL)
{
if (strcmp(current->name, targetName) == 0)//找到索引
{
parent = current->pos;
break;
}
current = current->next;
}
struct node * createone = (struct node *)malloc(sizeof(struct node));
strcpy(createone->name, newName);
createone->type = false;
createone->child = NULL;
createone->brotherHead = NULL;
createone->brotherTail = createone->brotherHead;//构造目录树节点
if (parent->child == NULL)
{
parent->child = createone;
parent->child->brotherTail = createone;
}
else
{
parent->child->brotherTail->brotherHead = createone;
parent->child->brotherTail = createone;
}//目录树中添加节点
struct index * newone = (struct index *)malloc(sizeof(struct index));
strcpy(newone->name, newName);
newone->next = NULL;
newone->pos = createone;//构造索引节点
if (indexFile == NULL)
{
indexFile = newone;
indexFileTail = newone;
}
else
{
indexFileTail->next = newone;
indexFileTail = newone;
}//目录树中添加节点
}
void filesystem::createdir(char newName[], char targetName[])
{
struct index * current = indexDir;
struct node * parent = NULL;
while(current != NULL)
{
if (strcmp(current->name, targetName) == 0)//找到索引
{
parent = current->pos;
break;
}
current = current->next;
}
struct node * createone = (struct node *)malloc(sizeof(struct node));
strcpy(createone->name, newName);
createone->type = true;
createone->child = NULL;
createone->brotherHead = NULL;
createone->brotherTail = createone->brotherHead;//构造目录树节点
if (parent->child == NULL)
{
parent->child = createone;
parent->child->brotherTail = createone;
}
else
{
parent->child->brotherTail->brotherHead = createone;
parent->child->brotherTail = createone;
}//目录树中添加节点
struct index * newone = (struct index *)malloc(sizeof(struct index));
strcpy(newone->name, newName);
newone->next = NULL;
newone->pos = createone;//构造索引节点
indexDirTail->next = newone;
indexDirTail = newone;
//目录树中添加节点
}
void filesystem::listfile(char targetName[])
{
struct index * currentPtr = indexDir;
struct node * parent = NULL;
while(currentPtr != NULL)
{
if (strcmp(currentPtr->name, targetName) == 0)//找到索引
{
parent = currentPtr->pos;
break;
}
currentPtr = currentPtr->next;
}
if (parent == NULL)
return;
struct node * current = parent->child;
while(current != NULL)
{
if (!current->type)
printf("%s\n", current->name);
current = current->brotherHead;
}
}
void filesystem::listdir(char targetName[])
{
struct index * currentPtr = indexDir;
struct node * parent = NULL;
while(currentPtr != NULL)
{
if (strcmp(currentPtr->name, targetName) == 0)//找到索引
{
parent = currentPtr->pos;
break;
}
currentPtr = currentPtr->next;
}
if (parent == NULL)
return;
struct node * current = parent->child;
while(current != NULL)
{
if (current->type)
printf("%s\n", current->name);
current = current->brotherHead;
}
}
int main()
{
int n;
scanf("%d", &n);
int i;
for (i = 0; i < n; i ++)
{
filesystem a;
int num;
scanf("%d", &num);
int k;
for (k = 0; k < num; k ++)
{
char instruct[20];
scanf("%s", instruct);
if (strcmp(instruct, "CREATEFILE") == 0)
{
char newName[20];
char targetName[20];
scanf("%s", newName);
scanf("%s", targetName);
a.createfile(newName, targetName);
}
else if (strcmp(instruct, "CREATEDIR") == 0)
{
char newName[20];
char targetName[20];
scanf("%s", newName);
scanf("%s", targetName);
a.createdir(newName, targetName);
}
else if (strcmp(instruct, "LISTFILE") == 0)
{
char targetName[20];
scanf("%s", targetName);
a.listfile(targetName);
}
else if (strcmp(instruct, "LISTDIR") == 0)
{
char targetName[20];
scanf("%s", targetName);
a.listdir(targetName);
}
}
}
return 0;
}
这是一条镜像帖。来源:北邮人论坛 / acm-icpc / #87699同步于 2015/8/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
ACM_ICPC机器人发帖
[求助]bupt oj 91 文件系统 运行时错误
sj159372009
2015/8/15镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复