源程序及注释.txt

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

源程序及注释:
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define OVERFLOW 0
#include
#include
#include

typedef struct
{//表的结构
int *elem;
int length;
int listsize;
}SqList;

int InitList_Sq(SqList *l) // 构造一个空的线性表L。
{
int i;
l->elem = (int *)malloc(LIST_INIT_SIZE*sizeof(int));
if (!l->elem) exit(OVERFLOW); // 存储分配失败
l->length = 0; // 空表长度为0
l->listsize = LIST_INIT_SIZE; // 初始存储容量
for(i=0;i<=l->listsize;i++) //为系数赋初值
l->elem[i]=0;
return OK;
}// InitList_Sq

void getf(SqList *L){//获取多项式函数
int temp;
int i;
do{
scanf("%d%d",&temp,&i);

if (ilistsize)
L->elem[i]=temp;
else
exit(0);
}while (getchar()!='\n');
L->length=i;


}// getf

void ds(SqList *L,SqList *l){//多项式求导函数
int e;
int i=1;
while(i<=L->length)
{
l->elem[i-1]=i*L->elem[i];//(A*x^i)'=A*i*x^(i-1)
e=i-1;
printf("%dx^%d",l->elem[i-1],e);
if((i+1)<=L->length)
{
printf("+");
};
i++;
}
l->length=L->length-1;
}
void fd(SqList *L,SqList *l){//多项式求积分函数
int e;
int i=0;
while(i<=L->length){
l->elem[i+1]=L->elem[i]/(i+1);
e=i+1;
printf("%dx^%d",l->elem[i+1],e);
if((i+1)<=L->length)
{
printf("+");
};
i++;
}
l->length=L->length+1;

}
double qz(SqList *L,int x){//多项式求值函数
double sum;
int i;
sum=0;
for(i=0;i<=L->length;i++)
sum=L->elem[i]*pow(x,i)+sum;
return sum;
}
void SUM(SqList *L,SqList *l,SqList *la){
int i;
int e;
e=0;
i=0;
if(l->length>L->length)
{
la->length=l->length;
do{
la->elem[i]=l->elem[i]+L->elem[i];
i++;}while(i<=L->length);
while(i<=l->length)
{ i++;
la->elem[i]=l->elem[i];//l比L多出部分
}
}
else
{
la->length=L->length;
do{
la->elem[i]=l->elem[i]+L->elem[i];
i++;}while(i<=l->length);
while(i<=L->length)
{ i++;
la->elem[i]=L->elem[i];//L比l多出部分
}
}
printf("f+g=");
while(e<=la->length)
{
printf("%dx^%d",la->elem[e],e);
if((e+1)<=la->length)
{
printf("+");
};
e++;
}
}

void compare(SqList *L,SqList *l){//比较多项式大小函数
int num[9];
int e;
int x;
e=0;
x=0;
if(qz(L,x)>=qz(l,x))//此时x=0,此if判断0时f(x)>=g(x)否,确定好开头
{
num[0]=x;
while((x<100)&&(qz(L,x)>=qz(l,x))){//跳出循环即大小变号了
x++;}
num[1]=x-1;//f(x)>=g(x)
num[2]=x;//f(x)<=g(x)
while((x<100)&&(qz(L,x)<=qz(l,x))){
x++;}
num[3]=x-1;//f(x)<=g(x)
num[4]=x;//f(x)>=g(x)
while((x<100)&&(qz(L,x)>=qz(l,x))){
x++;}
num[5]=x-1;//f(x)>=g(x)
num[6]=x;//f(x)<=g(x)
while((x<100)&&(qz(L,x)<=qz(l,x))){
x++;}
num[7]=x-1;//f(x)<=g(x)
num[8]=x;//f(x)>=g(x)
while(num[e]!=100){
e=e+2;}
switch(e)

{
case 2:
if(qz(L,num[2])>=qz(l,num[2]))
{ printf("[%d,%d],f(x)>=g(x)\n",num[0],num[2]);}
else
{
printf("[%d,%d],f(x)>=g(x)\n",num[0],num[1]);
printf("[%d],f(x)<=g(x)\n",num[2]);
}
break;
case 4:
printf("[%d,%d],f(x)>=g(x)\n",num[0],num[1]);
if(qz(L,num[4])>=qz(l,num[4]))
{ printf("[%d,%d],f(x)<=g(x)\n",num[2],num[4]);}
else
{
printf("[%d,%d],f(x)<=g(x)\n",num[2],num[3]);
printf("[%d],f(x)>=g(x)\n",num[4]);
}
break;
case 6:
printf("[%d,%d],f(x)>=g(x)\n",num[0],num[1]);
printf("[%d,%d],f(x)<=g(x)\n",num[2],num[3]);
if(qz(L,num[6])>=qz(l,num[6]))
{ printf("[%d,%d],f(x)>=g(x)\n",num[4],num[6]);}
else
{
printf("[%d,%d],f(x)>=g(x)\n",num[4],num[5]);
printf("[%d],f(x)<=g(x)\n",num[6]);
}
break;
case 8:
printf("[%d,%d],f(x)>=g(x)\n",num[0],num[1]);
printf("[%d,%d],f(x)<=g(x)\n",num[2],num[3]);
printf("[%d,%d],f(x)>=g(x)\n",num[4],num[5]);
if(qz(L,num[8])>=qz(l,num[8]))
{ printf("[%d,%d],f(x)<=g(x)\n",num[6],num[8]);}
else
{
printf("[%d,%d],f(x)<=g(x)\n",num[6],num[7]);
printf("[%d],f(x)>=g(x)\n",num[8]);
}
break;
}
}
else
{
num[0]=x;
while((x<100)&&(qz(L,x)<=qz(l,x))){
x++;}
num[1]=x-1;//f(x)>=g(x)
num[2]=x;//f(x)<=g(x)
while((x<100)&&(qz(L,x)>=qz(l,x))){
x++;}
num[3]=x-1;
num[4]=x;
while((x<100)&&(qz(L,x)<=qz(l,x))){
x++;}
num[5]=x-1;
num[6]=x;
while((x<100)&&(qz(L,x)>=qz(l,x))){
x++;}
num[7]=x-1;
num[8]=x;
while(num[e]!=100){
e=e+2;}
switch(e)
{
case 2:
if(qz(L,num[2])<=qz(l,num[2]))
{ printf("[%d,%d],f(x)<=g(x)\n",num[0],num[2]);}
else
{
printf("[%d,%d],f(x)<=g(x)\n",num[0],num[1]);
printf("[%d],f(x)>=g(x)\n",num[2]);
}
break;
case 4:
printf("[%d,%d],f(x)<=g(x)\n",num[0],num[1]);
if(qz(L,num[4])<=qz(l,num[4]))
{ printf("[%d,%d],f(x)>=g(x)\n",num[2],num[4]);}
else
{
printf("[%d,%d],f(x)>=g(x)\n",num[2],num[3]);
printf("[%d],f(x)<=g(x)\n",num[4]);
}
break;
case 6:
printf("[%d,%d],f(x)<=g(x)\n",num[0],num[1]);
printf("[%d,%d],f(x)>=g(x)\n",num[2],num[3]);
if(qz(L,num[6])<=qz(l,num[6]))
{ printf("[%d,%d],f(x)<=g(x)\n",num[4],num[6]);}
else
{
printf("[%d,%d],f(x)<=g(x)\n",num[4],num[5]);
printf("[%d],f(x)>=g(x)\n",num[6]);
}
break;
case 8:
printf("[%d,%d],f(x)<=g(x)\n",num[0],num[1]);
printf("[%d,%d],f(x)>=g(x)\n",num[2],num[3]);
printf("[%d,%d],f(x)<=g(x)\n",num[4],num[5]);
if(qz(L,num[8])<=qz(l,num[8]))
{ printf("[%d,%d],f(x)>=g(x)\n",num[6],num[8]);}
else
{
printf("[%d,%d],f(x)>=g(x)\n",num[6],num[7]);
printf("[%d],f(x)<=g(x)\n",num[8]);
}
break;
}
}
}
void multiply(SqList *L,SqList *l,SqList *la){//多项式相乘函数
int i,j,max,e;
max=l->length+L->length;
la->length=max;
for(i=0;i<=max;i++)
la->elem[i]=0;
for(i=0;

i<=L->length;i++){
for(j=0;j<=l->length;j++)
la->elem[i+j]=L->elem[i]*l->elem[j]+la->elem[i+j];
}
e=0;
printf("f*g=");
while(e<=la->length)
{
printf("%dx^%d",la->elem[e],e);
if((e+1)<=la->length)
{
printf("+");
};
e++;
}
}
void sub(SqList *L,SqList *l,SqList *la){//多项式减法函数
int i;
int e;
e=0;
i=0;
if(l->length>L->length)
{
la->length=l->length;
do{
la->elem[i]=L->elem[i]-l->elem[i];
i++;}while(i<=L->length);
while(i<=l->length)
{ i++;
la->elem[i]=-l->elem[i];
}
}
else
{
la->length=L->length;
do{
la->elem[i]=L->elem[i]-l->elem[i];
i++;}while(i<=l->length);
while(i<=L->length)
{ i++;
la->elem[i]=L->elem[i];
}
}
printf("f-g=");
while(e<=la->length)
{
printf("%dx^%d",la->elem[e],e);
if((e+1)<=la->length)
{
printf("+");
};
e++;
}
;
}







void main()
{
int x;
SqList la;
SqList lb;
SqList lc;//三个存放多项式的表
SqList *lap;
SqList *lbp;
SqList *lcp;
int order;
lap=&la;
lbp=&lb;
lcp=&lc;

while(1)
{
printf("若想对多项式求导请键入: 1\n");
printf("若想对多项式求不定积分请键入: 2\n");
printf("若想对多项式求值请键入: 3\n");
printf("若想作多项式作加法请键入: 4\n");
printf("若想对多项式作减法请键入: 5\n");
printf("若想对多项式作乘法请键入: 6\n");
printf("若想对多项式比较大小请键入; 7\n");
printf("请按如下格式输入多项式:");
printf("f=Ax^0 +Cx^k+Dx^i+Ex^j+...或 系数 指数\n");
scanf("%d",&order);
switch (order)
{
case 1:
printf("f=");
InitList_Sq(lap);
InitList_Sq(lbp);
getf(lap);
printf("\n");
printf("f'=");
ds(lap,lbp);
printf("\n");
break;
case 2:
printf("f=");
InitList_Sq(lap);
InitList_Sq(lbp);
getf(lap);
printf("\n");
printf("F=");
fd(lap,lbp);
printf("\n");
break;
case 3:
printf("f=");
InitList_Sq(lap);
getf(lap);
printf("x=");
scanf("%d",&x);
printf("\n");
printf("f(%d)=%f",x,qz(lap,x));
printf("\n");
break;
case 4:
InitList_Sq(lap);
InitList_Sq(lbp);
InitList_Sq(lcp);
printf("f=");
getf(lap);
printf("g=");
getf(lbp);
SUM(lap,lbp,lcp);
printf("\n");
break;
case 5:
InitList_Sq(lap);
InitList_Sq(lbp);
InitList_Sq(lcp);
printf("f=");
getf(lap);
printf("g=");
getf(lbp);
sub(lap,lbp,lcp);
printf("\n");
break;
case 6:
InitList_Sq(lap);
InitList_Sq(lbp);
InitList_Sq(lcp);
printf("f=");
getf(lap);
printf("g=");
getf(lbp);
multiply(lap,lbp,lcp);
printf("\n");
break;
case 7:
InitList_Sq(lap);
InitList_Sq(lbp);
printf("f=");
getf(lap);
printf("g=");
getf(lbp);
compare(lap,lbp);
printf("\n");
break;
default:
printf("error");
}
printf("\n");
printf("\n")

;
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - \n");
}
}

相关文档
最新文档