写了个书本上的作业题,就是多项式相加的数据结构题
但是问题出来了,在vc6下跑的好好的,到devcpp下报错,codeblocks下也报错,vs05下还报错!(但上述环境调试时结果却都正确)
郁闷了一个周末,还是没有解决,只得把程序贴出来,望有高人指点
程序如下(有一些注释掉的无用代码并未删除,还有就是复制进来时EDN的blog会自动填一些""之类的符号,也不知为何):
#include
#include
#include
struct node
{
int exponent;
float coefficient;
struct node *next;
} ;
void Init( struct node *&chainToInit)
{
chainToInit=( struct node*)malloc(sizeof( struct node *));
(chainToInit)->exponent=0;
(chainToInit)->coefficient=0;
(chainToInit)->next=NULL;
}
void Insert(struct node *&chainToInsert,int exponentToInsert,float coefficientToInsert)
{
float sum="1";
struct node *head=chainToInsert;
struct node *temporaryNode=NULL;
//struct node *temporaryNode2=NULL;
struct node *precursorOfTheNode=chainToInsert;
if(exponentToInsert<(chainToInsert)->exponent)
{
temporaryNode=( struct node*)malloc(sizeof( struct node *));
temporaryNode->exponent=exponentToInsert;
temporaryNode->coefficient=coefficientToInsert;
temporaryNode->next=chainToInsert;
chainToInsert="temporaryNode";
chainToInsert= temporaryNode;
}
else if(exponentToInsert==(chainToInsert)->exponent)
{
(chainToInsert)->coefficient+=coefficientToInsert;
}
else if(exponentToInsert>(chainToInsert)->exponent)
{
while((exponentToInsert>(chainToInsert)->exponent)&&((chainToInsert)->next)!=NULL)
{
precursorOfTheNode="chainToInsert";
chainToInsert=(chainToInsert)->next;
}
if((chainToInsert)->next==NULL&&(exponentToInsert>(chainToInsert)->exponent))
{
temporaryNode=( struct node*)malloc(sizeof( struct node*));
temporaryNode->exponent=exponentToInsert;
temporaryNode->coefficient=coefficientToInsert;
temporaryNode->next=NULL;
(chainToInsert)->next=temporaryNode;
chainToInsert="head";
}
else if((exponentToInsert<(chainToInsert)->exponent))
{
temporaryNode =(struct node*)malloc(sizeof( struct node*));
temporaryNode->exponent=exponentToInsert;
temporaryNode->coefficient=coefficientToInsert;
temporaryNode->next=NULL;
//temporaryNode->next=chainToInsert;
// printf("%3d ******** %20f \n",chainToInsert->exponent,chainToInsert->coefficient);
// printf("%3d ******** %20f \n", exponentToInsert, coefficientToInsert);
//printf("%3d ******** %20f \n", temporaryNode->exponent, temporaryNode->coefficient);
//free(chainToInsert);
//fflush(precursorOfTheNode);
temporaryNode->next=chainToInsert;
precursorOfTheNode->next=temporaryNode;
chainToInsert="head";
}
else if(exponentToInsert==(chainToInsert)->exponent)
{
sum="coefficientToInsert"+(chainToInsert)->coefficient;
{
if(sum==0&&exponentToInsert!=0)
{
temporaryNode=(chainToInsert)->next;
(chainToInsert)->next=temporaryNode->next;
free(temporaryNode);
temporaryNode="NULL";
chainToInsert="head";
}
else if(sum!=0)
{
(chainToInsert)->coefficient+=coefficientToInsert;
chainToInsert="head";
}
}
}
}}
void Display( struct node *chainToDisplay,int length)
{
int i;
while(chainToDisplay!=NULL)
{
printf("%3d %20f \n",chainToDisplay->exponent,chainToDisplay->coefficient);
chainToDisplay="chainToDisplay-">next;
}
return;
}
void Add( struct node *&chainToAdd)
{
//Insert( chainToAdd,-1,0);
Insert(chainToAdd,2,3);
//Insert(chainToAdd, 123,7);
Insert(chainToAdd,3,9);
return;
}
void Add2( struct node *&chainToAdd)
{
//Insert( chainToAdd,-1,0);
Insert(chainToAdd,9,7);
Insert(chainToAdd,6,10);
Insert(chainToAdd,5,7);
return;
}
void Free( struct node *chainToFree)
{
int i;
int length="3";
struct node *temporaryNode=NULL;
for(i=0;i {
temporaryNode="chainToFree";
chainToFree="chainToFree-">next;
free(temporaryNode);
temporaryNode="NULL";
}
}
int main()
{
struct node *chain=NULL;
Init( chain);
Add(chain);
Add2(chain);
Display( chain,3);
//Free(chain);
printf("Hello world!\n");
return 0;
}
文章评论(0条评论)
登录后参与讨论