问题描述:设计一个一元多项式简单的计算器。
基本要求:一元多项式简单计算器的基本功能为:
(1)输入并建立多项式;
(2)输出多项式;
(3)两个多项式想加,建立并输出和多项式;
(4)两个多项式相减,建立并输出差多项式。
实现提示:可选择带头结点的单向循环链表或单链表存储多项式,头结点可存放多项式的参数,如项数等。
**********************************************************************************************************************************************************************************
下面是我写的程序 大虾帮我看看 问题出现在什么地方???????
7 个解决方案
#1
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node;
typedef struct Node *term;
struct Node{
float coe;
int exp;
term link;
} ;
typedef struct Node * PolyList;
PolyList CreatPolyList(int m);/*Creat a PolyList*/
void printPolyList(PolyList PPoly);/*print a PolyList*/
PolyList AddPolyList(PolyList PA, PolyList PB);/*PA PolyList adds PB;return PC PolyList=PA+PB*/
PolyList SubPolyList(PolyList PA, PolyList PB);/*PA PolyList subs PB;return PC PolyList=PA-PB*/
main()
{
PolyList PA,PB ,P;
char key;
int m;
clrscr();
printf("\n\n LIST OPTION\n\n");
printf(" choise one of following:\n\n");
printf(" 1.Create PolyList PA.\n");
printf(" 2.print PolyList PA.\n");
printf(" 3.Create PolyList PB.\n");
printf(" 4.print PolyList PB.\n");
printf(" 5.PolyList PA adds PolyList PB.\n");
printf(" 6.PolyList PA subs PolyList PB.\n");
printf(" 7.print the result of 5 or 6: \n");
printf(" 8.exit.\n");
printf("\n please enter your choise:");
key=getchar()-'0' ;
while(key<=0||key>=9)
{
printf("\n your choise is wrong,please choise a right one:");
key=getchar()-'0';
}
switch(key){
case 1:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PA=CreatPolyList(m);
break;
case 2:clrscr();printPolyList( PA); break;
case 3:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PB=CreatPolyList(m);
break;
case 4:clrscr();printPolyList( PB); break;
case 5:clrscr();P=AddPolyList( PA, PB); break;
case 6:clrscr();P=SubPolyList( PA, PB); break;
case 7:clrscr();printPolyList( P); break;
case 8:clrscr();exit(0);
}
}
/*define the CreatPolyList function*/
PolyList CreatPolyList(int m)
{
PolyList PPoly;
term p ,q;
int j;
PPoly=(PolyList)malloc(sizeof(struct Node));
if(PPoly!=NULL)
PPoly->link=NULL;
else
return (0);
p=PPoly;
for(j=0;j<m;j++)
{
q=(term)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("Out of space!!");
return (0);
}
else
{
printf("Input coef and expn of the PolyList pleast:\n");
scanf("%f%d",&q->coe,&q->exp);
q->link=NULL;
p->link=q;
}
}
return (PPoly);
}
/*define the print function*/
void printPolyList(PolyList PPoly)
{
PolyList p;
p=PPoly;
while(p!=NULL)
{
if(p->coe==0)
printf("0");
if(p->coe!=0&&p->exp==0)
printf("%f",p->coe);
if(p->coe!=0&&p->exp==1)
printf("%f*x",p->coe);
if(p->coe!=0&&p->exp!=0&&p->exp!=1)
printf("%fx^%d",p->coe,p->exp);
if(p->link!=NULL&&p->link->coe>=0)
printf("+");
p=p->link;
}
}
/*define the AddPolyList function*/
PolyList AddPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=(p->coe+q->coe);
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
else if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
else
{
t->link=q;
q=q->link;
t=t->link;
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
if(q!=NULL) t->link=q;
return(PC);
}
/*define the SubPolyList function*/
PolyList SubPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=p->coe-q->coe;
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
if(p->exp<q->exp)
{
t->link=q;
q=q->link;
t=t->link;
t->coe=-(q->coe);
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
while(q!=NULL)
{
t->link=q;
t->coe=-(q->coe);
q=q->link;
}
return(PC);
}
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node;
typedef struct Node *term;
struct Node{
float coe;
int exp;
term link;
} ;
typedef struct Node * PolyList;
PolyList CreatPolyList(int m);/*Creat a PolyList*/
void printPolyList(PolyList PPoly);/*print a PolyList*/
PolyList AddPolyList(PolyList PA, PolyList PB);/*PA PolyList adds PB;return PC PolyList=PA+PB*/
PolyList SubPolyList(PolyList PA, PolyList PB);/*PA PolyList subs PB;return PC PolyList=PA-PB*/
main()
{
PolyList PA,PB ,P;
char key;
int m;
clrscr();
printf("\n\n LIST OPTION\n\n");
printf(" choise one of following:\n\n");
printf(" 1.Create PolyList PA.\n");
printf(" 2.print PolyList PA.\n");
printf(" 3.Create PolyList PB.\n");
printf(" 4.print PolyList PB.\n");
printf(" 5.PolyList PA adds PolyList PB.\n");
printf(" 6.PolyList PA subs PolyList PB.\n");
printf(" 7.print the result of 5 or 6: \n");
printf(" 8.exit.\n");
printf("\n please enter your choise:");
key=getchar()-'0' ;
while(key<=0||key>=9)
{
printf("\n your choise is wrong,please choise a right one:");
key=getchar()-'0';
}
switch(key){
case 1:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PA=CreatPolyList(m);
break;
case 2:clrscr();printPolyList( PA); break;
case 3:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PB=CreatPolyList(m);
break;
case 4:clrscr();printPolyList( PB); break;
case 5:clrscr();P=AddPolyList( PA, PB); break;
case 6:clrscr();P=SubPolyList( PA, PB); break;
case 7:clrscr();printPolyList( P); break;
case 8:clrscr();exit(0);
}
}
/*define the CreatPolyList function*/
PolyList CreatPolyList(int m)
{
PolyList PPoly;
term p ,q;
int j;
PPoly=(PolyList)malloc(sizeof(struct Node));
if(PPoly!=NULL)
PPoly->link=NULL;
else
return (0);
p=PPoly;
for(j=0;j<m;j++)
{
q=(term)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("Out of space!!");
return (0);
}
else
{
printf("Input coef and expn of the PolyList pleast:\n");
scanf("%f%d",&q->coe,&q->exp);
q->link=NULL;
p->link=q;
}
}
return (PPoly);
}
/*define the print function*/
void printPolyList(PolyList PPoly)
{
PolyList p;
p=PPoly;
while(p!=NULL)
{
if(p->coe==0)
printf("0");
if(p->coe!=0&&p->exp==0)
printf("%f",p->coe);
if(p->coe!=0&&p->exp==1)
printf("%f*x",p->coe);
if(p->coe!=0&&p->exp!=0&&p->exp!=1)
printf("%fx^%d",p->coe,p->exp);
if(p->link!=NULL&&p->link->coe>=0)
printf("+");
p=p->link;
}
}
/*define the AddPolyList function*/
PolyList AddPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=(p->coe+q->coe);
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
else if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
else
{
t->link=q;
q=q->link;
t=t->link;
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
if(q!=NULL) t->link=q;
return(PC);
}
/*define the SubPolyList function*/
PolyList SubPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=p->coe-q->coe;
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
if(p->exp<q->exp)
{
t->link=q;
q=q->link;
t=t->link;
t->coe=-(q->coe);
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
while(q!=NULL)
{
t->link=q;
t->coe=-(q->coe);
q=q->link;
}
return(PC);
}
#2
不好意思啊 上面那个是比较早的
大家看这个好了 我修改后的
*******************************************************************
*******************************************************************
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node;
typedef struct Node *term;
struct Node{
float coe;
int exp;
term link;
} ;
typedef struct Node * PolyList;
PolyList CreatPolyList(int m);/*Creat a PolyList*/
void printPolyList(PolyList PPoly);/*print a PolyList*/
PolyList AddPolyList(PolyList PA, PolyList PB);/*PA PolyList adds PB;return PC PolyList=PA+PB*/
PolyList SubPolyList(PolyList PA, PolyList PB);/*PA PolyList subs PB;return PC PolyList=PA-PB*/
void menu(void) ;
main()
{
menu();
}
void menu(void)
{
PolyList PA,PB ,P;
char key;
int m;
clrscr();
printf("\n\n LIST OPTION\n\n");
printf(" choise one of following:\n");
printf(" 1.Create PolyList PA.\n");
printf(" 2.print PolyList PA.\n");
printf(" 3.Create PolyList PB.\n");
printf(" 4.print PolyList PB.\n");
printf(" 5. PolyList PA adds PolyList PB.\n");
printf(" 6. PolyList PA subs PolyList PB.\n");
printf(" 7.print the result of 5 or 6: \n");
printf(" 8.exit.\n");
printf("\n please enter your choise:");
key=getchar()-'0';
while(key<=0||key>=9)
{
printf("\n your choise is wrong,please choise a right one:");
key=getchar()-'0';
}
switch(key){
case 1:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PA=CreatPolyList(m);
menu();
break;
case 2:clrscr();printPolyList( PA); menu();break;
case 3:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PB=CreatPolyList(m);
menu();
break;
case 4:clrscr();printPolyList( PB); menu(); break;
case 5:clrscr(); P=AddPolyList( PA, PB); menu();break;
case 6: clrscr();P=SubPolyList( PA, PB); menu(); break;
case 7:clrscr();printPolyList( P); menu(); break;
case 8:clrscr(); exit(0);
}
}
/*define the CreatPolyList function*/
PolyList CreatPolyList(int m)
{
PolyList PPoly;
term p ,q;
int j;
PPoly=(PolyList)malloc(sizeof(struct Node));
if(PPoly!=NULL)
PPoly->link=NULL;
else
return (0);
p=PPoly;
for(j=0;j<m;j++)
{
q=(term)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("Out of space!!");
return (0);
}
else
{
printf("Input coef and expn of the PolyList pleast:\n");
scanf("%f%d",&q->coe,&q->exp);
q->link=NULL;
p->link=q;
}
}
return (PPoly);
}
/*define the print function*/
void printPolyList(PolyList PPoly)
{
PolyList p;
p=PPoly;
while(p!=NULL)
{
if(p->coe==0)
printf("0");
if(p->coe!=0&&p->exp==0)
printf("%f",p->coe);
if(p->coe!=0&&p->exp==1)
printf("%f*x",p->coe);
if(p->coe!=0&&p->exp!=0&&p->exp!=1)
printf("%fx^%d",p->coe,p->exp);
if(p->link!=NULL&&p->link->coe>=0)
printf("+");
p=p->link;
}
}
/*define the AddPolyList function*/
PolyList AddPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=(p->coe+q->coe);
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
else if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
else
{
t->link=q;
q=q->link;
t=t->link;
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
if(q!=NULL) t->link=q;
return(PC);
}
/*define the SubPolyList function */
PolyList SubPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=p->coe-q->coe;
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
if(p->exp<q->exp)
{
t->link=q;
q=q->link;
t=t->link;
t->coe=-(q->coe);
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
while(q!=NULL)
{
t->link=q;
t->coe=-(q->coe);
q=q->link;
}
return(PC);
}
大家看这个好了 我修改后的
*******************************************************************
*******************************************************************
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node;
typedef struct Node *term;
struct Node{
float coe;
int exp;
term link;
} ;
typedef struct Node * PolyList;
PolyList CreatPolyList(int m);/*Creat a PolyList*/
void printPolyList(PolyList PPoly);/*print a PolyList*/
PolyList AddPolyList(PolyList PA, PolyList PB);/*PA PolyList adds PB;return PC PolyList=PA+PB*/
PolyList SubPolyList(PolyList PA, PolyList PB);/*PA PolyList subs PB;return PC PolyList=PA-PB*/
void menu(void) ;
main()
{
menu();
}
void menu(void)
{
PolyList PA,PB ,P;
char key;
int m;
clrscr();
printf("\n\n LIST OPTION\n\n");
printf(" choise one of following:\n");
printf(" 1.Create PolyList PA.\n");
printf(" 2.print PolyList PA.\n");
printf(" 3.Create PolyList PB.\n");
printf(" 4.print PolyList PB.\n");
printf(" 5. PolyList PA adds PolyList PB.\n");
printf(" 6. PolyList PA subs PolyList PB.\n");
printf(" 7.print the result of 5 or 6: \n");
printf(" 8.exit.\n");
printf("\n please enter your choise:");
key=getchar()-'0';
while(key<=0||key>=9)
{
printf("\n your choise is wrong,please choise a right one:");
key=getchar()-'0';
}
switch(key){
case 1:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PA=CreatPolyList(m);
menu();
break;
case 2:clrscr();printPolyList( PA); menu();break;
case 3:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PB=CreatPolyList(m);
menu();
break;
case 4:clrscr();printPolyList( PB); menu(); break;
case 5:clrscr(); P=AddPolyList( PA, PB); menu();break;
case 6: clrscr();P=SubPolyList( PA, PB); menu(); break;
case 7:clrscr();printPolyList( P); menu(); break;
case 8:clrscr(); exit(0);
}
}
/*define the CreatPolyList function*/
PolyList CreatPolyList(int m)
{
PolyList PPoly;
term p ,q;
int j;
PPoly=(PolyList)malloc(sizeof(struct Node));
if(PPoly!=NULL)
PPoly->link=NULL;
else
return (0);
p=PPoly;
for(j=0;j<m;j++)
{
q=(term)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("Out of space!!");
return (0);
}
else
{
printf("Input coef and expn of the PolyList pleast:\n");
scanf("%f%d",&q->coe,&q->exp);
q->link=NULL;
p->link=q;
}
}
return (PPoly);
}
/*define the print function*/
void printPolyList(PolyList PPoly)
{
PolyList p;
p=PPoly;
while(p!=NULL)
{
if(p->coe==0)
printf("0");
if(p->coe!=0&&p->exp==0)
printf("%f",p->coe);
if(p->coe!=0&&p->exp==1)
printf("%f*x",p->coe);
if(p->coe!=0&&p->exp!=0&&p->exp!=1)
printf("%fx^%d",p->coe,p->exp);
if(p->link!=NULL&&p->link->coe>=0)
printf("+");
p=p->link;
}
}
/*define the AddPolyList function*/
PolyList AddPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=(p->coe+q->coe);
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
else if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
else
{
t->link=q;
q=q->link;
t=t->link;
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
if(q!=NULL) t->link=q;
return(PC);
}
/*define the SubPolyList function */
PolyList SubPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=p->coe-q->coe;
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
if(p->exp<q->exp)
{
t->link=q;
q=q->link;
t=t->link;
t->coe=-(q->coe);
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
while(q!=NULL)
{
t->link=q;
t->coe=-(q->coe);
q=q->link;
}
return(PC);
}
#3
是不是输出的不正确?是的话,原因在于操作完之后没有输出,再次调用menu()的时间又给那些变量了重新声明并初始化为一些随机的值,所以输出根本就不是你想要的结果了
#4
还有里面建立链表的也有问题,以及add,sub也存在一定的问题,最好一个一个的调试一下,不过最好是通过有序链表处理的话 效率要高很多
#5
重新调用menu()的时候PA的的值是0xcccccccc
#6
太长
#7
就是指针把我弄晕了~~~~
不知从何下手 哪位有原代码的话 麻烦发到我的邮箱 chenyoujian888@163.com 以作学习 参考谢谢!!!
不知从何下手 哪位有原代码的话 麻烦发到我的邮箱 chenyoujian888@163.com 以作学习 参考谢谢!!!
#1
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node;
typedef struct Node *term;
struct Node{
float coe;
int exp;
term link;
} ;
typedef struct Node * PolyList;
PolyList CreatPolyList(int m);/*Creat a PolyList*/
void printPolyList(PolyList PPoly);/*print a PolyList*/
PolyList AddPolyList(PolyList PA, PolyList PB);/*PA PolyList adds PB;return PC PolyList=PA+PB*/
PolyList SubPolyList(PolyList PA, PolyList PB);/*PA PolyList subs PB;return PC PolyList=PA-PB*/
main()
{
PolyList PA,PB ,P;
char key;
int m;
clrscr();
printf("\n\n LIST OPTION\n\n");
printf(" choise one of following:\n\n");
printf(" 1.Create PolyList PA.\n");
printf(" 2.print PolyList PA.\n");
printf(" 3.Create PolyList PB.\n");
printf(" 4.print PolyList PB.\n");
printf(" 5.PolyList PA adds PolyList PB.\n");
printf(" 6.PolyList PA subs PolyList PB.\n");
printf(" 7.print the result of 5 or 6: \n");
printf(" 8.exit.\n");
printf("\n please enter your choise:");
key=getchar()-'0' ;
while(key<=0||key>=9)
{
printf("\n your choise is wrong,please choise a right one:");
key=getchar()-'0';
}
switch(key){
case 1:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PA=CreatPolyList(m);
break;
case 2:clrscr();printPolyList( PA); break;
case 3:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PB=CreatPolyList(m);
break;
case 4:clrscr();printPolyList( PB); break;
case 5:clrscr();P=AddPolyList( PA, PB); break;
case 6:clrscr();P=SubPolyList( PA, PB); break;
case 7:clrscr();printPolyList( P); break;
case 8:clrscr();exit(0);
}
}
/*define the CreatPolyList function*/
PolyList CreatPolyList(int m)
{
PolyList PPoly;
term p ,q;
int j;
PPoly=(PolyList)malloc(sizeof(struct Node));
if(PPoly!=NULL)
PPoly->link=NULL;
else
return (0);
p=PPoly;
for(j=0;j<m;j++)
{
q=(term)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("Out of space!!");
return (0);
}
else
{
printf("Input coef and expn of the PolyList pleast:\n");
scanf("%f%d",&q->coe,&q->exp);
q->link=NULL;
p->link=q;
}
}
return (PPoly);
}
/*define the print function*/
void printPolyList(PolyList PPoly)
{
PolyList p;
p=PPoly;
while(p!=NULL)
{
if(p->coe==0)
printf("0");
if(p->coe!=0&&p->exp==0)
printf("%f",p->coe);
if(p->coe!=0&&p->exp==1)
printf("%f*x",p->coe);
if(p->coe!=0&&p->exp!=0&&p->exp!=1)
printf("%fx^%d",p->coe,p->exp);
if(p->link!=NULL&&p->link->coe>=0)
printf("+");
p=p->link;
}
}
/*define the AddPolyList function*/
PolyList AddPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=(p->coe+q->coe);
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
else if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
else
{
t->link=q;
q=q->link;
t=t->link;
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
if(q!=NULL) t->link=q;
return(PC);
}
/*define the SubPolyList function*/
PolyList SubPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=p->coe-q->coe;
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
if(p->exp<q->exp)
{
t->link=q;
q=q->link;
t=t->link;
t->coe=-(q->coe);
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
while(q!=NULL)
{
t->link=q;
t->coe=-(q->coe);
q=q->link;
}
return(PC);
}
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node;
typedef struct Node *term;
struct Node{
float coe;
int exp;
term link;
} ;
typedef struct Node * PolyList;
PolyList CreatPolyList(int m);/*Creat a PolyList*/
void printPolyList(PolyList PPoly);/*print a PolyList*/
PolyList AddPolyList(PolyList PA, PolyList PB);/*PA PolyList adds PB;return PC PolyList=PA+PB*/
PolyList SubPolyList(PolyList PA, PolyList PB);/*PA PolyList subs PB;return PC PolyList=PA-PB*/
main()
{
PolyList PA,PB ,P;
char key;
int m;
clrscr();
printf("\n\n LIST OPTION\n\n");
printf(" choise one of following:\n\n");
printf(" 1.Create PolyList PA.\n");
printf(" 2.print PolyList PA.\n");
printf(" 3.Create PolyList PB.\n");
printf(" 4.print PolyList PB.\n");
printf(" 5.PolyList PA adds PolyList PB.\n");
printf(" 6.PolyList PA subs PolyList PB.\n");
printf(" 7.print the result of 5 or 6: \n");
printf(" 8.exit.\n");
printf("\n please enter your choise:");
key=getchar()-'0' ;
while(key<=0||key>=9)
{
printf("\n your choise is wrong,please choise a right one:");
key=getchar()-'0';
}
switch(key){
case 1:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PA=CreatPolyList(m);
break;
case 2:clrscr();printPolyList( PA); break;
case 3:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PB=CreatPolyList(m);
break;
case 4:clrscr();printPolyList( PB); break;
case 5:clrscr();P=AddPolyList( PA, PB); break;
case 6:clrscr();P=SubPolyList( PA, PB); break;
case 7:clrscr();printPolyList( P); break;
case 8:clrscr();exit(0);
}
}
/*define the CreatPolyList function*/
PolyList CreatPolyList(int m)
{
PolyList PPoly;
term p ,q;
int j;
PPoly=(PolyList)malloc(sizeof(struct Node));
if(PPoly!=NULL)
PPoly->link=NULL;
else
return (0);
p=PPoly;
for(j=0;j<m;j++)
{
q=(term)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("Out of space!!");
return (0);
}
else
{
printf("Input coef and expn of the PolyList pleast:\n");
scanf("%f%d",&q->coe,&q->exp);
q->link=NULL;
p->link=q;
}
}
return (PPoly);
}
/*define the print function*/
void printPolyList(PolyList PPoly)
{
PolyList p;
p=PPoly;
while(p!=NULL)
{
if(p->coe==0)
printf("0");
if(p->coe!=0&&p->exp==0)
printf("%f",p->coe);
if(p->coe!=0&&p->exp==1)
printf("%f*x",p->coe);
if(p->coe!=0&&p->exp!=0&&p->exp!=1)
printf("%fx^%d",p->coe,p->exp);
if(p->link!=NULL&&p->link->coe>=0)
printf("+");
p=p->link;
}
}
/*define the AddPolyList function*/
PolyList AddPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=(p->coe+q->coe);
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
else if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
else
{
t->link=q;
q=q->link;
t=t->link;
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
if(q!=NULL) t->link=q;
return(PC);
}
/*define the SubPolyList function*/
PolyList SubPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=p->coe-q->coe;
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
if(p->exp<q->exp)
{
t->link=q;
q=q->link;
t=t->link;
t->coe=-(q->coe);
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
while(q!=NULL)
{
t->link=q;
t->coe=-(q->coe);
q=q->link;
}
return(PC);
}
#2
不好意思啊 上面那个是比较早的
大家看这个好了 我修改后的
*******************************************************************
*******************************************************************
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node;
typedef struct Node *term;
struct Node{
float coe;
int exp;
term link;
} ;
typedef struct Node * PolyList;
PolyList CreatPolyList(int m);/*Creat a PolyList*/
void printPolyList(PolyList PPoly);/*print a PolyList*/
PolyList AddPolyList(PolyList PA, PolyList PB);/*PA PolyList adds PB;return PC PolyList=PA+PB*/
PolyList SubPolyList(PolyList PA, PolyList PB);/*PA PolyList subs PB;return PC PolyList=PA-PB*/
void menu(void) ;
main()
{
menu();
}
void menu(void)
{
PolyList PA,PB ,P;
char key;
int m;
clrscr();
printf("\n\n LIST OPTION\n\n");
printf(" choise one of following:\n");
printf(" 1.Create PolyList PA.\n");
printf(" 2.print PolyList PA.\n");
printf(" 3.Create PolyList PB.\n");
printf(" 4.print PolyList PB.\n");
printf(" 5. PolyList PA adds PolyList PB.\n");
printf(" 6. PolyList PA subs PolyList PB.\n");
printf(" 7.print the result of 5 or 6: \n");
printf(" 8.exit.\n");
printf("\n please enter your choise:");
key=getchar()-'0';
while(key<=0||key>=9)
{
printf("\n your choise is wrong,please choise a right one:");
key=getchar()-'0';
}
switch(key){
case 1:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PA=CreatPolyList(m);
menu();
break;
case 2:clrscr();printPolyList( PA); menu();break;
case 3:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PB=CreatPolyList(m);
menu();
break;
case 4:clrscr();printPolyList( PB); menu(); break;
case 5:clrscr(); P=AddPolyList( PA, PB); menu();break;
case 6: clrscr();P=SubPolyList( PA, PB); menu(); break;
case 7:clrscr();printPolyList( P); menu(); break;
case 8:clrscr(); exit(0);
}
}
/*define the CreatPolyList function*/
PolyList CreatPolyList(int m)
{
PolyList PPoly;
term p ,q;
int j;
PPoly=(PolyList)malloc(sizeof(struct Node));
if(PPoly!=NULL)
PPoly->link=NULL;
else
return (0);
p=PPoly;
for(j=0;j<m;j++)
{
q=(term)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("Out of space!!");
return (0);
}
else
{
printf("Input coef and expn of the PolyList pleast:\n");
scanf("%f%d",&q->coe,&q->exp);
q->link=NULL;
p->link=q;
}
}
return (PPoly);
}
/*define the print function*/
void printPolyList(PolyList PPoly)
{
PolyList p;
p=PPoly;
while(p!=NULL)
{
if(p->coe==0)
printf("0");
if(p->coe!=0&&p->exp==0)
printf("%f",p->coe);
if(p->coe!=0&&p->exp==1)
printf("%f*x",p->coe);
if(p->coe!=0&&p->exp!=0&&p->exp!=1)
printf("%fx^%d",p->coe,p->exp);
if(p->link!=NULL&&p->link->coe>=0)
printf("+");
p=p->link;
}
}
/*define the AddPolyList function*/
PolyList AddPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=(p->coe+q->coe);
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
else if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
else
{
t->link=q;
q=q->link;
t=t->link;
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
if(q!=NULL) t->link=q;
return(PC);
}
/*define the SubPolyList function */
PolyList SubPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=p->coe-q->coe;
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
if(p->exp<q->exp)
{
t->link=q;
q=q->link;
t=t->link;
t->coe=-(q->coe);
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
while(q!=NULL)
{
t->link=q;
t->coe=-(q->coe);
q=q->link;
}
return(PC);
}
大家看这个好了 我修改后的
*******************************************************************
*******************************************************************
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node;
typedef struct Node *term;
struct Node{
float coe;
int exp;
term link;
} ;
typedef struct Node * PolyList;
PolyList CreatPolyList(int m);/*Creat a PolyList*/
void printPolyList(PolyList PPoly);/*print a PolyList*/
PolyList AddPolyList(PolyList PA, PolyList PB);/*PA PolyList adds PB;return PC PolyList=PA+PB*/
PolyList SubPolyList(PolyList PA, PolyList PB);/*PA PolyList subs PB;return PC PolyList=PA-PB*/
void menu(void) ;
main()
{
menu();
}
void menu(void)
{
PolyList PA,PB ,P;
char key;
int m;
clrscr();
printf("\n\n LIST OPTION\n\n");
printf(" choise one of following:\n");
printf(" 1.Create PolyList PA.\n");
printf(" 2.print PolyList PA.\n");
printf(" 3.Create PolyList PB.\n");
printf(" 4.print PolyList PB.\n");
printf(" 5. PolyList PA adds PolyList PB.\n");
printf(" 6. PolyList PA subs PolyList PB.\n");
printf(" 7.print the result of 5 or 6: \n");
printf(" 8.exit.\n");
printf("\n please enter your choise:");
key=getchar()-'0';
while(key<=0||key>=9)
{
printf("\n your choise is wrong,please choise a right one:");
key=getchar()-'0';
}
switch(key){
case 1:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PA=CreatPolyList(m);
menu();
break;
case 2:clrscr();printPolyList( PA); menu();break;
case 3:
clrscr();
printf("Input the number of terms of the PolyList you will Creat. m= ");
scanf("%d",&m);
PB=CreatPolyList(m);
menu();
break;
case 4:clrscr();printPolyList( PB); menu(); break;
case 5:clrscr(); P=AddPolyList( PA, PB); menu();break;
case 6: clrscr();P=SubPolyList( PA, PB); menu(); break;
case 7:clrscr();printPolyList( P); menu(); break;
case 8:clrscr(); exit(0);
}
}
/*define the CreatPolyList function*/
PolyList CreatPolyList(int m)
{
PolyList PPoly;
term p ,q;
int j;
PPoly=(PolyList)malloc(sizeof(struct Node));
if(PPoly!=NULL)
PPoly->link=NULL;
else
return (0);
p=PPoly;
for(j=0;j<m;j++)
{
q=(term)malloc(sizeof(struct Node));
if(q==NULL)
{
printf("Out of space!!");
return (0);
}
else
{
printf("Input coef and expn of the PolyList pleast:\n");
scanf("%f%d",&q->coe,&q->exp);
q->link=NULL;
p->link=q;
}
}
return (PPoly);
}
/*define the print function*/
void printPolyList(PolyList PPoly)
{
PolyList p;
p=PPoly;
while(p!=NULL)
{
if(p->coe==0)
printf("0");
if(p->coe!=0&&p->exp==0)
printf("%f",p->coe);
if(p->coe!=0&&p->exp==1)
printf("%f*x",p->coe);
if(p->coe!=0&&p->exp!=0&&p->exp!=1)
printf("%fx^%d",p->coe,p->exp);
if(p->link!=NULL&&p->link->coe>=0)
printf("+");
p=p->link;
}
}
/*define the AddPolyList function*/
PolyList AddPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=(p->coe+q->coe);
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
else if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
else
{
t->link=q;
q=q->link;
t=t->link;
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
if(q!=NULL) t->link=q;
return(PC);
}
/*define the SubPolyList function */
PolyList SubPolyList(PolyList PA, PolyList PB)
{
PolyList PC;
term p,q,t;
PC=(PolyList)malloc(sizeof(struct Node));
if(PC!=NULL)
PC->link=NULL;
else
return (0);
p=PA;q=PB;t=PC;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
t->link=p;
t->coe=p->coe-q->coe;
p=p->link;
q=q->link;
t=t->link;
t->link=NULL;
}
if(p->exp>q->exp)
{
t->link=p;
p=p->link;
t=t->link;
t->link=NULL;
}
if(p->exp<q->exp)
{
t->link=q;
q=q->link;
t=t->link;
t->coe=-(q->coe);
t->link=NULL;
}
}
if(p!=NULL) t->link=p;
while(q!=NULL)
{
t->link=q;
t->coe=-(q->coe);
q=q->link;
}
return(PC);
}
#3
是不是输出的不正确?是的话,原因在于操作完之后没有输出,再次调用menu()的时间又给那些变量了重新声明并初始化为一些随机的值,所以输出根本就不是你想要的结果了
#4
还有里面建立链表的也有问题,以及add,sub也存在一定的问题,最好一个一个的调试一下,不过最好是通过有序链表处理的话 效率要高很多
#5
重新调用menu()的时候PA的的值是0xcccccccc
#6
太长
#7
就是指针把我弄晕了~~~~
不知从何下手 哪位有原代码的话 麻烦发到我的邮箱 chenyoujian888@163.com 以作学习 参考谢谢!!!
不知从何下手 哪位有原代码的话 麻烦发到我的邮箱 chenyoujian888@163.com 以作学习 参考谢谢!!!