我正在尝试创建一个映射来存储键值对。我写了一个ContainsKey
函数——如果我找到密钥然后返回true
else false
。
我认为我的 EQ 声明有问题,但我不知道问题是什么。有人可以看看我的代码并给我一些指导吗?
这是我的头文件“exer36.h”
#ifndef worksheet36_exer36_h
#define worksheet36_exer36_h
#ifndef DYARRAYDICTH
# define DYARRAYDICTH
/*
dynamic array dictionary interface file
*/
# ifndef KEYTYPE
# define KEYTYPE char *
# endif
# ifndef VALUETYPE
# define VALUETYPE double
# endif
struct association {
KEYTYPE key;
VALUETYPE value;
};
# define TYPE struct association *
# include "exer14.h"
/* dictionary */
int dyArrayDictionaryContainsKey (struct DynArr * da, KEYTYPE key);
# endif
#endif
源文件“exer36.c”
#include <stdio.h>
#include <stdlib.h>
#include "exer36.h"
int dyArrayDictionaryContainsKey (struct DynArr *da, KEYTYPE key){
for (int i=0; i < da->size; i++)
{
if (EQ(((struct association *)da->data[i])->key,key))
return 1;
}
return 0;
}
头文件“exer14.h”
#ifndef worksheet14_exer14_h
#define worksheet14_exer14_h
#ifndef TYPE
#define TYPE int
#endif
# ifndef LT
# define LT(A, B) ((A) < (B))
# endif
#ifndef EQ
#define EQ(a, b) (a == b)
#endif
#endif
struct DynArr {
TYPE *data; /* pointer to the data array */
int size; /* Number of elements in the array */
int capacity; /* capacity ofthe array */
};
/* Dynamic Array Functions */
void initDynArr(struct DynArr *v, int capacity);
void freeDynArr(struct DynArr *v);
int sizeDynArr( struct DynArr *v);
void addDynArr(struct DynArr *v, TYPE val);
TYPE getDynArr(struct DynArr* da, int position);
void putDynArr(struct DynArr* da, int position, TYPE value);
void swapDynArr (struct DynArr* da, int i, int j);
void removeAtDyArr(struct DynArr* da, int index);
主文件
#include <stdio.h>
#include <stdlib.h>
#include "exer36.h"
int main(int argc, const char * argv[])
{
//Create a dynamic array
struct DynArr myArrD;
printf("The size of myArrD should be empty (0), return: %d\n", myArrD.size);
//Add a pair (Key = a AND Value = 1.0)
char * a = "a";
dyArrayDictionaryPut(&myArrD, a, 1.0);
printf("Added one association. Size of myArrD should be 1, return: %d\n", myArrD.size);
printf("%d\n",dyArrayDictionaryContainsKey(&myArrD, a));
return 0;
}