繁体   English   中英

在C中执行功能的命令

[英]Commands to execute functions in C

我正在使用Linux的LXLE 14.04发行版。 我想编写一个C程序来读取命令,解释并执行它们。 我希望该程序高效,并且我不想使用链接列表。 这些命令是对集合的操作。 每个集合可以包含0到127之间的任何值。 我决定将一个集合表示为包含128位的字符数组。 如果打开位置pos上的位,则组号为pos;如果关闭位置pos上的位,则组号不为pos。 例如,如果位置4的位为1,则数字4出现在集合中;如果位置11的位为1,则数字11出现在集合中。

该程序应读取命令并以某种方式解释它们。 有一些命令:read_set,print_set,union_set,intersect_set,sub_set和halt。

例如,终端中的命令read_set A,1,2,14,-1将导致将列表的值读入命令中的指定集中。 在这种情况下,命令中的指定集合为A。列表的结尾由-1表示。 因此,在编写此命令后,集合A将包含元素1,2,14。

到目前为止,这就是我所拥有的。 以下是文件set.h

#include <stdio.h>

typedef struct
{
    char array[16]; /*Takes 128 bits of storage*/
}set;



extern set A , B , C , D , E , F;

这是文件main.c

#include <stdio.h>
#include "set.h"
#include <string.h>
#include <stdlib.h>

set A , B , C , D , E , F; /*Variable definition*/




   void read_set(set s,char command[])
   {
        int i, number = 0 , pos; 

        char* str_num = strtok(NULL,"A, ");
        unsigned int flag = 1; 
        printf("I am in the function read_set right now\n");

        while(str_num != NULL) /*without str_num != NULL get        segmentation fault*/
       {
            number = atoi(str_num);
            if(number == -1)
                return;
             printf("number%d ",number);
             printf("str_num %c\n",*str_num);
            i = number/8;     /*Array index*/
            pos = number%8;  /*bit position*/
            flag = flag << pos;
            s.array[i] = s.array[i] | flag;

            str_num = strtok(NULL, ", ");

           if(s.array[i] & flag)
               printf("Bit at position %d is turned on\n",pos);
         else
            printf("Bit at position %d is turned off\n",pos);
       flag = 1;
    }

}

void print_set(set s)
{
    unsigned int flag = 1; int in_set = 0;
    int i = 0;
    while(s.array[i] != -1)
    {
        if(s.array[i] & flag)
        {
              in_set = s.array[i];
              printf("%d,",in_set );
        }
       i++;
      flag = 1;
   }

}
int main()
{ 
    #define CMD_LENGTH 256

    char command[CMD_LENGTH]; char* letter;
    printf("Please enter a command");
    gets(command);
    letter = strtok(command,"read_set ,");
    switch(*letter)
    {
        case 'A':
        {
           read_set(A,command);
            break;
       }
       case 'B':
       {
            read_set(B,command);
            break;
       }
       case 'C':
       {
         read_set(C,command);
         break;
        }
        case 'D':
        {
          read_set(D,command);
           break;
        }
       case 'E':
       {
           read_set(E,command);
           break;
       }
      case 'F':
      {
           read_set(F,command);
           break;
       }

   }



    return 0;
}

显然,写一堆switch语句并为每个命令使用strtok,并为每个命令重复在main函数中编写的代码以调用不同的函数不是一个好习惯。 我考虑过使用指向通用函数的指针,但是由于每个函数都接收不同的参数,因此我认为这不会起作用。

有更好的方法吗?

提前致谢!

更新#1:这是代码。 我已经对其进行了一些更改。

#include <stdio.h>
#include "set.h"
#include <string.h>
#include <stdlib.h>

set A , B , C , D , E , F; /*Variable definition*/
set sets[6];
/*Below I want to initialize  sets so that set[0] = A set[1] =    B     etc*/
sets[0].array = A.array;
sets[1].array = B.array;
sets[2].array = C.array;
sets[3].array = D.array;
sets[4].array = E.array;
sets[5].array = F.array;

void read_set(set s,char all_command[])
{
    int i, number = 0 , pos; 

    char* str_num = strtok(NULL,"A, ");
    unsigned int flag = 1; 
    printf("I am in the function read_set right now\n");

while(str_num != NULL) /*without str_num != NULL get segmentation fault*/
{ 
        number = atoi(str_num);
         if(number == -1)
        return;
       printf("number%d ",number);
       printf("str_num %c\n",*str_num);
       i = number/8;     /*Array index*/
       pos = number%8;  /*bit position*/
       flag = flag << pos;
      s.array[i] = s.array[i] | flag;

       str_num = strtok(NULL, ", ");

       if(s.array[i] & flag)
           printf("Bit at position %d is turned on\n",pos);
      else
         printf("Bit at position %d is turned off\n",pos);
       flag = 1;
    }

}


typedef struct 
{
    char *command;
    void (*func)(set,char*);
} entry;

entry chart[] = { {"read_set",&read_set} };

void (*getFunc(char *comm) ) (set,char*)
{
   int i;
   for(i=0; i<2; i++)
   {
       if( strcmp(chart[i].command,comm) == 0)
            return chart[i].func;
   }
   return NULL;
}

int main()
{

   #define PER_CMD 256

    char all_comm[PER_CMD];    void (*ptr_one)(set,char*) = NULL; char* comm; char* letter; 

    while(  (strcmp(all_comm,"halt") != 0 ) & (all_comm != NULL))
    {
        printf("Please enter a command");
        gets(all_comm);
        comm = strtok(all_comm,", ");
        ptr_one = getFunc(comm);
        letter = strtok(NULL,",");
        ptr_one(A,all_comm);
        all_comm[0] = '\0';
        letter[0] = '\0';
    }

    return 0;
}

我收到以下编译错误:main.c:9:8:错误:预期的 = , , , ; , asm 或标记前的属性。

我怎么了 我怎样才能解决这个问题?

非常感谢! @杨la

但是,对于您而言,使用switch几乎是解决此问题的最佳方法。

无需switch另一种方法是使用一种简单的方法来获取索引。 这是一个简单的解决方案。

set sets[6];
read_set(sets[*letter - 'A'], command);

然后,如果您需要读取命令,则需要另一个指向函数的指针数组。 如下所示:

void (*functions[3])(set,char[]);
functions[0] = read_set;

等等。 关键是将string覆盖为int ,因此可以将其视为数组的索引。

然后调用函数,例如functions[string_to_int(string)](set,char[]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM