Dev c++ 编程 出现停止工作
如题下面是我的代码,刚学线性表,写的很糙,大家多包涵。#include<stdio.h>#include<stdlib.h>typedefstructLnode{intc...
如题
下面是我的代码,刚学线性表,写的很糙,大家多包涵。
#include<stdio.h>
#include<stdlib.h>
typedef struct Lnode
{
int co;
int exp;
struct Lnode *next;
} LNode, *LinkList;
void creat( LinkList, int );
LinkList add( LinkList, LinkList );
void output( LinkList );
int main(void)
{
int func,n1,n2,n3;
LinkList pa, pb, pc, pd, pe;
scanf("%d",&func); //输入功能选择编号,当func为0时退出
if( 1 == func )
{
scanf("%d",&n1);
creat( pa, n1 );
scanf("%d",&n2);
creat( pb, n2 );
scanf("%d",&n3);
creat( pc, n3 );
pd = add( pa, pb );
pe = add( pd, pc );
output( pa );
output( pb );
output( pc );
output( pd );
output( pe );
}
}
void creat( LinkList p, int n) //线性表储存一元多项式
{
int c, e, i;
LinkList first, s, rear;
first = ( LinkList )malloc(sizeof(LNode));
rear = first;
for(i=1;i<=n;i++)
{
s = ( LinkList )malloc(sizeof(LNode));
scanf("%d%d",&c,&e);
s->co = c;
s->exp = e;
rear->next = s;
rear = s;
rear->next = NULL;
}
}
LinkList add( LinkList pa, LinkList pb ) //两个多项式相加
{
LinkList first, c, rear;
first = ( LinkList )malloc(sizeof(LNode));
rear = first;
rear->next = NULL;
pa = pa->next;
pb = pb->next;
while(pa&&pb)
{
if( pa->exp == pb->exp ) //指数相同时
{
c = ( LinkList )malloc(sizeof(LNode));
c->exp = pa->exp;
c->co = pa->co + pb->co;
rear->next = c;
rear = c;
pa = pa->next;
pb = pb->next;
}
else if( pa->exp < pb->exp) //指数不同时
{
c = ( LinkList )malloc(sizeof(LNode));
c->exp = pa->exp;
c->co = pa->exp;
rear->next = c;
rear = c;
pa = pa->next;
}
else
{
c = ( LinkList )malloc(sizeof(LNode));
c->exp = pb->exp;
c->co = pb->co;
rear->next = c;
rear = c;
pb = pb->next;
}
rear->next = NULL;
return first;
}
while(pa&&!pb)
{
if( c->next != NULL )
{
c = c->next;
}
c->next = pa;
}
while(!pa&&pb)
{
if( c->next != NULL )
{
c = c->next;
}
c->next = pb;
}
return first;
}
void output( LinkList p )
{
while( p->next != NULL )
{
p = p->next;
printf("<%d,%d>", p->co, p->exp);
printf("\n");
}
}
编译时提示的警告:
main.c: In function 'main':
main.c:19:7: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&func); //输入功能选择编号,当func为0时退出 (n1/n2/n3,creat里的c/e也有这个警告)
^
main.c: In function 'main':
main.c:23:8: warning: 'pa' may be used uninitialized in this function [-Wmaybe-uninitialized]
creat( pa, n1 ); (pb、pc也有这个警告)
^
拜托大家了 展开
下面是我的代码,刚学线性表,写的很糙,大家多包涵。
#include<stdio.h>
#include<stdlib.h>
typedef struct Lnode
{
int co;
int exp;
struct Lnode *next;
} LNode, *LinkList;
void creat( LinkList, int );
LinkList add( LinkList, LinkList );
void output( LinkList );
int main(void)
{
int func,n1,n2,n3;
LinkList pa, pb, pc, pd, pe;
scanf("%d",&func); //输入功能选择编号,当func为0时退出
if( 1 == func )
{
scanf("%d",&n1);
creat( pa, n1 );
scanf("%d",&n2);
creat( pb, n2 );
scanf("%d",&n3);
creat( pc, n3 );
pd = add( pa, pb );
pe = add( pd, pc );
output( pa );
output( pb );
output( pc );
output( pd );
output( pe );
}
}
void creat( LinkList p, int n) //线性表储存一元多项式
{
int c, e, i;
LinkList first, s, rear;
first = ( LinkList )malloc(sizeof(LNode));
rear = first;
for(i=1;i<=n;i++)
{
s = ( LinkList )malloc(sizeof(LNode));
scanf("%d%d",&c,&e);
s->co = c;
s->exp = e;
rear->next = s;
rear = s;
rear->next = NULL;
}
}
LinkList add( LinkList pa, LinkList pb ) //两个多项式相加
{
LinkList first, c, rear;
first = ( LinkList )malloc(sizeof(LNode));
rear = first;
rear->next = NULL;
pa = pa->next;
pb = pb->next;
while(pa&&pb)
{
if( pa->exp == pb->exp ) //指数相同时
{
c = ( LinkList )malloc(sizeof(LNode));
c->exp = pa->exp;
c->co = pa->co + pb->co;
rear->next = c;
rear = c;
pa = pa->next;
pb = pb->next;
}
else if( pa->exp < pb->exp) //指数不同时
{
c = ( LinkList )malloc(sizeof(LNode));
c->exp = pa->exp;
c->co = pa->exp;
rear->next = c;
rear = c;
pa = pa->next;
}
else
{
c = ( LinkList )malloc(sizeof(LNode));
c->exp = pb->exp;
c->co = pb->co;
rear->next = c;
rear = c;
pb = pb->next;
}
rear->next = NULL;
return first;
}
while(pa&&!pb)
{
if( c->next != NULL )
{
c = c->next;
}
c->next = pa;
}
while(!pa&&pb)
{
if( c->next != NULL )
{
c = c->next;
}
c->next = pb;
}
return first;
}
void output( LinkList p )
{
while( p->next != NULL )
{
p = p->next;
printf("<%d,%d>", p->co, p->exp);
printf("\n");
}
}
编译时提示的警告:
main.c: In function 'main':
main.c:19:7: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&func); //输入功能选择编号,当func为0时退出 (n1/n2/n3,creat里的c/e也有这个警告)
^
main.c: In function 'main':
main.c:23:8: warning: 'pa' may be used uninitialized in this function [-Wmaybe-uninitialized]
creat( pa, n1 ); (pb、pc也有这个警告)
^
拜托大家了 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询