永发信息网

c程序 找错

答案:3  悬赏:70  手机版
解决时间 2021-07-20 07:37

#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");
}

请各位看一下 实在没辙了

最佳答案

没有相应的数据类型,建议你用typedef关键字替换掉早不到的数据类型

全部回答

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"); }

程序里面有逻辑错误,只是可以运行了,具体那里少做了什么处理你自己试着找一找吧

编译错误都给你耐着性子揪出来了,不过在查错的同时发现你这个代码的逻辑有很大问题,很多地方甚至是一些没有道理的错误。代码给你贴出来,不过要在你这个代码的基础上改正确真是一大挑战,我劝你还是重写一个好了。。。 #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"); }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
倪宝铎的籍贯
杭州哪里有电视剧、电影短片后期包装强化班?
金融危机过了,房价还会跌吗?
转区问题!!!!!!!
听说拍拍钻石级买家每月能领40元红包 请问怎
41狂战带什么太刀??
散文 站在.........的门口 800字左右
怎样才能快速学会骑单车?
2010年贵阳奥林匹克化学竞赛报名时间及
PSP最便宜多少钱啊
哪位朋友帮忙起个名!
用什么建议的方法可以祛除痘印啊?
问问积分、经验、级别算法和用处?知道的友友
为什么电脑不动就会花屏
物体的移动速度可否叠加
推荐资讯
女人更年期一般是什么时候到来??
微信朋友找我借了700元,拉黑了可以报警吗?
关于在工厂内受伤如何理赔
中国武术是不是花拳绣腿
一起来看流星雨结局不怎么好
我问她你希望在我朋友面前怎么介绍你,女朋友
识别以营销为导向的组织的主要特征
英语翻译We refer to the Agreement made as
我的任务列表怎么没有鸡毛信件任务
为什么我把TXT格式的文档保存在手机里~用电子
胖胖的女生真的不讨人喜欢吗?真的不漂亮吗?
经济学专业与英语有联系没
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?