求指导-----单链表实现多项式表示,,,运行出现“内存不能为read”

caili_xi 2012-07-01 08:09:56
#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;
}
...全文
43 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
caili_xi 2012-07-01
  • 打赏
  • 举报
回复
嗯,已经找到问题

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧