/*编译时报错,把文件改成cpp后缀,编译通过并且可以运行,底下是代码,最下面是报错信息*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LISTINCREMENT 10
#define LIST_INIT_SIZE 100
typedef int Status;
typedef struct
{
char name[100];
}ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList(SqList *L)
{
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
Status ListInsert(SqList *L,int i,ElemType e)
{
ElemType *newbase,*p,*q;
if(i<1||i>L->length+1) return ERROR;
if(L->length>=L->listsize)
{
newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
q=L->elem+i-1;
for(p=L->elem+L->length-1;p>=q;--p) *(p+1)=*p;
*q=e;
++L->length;
return OK;
}
Status ListDelete(SqList *L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1||i>L->length) return ERROR;
p=L->elem+i-1;
*e=*p;
q=L->elem+L->length-1;
for(++p;p<=q;p++) *(p-1)=*p;
L->length--;
return OK;
}
int LocateElem(SqList *L,ElemType e,Status (*compare)(ElemType,ElemType))
{
ElemType *p;
int i=1;
p=L->elem;
while(i<=L->length&&!compare(*p++,e)) ++i;
if(i<=L->length) return i;
else return 0;
}
Status cmp(ElemType e1, ElemType e2)
{
return (Status)!strcmp(e1.name,e2.name);
}
void ListShow(SqList *L)
{
int i;
for(i=0;i<L->length;i++)
{
if(i) printf(" ");
printf("%s",L->elem[i].name);
}
printf("\n");
}
int main()
{
//SqList *namelist=(SqList *)malloc(sizeof(SqList));
SqList namelist;
InitList(&namelist);
char strInstruct[10];
int pos;
ElemType e;
while(~scanf("%s",strInstruct))
{
if(!strcmp(strInstruct,"insert"))
{
scanf("%d%s",&pos,e.name);
ListInsert(&namelist,pos,e);
}
else if(!strcmp(strInstruct,"show"))
{
ListShow(&namelist);
}
else if(!strcmp(strInstruct,"delete"))
{
scanf("%s",e.name);
pos=LocateElem(&namelist,e,cmp);
ListDelete(&namelist,pos,&e);
}
else if(!strcmp(strInstruct,"search"))
{
scanf("%s",e.name);
printf("%d\n",LocateElem(&namelist,e,cmp));
}
}
return 0;
}//C编译时报错,把文件改成cpp后缀,编译通过并且可以运行,以下是错误代码
BigBang.c e:\数据结构\sjjg\bigbang\bigbang.c(85) : error C2143: syntax error : missing ";" before "type" e:\数据结构\sjjg\bigbang\bigbang.c(86) : error C2143: syntax error : missing ";" before "type" e:\数据结构\sjjg\bigbang\bigbang.c(87) : error C2275: "ElemType" : illegal use of this type as an expression e:\数据结构\sjjg\bigbang\bigbang.c(13) : see declaration of "ElemType" e:\数据结构\sjjg\bigbang\bigbang.c(87) : error C2146: syntax error : missing ";" before identifier "e" e:\数据结构\sjjg\bigbang\bigbang.c(87) : error C2065: "e" : undeclared identifier e:\数据结构\sjjg\bigbang\bigbang.c(88) : error C2065: "strInstruct" : undeclared identifier e:\数据结构\sjjg\bigbang\bigbang.c(90) : warning C4047: "function" : "const char *" differs in levels of indirection from "int " e:\数据结构\sjjg\bigbang\bigbang.c(90) : warning C4024: "strcmp" : different types for formal and actual parameter 1 e:\数据结构\sjjg\bigbang\bigbang.c(92) : error C2065: "pos" : undeclared identifier e:\数据结构\sjjg\bigbang\bigbang.c(92) : error C2224: left of ".name" must have struct/union type e:\数据结构\sjjg\bigbang\bigbang.c(93) : error C2115: "function" : incompatible types e:\数据结构\sjjg\bigbang\bigbang.c(93) : warning C4024: "ListInsert" : different types for formal and actual parameter 3 e:\数据结构\sjjg\bigbang\bigbang.c(95) : warning C4047: "function" : "const char *" differs in levels of indirection from "int " e:\数据结构\sjjg\bigbang\bigbang.c(95) : warning C4024: "strcmp" : different types for formal and actual parameter 1 e:\数据结构\sjjg\bigbang\bigbang.c(99) : warning C4047: "function" : "const char *" differs in levels of indirection from "int " e:\数据结构\sjjg\bigbang\bigbang.c(99) : warning C4024: "strcmp" : different types for formal and actual parameter 1 e:\数据结构\sjjg\bigbang\bigbang.c(101) : error C2224: left of ".name" must have struct/union type e:\数据结构\sjjg\bigbang\bigbang.c(102) : error C2115: "function" : incompatible types e:\数据结构\sjjg\bigbang\bigbang.c(102) : warning C4024: "LocateElem" : different types for formal and actual parameter 2 e:\数据结构\sjjg\bigbang\bigbang.c(103) : warning C4133: "function" : incompatible types - from "int *" to "struct ElemType *" e:\数据结构\sjjg\bigbang\bigbang.c(105) : warning C4047: "function" : "const char *" differs in levels of indirection from "int " e:\数据结构\sjjg\bigbang\bigbang.c(105) : warning C4024: "strcmp" : different types for formal and actual parameter 1 e:\数据结构\sjjg\bigbang\bigbang.c(107) : error C2224: left of ".name" must have struct/union type e:\数据结构\sjjg\bigbang\bigbang.c(108) : error C2115: "function" : incompatible types e:\数据结构\sjjg\bigbang\bigbang.c(108) : warning C4024: "LocateElem" : different types for formal and actual parameter 2 执行 cl.exe 时出错.
解决方案
10
上面讲的已经相当明确了。
int main()
{
//SqList *namelist=(SqList *)malloc(sizeof(SqList));
SqList namelist;
char strInstruct[10];
int pos;
ElemType e;
InitList(&namelist);
while(~scanf("%s",strInstruct))
{
if(!strcmp(strInstruct,"insert"))
{
scanf("%d%s",&pos,e.name);
ListInsert(&namelist,pos,e);
}
else if(!strcmp(strInstruct,"show"))
{
ListShow(&namelist);
}
else if(!strcmp(strInstruct,"delete"))
{
scanf("%s",e.name);
pos=LocateElem(&namelist,e,cmp);
ListDelete(&namelist,pos,&e);
}
else if(!strcmp(strInstruct,"search"))
{
scanf("%s",e.name);
printf("%d\n",LocateElem(&namelist,e,cmp));
}
}
return 0;
}//C编译时报错,把文件改成cpp后缀,编译通过并且可以运行,以下是错误代码
10
//SqList *namelist=(SqList *)malloc(sizeof(SqList));
SqList namelist;
SqList namelist;
char strInstruct[10];
int pos;
ElemType e;
InitList(&namelist);
这样编译通过 你试试