69,369
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct equ)
struct equ
{
int coe;
int exp;
struct equ*next;
};
(void)
分号前面差着一个equ
改为include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct equ)
struct equ
{
int coe;
int exp;
struct equ*next;
}equ;
还有就是可能是在定义的时候过于混乱了,回归用的是时候就不太准确,建议可以减少一点定义的用量。
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(muleqt)
typedef struct muleqt
{
int a,n;
struct muleqt *next;
}muleqt;
//a1x^n1+a2x^n2...
muleqt *build(int n)
{
int i;
muleqt *p1,*p2,*head;
head=malloc(LEN);
p1=p2=head;
for (i=1; i<n; i++)
{
scanf("%d %d",&p1->a,&p1->n);
p2=malloc(LEN);
p1->next=p2;
p1=p2;
}
if (n!=0) scanf("%d %d",&p1->a,&p1->n);
p1->next=NULL;
return head;
}
void print(muleqt *head)
{
muleqt *p=head;
for (; p->a==0; p=p->next) ;
if (p->a!=0 && p->n!=0)
{
if(p->a!=1)printf("%dx^%d",p->a,p->n);
else printf("x^%d",p->n);
}
else if (p->n==0) printf("%d",p->a);
p=p->next;
while (p!=NULL)
{
if (p->a!=0)
{
if (p->a<0) printf("%dx",p->a);
else if (p->a==1)
{
if (p->n!=0) printf("+x");
if (p->n==0) printf("+1");
}
else if (p->a>1) printf("+%dx",p->a);
if (p->n!=0 && p->n!=1) printf("^%d",p->n);
}
p=p->next;
}
}
int main()
{
int n;
muleqt *head;
scanf("%d",&n);
head=build(n);
print(head);
return 0;
}
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct equ)
struct equ
{
int coe;
int exp;
struct equ*next;
};
struct equ*create(void)
{
int i,n;
struct equ *head = NULL;
struct equ *pNew,*pTail;
scanf("%d",&n);
pTail = pNew=(struct equ*)malloc(LEN);
if (!pTail)
exit(0);
scanf("%d%d", &pNew->coe,&pNew->exp);
for(i=0;i<n;i++)
{
if(head==NULL) head=pNew;
else
pTail->next = pNew;
pTail = pNew;
pNew = (struct equ*)malloc(LEN);
if (!pNew)
exit(0);
scanf("%d%d", &pNew->coe,&pNew->exp);
}
//pNew->next=NULL;
free(pNew);
pTail->next = NULL;
return head;
}
void output(struct equ *head)
{
struct equ*p=head;
while(p!=NULL)
{
if (p->exp!=0)
printf("%dx^%d",p->coe,p->exp);
else
printf("%d",p->coe);
p = p->next;
if (p != NULL)
printf("+");
}
}
int main()
{
int i,j,k;
struct equ *e;
e=create();
/*
scanf("%d",&k);
for(i=0;i<k;i++)
scanf("%d%d",&e->coe,&e->exp);
*/
output(e);
return 0;
}
参考一下吧
链表最后一个节点处理的不对。最后申请的空间并没有用上,加到链表的最后一个节点的next要指向NULL;
main(注意单词拼写)函数内的code有冗余,建议去掉。struct equ*create(void)
{
int i,n;
scanf("%d",&n);
struct equ *head = NULL;
struct equ *pNew,*pTail;
pTail=pNew=(struct equ*)malloc(LEN);
scanf("%d%d",&pNew->coe,&pNew->exp);
for(i=0;i<n;i++)
{
pNew->next=NULL;//添加本行
if(head==NULL) head=pNew;
else pTail->next=pNew;
pTail=pNew;
pNew=(struct equ*)malloc(LEN);
scanf("%d%d",&pNew->coe,&pNew->exp);
}
//pNew->next=NULL;//注释掉本行,将该行改到for循环中
return head;
}