我使用的是 Linux LXLE 14.04 发行版。 我想编写一个 C 程序来读取命令、解释并执行它们。我希望该程序高效,但我不想使用 一个链接列表。 这些命令是对集合的操作。 每个集合可以包含 0 到 127 之间的任何值(包括 0 和 127)。 我决定将一个集合表示为包含 128 位的字符数组。 如果位置 pos 处的位打开,则数字 pos 在集合中,如果位置 pos 处的位关闭,则数字 pos 为 不存在于集合中。例如,如果位置 4 的位为 1,则数字 4 出现在集合中,如果位置 11 的位为 1,则数字 4 出现在集合中。 集合中有 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���� 或 ���属性��� 位于���.��� token 之前
我的错误是什么?我该如何解决这个问题?
非常感谢! @声称杨
最佳答案
但是,就您而言,使用 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[]);
关于C 中执行函数的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41481735/