c程序 找错
解决时间 2021-07-20 07:37
- 提问者网友:謫仙
- 2021-07-19 18:07
#include "stdio.h"
#define list_init_size 100
#define listincreament 10
#define ElemType int
enum status{ok,fail};
typedef struct{
ElemType *elem;
int length;
int listsize;
}sqlist;
status initlist_sq(sqlist *L)
{
(*L).elem=(ElemType*)malloc(list_init_size*sizeof(ElemType));
if(!L->elem) {return fail; exit(0);}
L->length=0;
L->listsize=list_init_size;
return(ok);
}
status listinsert_sq(sqlist *L,int i,ElemType e)
{
sqlist *newbase;
int *p,*q;
if(i<1||i>L->length+1) {return fail;exit(1);}
if(L->length>=L->listsize)
{
newbase=(ElemType*)realloc(L->elem,(L->listsize+listincreament)*sizeof(ElemType));
if(!newbase) {exit(1);return fail;}
L->list=newbase;
L->listsize+=listincreament;
}
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_sq(sqlist *L,int i,ElemType *e)
{
int *p,*q;
if(i<1||i>(*L).length) {return fail;exit(2);}
p=&L->elem[i-1];
q=&L->elem[length-1];
e=p;
for(++p;p<=q;++p) *(p-1)=*p;
--L->length;
return ok;
}
void main()
{
sqlist *L;char e,k;int i;enum status m;
m=initlist_sq(*L);
if(m!="ok")
{
printf("not successfull\n");
getchar();
exit();
}
printf("please input the dataes:\n");
i=1;
while(1)
{
scanf("%s",&e);
getchar();
m=listinsert_sq(*L,i,e);
i++;
if(m==fail){printf("not successfull\n");exit();}
printf("please input stop or continue\n");
scanf("%s",&k);
getchar();
if(k=="stop") break;
}
printf("you want insert the data y or n ?\n");
scanf("%c",&k);
if(k=='y')
while(1)
{
printf("data:e=");
scanf("%s",&e);
printf("\n");
printf("location:i=");
scanf("%d",&i);
printf("\n");
m=listinsert_sq(*L,i,e);
if(m!="ok"){printf("not successful\n");goto loop1;}
printf(" is successfull\n");
printf("you want to continue insert other data y or n?\n");
scanf("%c",&k);
if(k!='y') break;
}
loop1:printf(" you want to delete the data y or n?\n");
scanf("%c",&k);
if(k=='y')
loop2:
while(1)
{
printf("i=");
scanf("%d",&i);
m=listdelete_sq(*L,i,&e);
if(m!="ok"){printf("not successful\n");goto loop2;}
printf("%s",e);
printf("is deleted\n");
printf("you want to continue todelete other data y or n?\n");
scanf("%c",&k);
if(k!='y') break;
}
printf("over\n");
}
请各位看一下 实在没辙了
最佳答案
- 五星知识达人网友:十年萤火照君眠
- 2021-07-19 18:43
没有相应的数据类型,建议你用typedef关键字替换掉早不到的数据类型
全部回答
- 1楼网友:琴狂剑也妄
- 2021-07-19 20:01
status initlist_sq(sqlist *L) 这几个函数定义的时候不要用status 用int,一共有3个这样顶一的函数,返回值全换成int
m!="ok") 这里不应该加双引号
if(k=="stop") break; 哪个老师教你这样写的? k是个字符不是字符串,能跟字符串比较吗? 另外字符串也不是用==比较的,用strcmp啊。,你那里居然能编译过去?
自己先改一下,一会给你发出正确代码来
#include "stdio.h"
#include "stdlib.h"
#include <string.h>
#define list_init_size 100
#define listincreament 10
#define ElemType char
enum status{ok,fail};
typedef struct{
ElemType *elem;
int length;
int listsize;
}sqlist;
enum status initlist_sq(sqlist *L)
{
(*L).elem=(ElemType*)malloc(list_init_size*sizeof(ElemType));
if(!L->elem) return fail;
else
{
L->length=0;
L->listsize=list_init_size;
return(ok);
}
}
enum status listinsert_sq(sqlist *L,int i,ElemType e)
{
char *newbase;
char *p,*q;
if(i<1||i>L->length+1) return fail;
else
{
if(L->length>=L->listsize)
{
newbase=(ElemType*)realloc(L->elem,(L->listsize+listincreament)*sizeof(ElemType));
if(!newbase) {return fail;exit(1);}
L->elem=newbase;
L->listsize+=listincreament;
}
q=&L->elem[i-1];
for(p=&L->elem[L->length-1];p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return (ok);
}
}
enum status listdelete_sq(sqlist *L,int i,ElemType *e)
{
char *p,*q;
if(i<1||i>(*L).length) return fail;
else
{
p=&L->elem[i-1];
q=&L->elem[L->length-1];
e=p;
for(++p;p<=q;++p) *(p-1)=*p;
--L->length;
return ok;
}
}
void main()
{
sqlist *L;char e,k,stop[10]={0};int i;
enum status m;
L=new sqlist;
m=initlist_sq(L);
if(m!=ok)
{
printf("not successfull\n");
getchar();
exit(0);
}
i=1;
while(1)
{
printf("please input the dataes:\n");
scanf("%s",&e);
getchar();
m=listinsert_sq(L,i,e);
i++;
if(m==fail){printf("not successfull\n");return;}
printf("please input stop or continue\n");
scanf("%s",stop);
getchar();
if(strcmp(stop,"stop")==0)
break;
}
printf("you want insert the data y or n ?\n");
scanf("%c",&k);
if(k=='y')
while(1)
{
printf("data:e=");
scanf("%s",&e);
printf("\n");
printf("location:i=");
scanf("%d",&i);
printf("\n");
m=listinsert_sq(L,i,e);
if(m!=ok){printf("not successful\n");goto loop1;}
printf(" is successfull\n");
printf("you want to continue insert other data y or n?\n");
scanf("%c",&k);
if(k!='y') break;
}
loop1:printf(" you want to delete the data y or n?\n");
scanf("%c",&k);
if(k=='y')
{
loop2:
while(1)
{
printf("i=");
scanf("%d",&i);
m=listdelete_sq(L,i,&e);
if(m!='ok'){printf("not successful\n");goto loop2;}
printf("%s",e);
printf("is deleted\n");
printf("you want to continue todelete other data y or n?\n");
scanf("%c",&k);
if(k!='y') break;
}
}
printf("over\n");
}
程序里面有逻辑错误,只是可以运行了,具体那里少做了什么处理你自己试着找一找吧
- 2楼网友:蓝房子
- 2021-07-19 19:36
编译错误都给你耐着性子揪出来了,不过在查错的同时发现你这个代码的逻辑有很大问题,很多地方甚至是一些没有道理的错误。代码给你贴出来,不过要在你这个代码的基础上改正确真是一大挑战,我劝你还是重写一个好了。。。
#include "stdio.h"
#define list_init_size 100
#define listincreament 10
#define ElemType int
enum status{ok,fail};
typedef struct{
ElemType *elem;
int length;
int listsize;
}sqlist;
status initlist_sq(sqlist *&L)
{
(*L).elem=(ElemType*)malloc(list_init_size*sizeof(ElemType));
if(!L->elem) {return fail; exit(0);}
L->length=0;
L->listsize=list_init_size;
return(ok);
}
status listinsert_sq(sqlist *L,int i,ElemType e)
{
sqlist *newbase;
int *p,*q;
if(i<1||i>L->length+1) {return fail;exit(1);}
if(L->length>=L->listsize)
{
newbase=(sqlist*)realloc(L->elem, (L->listsize+listincreament)*sizeof(ElemType));
if(!newbase) {exit(1);return fail;}
L=newbase;
L->listsize+=listincreament;
}
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_sq(sqlist *L,int i,ElemType *e)
{
int *p,*q;
if(i<1||i>(*L).length) {return fail;exit(2);}
p=&L->elem[i-1];
q=&L->elem[L->length-1];
e=p;
for(++p;p<=q;++p) *(p-1)=*p;
--L->length;
return ok;
}
void main()
{
sqlist *L;char e,c; char k[100];int i,u;enum status m;
m=initlist_sq(L);
if(m!=ok)
{
printf("not successfull\n");
getchar();
exit(1);
}
printf("please input the dataes:\n");
i=1;
while(1)
{
scanf("%s",&e);
getchar();
m=listinsert_sq(L,i,e);
i++;
if(m==fail){printf("not successfull\n");exit(1);}
printf("please input stop or continue\n");
scanf("%s",&k);
getchar();
if(strcmp(k,"stop")==0) break;
}
printf("you want insert the data y or n ?\n");
scanf("%c",&c);
if(c=='y')
while(1)
{
printf("data:e=");
scanf("%d",&u);
printf("\n");
printf("location:i=");
scanf("%d",&i);
printf("\n");
m=listinsert_sq(L,i,u);
if(m!=ok){printf("not successful\n");goto loop1;}
printf(" is successfull\n");
printf("you want to continue insert other data y or n?\n");
scanf("%c",&c);
if(c!='y') break;
}
loop1:printf(" you want to delete the data y or n?\n");
scanf("%c",&c);
if(c=='y')
loop2:
while(1)
{
printf("i=");
scanf("%d",&i);
m=listdelete_sq(L,i,&u);
if(m!=ok){printf("not successful\n");goto loop2;}
printf("%c",e);
printf("is deleted\n");
printf("you want to continue todelete other data y or n?\n");
scanf("%c",&c);
if(c!='y') break;
}
printf("over\n");
}
我要举报
大家都在看
推荐资讯