一个C语言的逻辑错误,求大神
#include<Stdio.h>#include<stdlib.h>#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#d...
#include<Stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef struct{
char name[10];
int num;
int score;
}ElemType;
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) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
Status ListInsert_Sq(SqList &L,int i,ElemType e){
ElemType *newbase,*q,*p;
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_Sq(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_Sq(SqList L,int a,Status(*compare)(int,int)){
int i;
ElemType *p;
i=1;
p=L.elem;
while(i<=L.length&&!(*compare)(p->score,a))
{
++i;
p++;
}
if(i<=L.length)
return i;
else return 0;
}
int Equal(int a,int b){
if(a==b)
return OK;
}
void GetOut()
{
SqList L;
int i;
ElemType *p,e;
p=L.elem;
for(i=1;i<=5;i++)
{
e=*p;
printf("学号:%d 姓名:%s 成绩:%d\n",e.num,e.name,e.score);
p++;
}
}
int main(){
SqList L;
int i,j;
ElemType e,*p;
InitList_Sq(L);
p=L.elem;
for(i=1;i<=5;i++)
{
ListInsert_Sq(L,i,e);
scanf("%d%d%s",&p->num,&p->score,p->name);
p++;
}
j=LocateElem_Sq(L,100,Equal);
ListDelete_Sq(L,j,e);
GetOut();
return 0;
}
题目要求是输入5位学生的姓名学号成绩,删除成绩为100的学生的信息,再把剩下所有学生的信息输出 展开
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef struct{
char name[10];
int num;
int score;
}ElemType;
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) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
Status ListInsert_Sq(SqList &L,int i,ElemType e){
ElemType *newbase,*q,*p;
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_Sq(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_Sq(SqList L,int a,Status(*compare)(int,int)){
int i;
ElemType *p;
i=1;
p=L.elem;
while(i<=L.length&&!(*compare)(p->score,a))
{
++i;
p++;
}
if(i<=L.length)
return i;
else return 0;
}
int Equal(int a,int b){
if(a==b)
return OK;
}
void GetOut()
{
SqList L;
int i;
ElemType *p,e;
p=L.elem;
for(i=1;i<=5;i++)
{
e=*p;
printf("学号:%d 姓名:%s 成绩:%d\n",e.num,e.name,e.score);
p++;
}
}
int main(){
SqList L;
int i,j;
ElemType e,*p;
InitList_Sq(L);
p=L.elem;
for(i=1;i<=5;i++)
{
ListInsert_Sq(L,i,e);
scanf("%d%d%s",&p->num,&p->score,p->name);
p++;
}
j=LocateElem_Sq(L,100,Equal);
ListDelete_Sq(L,j,e);
GetOut();
return 0;
}
题目要求是输入5位学生的姓名学号成绩,删除成绩为100的学生的信息,再把剩下所有学生的信息输出 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询