创建一个命令提示符然后控制一些字符串方法来做

发布于 2024-11-30 06:00:45 字数 2018 浏览 1 评论 0原文

我想像命令提示符一样执行我的程序。我认为它必须执行一些字符串方法。

例如:COM> length "string " 必须返回“string”的长度(命令必须大于三个字符,即 len "asdf" 即可)。除此之外,我添加了三个方法,“reverse”、“ToUpper”和“ToLower”以及转义“quit”,并且它不一定是相邻的或不是(命令和字符串之间可以是任何空格)。

我按照自己的方式写的,但我认为它没有优化。那么你有什么技巧可以让它更快并且可重用吗?

(我不关心任何例外,所以我知道有一些例外。)

class Program
{
    static void Main(string[] args)
    {
        string str;
        for (; ; )
        {
            Console.Write("CSD>");
            str = Console.ReadLine();
            if (str == "quit")
                break;
            commandControl(str);
        }
    }

    public static void commandControl(string str)
    {
        string strCom, strString;
        int index;

        str = str.Trim();
        index = str.IndexOf(" ");

        strCom = str.Substring(0, index);
        str = str.Substring(index);
        str = str.Trim();
        strString = str.Substring(1, str.Length - 2);

        string[] strComArry = { "length", "reverse", "upper", "lower" };
        int i;

        if (strCom.Length >= 3)
        {
            for (i = 0; i < strComArry.Length; i++)
                if (strCom.Length <= strComArry[i].Length)
                    if (strCom == strComArry[i].Substring(0, strCom.Length))
                        break;

            switch (i)
            {
                case 0:
                    Console.WriteLine(strString.Length);
                    break;
                case 1:
                    for (int j = strString.Length - 1; j >= 0; --j)
                        System.Console.Write(strString[j]);
                    Console.WriteLine();
                    break;
                case 2:
                    Console.WriteLine(strString.ToUpper());
                    break;
                case 3:
                    Console.WriteLine(strString.ToLower());
                    break;
            }
        }
        else
            Console.WriteLine("Command must be more than 3 characters !...");
    }
}

I wanted to do my program like a command prompt. I thought that it must perform some string methods.

For example: COM> length "string " must return the length of "string " (the command must be bigger than three characters, that is, len "asdf" is OK). In addition to this I added three methods, "reverse", "ToUpper" and "ToLower" and to escape, "quit", and it is not necessarily adjecent or not (can be any spaces between the command and the string).

I wrote on my way, but I think it is not optimized. So do you have any trick to make it faster and reusable?

(I didn't care about any exception so I know there are some exceptions.)

class Program
{
    static void Main(string[] args)
    {
        string str;
        for (; ; )
        {
            Console.Write("CSD>");
            str = Console.ReadLine();
            if (str == "quit")
                break;
            commandControl(str);
        }
    }

    public static void commandControl(string str)
    {
        string strCom, strString;
        int index;

        str = str.Trim();
        index = str.IndexOf(" ");

        strCom = str.Substring(0, index);
        str = str.Substring(index);
        str = str.Trim();
        strString = str.Substring(1, str.Length - 2);

        string[] strComArry = { "length", "reverse", "upper", "lower" };
        int i;

        if (strCom.Length >= 3)
        {
            for (i = 0; i < strComArry.Length; i++)
                if (strCom.Length <= strComArry[i].Length)
                    if (strCom == strComArry[i].Substring(0, strCom.Length))
                        break;

            switch (i)
            {
                case 0:
                    Console.WriteLine(strString.Length);
                    break;
                case 1:
                    for (int j = strString.Length - 1; j >= 0; --j)
                        System.Console.Write(strString[j]);
                    Console.WriteLine();
                    break;
                case 2:
                    Console.WriteLine(strString.ToUpper());
                    break;
                case 3:
                    Console.WriteLine(strString.ToLower());
                    break;
            }
        }
        else
            Console.WriteLine("Command must be more than 3 characters !...");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2024-12-07 06:00:45

我不会提供完整的工作示例,但请看一下下面的代码片段。这个想法是使用动作字典来存储命令的操作。这样,您可以通过添加到“Methods”字典来向解释器添加新功能。

这个想法是调用 InitializeFunctions 来注册您希望在解释器中可用的所有函数,然后调用命令控制来解释其中一个。

(我给出了两个关于如何添加函数、lambda 语法和引用普通函数的示例。此外,这些函数假设您已经解析了什么是命令和什么是参数,您已经在示例中完成了这些操作。)

    static Dictionary<string, Action<string>> Methods = new Dictionary<string, Action<string>>();

    public static void InitializeFunctions()
    {
        Methods.Add("ToLower", a => Console.WriteLine(a.ToLower()));
        Methods.Add("ToUpper", ToUpper);
    }

    static void ToUpper(string str)
    {
        Console.WriteLine(str.ToUpper());
    }

    public static void commandControl(string str, string param)
    {
        if(str.Length < 4)
            Console.WriteLine("Command must be more than 3 characters !...");
        if (Methods.ContainsKey(str))
            Methods[str].Invoke(param);
        else
            Console.WriteLine("Invalid Command");
    }

I'm not going to provide a full working example, but take a look at the snippet below. This idea is to use a dictionary of actions to store what to do with commands. That way, you can add new functionality to the interpreter by adding to the "Methods" dictionary.

The idea is call InitializeFunctions to register all of the functions you want available in the interpreter, and then call command control to interpret one.

(I gave two examples on how to add functions, a lambda syntax and referencing a normal function. Also those functions assume you have already parsed what is a command and what is a parameter, which you have done already in your example.)

    static Dictionary<string, Action<string>> Methods = new Dictionary<string, Action<string>>();

    public static void InitializeFunctions()
    {
        Methods.Add("ToLower", a => Console.WriteLine(a.ToLower()));
        Methods.Add("ToUpper", ToUpper);
    }

    static void ToUpper(string str)
    {
        Console.WriteLine(str.ToUpper());
    }

    public static void commandControl(string str, string param)
    {
        if(str.Length < 4)
            Console.WriteLine("Command must be more than 3 characters !...");
        if (Methods.ContainsKey(str))
            Methods[str].Invoke(param);
        else
            Console.WriteLine("Invalid Command");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文