多项式计算器代码
多项式计算器-C语言作业
command = getchar ();
temp = getchar ();
if (command != 'y' && command != 'Y')
{
printf ("操作已取消。\n\n");
return ERROR;
scanf ("%d",&i);
temp = getchar ();
if (i <= 0 || i > 10) //检查i值合法性
{
printf ("输入编号不存在!\n\n");
return ERROR;
}
if (headunit[i - 1] != NULL) //若输入编号已存放信息则出现选项
{
printf ("输入系数为零!添项已取消。\n\n");
return ERROR;
}
if (q -> exp < p -> exp)
{
printf ("欲执行操作的多项式已存放信息,确认操作?\n输入y继续执行,输入其他字符取消:");
command = getchar ();
temp = getchar ();
if (command != 'y' && command != 'Y')
{
printf ("操作已取消。\n\n");
temp = getchar ();
while (p -> coef == 0) //第一项的系数不为零,此时未设取消输入的控制链接,因为操作并不繁琐,可在输入第二项时退出创建
数据结构C语言实现多项式源代码
一、一元稀疏多项式计算器#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define OK 1#define ERROR 0#define OVERFLOW -1typedef int Status;#define LIST_INIT_SIZE 51 //假设次数不超过50typedef struct{//coef表示系数,expo表示指数。
float coef;int expo;}ElemType;typedef struct LNode{ //结点类型ElemType data;struct LNode *next;}LNode,*LinkList;//记:Link与Position均为指向LNode的指针类型typedef LinkList Polynomail;//在单链表的基础上定义并实现//多项式用链表存储Status CreateList_L(LinkList &L){int n;printf("请输入需创建的多项式的长度:\n");scanf("%d",&n);LNode *CurPtr,*PrePtr;//创建递增链表L=(LinkList) malloc(sizeof(LNode));L->next=NULL;if(!L) exit(OVERFLOW);PrePtr=L;for(int i=1;i<=n;++i){CurPtr=(LNode *)malloc(sizeof(LNode));printf("请输入不同的多项式的非0系数:");scanf("%f",&(CurPtr->data).coef );printf("请输入不同的多项式的指数:");scanf("%d",&(CurPtr->data).expo );CurPtr->next=NULL;PrePtr->next=CurPtr;PrePtr=PrePtr->next;//插入到表头}CurPtr->next =L;//冒泡排序法对多项式按指数由大到小排列。
(完整word版)一元多项式计算器报告(C语言编程)
题目:编制一个一元多项式基本运算的程序姓名: 学号:PB110130一、需求分析1.在通常的应用中,多项式的次数可能很高且变化很大,使得顺序存储结构的最大长度很难确定。
由稀疏多项式的特点,故采用链式存储结构,可以不会带来浪费存储空间。
2.程序中单链表存储,根据链表的指数域,对链表进行升序排序,可给运算带来方便。
3.程序设计是在VC6.0环境下设计的的。
4.程序执行的命令为(程序主界面):二、概要设计抽象数据类型一元多项式的定义如下:1.LNode *MakeNode(double coef, int exp) 通过传入指数和系数创建一个节点,返回该节点的地址。
2.void InitList(LinkList &L)初始化,带头节点3.void PrintPolyn (LinkList L) 传入链表的指针,打印该链表4.LinkList CreatPolyn(void)//输入m项的系数和指数,建立表示一元多项式的有序链表L5.double SumPolyn(LinkList L,double x) 传入链表的指针及x值,求多项式的值。
6.void DestroyPolyn (LinkList &L) 销毁多项式,去掉头节点7.void ClearPolyn (LinkList &L) 清空多项式,保留节点实验报告8.void CopyPolyn (LinkList La,LinkList &Lb) 将La位置的多项式复制到Lb位置9.void AddPolyn(LinkList L,LinkList J ,LinkList &K) 将a和b多项式相加存到c10.void MultiplyPolyn(LinkList L,LinkList J,LinkList &K)将a和b相减存到c11. void MultiplyPolyn(LinkList L,LinkList J,LinkList &K)将a和b多项式相乘存到c12。
多项式计算器C语言代码
printf("x^%d",p->exp);
}
else if(p->exp!=1){printf("+x^%d",p->exp);}
else if(p->coef!=1){
do{
printf("输入有误!已输入过常数项,请输入非常数项:");
printf("输入系数%d:",i);
scanf("%d",&c);
printf("输入指数%d:",i);
scanf("%d",&e);
}while(e==0);
}
else if(p->exp!=1&&p->coef==1){printf("+x^%d",p->exp);}
else if(p->coef!=1&&p->exp==1){
printf("%d",p->coef);
printf("+x",p->exp);}
Node* q;
q=L;
while(q->next!=NULL&&q->exp!=e){
q=q->next;
}
if(q->next==NULL)return 1;
else return 0;
}
int compare(int a,int b){
多功能计算器的程序设计及代码示例
多功能计算器的程序设计及代码示例计算器一直是人们日常生活中必备的工具之一。
随着科技的进步,计算器也变得越来越智能和多功能化。
本文将介绍多功能计算器的程序设计和提供代码示例,旨在帮助读者更好地理解和应用多功能计算器。
I. 程序设计多功能计算器的程序设计需要考虑基本的数学运算、科学计算、统计功能、单位转换、日期计算、金融和货币计算等方面的功能。
在设计中,我们可以采用面向对象的思想,将不同的功能模块化,从而提高代码的可读性和可维护性。
II. 代码示例下面是一个简单的多功能计算器的代码示例,其中包含了基本的加减乘除运算和科学计算功能。
```pythonclass Calculator:def add(self, num1, num2):return num1 + num2def subtract(self, num1, num2):return num1 - num2def multiply(self, num1, num2):return num1 * num2def divide(self, num1, num2):if num2 != 0:return num1 / num2else:return "Error: Division by zero"def square_root(self, num):return num ** 0.5def power(self, num, exponent):return num ** exponentdef factorial(self, num):if num < 0:return "Error: Factorial of a negative number" elif num == 0:return 1else:result = 1for i in range(1, num+1):result *= ireturn result```III. 功能扩展在上述的代码示例基础上,可以进一步扩展计算器的功能。
数据结构——多项式运算器源代码
#include<stdio.h>#include<malloc.h>#include<math.h>#include<stdlib.h>typedef struct{float coef;int expn;}term,ElemType;typedef struct LNode{ElemType data;struct LNode *next;}*Link;typedef struct{Link head,tail;int len;}LinkList;/*====================链表-ADT====================*/ void MakeNode(Link &p,ElemType e){p=(Link)malloc(sizeof(struct LNode));if(!p) exit(1);p->data=e;p->next=NULL;}//MakeNodevoid InitList(LinkList &L){L.head=L.tail=(Link)malloc(sizeof(LNode));if(!L.head) exit(1);L.head->next=NULL;L.len=0;}//InitListvoid DestroyList(LinkList &L){Link p,q;p=L.head->next;while(p){q=p->next;free(p);p=q;}L.head->next=NULL;L.tail=L.head;L.len=0;}//DestroyListvoid InsLast(LinkList &L,Link s){L.tail->next=s;L.tail=s;L.len++;L.tail->next=NULL;}//InsLastvoid Append(LinkList &L,Link s){Link q;int m;if(!s) exit(1);L.tail->next=s;q=s;m=1;while(q->next){m++;q=q->next;}L.len+=m;L.tail=q;}//Append/*====================多项式-ADT====================*/ void CreatPolyn(LinkList &L){term e;Link s;scanf("%f,%d",&e.coef,&e.expn);while(e.coef){MakeNode(s,e);InsLast(L,s);scanf("%f,%d",&e.coef,&e.expn);}L.tail->next=NULL;}//CreatPolynint Judge(int n){if(n>19||n<0){printf("存储位置选择错误!请重新选择0-19.\n");return 1;}elsereturn 0;}//Judgevoid PrintPolyn(LinkList L){Link p;p=L.head->next;if(!p)printf("NULL\n");else {printf("y=");if(p->data.coef>0){if(p->data.expn==0)printf("%f",p->data.coef);elseprintf("%fx^%d",p->data.coef,p->data.expn);p=p->next;}else{if(p->data.expn==0)printf("%f",p->data.coef);elseprintf("%fx^%d",p->data.coef,p->data.expn);p=p->next;}while(p){if(p->data.coef>0){if(p->data.expn==0)printf("+%f",p->data.coef);elseprintf("+%fx^%d",p->data.coef,p->data.expn);p=p->next;}else{if(p->data.expn==0)printf("%f",p->data.coef);elseprintf("%fx^%d",p->data.coef,p->data.expn);if(p->next)p=p->next;}}printf("\n");}}//PrintPolynvoid PrintAll(LinkList S[]){int i;for(i=0;i<20;i++){printf("%d.",i);PrintPolyn(S[i]);}}//PrintAllvoid CopyPolyn(LinkList L,LinkList &S){term e;Link s,p;p=L.head->next;while(p){e.coef=p->data.coef;e.expn=p->data.expn;MakeNode(s,e);InsLast(S,s);p=p->next;}S.tail->next=NULL;}//CopyPolynint AddPolyn(LinkList &L,LinkList P,LinkList Q){ Link a,b,c;term e;a=P.head->next;b=Q.head->next;while((a!=NULL)&&(b!=NULL)){if(a->data.expn<b->data.expn){e.coef=a->data.coef;e.expn=a->data.expn;MakeNode(c,e);InsLast(L,c);a=a->next;}else if(a->data.expn>b->data.expn){e.coef=b->data.coef;e.expn=b->data.expn;MakeNode(c,e);InsLast(L,c);b=b->next;}else if((a->data.coef+b->data.coef)!=0){e.coef=a->data.coef+b->data.coef;e.expn=a->data.expn;MakeNode(c,e);InsLast(L,c);a=a->next;b=b->next;}else if((a->data.coef+b->data.coef)==0){a=a->next;b=b->next;}}if(!a&&!b){L.tail->next=NULL;return 0;}if(!b){while(a){e.coef=a->data.coef;e.expn=a->data.expn;MakeNode(c,e);InsLast(L,c);a=a->next;}}if(!a){while(b){e.coef=b->data.coef;e.expn=b->data.expn;MakeNode(c,e);InsLast(L,c);b=b->next;}}return 0;}//AddPolynvoid SubtractPolyn(LinkList &L,LinkList P,LinkList Q){ Link p;p=Q.head->next;while(p){p->data.coef=0-p->data.coef;p=p->next;}AddPolyn(L,P,Q);p=Q.head->next;while(p){p->data.coef=0-p->data.coef;p=p->next;}}//SubtractPolyndouble ResultPolyn(LinkList L,float x){Link p;double y=0;p=L.head->next;while(p){y+=(pow(x,p->data.expn)*p->data.coef);p=p->next;}return y;}//ResultPolynvoid MultiplyPolyn(LinkList &L,LinkList P,LinkList Q){ Link l,p,q;LinkList E,F;InitList(E);InitList(F);int i,j;term e;p=P.head->next;q=Q.head->next;for(i=0;i<Q.len;i++){for(j=0;j<P.len;j++){e.coef=p->data.coef*q->data.coef;e.expn=p->data.expn+q->data.expn;MakeNode(l,e);InsLast(E,l);p=p->next;}AddPolyn(L,E,F);DestroyList(E);DestroyList(F);CopyPolyn(L,F);if(i!=Q.len-1)DestroyList(L);q=q->next;p=P.head->next;}}//MultiplyPolynvoid Polyn_NFang(LinkList &L,LinkList P,int n){ int i;LinkList E;InitList(E);CopyPolyn(P,E);for(i=0;i<n;i++){MultiplyPolyn(L,E,P);DestroyList(E);CopyPolyn(L,E);if(i!=n-1)DestroyList(L);}}//Polyn_NFangvoid Polyn_NDao(LinkList &L,LinkList P,int n){ Link p,q;int i,j;CopyPolyn(P,L);for(i=0;i<n;i++){p=L.head->next;q=L.head;for(j=0;j<L.len;j++){if(p->data.expn==0){q->next=p->next;free(p);p=q->next;L.len--;}p->data.coef=p->data.coef*p->data.expn;p->data.expn=p->data.expn-1;p=p->next;q=q->next;}}}//Polyn_NDaoint Polyn_Ji(LinkList &L,LinkList P){Link p;int i;CopyPolyn(P,L);p=L.head->next;for(i=0;i<L.len;i++){if(p->data.expn==-1){printf("对不起,本程序不能实现对-1次方的积分.请重新选择.\n");DestroyList(L);return 0;}elsep=p->next;}for(i=0;i<L.len;i++){p->data.coef=p->data.coef/(p->data.expn+1);++p->data.expn;p=p->next;}return 0;}//Polyn_Jidouble Polyn_Ding(LinkList L,float a,float b){double m,n;double e=0;LinkList P;InitList(P);Polyn_Ji(P,L);m=ResultPolyn(P,a);n=ResultPolyn(P,b);e=n-m;return e;}//Polyn_Ding/*====================main====================*/void main(){int n,m,i,a,b,c;float x,e,f;double d;LinkList S[20];for(i=0;i<20;i++)InitList(S[i]);printf("说明:共有二十个多项式备用位置:0~19。
一元稀疏多项式计算器源代码
一元稀疏多项式计算器源代码#include#includetypedefstructnode{浮腔;intexp;structnode*next;}lnode,*polynmial;voidcreate(多项式&l);//输入并建立一个多项式lvoiddisplay(polynamiall)//显示,输出多项式lvoidsort(polynomy&L)//多项式L按void reverse(polynomy&L)//Inverse void select()//用户选择的加减运算voidadd(polynmialla,polynmiallb,polynmial&lc);//多项式voidsubtract(polynmialla,polynmiallb,polynmial&ld);//多项式Void create(多项式&L)//输入并创建多项式L{inti,n;静态结构节点*p;斯坎夫(\l=(structnode*)malloc(sizeof(structnode));l->next=null;for(i=0;i<n;i++){p=(structnode*)malloc(sizeof(structnode));scanf(\p->next=l->next;l->next=p;}voiddisplay(polynmiall)//显示,输出多项式l{结构节点*p,*q;intflag=0;intk=0;q=l->next;while(q){if(q->coef!=0)k++;q=q->next;la,lb相加La减去lbld,结果是}printf(\p=l->next;if(p->coef!=0){printf(\flag++;}用于(p=p->next;p;p=p->next){if(p->coef!=0){printf(\flag++;}}if(flag==0)printf(\else)printf(\}Void port(多项式&L)//多项式L按指数排序{polynmialp,q,r,u;p=l->next;l->next=null;while(p!=null){r=l;q=l->next;while((q!=null)&&(q->exp<=p->exp)){r=q;q=q->next;}u=p->next;r->next=p;p->next=q;p=u;}voidreverse(polynmial&l)//逆置{polynmialh;静态结构节点*p,*q,*s;h=(structnode*)malloc(sizeof(structnode));h->next=null;p=(structnode*)malloc(sizeof(structnode));s=l->next;p->coef=s->coef;p->exp=s->exp;p->next=s->next;while(s){p->coef=s->coef;p->exp=s->exp;p->next=s->next;q=h->next;h->next=p;p->next=q;p=(structnode*)malloc(sizeof(structnode));s=s->next;}p=h->next;q=l->next;while(p){q->coef=p->coef;q->exp=p->exp;q=q->next;p=p->next;}}Void select()//用户选择加法和减法{printf(\请选择加减操作\\n\Printf(\添加两个单变量多项式\\n\Printf(\减去两个单变量多项式\\n\}voidadd(polynmialla,polynmiallb,polynmial&lc)//{结构节点*pa,*pb;staticstructnode*pc;lc=(structnode*)malloc(sizeof(structnode));La,LB多项式加法pa=la->next;pb=lb->next;lc->next=null;while(pa&&pb){pc=(structnode*)malloc(sizeof(structnode));如果(pa->exp<pb->exp){pc->next=lc->next;lc->next=pc;pc->coef=pa->coef;pc->exp=pa->exp;pa=pa->next;}其他的if(pa->exp==pb->exp){pc->next=lc->next;lc->next=pc;pc->exp=pa->exp;pc->coef=pa->coef+pb->coef;pa=pa->next;pb=pb->next;}else{pc->next=lc->next;lc->next=pc;pc->coef=pb->coef;pc->exp=pb->exp;pb=pb->next;}}而(宾夕法尼亚州){pc=(structnode*)malloc(sizeof(structnode));pc->next=lc->next;lc->next=pc;pc->coef=pa->coef;pc->exp=pa->exp;pa=pa->next;}while(pb){pc=(structnode*)malloc(sizeof(structnode));pc->next=lc->next;lc->next=pc;pc->coef=pb->coef;pc->exp=pb->exp;pb=pb->next;}}Voidsubtract(polynmialla,polynmiallb,polynamil&LD)//多项式{structnode*pa,*pb;staticstructnode*pd;ld=(structnode*)malloc(sizeof(structnode));pa=la->next;pb=lb->next;ld->next=null;while(pa和pb){pd=(structnode*)malloc(sizeof(structnode));if(pa->exp<pb->exp){pd->next=ld->next;ld->next=pd;pd->coef=pa->coef;pd->exp=pa->exp;pa=pa->next;}else如果(pa->exp==pb->exp){pd->next=ld->next;ld->next=pd;pd->exp=pa->exp;pd->coef=pa->coef pb->coef;pa=pa->next;pb=pb->next;}否则{pd->next=ld->next;ld->next=pd;pd->coef=pb->coef;pd->exp=pb->exp;la减去lb,结果给ld。
(2023)一元稀疏多项式计算器实验报告c编写,附源代码(一)
(2023)一元稀疏多项式计算器实验报告c编写,附源代码(一)实验报告:(2023)一元稀疏多项式计算器实验目的本实验旨在编写一款一元稀疏多项式计算器,实现对两个多项式的加、减、乘、求导、求值等操作。
实验环境•操作系统:Windows 10•开发工具:Visual Studio Code•编程语言:C实验过程1. 首先定义多项式的结构体typedef struct PolyTerm {int coef;// 系数unsigned int power;// 指数struct PolyTerm* next;// 下一项}PolyTerm;typedef struct Poly {int degree;// 多项式最高次项PolyTerm* head;// 首项指针}Poly;2. 实现多项式的输入与输出void inputPoly(Poly* poly);// 输入多项式void outputPoly(Poly* poly);// 输出多项式3. 实现多项式的加、减、乘操作Poly* addPoly(Poly* p1, Poly* p2);// 多项式加法Poly* subPoly(Poly* p1, Poly* p2);// 多项式减法Poly* multPoly(Poly* p1, Poly* p2);// 多项式乘法4. 实现多项式求导void derivative(Poly* poly);// 多项式求导5. 实现多项式求值int evaluate(Poly* poly,int x);// 多项式求值6. 主函数的实现主函数通过简单的菜单方式,实现用户输入选项,选择需要进行的操作。
实验结果通过对多项式加、减、乘、求导、求值等操作的实现,成功完成了一元稀疏多项式计算器的编写,实现了对多项式运算的基本掌握。
实验总结在本次实验中,我们通过C语言实现了一元稀疏多项式计算器,并体验了多项式运算的具体操作。
计算器和多项式的C语言程序
if(!N)return ERROR;
N->coef=0.0;N->index=0;
N->next=NULL;
scanf("%f",&N->coef);
scanf("%d",&N->index);
if(N->index<0)return ERROR;
last->next=N;
if(p->index==0)
{
if(p->coef>0)printf("%g",p->coef);
else printf("-%g",0-p->coef);
}
else if(p->index==1)
{
if(p->coef>0)
{
for(PolyNode*p=defInteg->next;p!=NULL;p=p->next)
upRes+=p->coef*(float)pow(up,p->index);
for(p=defInteg->next;p!=NULL;p=p->next)
downRes+=p->coef*(float)pow(down,p->index);
List->num=0,List->sign=0;
List->next=NULL;
return OK;
}
Status EquListCreate(EquList& List)
{
EquListInit(List);
多项式计算器
system("cls"); cout<<"请输入第一个多项式\n"; p1=create(); system("cls"); cout<<"请输入第二个多项式\n"; p2=create(); i=0; break; case 2: if(p1->next==NULL)
cout<<"\t 第一个多项式为空!\n"; else cout<<"\t 第一个多项式删除成功!\n"; if(p2->next==NULL)
}
void divide(poly k1,poly k2,poly l1,poly l2){ //两个多项式相除 poly q1=new pol,q2;
q1->next=NULL; q2=k1->next; while(q2)//复制 k1 至 q1 {poly p=new pol;
*p=*q2; p->next=q1->next; q1->next=p; q2=q2->next; } rank(q1); q2=k2->next; poly l3=new pol; l1->next=NULL; l2->next=NULL; l3->next=NULL; while(q1->next->index>=q2->index)//辗转相除法 {
一元稀疏多项式计算器c语言
一元稀疏多项式计算器c语言下面是一个使用C语言编写的一元稀疏多项式计算器的示例代码。
```c#include<stdio.h>#include<stdlib.h>//定义多项式的一个节点typedef struct Nodeint coefficient; // 系数int exponent; // 指数struct Node* next; // 下一个节点} Node;//创建一个节点Node* createNode(int coefficient, int exponent)Node* newNode = (Node*)malloc(sizeof(Node));newNode->coefficient = coefficient;newNode->exponent = exponent;newNode->next = NULL;return newNode;//插入节点到多项式中Node* insertNode(Node* head, int coefficient, int exponent) Node* newNode = createNode(coefficient, exponent);if(head == NULL)return newNode;} elseNode* temp = head;while(temp->next != NULL)temp = temp->next;}temp->next = newNode;return head;}//打印多项式void printPolynomial(Node* head)Node* temp = head;while(temp != NULL)printf("%dx^%d ", temp->coefficient, temp->exponent);temp = temp->next;if(temp != NULL)printf("+ ");}}printf("\n");//计算两个多项式的和Node* addPolynomials(Node* polynomial1, Node* polynomial2) Node* result = NULL;while(polynomial1 != NULL && polynomial2 != NULL)if(polynomial1->exponent > polynomial2->exponent)result = insertNode(result, polynomial1->coefficient, polynomial1->exponent);polynomial1 = polynomial1->next;} else if(polynomial1->exponent < polynomial2->exponent) result = insertNode(result, polynomial2->coefficient, polynomial2->exponent);polynomial2 = polynomial2->next;} elseresult = insertNode(result, polynomial1->coefficient + polynomial2->coefficient, polynomial1->exponent);polynomial1 = polynomial1->next;polynomial2 = polynomial2->next;}}//将剩余的节点加入结果中while(polynomial1 != NULL)result = insertNode(result, polynomial1->coefficient, polynomial1->exponent);polynomial1 = polynomial1->next;}while(polynomial2 != NULL)result = insertNode(result, polynomial2->coefficient, polynomial2->exponent);polynomial2 = polynomial2->next;}return result;//主函数int maiNode* polynomial1 = NULL;Node* polynomial2 = NULL;Node* result = NULL;//输入第一个多项式int numTerms1;printf("Enter the number of terms in polynomial 1: ");scanf("%d", &numTerms1);printf("Enter each term of polynomial 1 (coefficient exponent):\n");for(int i=0; i<numTerms1; i++)int coefficient, exponent;scanf("%d %d", &coefficient, &exponent);polynomial1 = insertNode(polynomial1, coefficient, exponent);}//输入第二个多项式int numTerms2;printf("Enter the number of terms in polynomial 2: ");scanf("%d", &numTerms2);printf("Enter each term of polynomial 2 (coefficient exponent):\n");for(int i=0; i<numTerms2; i++)int coefficient, exponent;scanf("%d %d", &coefficient, &exponent);polynomial2 = insertNode(polynomial2, coefficient, exponent);}//打印两个多项式printf("\nPolynomial 1: ");printPolynomial(polynomial1);printf("Polynomial 2: ");printPolynomial(polynomial2);//计算两个多项式的和result = addPolynomials(polynomial1, polynomial2);//打印结果多项式printf("\nSum of polynomials: ");printPolynomial(result);return 0;```这个计算器使用了链表来表示多项式,每个节点包含一个系数和一个指数。
C语言多元多项式计算编程
#include<stdio.h>#include<malloc.h>#include<stdlib.h>typedef struct polynode{int a; //多项式的系数int n; //指数struct polynode *next;}node;node *create() //用尾插法建立一元多项式的链表{node *h,*r,*s;int c,e;h=(node*)malloc(sizeof(node));r=h;printf("a:");scanf("%d",&c);printf("n: ");scanf("%d",&e);while(c!=0) //输入系数为0时,多项式的输入结束{s=(node*)malloc(sizeof(node));s->a=c;s->n=e;r->next=s;r=s;printf("a:");scanf("%d",&c);printf("n: ");scanf("%d",&e);}r->next=NULL;return(h);}void print(node *p) //输出函数,打印出一元多项式{while(p->next!=NULL){p=p->next;printf(" %d*x^%d",p->a,p->n);}}void polyadd(node *ha, node *hb)//一元多项式相加函数,用于将两个多项式相加,然后将和多项式存放在多项式ha中,并将多项式hb删除{node *p,*q,*pre,*temp;int sum;//同指数的系数之和int i;printf("请输入多项式ha的系数与指数:\n");ha=create();print(ha);printf("\n请输入多项式hb的系数与指数:\n");hb=create();print(hb);p=ha->next;q=hb->next;pre=ha;while(p!=NULL&&q!=NULL){if(p->n<q->n){pre->next=p;pre=pre->next;p=p->next;}else if(p->n==q->n){sum=p->a+q->a;if(sum!=0){p->a=sum;pre->next=p;pre=pre->next;p=p->next;temp=q;q=q->next;free(temp);}else //如果系数和为零,则删除结点p与q,并将指针指向下一个结点{temp=p->next;free(p);p=temp;temp=q->next;free(q);q=temp;}}else{pre->next=q;pre=pre->next;q=q->next;}}if(p!=NULL) //将多项式A中剩余的结点加入到和多项式中pre->next=p;elsepre->next=q;printf("运算后的结果为:\n");print(ha);printf("\n");}void multipoly(node *ha,node *hb){ node *p,*q,*n,*m;int i;printf("请输入多项式ha的系数与指数:\n");ha=create();print(ha);printf("\n请输入多项式hb的系数与指数:\n");hb=create();print(hb);p=ha->next;n=(node*)malloc(sizeof(node));n->next=NULL;while(p!=NULL){ m=(node*)malloc(sizeof(node));for(q=hb->next;q;q=q->next){ m->a=p->a*q->a;m->n=p->n+q->n;m->next=NULL;}p=p->next;polyadd(n,m);}printf("多项式的积是:\n");print(n);printf("\n");}//...........两个多项式相减........void sub(node *ha,node *hb){printf("对不起,此功能还未开启!请稍候再试....\n"); }void main(){int i;node *ha,*hb;printf("\n请选择你要进行的运算:\n");printf("1.加\n2.减\n3.乘\n4.除\n");scanf("%d",&i);switch(i){case 1: polyadd(ha,hb);break;case 2: sub(ha,hb);break;case 3: multipoly(ha,hb);break;}}。
多项式程序代码
}
else
{
y[max-i]=(a[m-i])%2;由ASCII码转为二进制0、1
}
}
break;
}
case 3
{
for (i=0;i=n;i++)
{
for (j=0;j=m;j++)
for (i=0;i=max;i++)
printf(%d,y[i]);多项式运算结果系数
printf(n);
printf(多项式运算余数系数:n);
for (i=max;im+1;i++)
printf(%d,d[i]);多项式运算结果系数
printf(n);
if(in)
b[i]=0;除数左移(m-n)位
else
b[i]=(b[i])%2;由ASCII码转为二进制0、1
}
for (i=0;i=m;i++)
{
if(mn) 被除数次数小于除数次数
{
{
b[i]=getchar();
if( b[i]=='n')
break;
}
printf(请输入多项式运算符序号(1~4):nБайду номын сангаас;
scanf(%d,&c);
m=strlen(a)-2;第一个多项式长度
n=strlen(b)-2;第二个多项式长度
{
{
d[j]=(a[j])%2;赋予被除数
}
}
}
for (j=0;jm-n;j++)
多项式计算器
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define OK 1typedef struct Polynomial{//数据成员float coef;//系数int expn;//指数struct Polynomial *next;//指针}Polynomial,*Polyn;typedef int Status;Status Insert(Polyn a,Polyn b)//多项式按指数由高到低排列{//将a插入b 中Polyn p1=b,p2=b->next;while(p2&&p2->expn>a->expn){p1=p1->next;p2=p2->next;}if(p2&&a->expn==p2->expn)//如果要插入的a的指数与p2相等,则把两者系数合并{p2->coef=p2->coef+a->coef;free(a);//合并后释放结点aif(!p2->coef)//合并后如果系数为零,则释放结点{p1->next=p2->next;free(p2);}}else//插入新结点{a->next=p2;p1->next=a;}return OK;}//创建含有n个链表类型结点的项,含有n项的多项式Polyn CreatePolyn(Polyn Y,int n ){int i; Polyn p;Y=(Polyn)malloc(sizeof(struct Polynomial));Y->next=NULL;for(i=0;i<n;i++){p=(Polyn)malloc(sizeof(struct Polynomial));printf("请输入第%d项的系数与指数\n:",i+1);scanf("%f %d",&p->coef,&p->expn);Insert(p,Y);}return Y;}void DestoryPolyn(Polyn p){//销毁Polyn q1,q2;q1=p->next;q2=q1->next;while(q1->next){free(q1);q1=q2;q2=q2->next;}}void PrintPolyn(Polyn P){//多项式输出Polyn p=P->next;int i=1;//多项式计数if(!p){putchar('0');printf("\n");return ;}while(p){if(p->coef>0&&i!=1) putchar('+');if(p->coef!=1&&p->coef!=-1){printf("%g",p->coef);if(p->expn==1)putchar('X');else if(p->expn)printf("X^%d",p->expn);}else{if(p->coef==1){if(!p->expn) putchar('1');else if(p->expn==1) putchar('X');else printf("X^%d",p->expn);}if(p->coef==-1){if(!p->expn) printf("-1");else if(p->expn==1) printf("-X");else printf("-X^%d",p->expn);}}p=p->next;i++;}printf("\n");}int Compare(Polyn a,Polyn b){//此段代码在网上百度if(a&&b)//a,b都存在{if(a->expn>b->expn/*||!b*/) return 1;else if(a->expn<b->expn/*||!a*/) return -1;else return 0;}else if(!a&&b) return -1;else return 1;}Polyn AddPolyn(Polyn pa,Polyn pb){//多项式相加Polyn pa1=pa->next;Polyn pb1=pb->next;Polyn pc,pc1,head;//将pa+pb用pc带回head=(Polyn)malloc(sizeof(struct Polynomial));head->next=NULL;pc=head;while(pa1||pb1){pc1=(Polyn)malloc(sizeof(struct Polynomial));switch(Compare(pa1,pb1)){case 1:{pc1->coef=pa1->coef;pc1->expn=pa1->expn;pa1=pa1->next;break;}case 0:{pc1->coef=pa1->coef+pb1->coef;pc1->expn=pa1->expn;pa1=pa1->next;pb1=pb1->next;break;}case -1:{pc1->coef=pb1->coef;pc1->expn=pb1->expn;pb1=pb1->next;break;}}/*if(pc1->coef!=0){pc1->next=head->next;head->next=pc1;head=pc1;}else free(pc1);*/Insert(pc1,pc);}return pc;}Polyn SubtractPolyn(Polyn pa,Polyn pb){//多项式相减Polyn pb1=pb;Polyn pb2=pb->next;Polyn pj;while(pb2){pb2->coef*=-1;pb2=pb2->next;}pj=AddPolyn(pa,pb1);for(pb2=pb1->next;pb2;pb2=pb2->next)//用完之后要把pb的系数再变回去pb2->coef*=-1;return pj;//一定要在变完系数再返回,否则再算加法的时候就会出错。
多项式计算器
//printf("加法运算中···");
struct node *p1 = head_1,*p2 = head_2; //用p1,p1分别记录两个链表
struct node *x,*q1,*q2;
if(scanf("%f",&exp)){
//printf("exp=%f\t",exp);
p->exp = int(exp);
//printf("p->exp=%d\t",p->exp);
if(p->exp < exp){
printf("指数不能为小数!请重新输入正确的指数:");
}else{
break;
return head;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*---------------------------------多项式输出函数---------------------------------------*/
/*------------------------------多项式输入函数----------------------------------------*/
struct node* Input(){
struct node *head,*p,*q;
多项式 代码
#include<stdio.h>#include<malloc.h>#include<stdlib.h>#define OK 1#define ERROR 0#define OVERFLOW -1//定义多项式的项typedef int Status;typedef struct {float coef;int expn;}PolyTerm,ElemType;typedef struct PolyNode{PolyTerm data;struct PolyNode *next;}PolyNode,*PolyList;void menu() //主菜单{printf("\n");printf("--------主菜单----------\n");printf(" 1.输出多项式\n");printf(" 2.多项式求导\n");printf(" 3.多项式求积分\n");printf(" 4.计算函数值\n");printf(" 5.多项式相加\n");printf(" 6.多项式相减\n");printf(" 7.多项式相乘\n");printf(" 0.退出\n");printf("--------------------------------\n");}void Insert(PolyNode *p,PolyList L) //小项p插入到多项式H中,使得该多项式为指数降序{if(p->data.coef==0) free(p); //系数为0,即该小项为空,则删除该结点即释放该结点空间else{PolyNode *q1,*q2;q1=L;q2=L->next;while(q2 && p->data.expn < q2->data.expn) //查找插入位置{q1=q2;q2=q2->next;}if(q2 && p->data.expn == q2->data.expn) //将指数相同项合并{q2->data.coef = q2->data.coef +p->data.coef;free(p); //删除p结点if(!q2->data.coef) //系数为0,则释放结点{q1->next=q2->next;free(q2);}}else //新指数,将结点插入{p->next=q2;q1->next=p;}}}PolyList CreatePolyn(PolyList L,int m){ //建立多项式,m为项数int i;PolyNode *p;p=L=(PolyList)malloc(sizeof(PolyNode));L->next=NULL;for(i=0;i<m;i++){p=(PolyNode*)malloc(sizeof(PolyNode));//建立新结点以接收数据printf("请输入第%d项的系数与指数:",i+1);scanf("%f %d",&p->data.coef,&p->data.expn);Insert(p,L); //调用Insert函数插入结点p}return L; //返回该多项式}void DestroyPolyn(PolyList L) //销毁多项式,从头结点开始逐项删除各个结点{PolyNode *q1,*q2;q1=L->next;q2=q1->next;while(q1->next){free(q1);q1=q2;q2=q2->next;}}int PrintPolyn(PolyList L) //按系数从高到低输出多项式{PolyNode *q=L->next;int flag=1;//项数计数器if(!q)//若多项式为空,返回0return ERROR;while(q){if(q->data.coef >0 && flag!=1) putchar('+'); //系数大于0且不是第一项if(q->data.coef !=1 && q->data.coef !=-1)//系数非1或-1的普通情况{printf("%g",q->data.coef); //g????????//系数if(q->data.expn==1) putchar('X'); //指数else if(q->data.expn) printf("X^%d",q->data.expn);}else{if(q->data.coef==1)//系数为1时,可省略系数{if(!q->data.expn) putchar('1'); //指数为0else if(q->data.expn==1) putchar('X'); //指数为1else printf("X^%d",q->data.expn);}if(q->data.coef==-1)//系数为-1时,直接输入-{if(!q->data.expn) printf("-1");else if(q->data.expn==1) printf("-X");else printf("-X^%d",q->data.expn);}}q=q->next;flag++;}printf("\n");}int compare(PolyNode *f,PolyNode *g){ //两个小项按指数比较大小if(f&&g){if(!g||f->data.expn > g->data.expn) return 1;else if(!f||f->data.expn < g->data.expn) return -1;else return 0;}else if(!f&&g) return -1;//f多项式已空,但g多项式非空else return 1;//g多项式已空,但f多项式非空}PolyList AddPolyn(PolyList pf,PolyList pg){//多项式相加PolyList qf=pf->next;PolyList qg=pg->next;PolyList headc,hc,qc;hc=(PolyList)malloc(sizeof(PolyNode));//建立头结点hc->next=NULL;headc=hc;while(qf||qg) //其一非空{qc=(PolyList)malloc(sizeof(PolyNode));switch(compare(qf,qg)){case 1: //qf>qg{qc->data.coef=qf->data.coef;qc->data.expn=qf->data.expn;qf=qf->next;break;}case 0: //qf=qg{qc->data.coef=qf->data.coef+qg->data.coef;qc->data.expn=qf->data.expn;qf=qf->next;qg=qg->next;break;}case -1: //qf<qg{qc->data.coef=qg->data.coef;qc->data.expn=qg->data.expn;qg=qg->next;break;}}if(qc->data.coef!=0) //合系数不为0{qc->next=hc->next;hc->next=qc;hc=qc;}else free(qc);//合系数为0时,释放该结点}return headc;}PolyList SubtractPolyn(PolyList pf,PolyList pg){//多项式相减PolyList h=pg;PolyList p=pg->next;PolyList pd;while(p){ //将多项式pg的系数取反p->data.coef*=-1;p=p->next;}pd=AddPolyn(pf,h);for(p=h->next;p;p=p->next) p->data.coef*=-1;//恢复多项式g的系数return pd;}float ValuePolyn(PolyList head,float x) //输入x值,计算{PolyList p;int i;float sum=0,t;for(p=head->next;p;p=p->next){t=1;for(i=p->data.expn;i!=0;){if(i<0) {t=t/x;i++;}//指数小于0,进行除法else {t=t*x;i--;}//指数大于0,进行乘法}sum=sum+p->data.coef*t;}return sum;}PolyList Derivative(PolyList head) //多项式求导{PolyList q=head->next,p1,p2,hd;hd=p1=(PolyList)malloc(sizeof(PolyNode));//建立头结点hd->next=NULL;while(q){if(q->data.expn!=0) //该项不是常数项时{p2=(PolyList)malloc(sizeof(PolyNode));p2->data.coef=q->data.coef * q->data.expn;p2->data.expn=q->data.expn -1;p2->next=p1->next;//连接结点p1->next=p2;p1=p2;}q=q->next;}return hd;}PolyList Integral(PolyList head) //多项式求积分{PolyList q=head->next,p1,p2,hd;hd=p1=(PolyList)malloc(sizeof(PolyNode));//建立头结点hd->next=NULL;while(q){p2=(PolyList)malloc(sizeof(PolyNode));if(q->data.expn!=0) //该项不是常数项时{p2->data.expn=q->data.expn +1;p2->data.coef=q->data.coef / p2->data.expn;}else{p2->data.expn=1;p2->data.coef=q->data.coef;}p2->next=p1->next; //连接结点p1->next=p2;p1=p2;q=q->next; //后移结点}return hd;}PolyList MultiplyPolyn(PolyList pf,PolyList pg) //多项式相乘{PolyList ha,pa;PolyList qf=pf->next;PolyList qg=pg->next;ha=(PolyList)malloc(sizeof(PolyNode));//建立头结点ha->next=NULL;for(;qf;qf=qf->next){for(qg=pg->next;qg;qg=qg->next){pa=(PolyList)malloc(sizeof(PolyNode));pa->data.coef=qf->data.coef * qg->data.coef; // 系数相乘pa->data.expn=qf->data.expn + qg->data.expn; //指数相加Insert(pa,ha);//调用Insert函数以合并指数相同的项}}return ha;}void main(){int m,n,sign=-1;float x;PolyList pf=0,pg=0,pc;menu();//显示主界面printf("请输入f的项数:");scanf("%d",&m);pf=CreatePolyn(pf,m);//建立多项式fprintf("请输入g的项数:");scanf("%d",&n);pg=CreatePolyn(pg,n);//建立多项式gwhile(sign){printf("\n请选择操作:");scanf("%d",&sign);switch(sign){case 1://输出多项式{printf("\n多项式f(x)=");PrintPolyn(pf);printf("\n多项式g(x)=");PrintPolyn(pg);printf("\n\n");sign=-1;menu();break;}case 2://求导{pc=Derivative(pf);printf("\n求导后f'(x)=");PrintPolyn(pc);pc=Derivative(pg);printf("\n求导后g'(x)=");PrintPolyn(pc);printf("\n\n");sign=-1;menu();break;}case 3://求积分{pc=Integral(pf);printf("\n积分f(x)=");PrintPolyn(pc);pc=Integral(pg);printf("\n积分g(x)=");PrintPolyn(pc);printf("\n\n");sign=-1;menu();break;}case 4://求函数值{printf("输入x的值:");scanf("%f",&x);printf("f(%3.2f)=%g\n",x,ValuePolyn(pf,x));printf("\n再输入一个x的值:");scanf("%f",&x);printf("g(%3.2f)=%g\n",x,ValuePolyn(pg,x));printf("\n\n");sign=-1;menu();break;}case 5://多项式相加{pc=AddPolyn(pf,pg);printf("\n多项式之和f(x)+g(x)=");PrintPolyn(pc);printf("\n\n");sign=-1;menu();break;}case 6://多项式相减{pc=SubtractPolyn(pf,pg);printf("\n多项式之差f(x)-g(x)=");PrintPolyn(pc);printf("\n\n");sign=-1;menu();break;}case 7://多项式相乘{pc=MultiplyPolyn(pf,pg);printf("\n多项式之积f(x)*g(x)=");PrintPolyn(pc);printf("\n\n");sign=-1;menu();break;}case 0://退出程序,销毁多项式{printf("\n程序已结束!\n");DestroyPolyn(pf);DestroyPolyn(pg);sign=0;break;}default:{printf("\n您的选择错误,请重新选择!\n");sign=-1;menu();}}}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
mynode ->exp = node ->exp;
if ((mynode ->con == 1 ) && (mynode ->exp == 1))
printf("x");
else if ((mynode ->con == 1) && (mynode ->exp != 1) && (mynode ->exp != 0) )
else printf("%.2f x^%d",mynode ->con ,mynode ->exp );
free(mynode);
}
void operplus()
{
head1 = point[0];
head2 = point[1];
head = (listnode *)malloc(sizeof(listnode));
listnode * p;
listnode * q;
listnode * r;
listnode * head1;
listnode * head2;
int head_num = 0;
listnode * point[2];
void input()
{
int j = 0;
{
if ((p ->exp) ==(p->next->exp ) && (p ->next ->next != NULL))
{
p ->con = p ->con + p -> next ->con
swap = (listnode *)malloc (sizeof(listnode));
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
}
r = (listnode *) malloc (sizeof(listnode));
head ->exp = head1 ->exp + head2 ->exp
head ->next = NULL;
if (head1 ->exp == 0)
goto first_over;
p = head1 ->next;
for (;p ->next != NULL;p = p ->next )
for (;p ->next != NULL;p = p ->next )
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = -(p ->con)
r ->exp = p ->exp
r ->next = head ->next
head ->exp = head1 ->exp + head2 ->exp
head ->next = NULL; if (head1 ->exp == 0)
goto first_over; p = head1 ->next;
for (;p ->next != NULL;p = p ->next )
{
myhead = real_myhead;
for (j = 0;j < num-1; j ++)
{
if ((myhead ->next ->exp ) < (myhead->next ->next->exp ))
{
swap = (listnode *)malloc (sizeof(listnode));
goto next;
p = (listnode *) malloc (sizeof (listnode));
j++;
p ->con = con;
p ->exp = exp;
p ->next =head ->next;
head ->next = p;
goto start;
next: head ->exp =j;
point [head_num] = head;
head_num ++;
}
int bubble_sort(listnode * myhead)
{
int i,j;
listnode * swap;
//free(swap);
//goto next;
return 0;
}
}
next:return 0;
}
void myprintf(listnode * node)
{
listnode * mynode; mynode = (listnode *)malloc (sizeof(listnode));
swap ->exp = myhead ->next ->exp;
swap ->con = myhead ->next ->con;
myhead ->next ->exp = (myhead->next ->next)->exp
printf("x^%d",mynode ->exp );
else if ((mynode ->con == 0)) ;
else if ((mynode ->con != 1) && (mynode ->exp == 1))
printf("%.2f x",mynode ->con );
int exp;
float con;
head = (listnode *) malloc (sizeof(listnode));
head ->next = NULL;
start: scanf("%f %d",&con,&exp);
if (exp == 0 && con == 0)
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
first_over: if (head ->exp == 0)
goto second_over;
p = head2 ->next;
for (;p ->next != NULL;p = p ->next )
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
free (swap);
}
myhead =myhead ->next;
}
}
myhead = real_myhead;
if (myhead ->exp == 0)
return 0;
p = myhead ->next;
for (;p ->next != NULL;p = p ->next )
second_over: bubble_sort(head);
bubble_sort(head);
}
int opermul()
head ->next = r;
}
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
head ->next = r;
}
r = (listnode *) malloc (sizeof(listnode));
r ->con = -(p ->con );
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
myhead ->next ->con = (myhead->next ->next)->con
myhead ->next ->next ->exp = swap ->exp
myhead ->next ->next ->con = swap ->con
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp