Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

坚持不用任何库函数,练代码能力

Posted by karl_sc at 2017-09-06 15:15:27 on Problem 1057
//http://poj.org/problem?id=1057
#include<stdio.h>
struct Dir
{
    char name[16];
    int level;
    Dir *parent;

    char sonFile[8][16];
    int fileNum;
};
void strCopy(char *, const char *);
int strCmp(const char *, const char *);
void strSort(char[][16], int);

void setDir(Dir*, const char*, int, Dir*);

void printDir(Dir);
void printFile(Dir);

int main()
{
    for(int T = 1;; T++)
    {
        char tStr[16];
        gets(tStr);
        if(!strCmp(tStr, "#"))
            break;
        else
            printf("DATA SET %d:\n", T);

        Dir root;
        setDir(&root, "ROOT", 0, NULL);
        printDir(root);

        Dir *parentDir = &root;

        while(1)
        {
            if(tStr[0] == ']' || tStr[0] == '*')
            {
                strSort(parentDir->sonFile, parentDir->fileNum);
                printFile(*parentDir);
            }
            else if(tStr[0] == 'f')
                strCopy(parentDir->sonFile[parentDir->fileNum++], tStr);
            else if(tStr[0] == 'd')
            {
                Dir *dir = new Dir();
                setDir(dir, tStr, parentDir->level + 1, parentDir);
                printDir(*dir);
                parentDir = dir;
            }
            if(tStr[0] == ']')
            {
                parentDir = parentDir->parent;
            }
            else if(tStr[0] == '*')
            {
                puts("");
                break;
            }
            gets(tStr);
        }
    }
}
void strCopy(char *str1, const char *str2)
{
    int i;
    for(i = 0; str2[i]; i++)
        str1[i] = str2[i];
    str1[i] = 0;
}
int strCmp(const char *str1, const char *str2)
{
    for(int i = 0; str1[i] || str2[i]; i++)
    {
        if(str1[i] < str2[i])
            return -1;
        else if(str1[i] > str2[i])
            return 1;
    }
    return 0;
}
void strSort(char strArr[][16], int siz)
{
    for(int i = 0; i < siz - 1; i++)
        for(int j = i + 1; j < siz; j++)
            if(strCmp(strArr[i], strArr[j]) == 1)
                for(int k = 0; strArr[i][k] || strArr[j][k]; k++)
                {
                    char temp = strArr[i][k];
                    strArr[i][k] = strArr[j][k];
                    strArr[j][k] = temp;
                }
}
void setDir(Dir *dir, const char *name, int level, Dir* parent)
{
    strCopy(dir->name, name);
    dir->level = level;
    dir->parent = parent;
    dir->fileNum = 0;
}
void printDir(Dir dir)
{
    for(int i = 0; i < dir.level; i++)
        printf("|     ");
    puts(dir.name);
}
void printFile(Dir dir)
{
    for(int i = 0; i < dir.fileNum; i++)
    {
        for(int j = 0; j < dir.level; j++)
            printf("|     ");
        puts(dir.sonFile[i]);
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator