C语言中多项式加法问题

来源:百度知道 编辑:UC知道 时间:2024/09/24 01:42:01
在下是初学者,请教大家了
他的要求是:
多项式以二元组的方式从文件输入。如多项式4x^2+3s^5则输入:
(4,2)(3,5)。以链表的形式存放多项式,相加后的多项式以二元组的形式输出到屏幕上。
这是我写的程序,相加的函数不对,请大家改一下谢谢了,明天就要交了~~
#define LEN sizeof(struct STD)
#define NULL 0
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
struct STD
{
int a;
int b;
struct STD *next;
};
int V;
struct STD *creat1(void)
{
FILE *fp;
struct STD *pN,*pT;
struct STD *head=NULL;
pT=pN=(struct STD*)malloc(LEN);
fp=fopen("D:\\IN1.TXT","r");
if (fp==NULL) printf("NULL\n");
fscanf(fp,"(%d,%d)",&pN->a,&pN->b);
while(pN->a!=0)
{
if (head==NULL) head=pN;
else pT->next=pN;
pT=pN;
pN=(struct STD*)malloc(LEN);
fscanf(fp,"(%d,%d)",&pN->a,&pN->b);
}
pT->next=NULL;
V=pT->b;
fclose(fp);
return head;

/*create1()函数和count()函数都有些问题,我把这两个函数改了一下*/

#define LEN sizeof(struct STD)
#define NULL 0
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
struct STD
{
int a;
int b;
struct STD *next;
};
int V;
struct STD *creat1(void)
{
FILE *fp;
struct STD *pN,*pT,*p;
struct STD *head=NULL;
pT=pN=(struct STD*)malloc(LEN);
fp=fopen("D:\\IN1.TXT","r");
if (fp==NULL)
{
printf("NULL\n");
return NULL;
}
while(!feof(fp))
{
fscanf(fp,"(%d,%d)",&pN->a,&pN->b);
/*if(pN->a!=0)
{ */
if (head==NULL) head=pN;
else pT->next=pN;
p=pT;
pT=pN;
pN=(struct STD*)malloc(LEN);
/*fscanf(fp,"(%d,%d)",&pN->a,&pN->b); */
}
if(head!=NULL)
p->next=NULL;
V=pT->b;
fclose(fp);
retu