主题:雪中跪求一个简单的一元多项式计算器的设计!!
xing55110
[专家分:0] 发布于 2005-01-11 12:22:00
各位大叔大姐!!!!!!小弟实习作业马上就要交了,可惜平时没怎么用功,不知道怎么做啊~~~~~~~~~~~55555555 那位好心人能帮我写个么, 小弟在这里万分感谢了,要是没有这方面的资料,也可以帮我做“Josephus 问题”的实习作业,或者是“按层遍历二叉树的算法设计探讨”
万分感谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
我的邮箱:xing55110123@163.com
沙发
潜潜 [专家分:0] 发布于 2005-07-01 11:47:00
#include "stdio.h"
#include "conio.h"
#include <stdlib.H>
struct Polynomial
{struct Polynomial *next;
float xi;
int zhi;
};
typedef struct Polynomial Poly;
typedef struct Polynomial * Polyn;
void CreatPolyn(Polyn *);
void DestroyPoln(Polyn *);
void PrintPolyn(Polyn *);
void AddPolyn(Polyn *);
void PolynLength(Polyn *);
void SubtractPolyn(Polyn *);
void MultiplyPolyn(Polyn *);
main()
{Polyn p=NULL;int a;
while(1)
{printf("\nEnter a choice\n");
printf("0:exit\n1:CreatPolyn\n2:DestroyPoln;\n3:PrintPolyn;\n4:PolynLength;\n");
printf("5:AddPolyn;\n6:SubtractPolyn;\n7:MultiplyPolyn;\n");
scanf("%d",&a);
switch(a)
{case 0:exit(0);
case 1:CreatPolyn(&p);break;
case 2:DestroyPoln(&p);break;
case 3:PrintPolyn(&p);break;
case 4:PolynLength(&p);break;
case 5:AddPolyn(&p);break;
case 6:SubtractPolyn(&p);break;
case 7:MultiplyPolyn(&p);break;
}
}}
void CreatPolyn(Polyn *pt)
{int a,b,c,zhi;Polyn s,l,k,p;char ss[20];FILE *file; float xi;
printf("0:return,1:Enter use keyboard,2:Enter use file\n");
scanf("%d",&a);
s=malloc(sizeof(Poly));
s->next=NULL;
*pt=s;k=s->next;p=s;
switch(a)
{case 1:printf("Enter the number of note\n");
scanf("%d",&b);
for(c=1;c<=b;c++)
{l=malloc(sizeof(Poly));
printf("\nEnter xishu&&zhishu\n");
scanf("%f%d",&l->xi,&l->zhi);
s=p;k=s->next;
while(k&&k->zhi<l->zhi)
{k=k->next;s=s->next;}
if(k->zhi==l->zhi)
{k->xi+=l->xi;
if(k->xi==0)
{s->next=k->next;free(k);}
free(l);
}
else
{l->next=k;s->next=l;}
}
break;
case 2:printf("Enter file name\n");
scanf("%s",ss);
if((file=fopen(ss,"rt"))==NULL)
{printf("file could not been opened\n");return;}
fscanf(file,"%f,%d;",&xi,&zhi);
while(!feof(file))
{l=malloc(sizeof(Poly));
l->xi=xi;l->zhi=zhi;
s=p;k=s->next;
while(k&&k->zhi<l->zhi)
{k=k->next;s=s->next;}
if(k->zhi==l->zhi)
{k->xi+=l->xi;
if(k->xi==0)
{s->next=k->next;free(k);}
free(l);
}
else
{l->next=k;s->next=l;}
fscanf(file,"%f,%d;",&xi,&zhi);
}break;
case 0:return;
}
printf("Polyn has been created\n");
}
void DestroyPoln(Polyn *p)
{Polyn s,l;
l=s=*p;
if(s==NULL)
{printf("Polyn has been destroyed\n");return;}
else
{ do
{l=s->next;
free(s);
s=l;
}while(l);
}
*p=NULL;
printf("Polyn has been destroyed\n");
}
void PrintPolyn(Polyn *p)
{Polyn s;int a;
s=*p;
if(s==NULL) {printf("Polyn is not extend\n");return;}
if(s->next==NULL){printf("Polyn is NULL\n");return;}
s=s->next;
printf("xishu zhishu\n");
for(a=1;s;a++,s=s->next)
{printf("%-16f%d\n",s->xi,s->zhi);
if(a%10==0){printf("Enter anykey to continue\n");getch();}
}
}
void AddPolyn(Polyn *p)
{int a,b,c,zhi;Polyn s,l,k;FILE *file;char ss[20];float xi;
s=*p;k=s->next;
printf("0:return,1:Enter use keyboard,2:Enter use file\n");
scanf("%d",&a);
switch(a)
{case 1:l=malloc(sizeof(Poly));
printf("\nEnter xishu&&zhishu\n");
scanf("%f%d",&l->xi,&l->zhi);
while(k&&k->zhi<l->zhi)
{k=k->next;s=s->next;}
if(k->zhi==l->zhi)
{k->xi+=l->xi;
if(k->xi==0)
{s->next=k->next;free(k);}
free(l);
}
else
{l->next=k;s->next=l;}
break;
case 2:printf("Enter file name\n");
scanf("%s",ss);
if((file=fopen(ss,"rt"))==NULL)
{printf("file could not been opened\n");return;}
fscanf(file,"%f,%d;",&xi,&zhi);
while(!feof(file))
{l=malloc(sizeof(Poly));
l->zhi=zhi;l->xi=xi;
s=*p;k=s->next;
while(k&&k->zhi<l->zhi)
{k=k->next;s=s->next;}
if(k->zhi==l->zhi)
{k->xi+=l->xi;
if(k->xi<0.00000001)
{s->next=k->next;free(k);}
free(l);
}
else
{l->next=k;s->next=l;}
fscanf(file,"%f,%d;",&xi,&zhi);
}
break;
case 0:return;
}
}
void PolynLength(Polyn *p)
{Polyn s;int a;
s=*p;
if(s==NULL) {printf("Polyn is not extend\n");return;}
if(s->next==NULL){printf("Polyn is NULL\n");return;}
s=s->next;
for(a=0;s;a++)
s=s->next;
printf("\nPolyn Length is %d\n",a);
}
void SubtractPolyn(Polyn *p)
{Polyn s=*p,l,k,x;
CreatPolyn(&k);
l=s;
s=s->next;
x=k->next;free(k);k=x;x=x->next;
while(s&&k)
{while(s->zhi<k->zhi)
{l=l->next;s=s->next;}
if(s->zhi==k->zhi)
{s->xi+=k->xi;
free(k);k=x;x=x->next;
if(s->xi<0.0000001)
{l->next=s->next;
free(s);
s=l->next;
}
}
else if(s->zhi>k->zhi)
{k->next=s;l->next=k;s=k;
k=x;x=x->next;
}
}
if(s==NULL)l->next=k;
}
void MultiplyPolyn(Polyn *p)
{
}