实验报告加怎么提高扩展性(有偿)有偿!50

#include
#include
#include
#include
#include
#include
#include <Windows.h>
using namespace std;

vector split_str(string s)
{
vector tokens;
istringstream iss(s);
do
{
string sub;
iss >> sub;
tokens.push_back(sub);
} while (iss);

tokens.erase(tokens.end() - 1); // 删除最后的换行回车符

return tokens;

}

int main(int argc, char** argv)
{
cout << "==============================================" << endl;
cout << " 欢迎使用命令行文件管理器" << endl;
cout << " ver: 0.1 by gl" << endl;
cout << "==============================================" << endl;
cout << "命令格式: ? xx1 xx2 …… xxN" << endl;
cout << " ?表示操作选项,xx1~xxN表示操作参数" << endl;
cout << "\n操作选项如下:" << endl;
cout << " md 创建文件夹 rd 删除文件夹" << endl;
cout << " cd 改变当前文件夹 ld 列出文件夹内容" << endl;
cout << " cp 复制文件/文件夹 rf 删除文件" << endl;
cout << " q 退出\n" << endl;

cout << "?";
string commandLine;
vector<string> commandArgs;

getline(cin, commandLine);
commandArgs = split_str(commandLine);

while (commandArgs.size() > 0 && "q" != commandArgs[0])
{
    if ("md" == commandArgs[0])
    {
        //创建文件夹
        if (commandArgs.size() < 2)
        {
            cout << "error: 请输入文件夹名字" << endl;
        }
        else
        {
            string dirName = commandArgs[1];
            if (CreateDirectory(dirName.c_str(), NULL))
            {
                cout << "创建文件夹‘" << dirName << "’成功" << endl;
            }
            else
            {
                cout << "error: 创建文件夹‘" << dirName << "’失败。" << endl;
            }
        }
    }
    else if ("rd" == commandArgs[0])
    {
        //删除文件夹
        if (commandArgs.size() < 2)
        {
            cout << "error: 请输入文件夹名字" << endl;
        }
        else
        {
            string dirName = commandArgs[1];
            if (RemoveDirectory(dirName.c_str()))
            {
                cout << "删除文件夹‘" << dirName << "’成功" << endl;
            }
            else
            {
                cout << "error: 删除文件夹‘" << dirName << "’失败。" << endl;
            }
        }
    }
    else if ("rf" == commandArgs[0])
    {
        //删除文件
        if (commandArgs.size() < 2)
        {
            cout << "error: 请输入文件名字" << endl;
        }
        else
        {
            string fileName = commandArgs[1];
            if (DeleteFile(fileName.c_str()))
            {
                cout << "删除文件‘" << fileName << "’成功" << endl;
            }
            else
            {
                cout << "error: 删除文件‘" << fileName << "’失败。" << endl;
            }
        }
    }
    else if ("cd" == commandArgs[0])
    {
        //改变当前文件夹
        if (commandArgs.size() < 2)
        {
            cout << "error: 请输入文件夹名字" << endl;
        }
        else
        {
            string dirName = commandArgs[1];
            if (SetCurrentDirectory(dirName.c_str()))
            {
                char curDir[300];
                GetCurrentDirectory(300, curDir);
                cout << "当前工作目录更改为:" << curDir << endl;
            }
            else
            {
                cout << "error: 改变当前文件夹失败。" << endl;
            }
        }
    }
    else if ("ld" == commandArgs[0])
    {
        //列出文件夹内容
        WIN32_FIND_DATA ffd;
        string szDir = ".\\*";
        HANDLE hFind = INVALID_HANDLE_VALUE;
        LARGE_INTEGER filesize;

        hFind = FindFirstFile(szDir.c_str(), &ffd);    // 找到第一个文件

        if (INVALID_HANDLE_VALUE == hFind)
        {
            cout << " Fail to find file in the directory." << endl;
        }
        else 
        {
            do     // 循环遍历列出所有文件信息
            {
                if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    cout << setw(6) << left << "<Dir>";
                    cout << right << setw(18) << " " << left << setw(8);
                    cout << ffd.cFileName << endl;
                }
                else
                {
                    filesize.LowPart = ffd.nFileSizeLow;
                    filesize.HighPart = ffd.nFileSizeHigh;
                    cout << setw(6) << left << "-";
                    cout << right << setw(10) << filesize.QuadPart << left << setw(8) << " bytes";
                    cout << ffd.cFileName << endl;
                }
            } while (FindNextFile(hFind, &ffd) != 0);

            FindClose(hFind);
        }
    }
    else if ("cp" == commandArgs[0])
    {
        //复制文件/文件夹
        if (commandArgs.size() < 3)
        {
            cout << "error: 请输入源文件/文件夹名和目标文件/文件夹名。" << endl;
        }
        else
        {
            string sourceFileName = commandArgs[1];
            string targetFileName = commandArgs[2];
            ifstream sourceFile(sourceFileName.c_str(), ios::binary);
            ofstream targetFile(targetFileName.c_str(), ios::binary);
            targetFile << sourceFile.rdbuf();
            targetFile.close();
            sourceFile.close();
            cout << "复制成功。" << endl;
        }
    }
    else
    {
        cout << "error: 命令错误。" << endl;
    }

    cout << "?";
    getline(cin, commandLine);
    commandArgs = split_str(commandLine);
}
return 0;

}

img

img

不要用简单的if else,用类跟回调函数