⑴ 數據結構(c語言)用單鏈表存儲一元多項式,並實現兩個多項式的相加運算,怎麼做
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*單項鏈表的聲明*/
typedef struct PolynNode{
int coef; // 系數
int expn; // 指數
struct PolynNode *next; }PolynNode,*PolynList;
/*正位序(插在表尾)輸入n個元素的值,建立帶表頭結構的單鏈線性表*/
/*指數系數一對一對輸入*/ void CreatePolyn(PolynList &L,int n)
{
int i;
下載
原文檔已轉碼為如下格式,以便移動設備查看
數據結構(c語言)用單鏈表存儲一元多項式,並實現兩個多項式的相加運算【最新】
閱讀:1037次 頁數:36頁 2016-03-21 舉報
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*單項鏈表的聲明*/
typedef struct PolynNode{
int coef; // 系數
int expn; // 指數
struct PolynNode *next; }PolynNode,*PolynList;
/*正位序(插在表尾)輸入n個元素的值,建立帶表頭結構的單鏈線性表*/
/*指數系數一對一對輸入*/ void CreatePolyn(PolynList &L,int n)
{
int i;
PolynList p,q;
L=(PolynList)malloc(sizeof(PolynNode)); // 生成頭結點
L->next=NULL;
q=L;
printf("成對輸入%d個數據 ",n);
for(i=1;i<=n;i++)
{
p=(PolynList)malloc(sizeof(PolynNode));
scanf("%d%d",&p->coef,&p->expn); //指數和系數成對輸入
q->next=p;
q=q->next;
}
p->next=NULL;
}
// 初始條件:單鏈表L已存在
// 操作結果: 依次對L的每個數據元素調用函數vi()。一旦vi()失敗,則操作失敗
void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType)) {
PolynList p=L->next;
while(p)
{
vi(p->coef, p->expn);
if(p->next)
{
printf(" + "); //「+」號的輸出,最後一項後面沒有「+」
}
p=p->next;
}
printf(" ");
}
/*ListTraverse()調用的函數(類型要一致)*/ void visit(ElemType c, ElemType e) {
if(c != 0)
{
printf("%dX^%d",c,e); //格式化輸出多項式每一項
}
}
/* 多項式相加,原理:歸並 */ /* 參數:兩個已經存在的多項式 */ /* 返回值:歸並後新的多項式的頭結點 */
PolynList MergeList(PolynList La, PolynList Lb) {
PolynList pa, pb, pc, Lc;
pa = La->next;
pb = Lb->next;
Lc = pc = La; // 用La的頭結點作為Lc的頭結點
while(pa&&pb)
{
if(pa->expn < pb->expn)
{
pc->next = pa; //如果指數不相等,pc指針連上指數小的結
點,
pc = pa;
pa = pa->next; //指向該結點的指針後移
}
else if (pa ->expn > pb->expn )
{
pc->next = pb; //pc指針連上指數小的結點,
pc = pb;
pb = pb->next; //指向該結點的指針後移
}
else //(pa ->expn = pb->expn )
{
pa->coef = pa->coef + pb->coef; //指數相等時,系數相加
pc->next = pa;
pc = pa;
pa = pa->next; //兩指針都往後移
pb = pb->next;
}
}
pc->next = pa ? pa:pb; // 插入剩餘段
return Lc;
}
void main()
{
PolynList ha,hb,hc;
printf("非遞減輸入多項式ha, ");
CreatePolyn(ha,5); // 正位序輸入n個元素的值
printf("非遞減輸入多項式hb, ");
CreatePolyn(hb,5); // 正位序輸入n個元素的值
⑵ C/C++數據結構與演算法,一元多項式問題
給你一個大致的思路(以下是思路的偽代碼):
1)鏈運洞使用鏈表,鏈表的每個節點表示多項式的一個項,結點定義如下:
typedef struct
{
double coeff;
int power;
pItem next;
} item, *pItem;
2) 定義鏈表頭指針
pItem head = null;
3) 打開輸入文件
4) 從文件棚枯讀入,每讀入一行,動態生悄消成一個項並加入到鏈表中
/*申請內存,動態建立一個項*/
pItem p = (pItem)malloc(sizeof(item));
/*假設讀入系數為4, 冪為1*/
p->coeff = 4;
p->power = 1;
/*加入到鏈表中*/
pItem q = head;
while(q!= null)
{
q=q->next;
}
q.next = p;
p.next = null;
5) 輸入表達式
pItem r = head;
while(r != null)
{
printf("%fx^%d", r->coeff, r.power);
r = r->next;
}
ps. 上面是偽代碼,忽略了諸如讀入文件、輸出是每個項前的運算符等很多細節。僅供參考
⑶ 用單鏈表實現一元多項式加法
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct _POLYNODE{
int coef;//系數
int exp;//指數
struct _POLYNODE *next;
}polynode,*polyptr;
void createPoly(polynode **P, char ch[]);//建立多項式鏈表
void polyAdd(polynode *A,polynode *B);/雹搜/多項式加
void polyMinus(polynode *A,polynode *B);//減
void polyMulti(polynode *A,polynode *B);//乘
void polyDiv(polynode *A,polynode *B);//除
void order(polynode **P);//排序
void display(polynode *P);//展示多項式
void destroy(polynode **P);//銷毀多項式
void menu();//命令菜單
int isPut(char ch[]);
//菜單
void menu(){
printf("1.輸入多項式.\n"
"2.多項式相加.\n"
"3.多項式相減.\n"
"4.多項式相乘.\n"
"5.多項式相除.\n"
"6.顯示多項式.\n"
"7.銷毀塌賀多項式.\n"
"8.退出.\n");
}
//判斷菜單選擇
int IsChoice(int choice){
if(0 < choice && 9 > choice)
return 1;
else
return 0;
}
int isPut(char ch[]){
int i,j = 1;
for(i = 0; ch[i] != '\0'; i++){
{if(0 == j && '^' == ch[i])
return 0;
if('^' == ch[i] && 1 == j)
j = 0;
if(('+' ==ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && 0 == j)
j = 1;
}
if('.' != ch[i] && 'x' != ch[i] && 'X' != ch[i] && '^' != ch[i] && '+' != ch[i] && '-' != ch[i] && '*' != ch[i] && '/' != ch[i] && !isdigit(ch[i]))
return 0;
else{
if('+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0] || '.' == ch[0])
return 0;
if('\0' == ch[i+1] && '+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0])
return 0;
// 上面是判斷字元串首尾是否合格 下面是中間部分
if(0 != i && ch[i+1] != '\源衫歷0' ){
if(('X' == ch[i] || 'x' == ch[i]) && !isdigit(ch[i-1]) && '+' != ch[i-1] && '-' != ch[i-1] && '*' != ch[i-1] && '/' != ch[i-1] && '.' != ch[i-1])
return 0;
if(('X' == ch[i] || 'x' == ch[i]) && '^' != ch[i+1] && '+' != ch[i+1] && '-' != ch[i+1] && '*' != ch[i+1] && '/' != ch[i+1])
return 0;
if(('+' == ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && !isdigit(ch[i-1]) && 'X' != ch[i-1] && 'x' != ch[i-1] && !isdigit(ch[i+1]) && 'X' != ch[i+1] && 'x' != ch[i+1])
return 0;
if('^' == ch[i] && 'X' != ch[i-1] && 'x' != ch[i-1])
return 0;
if('^' == ch[i] && !isdigit(ch[i+1]))
return 0;
if('.' == ch[i] && !isdigit(ch[i+1]) && !isdigit(ch[i-1]))
return 0;
}
}
}
return 1;
}
void createPoly(polynode **P, char ch[]){
char *t = ch;
int i = 0, j = 1;
int iscoef = 1,isminus = 1;
polyptr Q,L;
if('-' == ch[0]){
isminus = -1;
*t++;
}
while('\0' != *t){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = 1;
Q->exp = 0;
Q->next = NULL;//申請節點,初始化參數為1.
if(-1 == isminus){
Q->coef *= isminus;
isminus = 1;
}
while('+' != *t && '-' != *t && '*' != *t && '/' != *t && '\0' != *t){
if('x' != *t && 'X' != *t){
while(isdigit(*t)){
i =((int)*t - 48) + i*10;
t++;
j *= i;
}//抽取數字
if(1 == iscoef && 0 != i){
Q->coef *= i;
}
if(0 == iscoef){
Q->exp += i;
iscoef = 1;
}
//如果i=0,則
}
else{
iscoef = 0;
t++;
if('^' == *t)
t++;
else
Q->exp = 1;
}
i = 0;
}//while 遍歷到加減乘除,則退出循環,到下一新的節點
iscoef = 1;
if('\0' != *t){
if('-' == *t)
isminus = -1;
t++;
}
if(0 == j){
Q->coef = 0;
j = 1;
}
printf("系數:%d,指數:%d\n",Q->coef,Q->exp);
if(NULL == *P){
*P = Q;
}
else{
L->next = Q;
}
L = Q;
}//while遍歷整個字元串
}
void polyAdd(polynode *A,polynode *B){
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多項式未被建立,請先輸入多項表達式.\n");
return ;
}
while(NULL != P){//復制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//復制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
L->next = COPYA;//把COPYA,COPYB兩個多項式連接起來,整理一下就OK了.
order(©B);
order(©B);
printf("相加結果為:");
display(COPYB);
destroy(©B);
}
void polyMinus(polynode *A,polynode *B){//相減和相加差不多
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多項式未被建立,請先輸入多項表達式.\n");
return ;
}
while(NULL != P){//復制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//復制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = -(P->coef);
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
L->next = COPYA;//把COPYA,COPYB兩個多項式連接起來,整理一下就OK了.
order(©B);
order(©B);
printf("相減結果為:");
display(COPYB);
destroy(©B);
}
void polyMulti(polynode *A,polynode *B){
polyptr R = A, P = B, Q, L = NULL, T;
if(NULL == A || NULL == B){
printf("多項式未被建立,請先輸入多項表達式.\n");
return ;
}
if(0 == A->coef || 0 == B->coef){
printf("多項式乘積為:0\n");
return ;
}
while(NULL != R){
while(NULL != P){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;
if(NULL == L)
L = Q;
else
T->next = Q;
T = Q;
P = P->next;
}
P = B;
R = R->next;
}
order(&L);
order(&L);
printf("多項式乘積為:\n");
display(L);
destroy(&L);
}
void polyDiv(polynode *A,polynode *B){//多項式除法
polyptr P = A, Q,L,R,T,C,D;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多項式未被建立,請先輸入多項表達式.\n");
return ;
}
if(A->coef == 0){
printf("0.\n");
return ;
}
if(B->coef == 0){
printf("除數為0,錯誤!\n");
return ;
}
if(A->coef < B->coef){
printf("商:0,余數為:");
display(A);
return ;
}
while(NULL != P){//復制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//復制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = -(P->coef);
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
C = P = Q = L = R = T = NULL;
//------------------開始計算
while(COPYA->exp >= COPYB->exp){
D = COPYA;
while(NULL != D->next)
D = D->next;
R = COPYB;
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = (-COPYA->coef) / R->coef;
Q->exp = COPYA->exp - R->exp;
Q->next = NULL;
if(NULL == L)
L = Q;
else
P->next = Q;
P = Q;
while(NULL != R){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;
if(NULL == T)
T = Q;
else
C->next = Q;
C = Q;
R = R->next;
}
D->next = T;
order(©A);
order(©A);
T = NULL;
C = NULL;
}
order(&L);
order(©A);
printf("A除以B,商:");
display(L);
printf("余數:");
display(COPYA);
destroy(&L);
destroy(©A);
destroy(©B);
}
void display(polynode *P){
//考慮情況有系數為1,指數為1,0,一般數;系數為系數不為1,指數為1,0,一般數;
//系數為負數,指數為1,0,一般數,主要考慮中間符號問題.
if(NULL == P){
printf("多項式為空.\n");
return ;
}
if(1 == P->coef){
if(0 == P->exp)
printf("1");
else if(1 == P->exp) printf("x");
else printf("x^%d",P->exp);
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
P = P->next;
while(NULL != P){
if(0 < P->coef){
if(1 == P->coef){
if(0 == P->exp)
printf("+1");
else if(1 == P->exp) printf("+x");
else printf("+x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("+%d",P->coef);
else if(1 == P->exp) printf("+%dx",P->coef);
else
printf("+%dx^%d",P->coef,P->exp);
}
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
}
P = P->next;
}
printf("\n");
}
void destroy(polynode **P){
polyptr Q = *P;
if(NULL == *P)
return ;
while(*P != NULL){
Q = *P;
*P = (*P)->next;
delete Q;
}
}
void order(polynode **P){
//首先 系數為零的要清掉,其次指數從高到低排序,再者系數相同的要合並.
polyptr prev,curr,OUT,INcurr;//前一節點和當前節點
int temp;
//出去第一節點系數為0的項
while(NULL != *P){
if(0 != (*P)->coef)
break;
else{
if(NULL == (*P)->next)
return;
curr = *P;
(*P) = (*P)->next;
delete curr;
}
}
if(NULL == *P || NULL == (*P)->next)//如果只剩1項或空,則不需要整理,退出函數
return;
//冒泡排序
OUT = INcurr = *P;
while(NULL != OUT->next){//外循環
while(NULL != INcurr->next){//內循環
prev = INcurr;
INcurr = INcurr->next;
if(prev->exp < INcurr->exp){
temp = prev->coef;
prev->coef = INcurr->coef;
INcurr->coef = temp;//交換系數
temp = prev->exp;
prev->exp = INcurr->exp;
INcurr->exp = temp;//交換指數
}
}
OUT = OUT->next;
INcurr = *P;
}
//去除0項
prev = curr = *P;
curr = curr->next;
while(NULL != curr){
if(0 == curr->coef){
prev->next = curr->next;
delete curr;
curr = prev->next;
}
else{
prev = curr;
curr = curr->next;
}
}
//合並同類項
OUT = INcurr = *P;
while(NULL != OUT->next){
while(NULL != INcurr->next){
prev = INcurr;
INcurr = INcurr->next;
if(INcurr->exp == OUT->exp){
OUT->coef += INcurr->coef;
prev->next = INcurr->next;
delete INcurr;
INcurr = prev;
}
}
INcurr = OUT = OUT->next;
if(NULL == OUT)
return;
}
}
void main(){
int choice;
// int i;
char ch[100];
polynode *polyA,*polyB;
polyA = polyB = NULL;
menu();
scanf("%d",&choice);
while(!IsChoice(choice)){
menu();
printf("輸入錯誤,重新輸入.\n");
scanf("%d",&choice);
}
while(8 != choice){
switch(choice){
case 1:
if(NULL != polyA || NULL != polyB){
destroy(&polyA);
destroy(&polyB);
printf("原多項式被銷毀.\n");
}
printf("多項式輸入格式:4x^3+7x^2+x+6--x不分大小寫.\n輸入多項式A:\n");
scanf("%s",&ch);
while(!isPut(ch)){
printf("輸入錯誤!重新輸.\n");
scanf("%s",&ch);
}
createPoly(&polyA,ch);//建立多項式A鏈表
printf("輸入多項式B:\n");
scanf("%s",&ch);
while(!isPut(ch)){
printf("輸入錯誤!重新輸.\n");
scanf("%s",&ch);
}
createPoly(&polyB,ch);//建立多項式B鏈表
order(&polyB);
order(&polyA);//整理排序多項式,默認降冪
printf("建立多項式成功!多項式:\nA為:");
display(polyA);
printf("B為:");
display(polyB);
break;
case 2:
polyAdd(polyA,polyB);
break;
case 3:
polyMinus(polyA,polyB);
break;
case 4:
polyMulti(polyA,polyB);
break;
case 5:
printf("PS:系數只支持整數,計算除法存在誤差;\n如果除數所有項系數為1,能得到正確答案,或者某些情況系數剛好被整除.");
polyDiv(polyA,polyB);
break;
case 6:
printf("------顯示多項式------\nA :");
display(polyA);
printf("B :");
display(polyB);
break;
case 7:
destroy(&polyA);
destroy(&polyB);
printf("此多項式已被清空.\n");
break;
default:
return ;
}
choice = 0;
menu();
scanf("%d",&choice);
while(!IsChoice(choice) || 0 == choice){
menu();
printf("輸入錯誤,重新輸入.\n");
scanf("%d",&choice);
}
}
}
⑷ 一元多項式計算器 (1)設Pn(x)和Qm(x)分別為兩個一元多項式,利用單鏈表
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef int Status;
typedef struct test{//定義多項式游裂的存儲結構
int xs;
int zs;
struct test *next;
}PolyNode,*PolyList;
PolyList p,q,r;
void createPolyList(PolyList *L){//創建多項式
(*L) = (PolyList)malloc(sizeof(PolyNode));
(*L)->next = NULL;
r=(*L);
int xs,zs;
scanf("%d %d",&xs,&zs);
while(xs!=0){
//生成新節點;
p = (PolyList)malloc(sizeof(PolyNode));
p->next = NULL;
p->xs = xs;
p->zs = zs;
//把新節點已尾接法方式插入鏈表;
r->next=p; r=p;
scanf("%d %d",&xs,&zs);
}
}
void addPolyList(PolyList la,PolyList lb,PolyList *lc){//多項式加法
PolyList pa,pb,pc;
(*lc) = (PolyList)malloc(sizeof(PolyNode));
(*lc)->next = NULL; r=(*lc);
pa=la->next; pb=lb->next;
while((pa!=NULL)&&(pb!=NULL)){
if(pa->zs < pb->zs){
//復制la的當前元局森素節點
pc=(PolyList)malloc(sizeof(PolyNode));
pc->next=NULL;
pc->zs=pa->zs;
pc->xs=pa->xs;
//把新節點加入神臘閉lc
r->next=pc; r=pc;
pa=pa->next;
}else if(pa->zs == pb->zs){
//將兩個系數相加和賦值給變數sum
int sum;
sum=pa->xs+pb->xs;
if(sum!=0){
//生成新節點並適當賦值;
pc=(PolyList)malloc(sizeof(PolyNode));
pc->next=NULL;
pc->xs=sum; pc->zs=pa->zs;
//把新節點加入lc
r->next=pc; r=pc;
}
pa=pa->next;pb=pb->next;
}else if(pa->zs > pb->zs){
//復制lb的當前元素節點
pc=(PolyList)malloc(sizeof(PolyNode));
pc->next=NULL;
pc->zs=pb->zs;
pc->xs=pb->xs;
//把新節點加入lc
r->next=pc; r=pc;
pb=pb->next;
}
}
if(pa){
pc->next=pa;
}else if(pb){
pc->next=pb;
}
}
void printList(PolyList l){//列印多項式
p=l->next;
while(p){
printf("\n%2d %2d",p->xs,p->zs);
p=p->next;
}
printf("\n");
}
void oppositePolyList(PolyList *l){//多項式的系數取相反數
PolyList p=(*l)->next;
while(p){
p->xs=-p->xs;
p=p->next;
}
}
void subtractionPolyList(PolyList la,PolyList lb,PolyList *lc){//多項式減法
oppositePolyList(&lb);
addPolyList(la,lb,lc);
oppositePolyList(&lb);
}
void reverse (PolyList *l){//多項式逆置
PolyList p,q,r;
p=*l;
q=p->next;
p->next=NULL;
while(q){
r=q->next;
q->next=p->next;
p->next=q;
q=r;
if(r)
r=r->next;
}
}
int main(){
PolyList la,lb,lc;
createPolyList(&la);
createPolyList(&lb);
subtractionPolyList(la,lb,&lc);
printf("\nla: ");
printList(la);
printf("\nlb: ");
printList(lb);
printf("\nlc: ");
printList(lc);
printf("\nre_lc: ");
reverse(&lc);
printList(lc);
return 0;
}
————————————————
版權聲明:本文為CSDN博主「Java功程師」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_3766/article/details/80648368
⑸ 一個用單鏈表存儲一元多項式的c語言程序,我覺得出錯的地方,卻改不對。還請大家指正。謝謝!
void CreateList(LinkList *&L,ElemType a[],ElemType b[],int n)
錯了,應該是void CreateList(LinkList**l,ElemType a[],ElemTpye b[],int n);
Add()那個函數也錯了,是地址錯誤。我給我寫的給你吧:
但是要自己輸入數據
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct ploynode
{
double coef;
int exp;
struct ploynode*link;
}ploynode;
typedef ploynode* NODE;
char Compare(int x,int y)
{
if(x==y) return ('枝叢=');
else if(x>y) return ('>');
else return ('<');
}
NODE Creat()
{
NODE head,p;
head=p=new ploynode;
while(1)
{
cin>>p->coef>>p->exp;
if(p->coef==0) break;
p->link=new ploynode;
p=p->link;
}
p->link=NULL;
return head;
}
void Output(NODE head)
{
NODE p=head;
while(p!=NULL)
{
if(p->coef)
{
if(p->exp!=0)
{
if(p->coef!=1)
cout<笑絕<p->coef;
if(p->exp!=1) cout<<"x^"<<p->exp;
else cout<<"x";
}
else cout<<p->coef;
if(p->coef>0&&p->link&&p->link->coef!=0) cout<<'+';
}
p=p->碰搭姿link;
}
cout<<endl;
}
NODE Attach(double c,int e,NODE d)
{
NODE x;
x=new ploynode;
x->coef=c;
x->exp=e;
d->link=x;
return x;
}
NODE PolyAdd(NODE a,NODE b)//多項式的加法實現
{
NODE p,q,d,c;
int x;
p=a;q=b;
c=new ploynode;d=c;
while((p!=NULL)&&(q!=NULL))
switch(Compare(p->exp,q->exp))
{
case '=':x=p->coef+q->coef;
if(x!=0)
d=Attach(x,p->exp,d);
p=p->link;q=q->link;
break;
case '>':d=Attach(p->coef,p->exp,d);
p=p->link;
break;
case '<':d=Attach(q->coef,q->exp,d);
q=q->link;
break;
}
while(p!=NULL)
{
d=Attach(p->coef,p->exp,d);
p=p->link;
}
while(q!=NULL)
{
d=Attach(q->coef,q->exp,d);
q=q->link;
}
d->link=NULL;
p=c;c=c->link;delete p;
return c;
}
NODE Multi(NODE a,NODE b)
{
NODE ploy[10],p,q,dst[10],Dst,temp;
q=a;p=b;
double c;int e;
int i=0,count;
while(p!=NULL)
{
if(p->coef==0) break;
ploy[i]=p;
p=p->link;
i++;
}
count=i;
for(i=0;i<count;i++)
ploy[i]->link=NULL;
for(i=0;i<count;i++)
dst[i]=new ploynode;
for(i=0;i<count;i++)
{
q=a;temp=dst[i];
while(q!=NULL)
{
if(q->coef==0) break;
c=q->coef*ploy[i]->coef;
e=q->exp+ploy[i]->exp;
temp=Attach(c,e,temp);
q=q->link;
}
temp->link=NULL;
}
for(i=0;i<count;i++)
dst[i]=dst[i]->link;
Dst=dst[0];
for(i=1;i<count;i++)
Dst=PolyAdd(Dst,dst[i]);
return Dst;
}
void main()
{
NODE src1,src2,dst;
src1=new ploynode;
src2=new ploynode;
dst=new ploynode;
src1=Creat();
cout<<"src1:"<<endl;
Output(src1);
src2=Creat();
cout<<"src2:"<<endl;
Output(src2);
dst=PolyAdd(src1,src2);
cout<<"dst:"<<endl;
Output(dst);
}
⑹ C語言:用單鏈表實現任意兩個一元多項式的加、減法運算
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 7
typedef enum { add, nul, sub, div1, yu, l, r }OP;
int a[N][N] = {
{ 0, 0, -1, -1, -1, 1, 2 },
{ 0, 0, -1, -1, -1, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
};
int top;
OP beg;
int b[1024];
OP op[1024];
void init_nu( ){ top = 0; }
void push_nu( int term ){ b[top++] = term; }
int pop_nu( ){ return b[--top]; }
int is_empty_nu( ){ return top == 0; }
void destory_nu( ) { top = 0; }
void init_sign( ){ beg = 0; }
void push_sign( OP sign ){ op[beg++] = sign; }
void destory_sign( ){ beg = 0; }
OP pop_sign( ){ return op[--beg];}
OP get_sign( ){ return op[beg - 1]; }
int is_empty_sign( ){return beg == 0; }
int eval()
{
int i, j;
i = pop_nu();
j = pop_nu();
switch( pop_sign() )
{
case '+': push_nu( j + i ); break;
case '-': push_nu( j - i ); break;
case '*': push_nu( j * i ); break;
case '/': push_nu( j / i ); break;
case '%': push_nu( j & i ); break;
defult: break;
}
}
int change( char *s )
{
int i;
int n = strlen( s );
for( i = 0; i < n; i++ )
{
if( s[i] >= '0' && s[i] <= '9' )
push_nu( 0 );
while( s[i] >= '0' && s[i] <= '9' )
push_nu( 10 * pop_nu() + s[i++] - '0' );
switch( s[i] )
{
case '+': while( a[add][get_sign()] <= 0 )
eval();
push_sign( add ); break;
case '-': while( a[nul][get_sign()] <= 0 )
eval();
push_sign( nul ); break;
case '*': while( a[sub][get_sign()] <= 0 )
eval();
push_sign( sub ); break;
case '/': while( a[div1][get_sign()] <= 0 )
eval();
push_sign( div1 ); break;
case '%': while( a[yu][get_sign()] <= 0 )
eval();
push_sign( yu ); break;
case '(': push_sign( l ); break;
case ')': while( (get_sign()) != l )
eval();
pop_sign();
break;
defult: break;
}
}
return pop_nu();
}
int main( void )
{
char *s = "((5-3)*2+4/2&2+1)";
init_nu();
init_sign();
printf( "%d\n", change( s ));
destory_nu();
destory_sign();
return 0;
}
以前編的,希望對你有幫助。
別忘了給我分。