for(p!=NULL)
10年積累的成都網(wǎng)站建設(shè)、成都網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有蒲城免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
{
for((q!=NULL)(q-expn=p-expn))
改成
while(p!=NULL)
{
while((q!=NULL)(q-expn=p-expn))
我這有一個實現(xiàn)加減乘除的多項式程序,自己寫的,另外輸入形式為:-2x^3 +5x^2+3x+4 即可。
其中百度的現(xiàn)實問題,有一個?A和?B的 應(yīng)該是 COPYA, COPYB
去掉中間空格
支持整數(shù)多項式加減乘除。
#includestdio.h
#includestdlib.h
#includectype.h
typedef struct _POLYNODE{
int coef;//系數(shù)
int exp;//指數(shù)
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;//申請節(jié)點,初始化參數(shù)為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;
}//抽取數(shù)字
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 遍歷到加減乘除,則退出循環(huán),到下一新的節(jié)點
iscoef = 1;
if('\0' != *t){
if('-' == *t)
isminus = -1;
t++;
}
if(0 == j){
Q-coef = 0;
j = 1;
}
printf("系數(shù):%d,指數(shù):%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("多項式未被建立,請先輸入多項表達(dá)式.\n");
return ;
}
while(NULL != P){//復(fù)制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){//復(fù)制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("相加結(jié)果為:");
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("多項式未被建立,請先輸入多項表達(dá)式.\n");
return ;
}
while(NULL != P){//復(fù)制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){//復(fù)制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("相減結(jié)果為:");
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("多項式未被建立,請先輸入多項表達(dá)式.\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("多項式未被建立,請先輸入多項表達(dá)式.\n");
return ;
}
if(A-coef == 0){
printf("0.\n");
return ;
}
if(B-coef == 0){
printf("除數(shù)為0,錯誤!\n");
return ;
}
if(A-coef B-coef){
printf("商:0,余數(shù)為:");
display(A);
return ;
}
while(NULL != P){//復(fù)制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){//復(fù)制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("余數(shù):");
display(COPYA);
destroy(L);
destroy(?A);
destroy(?B);
}
void display(polynode *P){
//考慮情況有系數(shù)為1,指數(shù)為1,0,一般數(shù);系數(shù)為系數(shù)不為1,指數(shù)為1,0,一般數(shù);
//系數(shù)為負(fù)數(shù),指數(shù)為1,0,一般數(shù),主要考慮中間符號問題.
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){
//首先 系數(shù)為零的要清掉,其次指數(shù)從高到低排序,再者系數(shù)相同的要合并.
polyptr prev,curr,OUT,INcurr;//前一節(jié)點和當(dāng)前節(jié)點
int temp;
//出去第一節(jié)點系數(shù)為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項或空,則不需要整理,退出函數(shù)
return;
//冒泡排序
OUT = INcurr = *P;
while(NULL != OUT-next){//外循環(huán)
while(NULL != INcurr-next){//內(nèi)循環(huán)
prev = INcurr;
INcurr = INcurr-next;
if(prev-exp INcurr-exp){
temp = prev-coef;
prev-coef = INcurr-coef;
INcurr-coef = temp;//交換系數(shù)
temp = prev-exp;
prev-exp = INcurr-exp;
INcurr-exp = temp;//交換指數(shù)
}
}
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);//整理排序多項式,默認(rèn)降冪
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:系數(shù)只支持整數(shù),計算除法存在誤差;\n如果除數(shù)所有項系數(shù)為1,能得到正確答案,或者某些情況系數(shù)剛好被整除.");
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);
}
}
}
#include stdio.h
int Fluction(int);//聲明實現(xiàn)多項式 1-1/2+1/3-1/4+1/5-1/6+...的功能函數(shù)
double sum;//定義全局變量(其實一般不推薦定義全局變量)
int main()
{
int m,n;//m個測試實例,求前 n項和
while(scanf("%d",m)!=EOF)
{
for(int i=1;i=m;i++)//輸入 m個測試實例,所以循環(huán) m次
{
scanf("%d",n);
Fluction(n);//調(diào)用函數(shù),傳參 n
printf("%.2lf\n",sum);//保留兩位小數(shù)輸出
}
}
}
int Fluction(int t)//函數(shù)定義,實現(xiàn) 1-1/2+1/3-1/4+1/5-1/6+...多項式
{
int sign=1;//定義符號
double x;
sum=0;
for(int i=1;i=t;i++)//要求前幾項的和就循環(huán)幾次
{
x=(double)sign/i;//強(qiáng)制轉(zhuǎn)變類型
sum+=x;
sign*=-1;
}
return sum;//一定要定義它返回 sum的值,否則,函數(shù)會自動返回 0
}
#includestdio.h
#includemalloc.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-expop-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;
}
//插入結(jié)點,結(jié)點非首
if(prev != NULL)
{
p-next=curr;
prev-next=p;
//添加代碼
}
//插入結(jié)點,結(jié)點為首
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;
}