我在C中有这3个文件,一个是头文件,另一个是dictionary.c,最后一个是main.c
dictionary.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dictionary.h"
/* Calculate the edition distance between two strings */
int getDistance(char* ref,char* word) {
char* w1=NULL;
char* w2=NULL;
int dist=0;
int i=0;
bool different=false;
/* Put the shortest word on w1 and the longest on w2. */
if(strlen(ref)<=strlen(word)) {
w1=ref;
w2=word;
} else {
w1=word;
w2=ref;
}
/* Copare char to char */
for(i=0;i<strlen(w2);i++) {
if(different) {
/* If the words are different, increase the distance */
dist++;
} else {
/* Compare the new char and word lenghts */
if(i<strlen(w1)) {
different=w1[i]!=w2[i];
} else {
/* w1 is shorter than w2 */
different=true;
}
if(different) {
/* If the words are different, increase the distance */
dist++;
}
}
}
return dist;
}
/* Get word information */
bool getWordInfo(char* str,char* word,int* languageID,int* wordID){
*languageID=-1;
*wordID=-1;
sscanf(str,"%s ; %d ; %d",word,languageID,wordID);
return *languageID>0 && *wordID>0;
}
WordList create(){
WordList l;
l.act = 1;
l.nelem = 0;
return l;
};
WordList insert(WordList l, tWordInfo e){
int i;
if (l.nelem == [250]){
printf("error");
}else{
for (i=l.nelem; l.act; i--){
l.WordInfo[i+1]= l.WordInfo[i];
}
l.WordInfo[l.act]= e;
l.nelem = l.nelem + 1;
l.act = l.act + 1;
}
return l;
}
dictionary.h
/* Declare the boolean type */
typedef enum {false, true} bool;
/* Calculate the edition distance between two strings */
int getDistance(char* ref,char* word);
/* Get word information */
bool getWordInfo(char* str,char* word,int* languageID,int* wordID);
char tWordInfo(int* languageID, int* wordID, char* word[128]);
typedef struct {
char tWordInfo[250];
int act;
int nelem;
}tWordList, WordList;
WordList create();
WordList insert(WordList l, tWordInfo e);
和最后一个main.c
#include <stdio.h>
#include "dictionary.h"
int main(int argc, char **argv)
{
char word1[128];
char word2[128];
int langId1,wordId1;
int langId2,wordId2;
int d1,d2;
WordList l;
tWordInfo e;
create();
printf("%d\n", l.act);
printf("%s\n", l.tWordInfo[l.act]);
printf("%d\n", l.nelem);
getWordInfo("hospital ; 1 ; 1",word1,&langId1,&wordId1);
getWordInfo("hola ; 1 ; 2",word2,&langId2,&wordId2);
d1=getDistance(word1,word2);
d2=getDistance(word2,word1);
printf("[%d,%d] => %s\n",langId1,wordId1,word1);
printf("[%d,%d] => %s\n",langId2,wordId2,word2);
printf("d(%s,%s)=%d\n",word1,word2,d1);
printf("d(%s,%s)=%d\n",word2,word1,d2);
return 0;
}
程序没有结束,但是我无法定义函数insert,在字典文件(.c和.h)中给出了这个错误错误:在'tWordInfo'之前的exepected声明说明符或'...'所以什么是问题在这里,我该如何解决这个问题?
另一个问题是l.act和l.nelem的printf没有给出1和0的值,l.act给出-2,l.nelem给出大数。为什么呢?
谢谢
答案 0 :(得分:0)
将TWordInfo声明为函数,然后在insert函数中用作参数类型。您需要为该参数设置不同的类型。