求指导-----单链表实现多项式表示,,,运行出现“内存不能为read”
#include <cstdlib>
#include <cstdio>
#include <iostream>
struct Node;
typedef struct Node *Polynomial;
typedef Polynomial List;
typedef Polynomial Position;
#define POLYA_FILE "./Poly1.txt"//多项式的系数和指数存在这里
#define POLYB_FILE "./Poly2.txt"
const int AScope = 3;
const int BScope = 4;
#define MaxScope (AScope>BScope? AScope:BScope)
struct Node
{
int Coefficient;
int Exponent;
Polynomial Next;
};
void ReadFile(char* FileName,int length,int* itemA,int* itemB)
{
int i;
FILE *infile;
errno_t err;
err = fopen_s(&infile,FileName, "r+");
if( err == 1 ) {
std::cout<< "The file "<<FileName <<"was not opened!";
}
for (i=0; i<length; i++)
{
fscanf(infile,"%d %d",&itemA[i],&itemB[i]);
}
fclose(infile);
}
List MakeEmpty()
{
List L;
//L = (List)malloc(sizeof(struct Node));
L = new Node;
if(L==NULL){
exit(1);
}
L->Next = NULL;
return L;
}
/*--Insert after P---*/
void Insert(int Coefficient,int Exponent, List L, Position P )
{
Position TemCell;
//TemCell = (Position)malloc(sizeof(struct Node));
TemCell = new Node;
if (TemCell == NULL)
{
exit(1);
}
TemCell ->Coefficient = Coefficient;
TemCell ->Exponent = Exponent;
P->Next= TemCell;
}
/*---*/
void DisplayPoly(List L)
{
Position P;
P=L->Next;
while (NULL != P)
{
std::cout<<P->Coefficient<<"X^"<<P->Exponent<<std::endl;
P=P->Next;
}
}
void CreatPoly(List Poly,int* Coefficient,int* Exponent,int Scope)
{
int i;
Position Use;
//从头结点后插入
Use = Poly;
for (i=0;i<Scope; i++)
{
Insert(Coefficient[i], Exponent[i], Poly, Use );
Use=Use->Next;
//std::cout<<Use->Next->Coefficient<<","<<Use->Next->Exponent<<std::endl;
}
}
int main()
{
int i;
List PolyA,PolyB;
Position Use;
int Coeff[MaxScope],Exp[MaxScope];
//---生成多项式PolyA
ReadFile(POLYA_FILE,AScope,Coeff,Exp);
PolyA= MakeEmpty();
CreatPoly(PolyA, Coeff,Exp, AScope);///
DisplayPoly(PolyA);
//---生成多项式PolyB
return 0;
}