一元稀疏多项式计算器 C语言编写.docx
- 文档编号:18618090
- 上传时间:2023-08-20
- 格式:DOCX
- 页数:18
- 大小:21.16KB
一元稀疏多项式计算器 C语言编写.docx
《一元稀疏多项式计算器 C语言编写.docx》由会员分享,可在线阅读,更多相关《一元稀疏多项式计算器 C语言编写.docx(18页珍藏版)》请在冰点文库上搜索。
一元稀疏多项式计算器C语言编写
一元稀疏多项式计算器C语言编写
悬赏分:
120|提问时间:
2008-6-2320:
33|提问者:
骑着单车的熊
一元稀疏多项式计算器:
用C语言编写程序,实现一元稀疏多项式计算器。
具体要求如下:
(1)演示程序以用户与计算机对话的形式输入输出数据;
(2)实现两个一元稀疏多项式的加、减、乘运算;
(3)求一个多项式a的导函数a’;
(4)结果以类数学表达式的形式输出。
看清题目的要求我只要C语言的不要其他语言的还有解释一下编程的思路和过程谢谢了大家
请大家传给我能够使用的程序谢谢了
推荐答案
datastruct.h
typedefstructlist
{
intc;//多项式的项数
inte;//多项式的指数
structlist*next;//下一结点
};
typedefstructlist*LinkList;
typedefstructlistNode;
下面是线性表的操作相关函数声明,对应文件ListOper.h:
//File:
ListOper.h
#ifndefDATASTRUCT
#defineDATASTRUCT
#include"datastruct.h"
#endif
//somefunctionsdeclaretioin
boolCreateList(LinkList&L);
Node*CreateNode(inte,intc);
voidFreeList(LinkList&L);
voidSortList(LinkList&L);
voidDeleteNextNode(Node*d);
voidSweepNextNode(Node*s);
voidOutPutList(LinkList&L);
相关函数的实现,对应文件ListOper.cpp:
//File:
ListOper.cpp
#include
#include
#ifndefDATASTRUCT
#defineDATASTRUCT
#include"datastruct.h"
#endif
#include"ListOper.h"
usingnamespacestd;
boolCreateList(LinkList&L)
{
//TODO:
创建线性链表
Node*head;
head=(Node*)malloc(sizeof(Node));
if(head==NULL)
{
cout<<"内存分配错误"< returnfalse; } head->next=NULL; head->c=0; head->e=0; L=head; returntrue; } Node*CreateNode(inte,intc) { //TODO: 创建结点 Node*pos; pos=(Node*)malloc(sizeof(Node)); if(pos==NULL) { cout<<"内存分配错误"< exit (1); } pos->e=e; pos->c=c; pos->next=NULL; returnpos; } voidFreeList(LinkList&L) { //TODO: 释放整个线性表所占用的内存空间 Node*pos; Node*next; pos=L; while(pos! =NULL) { next=pos->next; free(pos); pos=next; } } voidSortList(LinkList&L) { boolflag=true;//是否需要排序标志 Node*head=L->next; Node*pos; Node*last; Node*temp; if(head->next==NULL) { return; } while(flag) { flag=true; last=head; pos=last->next; if(last==NULL||last->next==NULL) { break; } while(last! =NULL&&last->next! =NULL) { flag=false; pos=last->next; if(last->e { SweepNextNode(last); flag=true; } if(last->e==pos->e) { last->c+=pos->c; DeleteNextNode(last); flag=true; /*last=last->next; pos=last->next;*/ } last=last->next; } } } voidDeleteNextNode(Node*d) { Node*temp; temp=d->next; d->next=temp->next; free(temp); } voidSweepNextNode(Node*s) //一点偷懒的办法,只交换值,不修改指针 { intc,e; c=s->c;e=s->e; s->c=s->next->c;s->e=s->next->e; s->next->c=c;s->next->e=e; } voidOutPutList(LinkList&L) { Node*pos; pos=L->next; cout<<"输出表达式: "; while(pos! =NULL) { if(pos->c>0) { cout<<"+"; } if(pos->c! =1) { cout< } if(pos->e! =0) { cout<<"x^"; cout< } pos=pos->next; } cout< } 主单元文件main.cpp: #include #include #include #include"ListOper.h" usingnamespacestd; LinkListAnayString(charaString[],intaLength); intmain(intargc,char*argv[])//------------------------------- { LinkListL; charInStr[1024]; intlen; cout<<"一元稀疏多项式计算器"< cout<<"Copyright@1999-2004,GasheroLiu."< cout<<"作者: 刘晓明"< cout<<"请输入一个1024个字符以内的稀疏多项式: "; cin>>InStr; len=strlen(InStr); L=AnayString(InStr,len); SortList(L); OutPutList(L); FreeList(L); system("PAUSE"); return0; } LinkListAnayString(charaString[],intaLength)//--------------- //TODO: 字符串分析函数 { LinkListL=NULL; Node*pos=NULL; Node*last; Node*head; CreateList(L); head=L; last=head; intc=0; inte=0; chartemp[1]; chartp; boolplus=true; charstatus='n';//状态指示符,我省略了系数为负的情况 /* n: 非运算状态 c: 正在计算系数 e: 正在计算指数 p: 指数为0 f: 完成了一个项目的输入 */ for(inti=0;i { temp[0]=aString[i]; tp=temp[0]; switch(status) { case'n': { c=0;e=0; status='c'; if(tp=='-') { plus=false; continue; } if(isdigit(tp)) { c=atoi(temp); continue; } if(tp=='x')//多项式以x开头 { c=1; status='e'; continue; } } case'c': { if(isdigit(aString[i])) { if(plus) { c=c*10+atoi(temp); } else { c=c*10-atoi(temp); } continue; } if(tp=='x') { if(c==0) { c=1; } status='e'; e=0; continue; } //此处考虑了常数项出现在其他位置的可能 if(tp=='+') { plus=true; status='p'; continue; } if(tp=='-') { plus=false; status='p'; continue; } /*if(temp[0]=='^') { status='e'; e=0; continue; }*///此种情况不可能出现 continue; }//正在解析系数 case'e': { if(tp=='^') { continue; } if(isdigit(tp)) { e=e*10+atoi(temp); continue; } if(tp=='+') { plus=true; status='f'; continue; } if(tp=='-') { plus=false; status='f'; continue; } }//正在解析系数 case'p': { e=0; status='f'; continue; } case'f': { pos=CreateNode(e,c); last->next=pos; last=pos; c=0;e=0; status='c'; i--; continue; } } } pos=CreateNode(e,c); last->next=pos; returnL; 设计一个一元多项式简单的计算器(C语言) 悬赏分: 80|解决时间: 2008-11-1119: 19|提问者: 匿名 要求: (1)用带头节点的单链表表示多项式,表中每一个节点表示多项式的一项 (2)一元多项式简单计算器的基本功能为: 输入并建立多项式,输出多项式,2个多项式相减 最佳答案 #include #include typedefstructNode { intdata; structNode*next; }Node;//链表结点 typedefstructLinkList//定义链表数据类型 { Node*head;//表头结点 voidPrintMform()//输出多项式 { Node*p=head->next; intexp=head->data-1; while(p! =NULL) { if(exp==head->data-1) printf("%dX^%d",p->data,exp); else { if(p->data>0) printf("+%dX^%d",p->data,exp); if(p->data==0) printf("+0"); if(p->data< 0) printf("%dX^%d",p->data,exp); } exp--; p=p->next; } printf("\n"); } voidCreateByInput(intlength)//通过输入元素建立链表 { head=(Node*)malloc(sizeof(Node)); head->data=0;//表头节点存储链表真实长度 head->next=NULL; inti,temp; Node*p,*cur=head; for(i=1;i<=length;i++) { printf("structingLinkList,Pleaseinputthevalue: \n"); scanf("%d",&temp); p=(Node*)malloc(sizeof(Node)); p->data=temp; p->next=cur->next; cur->next=p; cur=cur->next; head->data++; } } }LinkList; voidmain() { LinkListL1,L2; intlength;//就是多项式的项数 printf("PleaseinputthefirstLinkList'slength: \n"); scanf("%d",&length); printf("begintostructthefirstLinkList\n");//开始构造第一个多项式 L1.CreateByInput(length); printf("begintostructthesecondLinkList\n");//开始构造第二个多项式 L2.CreateByInput(length); printf("thefirstduoxiangshiis: \n"); L1.PrintMform();//输出第一个多项式 printf("thesecondduoxiangshiis: \n"); L2.PrintMform();//输出第二个多项式 Node*p=L1.head->next;/////////////////从这里开始 Node*q=L2.head->next;//是计算多项式L1-L2,结果存入L1 while(p! =NULL) { p->data-=q->data; p=p->next; q=q->next; }/////////////////////////////////////到这里结束 printf("thesubstractis: \n"); L1.PrintMform(); system("pause"); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 一元稀疏多项式计算器 C语言编写 一元 稀疏 多项式 计算器 语言 编写