structFileInfo { /* * It contains a pointer to sub-dirs, and a string array to files. * Also a name for dir, two int variable records the number of Dirs and Files. */ FileInfo *Dir[100]; stringFile[100]; string _name; int nDirNum; int nFileNum; };
void _sort(string sString[], int slength) { /* * make the array in dictionary order. */ for(int i = 0; i <= slength - 1; i++) { for(int j = i; j <= slength - 1; j++) { if(sString[i] > sString[j]) { string temp; temp = sString[i]; sString[i] = sString[j]; sString[j] = temp; } } } return; }
void __sort(FileInfo *fileinfo) { /* * make files in fileinfo in dictionary order. */ _sort(fileinfo->File, fileinfo->nFileNum); return; }
boolRead(FileInfo *dir) { /* * This function reads the text from cmd and put the infomation into a tree dir. * * When there is another group of data, it returns true to continue. * If not, it return false to end the program. */ string name; int num1 = 0,num2 = 0; while(1) { /* scanf("%s", name);*/cin>>name; switch(name[0]) { case'f': dir->File[num1++] = name; break; case'd': dir->Dir[num2++] = new FileInfo; dir->Dir[num2 - 1]->_name = name; Read(dir->Dir[num2 - 1]); break; case']': dir->nDirNum = num2; dir->nFileNum = num1; __sort(dir); returntrue;//When ended,sort dir and put num1 and num2 to dir as the num of files and dirs. case'*': dir->nDirNum = num2; dir->nFileNum = num1; __sort(dir); returntrue; case'#': returnfalse; } } }
voidWrite(FileInfo *fileinfo, int nRecurTime) { /* * Dealing with the data from fileinfo. * dir and file output separately. */ for(int i = 0; i < fileinfo->nDirNum; i++) { for(int j = 0; j <= nRecurTime; j++) { printf("| "); } printf("%s", fileinfo->Dir[i]->_name.c_str()); putchar('\n'); Write(fileinfo->Dir[i], nRecurTime + 1); } for(int i = 0; i < fileinfo->nFileNum; i++) { for(int j = 0; j < nRecurTime; j++) { printf("| "); } printf("%s", fileinfo->File[i].c_str()); putchar('\n'); } delete fileinfo; return; }
intmain(void) { //Set of Data int num = 1; //input and output while(true) { FileInfo *fileinfo = new FileInfo; fileinfo->_name = "ROOT"; if(Read(fileinfo))//When read #, return false and then exit. { printf("DATA SET %d:", num++); putchar('\n'); printf("ROOT"); putchar('\n'); Write(fileinfo, 0); putchar('\n'); } else { putchar('\n'); return0; } } // fclose(stdin); // fclose(stdout); }