多項式相加c語言
❶ c語言 多項式加法
你的判斷有問題,代碼如下,自己找一下問題:
#include<iostream>
#include<queue>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<bitset>
#include<map>
#include<string>
usingnamespacestd;
intmain()
{
inti,a,b,first,array[2001];;
//while(scanf("%d",&n)!=EOF)
{
first=1;
memset(array,0,sizeof(array));
//多項式一
while(scanf("%d%d",&a,&b)&&(a!=0||b!=0))
array[a+1000]=b;
//多項式二
while(scanf("%d%d",&a,&b)&&(a!=0||b!=0))
//相同次數系數相加
array[a+1000]+=b;
//輸出
for(i=2001;i>=0;i--)
{
//系數為0的整數對不用輸出
if(array[i]==0)
continue;
//控制格式
if(first)
first=0;
else
printf(" ");
printf("%d%d",i-1000,array[i]);
}
printf(" ");
}
return0;
}
❷ c語言多項式相加
我這有一個實現加減乘除的多項式程序,自己寫的,另外輸入形式為:-2x^3 +5x^2+3x+4 即可。
其中網路的現實問題,有一個&;A和&;B的 應該是& COPYA, & COPYB
去掉中間空格
支持整數多項式加減乘除。
#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);
}
}
}
❸ 兩個多項式相加運算(用c語言)
#include<stdio.h>
#include<malloc.h>
#define Null 0
typedef struct Node
{
int coeff;
int expo;
Node *next;
}listNode,*list;
list createList()
{
list head;
head = (list)malloc(sizeof(listNode));
head = NULL;
printf("want to create a new node?y/n\n");
char ch;
fflush(stdin);
scanf("%c",&ch);
while(ch=='Y' || ch== 'y')
{
list p;
p = (list)malloc(sizeof(listNode));
printf("input coeff\n");
int coeff;
scanf("%d",&coeff);
p->coeff = coeff;
printf("input expo\n");
int expo;
scanf("%d",&expo);
p->expo = expo;
p->next = NULL;
//鏈表為空的時候,即添加首個元素
if(head == NULL)
{
head=p;//添加代碼
}
else
{
list prev,curr;
curr = head;
prev = NULL;
//找到添加位置
while(curr!=NULL && curr->expo>p->expo)
{
prev=curr;
curr=curr->next;//添加代碼
}
if(curr!=NULL && curr->expo == p->expo)
{
curr->coeff = curr->coeff + p->coeff;
printf("want to create a new node?y/n\n");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y' || ch== 'y')
continue;
else
return head;
}
//插入結點,結點非首
if(prev != NULL)
{
p->next=curr;
prev->next=p;
//添加代碼
}
//插入結點,結點為首
else
{
p->next=curr;
head=p;
//添加代碼
}
}
printf("want to create a new node?y/n\n");
fflush(stdin);
scanf("%c",&ch);
}
return head;
}
list add(list head1,list head2)
{
list head,newNode,ptr1,ptr2,ptr3;
ptr1 = head1;
ptr2 = head2;
head = NULL;
while(ptr1 != NULL && ptr2 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
if(ptr1->expo > ptr2->expo)
{
newNode->coeff = ptr1->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr1 = ptr1->next;
}
else if(ptr1->expo < ptr2->expo)
{
newNode->coeff = ptr2->coeff;
newNode->expo = ptr2->expo;
newNode->next = NULL;
ptr2 = ptr2->next;//添加代碼
}
else
{
newNode->coeff = ptr1->coeff + ptr2->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
if(head==NULL)
{
head = newNode;
}
else
{
ptr3 = head;
//添加代碼
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;
}
}
while(ptr1 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
newNode->coeff = ptr1->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr3 = head;
if(ptr3 == NULL)
head = ptr3 = newNode;
else
{
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;//添加代碼
}
ptr1 = ptr1->next;
}
while(ptr2 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
newNode->coeff = ptr2->coeff;
newNode->expo = ptr2->expo;
ptr3 = head;
if(ptr3 == NULL)
head = ptr3 = newNode;
else
{
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;
}
ptr2 = ptr2->next;
}
return head;
}
void display(list head)
{
list ptr = head;
while(ptr != NULL)
{
if(ptr != head )
printf("+");
printf("%d",ptr->coeff);
printf("x^");
printf("%d",ptr->expo);
ptr = ptr->next;
}
printf("\n");
}
int main(int argc, char* argv[])
{
list head,head1,head2;
printf("input the first list\n");
head1 = createList();
display(head1);
printf("input the second list\n");
head2 = createList();
display(head2);
head = add(head1,head2);
display(head);
return 0;
}
❹ C語言寫多項式相加怎麼寫
/*
2007-3-22
一元多項式的加法
*/
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedefstructPolyNode
{
intcoef;
intexp;
structPolyNode*next;
}node;
node*CreatePoly(void)
{
node*h,*tail,*s;
intcoef,exp;
h=(node*)malloc(sizeof(node));
if(!h)
{
exit(-1);
}
h->next=NULL;
tail=h;
printf("請輸入每一項額系數和指數(中間以逗號隔開):/n");
printf("coef,exp:");
scanf("%d,%d",&coef,&exp);
while(coef)
{
s=(node*)malloc(sizeof(node));
if(!s)
{
exit(-1);
}
s->coef=coef;
s->exp=exp;
s->next=tail->next;
tail->next=s;
tail=s;
printf("ceof,exp:");
scanf("%d,%d",&coef,&exp);
}
returnh;
}
voidPolyAdd(node*polya,node*polyb)
{
node*p,*q,*pre,*temp;
intsum=0;;
p=polya->next;
q=polyb->next;
pre=polya;
while(p&&q)
{
if(p->exp<q->exp)
{
pre->next=p;
pre=p;
p=p->next;
}
elseif(p->exp>q->exp)
{
pre->next=q;
pre=q;
q=q->next;
}
else
{
sum=p->coef+q->coef;
if(sum)
{
p->coef=sum;
pre->next=p;
pre=p;
p=p->next;
temp=q->next;
free(q);
q=temp;
}
else
{
temp=p->next;
free(p);
p=temp;
temp=q->next;
free(q);
q=temp;
}
}
}
pre->next=p?p:q;
}
intPrin(node*h)
{
node*p=h->next;
while(p)
{
printf("%d*x^%d",p->coef,p->exp);
p=p->next;
}
printf("/n");
return1;
}
intmain(void)
{
node*polya,*polyb;
printf("請輸入第一個一元多項式的系數和指數(假定以輸入系數為0來結束):/n");
polya=CreatePoly();
printf("請輸入的第一個一元多項式為:/n");
Prin(polya);
printf("請輸入第二個一元多項式的系數和指數(假定以輸入系數為0來結束):/n");
polyb=CreatePoly();
printf("請輸入的第二個一元多項式為:/n");
Prin(polyb);
printf("這兩個一元多項式相加後的結果為:/n");
PolyAdd(polya,polyb);
Prin(polya);
return1;
}
❺ C語言,描述多項式加法
1. #include"malloc.h"
2. typedef struct Polyn /*定義多項式每一項的類型*/
3. {
4. float cofe; /*每一項系數*/
5. int expn; /*每一項的指數*/
6. struct Polyn *next;
7. struct Polyn *prior;
8. }*Pol.yn,SNode;
9. void setPolyn(Polyn *t1,Polyn *t2,int n) /*建立多項式*/
10. {
11. float a;
12. int i,b;
13. *t1=(SNode *)malloc(sizeof(SNode)); /*創建頭節點*/
14. (*t1)->next=NULL;
15. for(i=0;i<n;i++)
16. {
17. *t2=(SNode *)malloc(sizeof(SNode));
18. scanf("%f,%d;",&a,&b);
19. (*t2)->cofe=a; (*t2)->expn=b;
20. (*t1)->next->prior=(*t2);
21. (*t2)->prior=(*t1);
22. (*t2)->next=(*t1)->next;
23. (*t1)->next=(*t2);
24. } /*創建每一項並連接成多項式*/
25. }
26. void print(Polyn *t) /*輸出多項式*/
27. {
28. Polyn p;
29. for(p=(*t)->next;p!=NULL;p=p->next)
30. printf("%f,%d;",p->cofe,p->expn);
31. printf("\n");
32. }
33. void arrange(Polyn *t) /*化簡多項式*/
34. {
35. float m1;
36. int m2;
37. Polyn p,q,r,s;
38. for(p=(*t)->next;p!=NULL;p=p->next)
39. for(q=p->next;q!=NULL;q=q->next)
40. if((p->expn)>(q->expn))
41. {
42. m1=p->cofe;p->cofe=q->cofe;q->cofe=m1;
43. m2=p->expn;p->expn=q->expn;q->expn=m2;
44. } /*冒泡法多項式指數排序*/
45. for(p=(*t)->next;p!=NULL;p=p->next)
46. if((p->expn)==(p->next->expn))
47. {
48. r=p->next;
49. p->cofe+=p->next->cofe;
50. p->next=p->next->next;
51. p=p->prior; /*指針指向上一結點(須重新處理現在處理 的結點)*/
52. free(r);
53. if((p->next->cofe)==0)
54. {
55. s=p->next;
56. p->next=p->next->next;
57. free(s);
58. }
59. } /*多項式相同指數項系數求和,化簡多項式*/
60. }
61. void linkPolyn(Polyn *t1,Polyn *t2) /*兩個多項式連接*/
62. {
63. Polyn p,q;
64. for(p=(*t2)->next;p!=NULL;p=p->next)
65. q=p;
66. p=(*t2)->next;
67. q->next=(*t1)->next;
68. (*t1)->next->prior=q;
69. (*t1)->next=p;
70. p->prior=(*t1);
71. }
72. main() /*主函數*/
73. {
74. Polyn La,la,Lb,lb;
75. int n,m; /*多項式項數*/
76. printf("enter La』s lenth:");
77. scanf("%d",&n);
78. setPolyn(&La,&la,n);
79. arrange(&La);
80. printf("after arrange La is:\n");
81. print(&La); /*輸入多項式La,化簡並輸出*/
82. printf("enter Lb』s lenth:");
83. scanf("%d",&m);
84. setPolyn(&Lb,&lb,m);
85. arrange(&Lb);
86. printf("after arrange Lb is:\n");
87. print(&Lb); /*輸入多項式Lb,化簡並輸出*/
88. linkPolyn(&La,&Lb); /*La與Lb連接,形成新的La*/
89. arrange(&La); /*化簡La*/
90. printf("after add Polyn is :\n");
91. print(&La); /*輸出結果*/
92. }
❻ 多項式相加 鏈表 C語言
#include#includestructnode{
intcoef; //系數
intindex; //指數
structnode*next;
};
//初始化linklist
structnode*init_ll(){
structnode*p;
p=(structnode*)malloc(sizeof(structnode)); //為節點指針分配內存空間
p->next=NULL; //*為節點的next值賦值為NULL方便表的遍歷,作為遍歷為結尾的判斷條件
returnp; //返回指針,便於操作
}
//頭插法插入linklist
voidinsert_ll(structnode*head,intcoef,intindex){
structnode*p;
p=(structnode*)malloc(sizeof(structnode)); //為節點指針分配內存空間
p->coef=coef; //實例化系數
p->index=index; //實例化指數
p->next=head->next; //*該節點的next指針指向head指向的節點
head->next=p; //*插入在頭指針的後面
}
//列印輸出linklist
voidreverse_ll(structnode*head){
structnode*p;
p=head->next;
while(p!=NULL){ //為空時結束循環
printf("%d次項系數為%d",p->index,p->coef); //列印輸出
p=p->next; //指針迭代,進行循環輸出
}
printf(" ");
}
//銷毀linklist釋放空間
voiddestory_ll(structnode*head){
structnode*p,*q;
p=head->next;
while(p!=NULL){
q=p;
p=p->next;
free(q);
}
free(head);
}
//兩個多項式進行相加操作
voidadd_poly(structnode*head1,intlength1,structnode*head2,intlength2){
structnode*p1;
structnode*p2;
if(length1>length2){
p1=head1->next;
p2=head2->next;
while(p1!=NULL){
if(p1->index==p2->index){
p1->coef=(p1->coef)+(p2->coef);
p2=p2->next;
}
p1=p1->next;
}
}
else{
p1=head1->next;
p2=head2->next;
while(p2!=NULL){
if(p1->index==p2->index){
p2->coef=(p2->coef)+(p1->coef);
p1=p1->next;
}
p2=p2->next;
}
}
}
//兩個多項式進行相減操作
voidsub_poly(structnode*head1,intlength1,structnode*head2,intlength2){
structnode*p1;
structnode*p2;
if(length1>length2){
p1=head1->next;
p2=head2->next;
while(p1!=NULL){
if(p1->index==p2->index){
p1->coef=(p1->coef)-(p2->coef);
p2=p2->next;
}
p1=p1->next;
}
}
else{
p1=head1->next;
p2=head2->next;
while(p2!=NULL){
p2->coef=-(p2->coef);
if(p1->index==p2->index){
p2->coef=(p2->coef)+(p1->coef);
p1=p1->next;
}
p2=p2->next;
}
}
}
main(void){
structnode*head1; //多項式1頭節點
structnode*head2; //多項式2頭節點
intindex_current=0; //要輸入的指數
intcoef_current=0; //要輸入的系數
intindex_max1; //多項式1次數
intindex_max2; //多項式2次數
int i=0; //循環控制變數
head1=init_ll();
printf("-請輸入第一個多項式的次數- ");
scanf_s("%d",&index_max1);
for(i=0;i<index_max1;i++){
printf("-請輸入%d次項系數:",index_current);
scanf_s("%d",&coef_current);
insert_ll(head1,coef_current,index_current); //頭插法
index_current++;
}
head2=init_ll();
index_current=0;
printf("-請輸入第二個多項式的次數- ");
scanf_s("%d",&index_max2);
for(i=0;i<index_max2;i++){
printf("-請輸入%d次項系數:",index_current);
scanf_s("%d",&coef_current);
insert_ll(head2,coef_current,index_current); //頭插法
index_current++;
}
add_poly(head1,index_max1,head2,index_max2); //進行相加操作
//sub_poly(head1,index_max1,head2,index_max2); //進行相減操作
printf("多項式1的結果為:");
reverse_ll(head1);
printf("多項式2的結果為:");
reverse_ll(head2);
destory_ll(head1);
destory_ll(head2);
getchar();
getchar();
}
❼ C語言 多項式相加
/*為了一元多項式相加能夠使用效率更高的算發,應該在多項式的排序操作中加入合並同類項,除非你可以保證輸入不含有同類項*/
/*今天就這些吧,明天爭取寫出來,走了!*/
#include<stdio.h>
#include<stdlib.h>
typedef struct DXS{
float xs;
int zs;
struct DXS *next;
} multinomial;
//輸入函數
multinomial *input(void)
{
multinomial *p,*h;
h=NULL;
printf("please input the xs and zs of the DXS:\n");
for(;;)
{
p=(multinomial *)malloc(sizeof(multinomial));
scanf("%f %d",&p->xs,&p->zs);
if(p->xs==0 && p->zs==0){free(p);break;}
p->next=h;
h=p;//從頭部插入,沒有問題
}
return(h);
}
//將多項式排序
multinomial *inorder(multinomial *h)
{
multinomial *s,*t,*m,*h3;
h3=h->next;//指向第二個結點
h->next=NULL;//拆下第一個結點,為什麼?
while(h3!=NULL)
{
s=h3;
h3=h3->next;
t=h;
m=h;
while(s->zs<t->zs&&t!=NULL)
{
m=t;
t=t->next;
}
if(m=t)
{s->next=t;
h=s;
}
else
{
s->next=t;
m->next=s;
}
}
return(h);
}
//兩個多項式相加
multinomial *add(multinomial *h1,multinomial *h2)
{
multinomial *u,*v,*w,*j;
w=h2;j=h2;
while(j!=NULL)
{
u=v=h1;w=j;
while(u->zs>w->zs&&u!=NULL)
{v=u;
u=u->next;
}
if(u->zs==w->zs) //指數相同,則系數相加
{
u->xs=u->xs+w->xs;
if(u->xs==0) //如果系數為0,則做刪除操作
{
if(u=v) //刪除表頭
h1=h1->next;
}
else if(u->next==NULL) // 刪除表尾
v=NULL;
else //刪除表中
{
u=u->next;
}
}
else if(u=v) //待插入點在在表頭
{
w->next=u;
h1->next=w;
}
else //待插入點不是在表頭
{
w->next=u;
v->next=w;
}
j=j->next;
}
return(h1);
}
void print(multinomial *h1)
{
printf("f(x)=");
while(h1!=NULL)
{
printf("%f",h1->xs);
if(h1->zs!=0)
printf("x^%f",h1->xs);
else printf("%f",h1->xs);
h1=h1->next;
if(h1!=NULL&&h1->xs>0)
printf("+");
}
}
void main()
{multinomial *h1,*h2;
printf("輸入第一個多項式的系數和指數(系數與指數之間用空格號隔開)");
h1=input();
printf("輸入第二個多項式的系數和指數(系數與指數之間用空格號隔開");
h2=input();
h1=inorder(h1);
h2=inorder(h2);
h1=add(h1,h2);
print(h1);
}
❽ 多項式相加(C語言)
嚴蔚敏的數據結構上有
❾ C語言寫多項式相加怎麼寫
分都不給。。。算了,以前寫了個就給你吧。/*此程序結構比較清晰,用單鏈表實現了多項式的加法。具體原理可以參考清華大學嚴蔚敏數據《結構C語言版》第二章注意:此程序假設輸入的多項式已經排好序(從低到高)程序在VC6下編譯通過*/#includenbsp;amp;lt;stdio.hamp;gt;#includenbsp;amp;lt;stdlib.hamp;gt;#includenbsp;amp;lt;malloc.hamp;gt;#includenbsp;amp;lt;conio.hamp;gt;nbsp;/*getchnbsp;function*/typedefnbsp;structnbsp;NODEnbsp;{nbsp;floatnbsp;nbsp;coef;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;//系數為實數nbsp;intnbsp;nbsp;nbsp;nbsp;expn;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;//指數為整數nbsp;structnbsp;NODEnbsp;*next;nbsp;nbsp;}NODE;NODEnbsp;*Creat(intnbsp;n);voidnbsp;print(NODEnbsp;*head);NODEnbsp;*AddPolyn(NODEnbsp;*head1,nbsp;NODEnbsp;*head2);NODEnbsp;*Delfirst(NODEnbsp;*head,nbsp;NODEnbsp;*q);voidnbsp;InsertBefore(NODEnbsp;*p1,nbsp;NODEnbsp;*p2);intnbsp;nbsp;compare(intnbsp;a,nbsp;intnbsp;b);main(){nbsp;NODEnbsp;*head1,nbsp;*head2,nbsp;*head3;nbsp;intnbsp;n1,nbsp;n2;nbsp;printf(「<br/>請輸入第一個多項式的項數nbsp;n1:「);nbsp;scanf(「%d「,nbsp;amp;n1);nbsp;head1nbsp;=nbsp;Creat(n1);nbsp;printf(「<br/>第一個多項式為:<br/>「);nbsp;print(head1);nbsp;nbsp;nbsp;nbsp;nbsp;printf(「<br/>請輸入第二個多項式的項數nbsp;n2:「);nbsp;scanf(「%d「,nbsp;amp;n2);nbsp;head2nbsp;=nbsp;Creat(n2);nbsp;printf(「<br/>第二個多項式為:<br/>「);nbsp;print(head2);nbsp;head3nbsp;=nbsp;AddPolyn(head1,nbsp;head2);nbsp;printf(「<br/>相加結果為:<br/>「);nbsp;print(head3);nbsp;printf(「<br/>「);nbsp;getch();}/*創建鏈表*/NODEnbsp;*Creat(intnbsp;n){nbsp;NODEnbsp;*current,nbsp;*previous,nbsp;*head;nbsp;intnbsp;i;nbsp;headnbsp;=nbsp;(NODEnbsp;*)malloc(sizeof(NODE));nbsp;/*創建頭結點*/nbsp;previousnbsp;=nbsp;head;nbsp;nbsp;for(inbsp;=nbsp;0;nbsp;inbsp;amp;lt;nbsp;n;nbsp;i++)nbsp;{nbsp;nbsp;currentnbsp;=nbsp;(NODEnbsp;*)malloc(sizeof(NODE));nbsp;nbsp;nbsp;nbsp;printf(「請輸入項數%d的系數和指數nbsp;:nbsp;「,i+1);nbsp;nbsp;scanf(「%f%d「,nbsp;amp;current-amp;gt;coef,nbsp;amp;current-amp;gt;expn);nbsp;nbsp;previous-amp;gt;nextnbsp;=nbsp;current;nbsp;nbsp;previousnbsp;=nbsp;current;nbsp;nbsp;}nbsp;previous-amp;gt;nextnbsp;=nbsp;NULL;nbsp;returnnbsp;head;}/*nbsp;一元多項式的加法nbsp;,總體考慮,可分qa的指數比qb小,或等於pb(如果系數相加等於0和不等於0),或大於pb裡面由InsertBefore和Delfirst兩個小模塊組成一部分*/NODEnbsp;nbsp;*AddPolyn(NODEnbsp;*head1,nbsp;NODEnbsp;*head2){nbsp;NODEnbsp;*ha,nbsp;*hb,nbsp;*qa,nbsp;*qb;nbsp;intnbsp;a,nbsp;b;nbsp;floatnbsp;sum;nbsp;hanbsp;=nbsp;head1;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;/*ha和hb指向頭結點*/nbsp;hbnbsp;=nbsp;head2;nbsp;qanbsp;=nbsp;ha-amp;gt;next;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;/*qa和qb指向頭結點的下一個結點*/nbsp;qbnbsp;=nbsp;hb-amp;gt;next;nbsp;while(qanbsp;amp;amp;nbsp;qb)nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;/*qa和qb均非空*/nbsp;{nbsp;nbsp;anbsp;=nbsp;qa-amp;gt;expn;nbsp;nbsp;nbsp;bnbsp;=nbsp;qb-amp;gt;expn;nbsp;nbsp;switch(compare(a,nbsp;b))nbsp;{nbsp;nbsp;casenbsp;-1nbsp;:
❿ 用c語言實現多項式相加
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
float xi;
int n;
struct node *next;
}term;
void sort(term *head)
{
term *p,*q,*s;
p=(term *)malloc(sizeof(term));
q=(term *)malloc(sizeof(term));
s=(term *)malloc(sizeof(term));
p=head;
while(p->next!=NULL)
{
s=p->next;
while(s!=NULL)
{
if(p->n>s->n)
{
q->n=p->n;
p->n=s->n;
s->n=q->n;
q->xi=p->xi;
p->xi=s->xi;
s->xi=q->xi;
}
s=s->next;
}
p=p->next;
}
}
term *polynadd(term *head1,term *head2)
{
term *p,*q,*s,*r,*head3;
float x;
p=head1->next;
q=head2->next;
head3=(term *)malloc(sizeof(term));
r=head3;
while(p!=NULL&&q!=NULL)
{
if(p->n==q->n)
{
x=p->xi+q->xi;
if(x!=0)
{
s=(term *)malloc(sizeof(term));
s->xi=x;
s->n=p->n;
r->next=s;
r=s;
}
p=p->next;
q=q->next;
}
else if(p->n>q->n)
{
s=(term *)malloc(sizeof(term));
s->n=q->n;
s->xi=q->xi;
r->next=s;
r=s;
q=q->next;
}
else
{
s=(term *)malloc(sizeof(term));
s->n=p->n;
s->xi=p->xi;
r->next=s;
r=s;
p=p->next;
}
}
while(p!=NULL)
{
s=(term *)malloc(sizeof(term));
s->n=p->n;
s->xi=p->xi;
r->next=s;
r=s;
p=p->next;
}
while(q!=NULL)
{
s=(term *)malloc(sizeof(term));
s->n=q->n;
s->xi=q->xi;
r->next=s;
r=s;
q=q->next;
}
r->next=NULL;
return head3;
}
term *createpolyn(int m)
{
term *p,*head,*q;
int i;
head=(term *)malloc(sizeof(term));
q=head;
for(i=0;i<m;i++)
{
p=(term *)malloc(sizeof(term));
printf("請輸入第%d項數的系數和指數\n",i+1);
scanf("%f%d",&p->xi,&p->n);
q->next=p;
q=p;
}
p->next=NULL;
return head;
}
dayin(term *head)
{
term *p,*q;
q=head->next;
if(q->xi==0)
printf(" ");
if(q->n==0&&q->xi>0&&q->xi!=1)
printf("%0.2f",q->xi);
if(q->n==0&&q->xi==1)
printf("1");
if(q->n==0&&q->xi<0)
printf("%0.2f",q->xi);
if(q->n==1&&q->xi==1)
printf("x");
if(q->n==1&&q->xi!=1&&q->xi>0)
printf("%0.2fx",q->xi);
if(q->n==1&&q->xi!=1&&q->xi<0)
printf("%0.2fx",q->xi);
if(q->n!=1&&q->n!=0&&q->xi>0&&q->xi!=1)
printf("%0.2fx^%d",q->xi,q->n);
if(q->n!=1&&q->n!=0&&q->xi<0)
printf("%0.2fx^%d",q->xi,q->n);
if(q->n!=0&&q->n!=1&&q->xi==1)
printf("x^%d",q->n);
p=q->next;
while(p!=NULL)
{
if(p->xi==0)
printf(" ");
if(p->n==0&&p->xi>0&&p->xi!=1)
printf("+%0.2f",p->xi);
if(p->n==0&&p->xi==1)
printf("+1");
if(p->n==0&&p->xi<0)
printf("%0.2f",p->xi);
if(p->n==1&&p->xi==1)
printf("+x");
if(p->n==1&&p->xi!=1&&p->xi>0)
printf("+%0.2fx",p->xi);
if(p->n==1&&p->xi!=1&&p->xi<0)
printf("%0.2fx",p->xi);
if(p->n!=1&&p->n!=0&&p->xi>0&&p->xi!=1)
printf("+%0.2fx^%d",p->xi,p->n);
if(p->n!=1&&p->n!=0&&p->xi<0)
printf("%0.2fx^%d",p->xi,p->n);
if(p->n!=0&&p->n!=1&&p->xi==1)
printf("+x^%d",p->n);
p=p->next;
}
printf("\n");
}
main()
{
int i,j;
term *head1,*head2,*head3;
printf("請輸入第一個多項式的項數:\n");
scanf("%d",&i);
head1=createpolyn(i);
sort(head1);
printf("\n");
dayin(head1);
printf("\n");
printf("請輸入第二個多項式的項數:\n");
scanf("%d",&j);
head2=createpolyn(j);
sort(head2);
printf("\n");
dayin(head2);
printf("\n\n");
head3=polynadd(head1,head2);
sort(head3);
printf("\n");
dayin(head3);
printf("\n");
}