关于多项式加法,请教大家了

来源:百度知道 编辑:UC知道 时间:2024/06/24 12:35:49
以二元组方式从文件输入,如(2,2)为2X^2;
以链表形式存放;
这是我写的程序计算函数不太会,请大家帮忙改一下,谢谢了:
#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;
}
struct STD *creat2(void)
{
struct STD *head=NULL;
str

说明:IN1.txt中多项式按降幂排列,a为系数,b为指数(幂次),p2输入时也应按降幂输入,以下为代码,用wintc编译运行成功

#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#define LEN sizeof(struct STD)

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;
int t;
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));
/*printf("%d %d;",pN->a,pN->b) ;
scanf("%d ",&t); */
}

pT->next=pN;
pT=pN;