我正在使用 Linux 的 LXLE 14.04 发行版。我想编写一个 C 程序来读取命令、解释并执行它们。我希望程序高效,我不想使用链表。命令是对集合的操作。每个集合可以包含从 0 到 127 的任何值(包括 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*/
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(sets[*letter-'A'],all_comm);
all_comm[0] = '\0';
letter[0] = '\0';
}
return 0;
}
我定义了一个名为 chart 的命令结构,每个命令都有一个命令名称和函数指针。然后我创建了一个可以在循环中匹配的这些结构的数组。
在主函数中,我创建了一个名为 ptr_one 的指针。ptr_one 根据用户输入的命令保存正确函数的值。问题是,由于用户决定使用哪个集合,我需要将集合表示为某个变量,以便可以将不同的集合发送到函数 ptr_one。我想过像这样在 main.c 中创建一个数组
set sets[6];
sets[0] = A;
sets[1] = B;
sets[2] = C;
sets[3] = D;
sets[4] = E;
sets[5] = F;
然后像这样 ptr_one(sets[*letter-'A'] , all_command) 在主函数中调用函数 ptr_one。这样,我将我的角色转换为一个集合。
问题是,在编写上面的代码时,我得到了以下编译错误:
错误:预期 ��=���、��、����、��;����、��asm��� 或��<strong>attribute���� 在��之前。 ��令牌
我还在文件 main.c 中尝试了以下内容
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;
但是我得到了这个编译错误,预期为 ��=����、��、����、��;����、��asm��� 或 ��attribute���� 之前.���� 代币
我知道有人问过类似的问题,因为它们似乎对我的具体情况没有帮助。我也厌倦了这个 set sets[6] = { {A.array},{B.array},{C.array},{D.array},{E.array},{F.array} } 但确实如此不编译。
我的错误是什么?如何初始化集合以使其保持集合 A 到 F?