怎样才能改正啊,我是新人不怎么会调试,请大神指导!
// 数据结构02.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof(struct polyn)
typedef struct polyn
{
double coef;
double e;
struct polyn *next;
}POLYN;
POLYN *creatpolyn();//创建多项式
void InsertPolyn(POLYN *head, POLYN* s);//排序插入,从小到大,且合并同类项
void polynout(POLYN *head, char *s);//输出多项式
POLYN *polynmulti(POLYN *ha, POLYN *hb);//多项式求积
void Clearlist(POLYN *head);//释放链表
int main()
{
POLYN *ha,*hb,*hc;
ha = creatpolyn();
polynout(ha,"ha;");
hb = creatpolyn();
polynout(hb,"hb;");
Clearlist(ha);
Clearlist(hb);
return 0;
}
POLYN *creatpolyn()
{
POLYN *phead = NULL;
POLYN *pnode = NULL;
phead = (struct polyn*)malloc(LEN);
if (NULL== phead)
return NULL;
phead ->coef = 0;
phead->e = -1;
phead->next = NULL;
POLYN polyn;
FILE *fp1;
fopen_s(&fp1,"F:\\file1.txt","rt");
if(!fp1)
{
printf("Error!Cannot open file.");
return 0;
}
while(fscanf(fp1,"%d %d\n",&polyn.coef,&polyn.e)==2)
{
POLYN *polynmulti = new POLYN;
memcpy(polynmulti,&polyn,LEN);
pnode=(struct polyn*)malloc(LEN);
if (NULL == pnode)
break;
pnode->coef = polyn.coef;
pnode->e = polyn.e;
pnode->next = NULL;
InsertPolyn(phead,pnode);
printf("coef1:%d,e1:%d\n",*phead,*pnode);
fclose(fp1);
}
FILE *fp2;
fopen_s(&fp2,"F:\\file2.txt","rt");
if(!fp2)
{
printf("Error!Cannot open file.");
return 0;
}
while(fscanf(fp2,"%d %d\n",&polyn.coef,&polyn.e)==2)
{
POLYN *polynmulti= new POLYN;
pnode=(struct polyn*)malloc(LEN);
if (NULL == pnode)
break;
pnode->coef = polyn.coef;
pnode->e = polyn.e;
pnode->next = NULL;
InsertPolyn(phead,pnode);
printf("coef2:%d,e2:%d\n",*phead,*pnode);
fclose(fp2);
}
return phead;
}
void InsertPolyn(POLYN *phead, POLYN* s)
{
POLYN *p = NULL;
POLYN *q = NULL;
POLYN *r = NULL;
if(NULL == phead||NULL==s)
return;
if(NULL == phead->next)
{
phead->next = s;
return;
}
p = phead->next;
q = s;
while(p!= NULL)
{
if(p->e == q->e)
{
p->coef += q->coef;
return;
}
p=p->next;
}
p = p->next;
if(p->e > q->e)
{
q->next = p;
phead->next = p;
return;
}
while(p!=NULL){
if(p->e > q->e)
break;
r = p;
p = p->next;
}
q->next = r->next;
r->next = q;
}
void polynout(POLYN *phead,char *s)
{
FILE *fp3;
fopen_s(&fp3,"F:\\output.txt","wt");
if(!fp3)
{
printf("Error!Cannot open file.");
}
POLYN *p = NULL;
if(NULL == phead)
return;
p = phead->next;
if(NULL != p)
{
while(1)
{
if(NULL == p)
{
break;
}
printf("coef:%d,e:%d\n",p->coef,p->e);
fprintf(fp3,"coef: %d, e:%d\n",p->coef,p->e);
p = p->next;
}
}
fclose(fp3);
}
POLYN *polynmulti(POLYN *ha,POLYN *hb)
{
POLYN *pa = NULL;
POLYN *pb = NULL;
POLYN *phead = NULL;
POLYN *pnode = NULL;
if(NULL == ha)
{
if(hb == NULL)
{
return NULL;
}
else{
return hb;
}
}
phead=(struct polyn*)malloc(LEN);
if(NULL == phead)
return NULL;
phead->coef = 0;
phead->e = -1;
phead->next = NULL;
pa = ha->next;
pb = hb->next;
while(NULL != pb)
{
while (NULL != pa)
{
double tempcoef = 0;
double tempe = 0;
tempcoef = pa->coef *pb->coef;
tempe = pa->e + pb->e;
pnode = (struct polyn *)malloc(LEN);
if(NULL == pnode)
break;
pnode->coef = tempcoef;
pnode->e = tempe;
pnode->next = NULL;
pa = pa->next;
InsertPolyn(phead,pnode);
}
pb = hb->next;
pa = ha->next;
}
return phead;
}
void Clearlist(struct polyn *phead)
{
POLYN *Head = phead;
POLYN *p = NULL;
if (NULL == phead)
return;
while (1)
{
if (NULL == Head)
break;
p = Head;
Head = Head->next;
free(p);
p = NULL;
}
}
谢谢!!!
3 个解决方案
#1
求大神解答啊=^=
请问有人可以帮忙看看吗
请问有人可以帮忙看看吗
#2
add breakpoint
#3
改了一部分供参考
typedef struct polyn
{
int coef; //如果要用double,则需要把fscanf和printf中的"%d"改为"%lf"。 double coef;
int e; //double e;
struct polyn *next;
}POLYN;
POLYN *creatpolyn(char *file_name); //改 POLYN *creatpolyn();//创建多项式
int main()
{
POLYN *ha,*hb,*hc;
ha = creatpolyn("F:\\file1.txt");//改 ha = creatpolyn();
polynout(ha,"ha;");
hb = creatpolyn("F:\\file2.txt");//改 hb = creatpolyn();
polynout(hb,"hb;");
Clearlist(ha);
Clearlist(hb);
return 0;
}
POLYN *creatpolyn(char *file_name) //改 POLYN *creatpolyn()
{
POLYN *phead = NULL;
POLYN *pnode = NULL;
phead = (struct polyn*)malloc(LEN);
if (NULL== phead)
return NULL;
phead ->coef = 0;
phead->e = -1;
phead->next = NULL;
POLYN polyn;
FILE *fp1;
fopen_s(&fp1,file_name,"rt"); //改 fopen_s(&fp1,"F:\\file1.txt","rt");
if(!fp1)
{
printf("Error!Cannot open file.");
return 0;
}
while(fscanf(fp1,"%d %d\n",&polyn.coef,&polyn.e)==2)
{
//删POLYN *polynmulti = new POLYN;
//删memcpy(polynmulti,&polyn,LEN);
pnode=(struct polyn*)malloc(LEN);
if (NULL == pnode)
break;
pnode->coef = polyn.coef;
pnode->e = polyn.e;
pnode->next = NULL;
InsertPolyn(phead,pnode);//改 InsertPolyn(phead,pnode);
//删printf("coef1:%d,e1:%d\n",*phead,*pnode);
//移下 fclose(fp1);
}
fclose(fp1);//移到此处
/*删. 以下代码将只返回第2个多项式的head. 可以用以上代码完成相同功能.
FILE *fp2;
fopen_s(&fp2,"F:\\file2.txt","rt");
if(!fp2)
{
printf("Error!Cannot open file.");
return 0;
}
while(fscanf(fp2,"%d %d\n",&polyn.coef,&polyn.e)==2)
{
POLYN *polynmulti= new POLYN;
pnode=(struct polyn*)malloc(LEN);
if (NULL == pnode)
break;
pnode->coef = polyn.coef;
pnode->e = polyn.e;
pnode->next = NULL;
InsertPolyn(phead,pnode);
printf("coef2:%d,e2:%d\n",*phead,*pnode);
fclose(fp2);
}
*/
return phead;
}
void InsertPolyn(POLYN *phead, POLYN* s)
{
POLYN *p = NULL;
POLYN *q = NULL;
POLYN *r = NULL;
if(NULL == phead||NULL==s)
return;
if(NULL == phead->next)
{
phead->next = s;
return;
}
p = phead->next;
r = phead; //加
q = s;
while(p!= NULL)
{
if(p->e == q->e)
{
p->coef += q->coef;
return;
}
//删p=p->next;
//删}
//删p = p->next;
if(p->e > q->e)
{
q->next = p;
r->next = q;//改 phead->next = p;
return;
}
else
{
//删while(p!=NULL){
//删if(p->e > q->e)
//删 break;
r = p;
p = p->next;
}
//删q->next = r->next;
//删r->next = q;
}
r->next = q;//加
}
#1
求大神解答啊=^=
请问有人可以帮忙看看吗
请问有人可以帮忙看看吗
#2
add breakpoint
#3
改了一部分供参考
typedef struct polyn
{
int coef; //如果要用double,则需要把fscanf和printf中的"%d"改为"%lf"。 double coef;
int e; //double e;
struct polyn *next;
}POLYN;
POLYN *creatpolyn(char *file_name); //改 POLYN *creatpolyn();//创建多项式
int main()
{
POLYN *ha,*hb,*hc;
ha = creatpolyn("F:\\file1.txt");//改 ha = creatpolyn();
polynout(ha,"ha;");
hb = creatpolyn("F:\\file2.txt");//改 hb = creatpolyn();
polynout(hb,"hb;");
Clearlist(ha);
Clearlist(hb);
return 0;
}
POLYN *creatpolyn(char *file_name) //改 POLYN *creatpolyn()
{
POLYN *phead = NULL;
POLYN *pnode = NULL;
phead = (struct polyn*)malloc(LEN);
if (NULL== phead)
return NULL;
phead ->coef = 0;
phead->e = -1;
phead->next = NULL;
POLYN polyn;
FILE *fp1;
fopen_s(&fp1,file_name,"rt"); //改 fopen_s(&fp1,"F:\\file1.txt","rt");
if(!fp1)
{
printf("Error!Cannot open file.");
return 0;
}
while(fscanf(fp1,"%d %d\n",&polyn.coef,&polyn.e)==2)
{
//删POLYN *polynmulti = new POLYN;
//删memcpy(polynmulti,&polyn,LEN);
pnode=(struct polyn*)malloc(LEN);
if (NULL == pnode)
break;
pnode->coef = polyn.coef;
pnode->e = polyn.e;
pnode->next = NULL;
InsertPolyn(phead,pnode);//改 InsertPolyn(phead,pnode);
//删printf("coef1:%d,e1:%d\n",*phead,*pnode);
//移下 fclose(fp1);
}
fclose(fp1);//移到此处
/*删. 以下代码将只返回第2个多项式的head. 可以用以上代码完成相同功能.
FILE *fp2;
fopen_s(&fp2,"F:\\file2.txt","rt");
if(!fp2)
{
printf("Error!Cannot open file.");
return 0;
}
while(fscanf(fp2,"%d %d\n",&polyn.coef,&polyn.e)==2)
{
POLYN *polynmulti= new POLYN;
pnode=(struct polyn*)malloc(LEN);
if (NULL == pnode)
break;
pnode->coef = polyn.coef;
pnode->e = polyn.e;
pnode->next = NULL;
InsertPolyn(phead,pnode);
printf("coef2:%d,e2:%d\n",*phead,*pnode);
fclose(fp2);
}
*/
return phead;
}
void InsertPolyn(POLYN *phead, POLYN* s)
{
POLYN *p = NULL;
POLYN *q = NULL;
POLYN *r = NULL;
if(NULL == phead||NULL==s)
return;
if(NULL == phead->next)
{
phead->next = s;
return;
}
p = phead->next;
r = phead; //加
q = s;
while(p!= NULL)
{
if(p->e == q->e)
{
p->coef += q->coef;
return;
}
//删p=p->next;
//删}
//删p = p->next;
if(p->e > q->e)
{
q->next = p;
r->next = q;//改 phead->next = p;
return;
}
else
{
//删while(p!=NULL){
//删if(p->e > q->e)
//删 break;
r = p;
p = p->next;
}
//删q->next = r->next;
//删r->next = q;
}
r->next = q;//加
}