有个程序有点问题,希望大家帮忙,本人急需望大家尽快给予帮助
#include<conio.h>
#include <stdio.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{int num;
char name[10];
int score;
char sex;
int age;
struct student *next;
};
struct student listA,listB;
int n;
int sum1=0,sum2=0;
struct student *abh;
main ()
{struct student *creat(void);
struct student *insert(struct student *,struct student*);
void print(struct student*);
struct student*ahead,*bhead,*abh , *p,*pt;
int i,iscore ;
int find=0;
clrscr();
printf("Input listA:\n");
ahead=creat();
sum1=sum1+n;
printf("Input listB:\n");
bhead=creat();
sum2=sum2+n;
abh=insert(ahead,bhead);
printf("abh");
printf("Input score:\n");
scanf("%d",&iscore);
pt=abh;
p=pt;
if(pt->score==iscore);
{p=pt->next;
abh=pt=p;
find=1;
}
pt=pt->next;
while(pt!=NULL)
{if(pt->score==iscore)
{p->next=pt->next;
find=1;
}
else
p=pt;
pt=pt->next;
}
if(!find)
printf("Not find%d",iscore);
p=abh;
printf("\n NO. NAME SCORE SEX AGE(year.month.day)\n");
while(p!=NULL)
{
printf("%10d%6s%6d%3c%3d\n",p->num,p->name,p->score,p->sex,p->age );
p=p->next;
}
}
struct student *creat(void)
{struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("Input num&name&score&sex&age of student:\n");
printf("If num is 0,stopt inputing:\n");
scanf("%d%s%d%c%d\n",&p1->num,p1->name,&p1->score,p1->sex,&p1->age );
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%d%s%d%c%d\n",&p1->num,p1->name,&p1->score,p1->sex,&p1->age );
}
p2->next=NULL;
return(head);
}
struct student*insert(struct student*ah,struct student*bh)
{struct student *pa1,*pa2,*pb1,*pb2;
pa2=pa1=ah;
pb2=pb1=bh;
do
{while((pb1->num>pa1->num)&&(pa1->next!=NULL) )
{pa2=pa1;
pa1=pa1->next;
}
if(pb1->num<=pa1->num)
{if(ah==pa1)
ah=pb1;
else pa2->next=pb1;
pb1=pb1->next;
pb2=pb2->next;
pb2->next=pa1;
pa2=pb2;
pb2=pb1;
}
}while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));
if((pb1->num>pa1->num)&&(pa1->next==NULL))
pa1->next=pb1;
return(abh);
}
void print(struct student*head)
{struct student*p;
printf("\nThere are%d records:\n",sum1+sum2);
p=head;
if(p!=NULL)
do
{printf("%10d%6s%6d%3c%3d",p->num,p->name,p->score,p->sex,p->age );
p=p->next;
}
while(p!=NULL);
}
该程序在运行时有点问题,不能正常的输入数据
问题点数:0、回复次数:1Top
1 楼huigll(会)回复于 2003-07-02 19:07:48 得分 0
//可以输入了,不过insert函数有问题,
#include<conio.h>
#include <stdlib.h>
#include <stdio.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[10];
int score;
char sex;
int age;
struct student *next;
};
struct student *creat(void);
struct student *insert(struct student *,struct student*);
void print(struct student*);
struct student listA,listB;
int n;
int sum1=0,sum2=0;
struct student *abh;
int main ()
{
struct student*ahead,*bhead,*abh , *p,*pt;
int iscore ;
int find=0;
//clrscr();
printf("Input listA:\n");
ahead=creat();
sum1=sum1+n;
printf("Input listB:\n");
bhead=creat();
sum2=sum2+n;
abh=insert(ahead,bhead);
printf("abh");
printf("Input score:\n");
scanf("%d",&iscore);
pt=abh;
p=pt;
if(pt->score==iscore)
{
p=pt->next;
abh=pt=p;
find=1;
}
pt=pt->next;
while(pt!=NULL)
{
if(pt->score==iscore)
{
p->next=pt->next;
find=1;
}
else
p=pt;
pt=pt->next;
}
if(!find)
printf("Not find%d",iscore);
p=abh;
printf("\n NO. NAME SCORE SEX AGE(year.month.day)\n");
while(p!=NULL)
{
printf("%10d%6s%6d%3c%3d\n",p->num,p->name,p->score,p->sex,p->age );
p=p->next;
}
return 0;
}
struct student *creat(void)
{
struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
// printf("Input num&name&score&sex&age of student:\n");
printf("If num is 0,stopt inputing:\n");
printf("num %d:",n+1);
scanf("%d",&p1->num);
printf("Name:");
scanf("%s",p1->name);
fflush(stdin);
printf("score:");
scanf("%d",&p1->score);
fflush(stdin);
printf("Sex:");
p1->sex=getchar();
printf("age:");
scanf("%d",&p1->age);
head=NULL;
while(1)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
//scanf("%d%s%d%c%d\n",&p1->num,p1->name,&p1->score,p1->sex,&p1->age );
printf("num %d:",n+1);
scanf("%d",&p1->num);
if(p1->num==0)
break;//退出循环
printf("Name:");
scanf("%s",p1->name);
fflush(stdin);//清空输入缓冲区
printf("score:");
scanf("%d",&p1->score);
fflush(stdin);
printf("Sex:");
p1->sex=getchar();
printf("age:");
scanf("%d",&p1->age);
}
p2->next=NULL;
free(p1);//p1没有用
return(head);
}
struct student*insert(struct student*ah,struct student*bh)
{
struct student *pa1,*pa2,*pb1,*pb2;
pa2=pa1=ah;
pb2=pb1=bh;
do
{
while((pb1->num>pa1->num)&&(pa1->next!=NULL) )
{
pa2=pa1;
pa1=pa1->next;
}
if(pb1->num<=pa1->num)
{
if(ah==pa1)
ah=pb1;
else
pa2->next=pb1;
pb1=pb1->next;
pb2=pb2->next;
pb2->next=pa1;
pa2=pb2;
pb2=pb1;
}
}while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));
if((pb1->num>pa1->num)&&(pa1->next==NULL))
pa1->next=pb1;
return(abh);
}
void print(struct student*head)
{
struct student*p;
printf("\nThere are%d records:\n",sum1+sum2);
p=head;
if(p!=NULL)
do
{
printf("%10d%6s%6d%3c%3d",p->num,p->name,p->score,p->sex,p->age );
p=p->next;
}while(p!=NULL);
}Top




