首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SIC命令检查器编程片段

SIC命令检查器编程片段
EN

Code Review用户
提问于 2017-09-24 05:05:47
回答 1查看 89关注 0票数 3

这个程序获得一个字符串,然后通过将它与8个可用选项进行比较来检查它是否是一个命令。它还检查它是否有参数。在了解长度和检查字符串相等性的帮助下实现了这些功能。

代码语言:javascript
复制
        /*  BASIC HELPERS START  */
        /*  BASIC LENGTH  */
//a simple function to get total length of a string/command
int helperLength(char a[])
{
    for(int c = 0; c<1000; c++)
        if (a[c] == '\0')
            return c;
}
        /*  BASIC STRING EQUALITY  */
//most equality comparison between 2 strings
//programmingsimplified basically gave EQ for the most part
int CCHelperEQ(char a[], char b[])
{
    int c = 0;
    while (a[c] == b[c]) 
    {
        if (a[c] == '\0' || b[c] == '\0')
            break;
        c++;
    }

    if (a[c] == '\0' && b[c] == '\0')
        return 0;
    else
        return -1;
}
/*  BASIC HELPERS END  */
/*  WITH and WITHOUT PARAMETERS START  */
    /*  COMMANDS WITH PARAMETERS   */
//check if the parameters have errors
//might need to use three new functions for each of the three parameters checkers
int CCHelperWP(char commWP[],char WCommand[], int Pstart, int Cend)
{//command with parameter, which command, start of parameters, end of command
//return 1 if paramets work
    int parC = Pstart;//parameter counter
    char string1[] = "load ";      //1
    char string4[] = "dump ";     //4
    char string6[] = "assemble ";  //6
    if (CCHelperEQ(commWP, string1) == 0)
    {
        while (parC < Cend)
        {//checks if the parameter can work for the command
            if (commWP[parC] == '\0')
                return 0;
            parC++;
        }
        return 1;
    }
    else if (CCHelperEQ(commWP, string4) == 0)
    {
        int dumpC=0;
        while (parC < Cend)
        {//checks if the parameter can work for the command
//dump needs two hex values but at this point I do not need to check the parameters are hex only that they exist so this is enough
            if (commWP[parC] == '\0' && dumpC == 0)
                return 0;
            else if (commWP[parC] == ' ' && dumpC == 0)
                dumpC++;
            else if (commWP[parC] == ' ' && dumpC == 1)
                return 0;
            parC++;
        }
        return 1;
    }
    else if (CCHelperEQ(commWP, string6) == 0)
    {
        while (parC < Cend)
        {//checks if the parameter can work for the command
            if (commWP[parC] == '\0')
                return 0;
            parC++;
        }
        return 1;
    }
    return 0;
}
    /*  COMMANDS WITHOUT PARAMETERS   */
//check the command we are using is one without parameters
//needed otherwise error where functions that need parameters could give a bug with the set-up on compare_command
int CCHelperWO(char commNP[])
{//command no parameter
    char string1[] = "execute";
    char string2[] = "debug";
    char string3[] = "help";
    char string4[] = "directory";   
    char string5[] = "exit";
    if (CCHelperEQ(commNP, string1) == 0)
    {
        return 1;
    }
    else if (CCHelperEQ(commNP, string2) == 0)
    {
        return 1;
    }
    else if (CCHelperEQ(commNP, string3) == 0)
    {
        return 1;
    }
    else if (CCHelperEQ(commNP, string4) == 0)
    {
        return 1;
    }
    else if (CCHelperEQ(commNP, string5) == 0)
    {
        return 1;
    }
    else return 0;
}
/*  WITH and WITHOUT PARAMETERS END  */
//compare_command main helper function
int CCHelper(char a[], char b[])
{
    int c = 0;
    int length = helperLength(a);
    while (a[c] == b[c]) 
    {
        if (a[c] == '\0' && b[c] == '\0')
        {//checks if the command is one that doesn't need parameters
            if(CCHelperWO(b) == 1)
            {    return 0;  }
            else return -1;
        }
        if (b[c] == '\0')
            if (1 == CCHelperWP(a, b, c, length))
                return 0;
        c++;
    }
}

int compare_command(char co[]) {
/* 
going to give an int value to each command
with lots of if statements to check if the words is the command
after the words is the command I will have other functions
that check for the different parameters required for each command.
*/
    char stringLF[] = "load ";      //1
    char stringExe[] = "execute";   //2
    char stringDeb[] = "debug";     //3
    char stringDSE[] = "dump ";     //4
    char stringH[] = "help";        //5
    char stringAF[] = "assemble ";  //6
    char stringDir[] = "directory"; //7
    char stringExit[] = "exit";     //8
    if (CCHelper(co, stringLF) == 0)
    {//With Parameter
        return 1;
    }
    else if (CCHelper(co, stringExe) == 0)
    {//Without Parameter
        return 2;
    }
    else if (CCHelper(co, stringDeb) == 0)
    {//Without Parameter
        return 3;
    }
    else if (CCHelper(co, stringDSE) == 0)
    {//With Parameter
        return 4;
    }
    else if (CCHelper(co, stringH) == 0)
    {//Without Parameter
        return 5;
    }
    else if (CCHelper(co, stringAF) == 0)
    {//With Parameter
        return 6;
    }
    else if (CCHelper(co, stringDir) == 0)
    {//Without Parameter
        return 7;
    }
    else if (CCHelper(co, stringExit) == 0)
    {//Without Parameter
        return 8;
    }
    else return 9;
}

我不知道这个编程片段是否有点过于复杂,或者使用指针是否有帮助。任何提示都会非常感谢,因为我觉得我使用了太多的函数来做我正在做的事情。

EN

回答 1

Code Review用户

回答已采纳

发布于 2017-09-24 06:23:25

你说:

我觉得我用了太多的功能来做我的工作。

我想你是对的。您似乎已经重新实现了2个标准库函数,只做了一些小小的更改。您的helperLength()函数可以编写为:

代码语言:javascript
复制
int helperLength(char a[])
{
    int result = strlen(a);
    if (result >= 1000)
    {
        result = 1000;
    }
    return result;
}

问题是,你为什么以这种方式限制长度为1000?似乎可以将对helperLength()的调用替换为对strlen()的调用,并在调用站点执行限制(如果需要的话),除非要在很多地方重用该函数。

同样,您的CCHelperEQ函数可以编写为:

代码语言:javascript
复制
int CCHelperEQ(char a[], char b[])
{
    return strcmp(a, b) != 0;
}

此外,你似乎在重复做同样的工作。在CCHelperWP()中,第一个if块中的while循环看起来与最后一个if块中的循环相同。你应该把它变成一个函数,然后两者都调用它。它也可能被这样的逻辑所取代:

代码语言:javascript
复制
return strlen(&commWP[Pstart]) >= (Pend - Pstart);

CCHelperWO()函数很好地利用了CCHelperEQ()函数。很好的可重用性!(但我仍然会将这些调用替换为只调用strlen()。)

如果您这样编写CCHelper()函数,它将更容易阅读:

代码语言:javascript
复制
int CCHelper(char a[], char b[])
{
    if (strcmp(a, b) == 0)
    {
        if (CCHelperWO(b) == 1)
        {
            return 0;
        }
        return -1;
    }

    int length = strlen(a);
    int bLen = strlen(b);
    if (CCHelperWP(a, b, bLen, length) == 1)
    {
        return 0;
    }
    // DO SOMETHING HERE!
}

请注意,在结束时,您可能已经到达了应该返回某些内容的状态,但没有返回语句。这是个错误,你需要在那里放点东西。

最后,可以使用表驱动的设计重写compare_command()函数。看起来会是这样的:

代码语言:javascript
复制
int compare_command(char co[]) {
    const char* commands[] = {
        "load ",      //1
        "execute",   //2
        "debug",     //3
        "dump ",     //4
        "help",        //5
        "assemble ",  //6
        "directory", //7
        "exit"     //8
    };
    const numEntries = sizeof(commands) / sizeof(commands[0]);
    for (int i = 0; i < numEntries; ++i)
    {
        if (strcmp(co, commands [ i ]) == 0)
        {
            return i + 1;
        }
    }
    return numEntries + 1;
}

一旦您完成了所有这些,我建议重新考虑您的命名策略。代码中的名称很难读懂。使用CC前缀是可以的,因为这是一个“命令检查器”。但是像CCHelperWOcommNP这样的名字非常简洁。我建议使用更长、更描述性的名称,如CCParseCommandNoParam()command用于这些特定名称。

此外,您可能需要重新考虑是否真的需要使用与没有参数的命令不同的参数来处理命令。它似乎是一个循环,它获取命令,然后检查它应该拥有多少参数,然后解析出许多参数会将其折叠为所有命令的一个代码路径。

票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/176405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档