error C2371: 'InitString' : redefinition; different basic types
本帖最后由 inter18099 于 2013-07-13 16:52:54 编辑 运行程序时出现错误:
D:\program\数据结构与算法\严蔚敏\daima\ch4\m.c(92) : warning C4013: 'InitString' undefined; assuming extern returning int
D:\program\数据结构与算法\严蔚敏\daima\ch4\m.c(127) : error C2371: 'InitString' : redefinition; different basic types
但是我怎么也找不到错误在哪,而且刚刚运行代码还好好的,不知道为什么一下子InitString()这个函数出问题了。
#include "c1.h"
#include "c4-2.h"
Status StrAssign(HString *T, char *chars) {
int i, j;
if((*T).ch) {
free((*T).ch);
}
i = strlen(chars);
if(!i) {
(*T).ch = NULL;
(*T).length = 0;
}
(*T).ch = (char*) malloc(i * sizeof(char));
if(!(*T).ch) {
exit(OVERFLOW);
}
for(j = 0; j <= i; j++) {
*((*T).ch + j) = *(chars++);
}
(*T).length = i;
return OK;
}
int StrLength(HString S) {
return S.length;
}
int StrCompare(HString S, HString T) {
while(*(S.ch) == *(T.ch)) {
if(*S.ch == '\0') {
return 0;
}
S.ch++;
T.ch++;
}
return *S.ch - *T.ch;
}
Status ClearString(HString *S) {
if((*S).ch) {
free((*S).ch);
(*S).ch = NULL;
}
(*S).length = 0;
return OK;
}
Status Concat(HString *T, HString S1, HString S2) {
int i;
if((*T).ch) {
free((*T).ch);
}
(*T).length = S1.length + S2.length;
(*T).ch = (char *) malloc((*T).length * sizeof(char));
if(!(*T).ch) {
exit(OVERFLOW);
}
for(i = 0; i < S1.length; i++) {
(*T).ch[i] = S1.ch[i];
}
for(i = 0; i < S2.length; i++) {
(*T).ch[i + S1.length] = S2.ch[i];
}
return OK;
}
Status SubString(HString *Sub, HString T, int pos, int len) {
int i;
if(pos < 1 || pos > StrLength(T) || len < 0 || len > StrLength(T) - pos + 1) {
return ERROR;
}
if((*Sub).ch) {
free((*Sub).ch);
}
(*Sub).ch = (char *) malloc((len + 1) * sizeof(char));
if(!(*Sub).ch) {
exit(OVERFLOW);
}
for(i = 0; i < len; i++) {
(*Sub).ch[i] = T.ch[pos + i - 1];
}
(*Sub).ch[i] = '\0';
(*Sub).length = len;
return OK;
}
int Index(HString S, HString T, int pos) {
int i;
HString Sub;
InitString(&Sub);
if(pos < 1 || pos > StrLength(S)) {
return ERROR;
}
for(i = pos; i < StrLength(S); i++) {
SubString(&Sub, S, i, StrLength(T));
if(StrCompare(Sub, T) == 0) {
return i;
}
}
return 0;
}
Status StrInsert(HString *S, int pos, HString T) {
int i, j = StrLength(*S);
if(pos < 1 || pos > StrLength(*S)) {
return ERROR;
}
(*S).ch = (char*) realloc((*S).ch, (StrLength(*S) + StrLength(T)) * sizeof(char));
if((*S).ch) {
exit(OVERFLOW);
}
(*S).length += StrLength(T);
for(; j >= pos; j--) {
i = StrLength(*S);
(*S).ch[i] = (*S).ch[j];
i--;
}
for(i = 1, j = pos; i <= StrLength(T); i++, j++) {
(*S).ch[j] = T.ch[i];
}
return OK;
}
void InitString(HString *T) {
(*T).length=0;
(*T).ch=NULL;
}
void StrPrint(HString T) {
int i;
for(i=0;i<T.length;i++)
printf("%c",T.ch[i]);
printf("\n");
}
void main() {
int i;
HString S1, S2, T, Sub;
char *SS1 = "beijing";
char *SS2 = "jing";
InitString(&S1);
InitString(&S2);
InitString(&T);
InitString(&Sub);
StrAssign(&S1, SS1);
StrAssign(&S2, SS2);
StrPrint(S1);
StrPrint(S2);
////////////////test StrCompare()////////////////////////
printf("%d\n", StrCompare(S1, S2));
/////////////////////////////////////////////////////////
for(i = 0; S1.ch[i] != '\0'; i++) {
printf("%c", S1.ch[i]);
}
printf("\n");
///////////////////test Concat()/////////////////////////
Concat(&T, S1, S2);
StrPrint(T);
////////////////////test SubString()/////////////////////
SubString(&Sub, S1, 1, 3);
StrPrint(Sub);
for(i = 0; Sub.ch[i] != '\0'; i++) {
printf("%c", Sub.ch[i]);
}
printf("\n");
////////////////////test Index()///////////////////////////
printf("%d\n", Index(S1, S2, 1));
////////////////////test StrInsert()//////////////////////
StrInsert(&S1, 1, S2);
StrPrint(S1);
}
/* c4-2.h 串的堆分配存储 */
typedef struct
{
char *ch; /* 若是非空串,则按串长分配存储区,否则ch为NULL */
int length; /* 串长度 */
}HString;
/* c1.h (程序名) */
#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */