长整数的代数计算----数据结构课程设计 下载本文

沈阳航空航天大学课程设计报告

free(qq); } } return OK; }

//========================乘法模块=========================== Status chengfa(NodeList opr1,NodeList opr2,NodeList &oprr) { NodeList ph1,ph2,pt1,pt2,p3,pt3,qq; int len,CF; long buffer; ph1=opr1; pt1=ph1->prior; ph2=opr2; pt2=ph2->prior; len=length(opr1)+length(opr2); Creat(oprr,len); qq=oprr->next; while(qq!=oprr) { qq->data=0; qq=qq->next; } buffer=CF=0; p3=oprr->prior; while(pt2!=ph2) { pt1=ph1->prior; pt3=p3; while(pt1!=ph1) { buffer=pt1->data*pt2->data+pt3->data+CF; CF=(int)buffer/10000; pt3->data=(int)buffer000; pt1=pt1->prior; pt3=pt3->prior; } pt3->data=CF; CF=0; pt2=pt2->prior; p3=p3->prior; }

return OK;

23

沈阳航空航天大学课程设计报告

}

//======================除法模块======================= //除法子函数

int chufa_zi(NodeList &opr1,NodeList opr2) { NodeList p1,p2,qh,qt,qq; int count,CF,buffer,flag; count=0; while(compare(opr1,opr2)!=-1)//opr2链长 { CF=buffer=0; p1=opr1->prior; p2=opr2->prior; while(p2!=opr2) { if(p1->data<(p2->data+CF)) { buffer=10000+p1->data-(p2->data+CF); CF=1; } else { buffer=p1->data-(p2->data+CF); CF=0; } p1->data=buffer; p1=p1->prior; p2=p2->prior; } if(p1!=opr1)//处理opr1链剩下的部份 { buffer=p1->data-CF; p1->data=buffer; }

//清头0 flag=0;

p1=opr1->next; while(p1->data==0 && p1->next!=opr1) { p1=p1->next; flag=1; } if(flag)

24

沈阳航空航天大学课程设计报告

{ qh=opr1->next;//保存无用结点的头尾指针 qt=p1->prior;//为释放做准备 opr1->next=p1;//重接next链 p1->prior=opr1;//重接prior链 qt->next=NULL; while(qh!=NULL) {//释放无用结点 qq=qh; qh=qh->next; free(qq); } } count++; } return count; }

//除法函数

Status chufa(NodeList opr1,NodeList opr2,NodeList &quti,NodeList &remand) {//quti为商数链,remand为余数链 int len_quti,len_reman,buffer; NodeList q1,q2,pq; if(compare(opr1,opr2)==-1)//除数比被除数大 { Creat(quti,1); quti->next->data=0; quti->next->next=quti; quti->prior=quti->next; remand=opr1; } else { len_quti=length(opr1)-length(opr2); len_reman=length(opr2); Creat(quti,len_quti+1);//开辟商数链 Creat(remand,len_reman);//开辟余数链 q1=opr1->next; q2=remand->next;//q2指向余数链remand的下一结点 //初始化remand链 while(q2!=remand) { q2->data=q1->data; q1=q1->next;

25

沈阳航空航天大学课程设计报告

q2=q2->next; } pq=quti->next; q1=q1->prior;//指针退回一步 while(q1!=opr1) { buffer=chufa_zi(remand,opr2); pq->data=buffer; if(q1->next!=opr1) { remand->prior->next=(NodeList)malloc(LEN); remand->prior->next->next=remand; remand->prior->next->prior=remand->prior; remand->prior=remand->prior->next; remand->prior->data=q1->next->data; } if(remand->next->data==0 && remand->next->next!=remand) { remand->next->next->prior=remand; remand->next=remand->next->next; } q1=q1->next; pq=pq->next; } pq=quti->prior; while(pq->data=='?') pq=pq->prior; pq->next=quti; quti->prior=pq; } return OK; }

//=========================主操作模块========================= Status kongzhi() { NodeList opr1,opr2,oprr,quti,remand; char str[MAX],ch; opr1=opr2=oprr=quti=remand=NULL; printf(\ printf(\ 欢迎使用长整数代数计算程序\\n\ printf(\本程序的操作提示========================\\n\ printf(\选择 1 将会进行加法操作===================\\n\ printf(\选择 2 将会进行减法操作===================\\n\

26