Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int coe[50],pow[50];
- void sort(int start,int end)
- {
- int i,j,temp;
- for(i=start;i<end;i++)
- {
- for(j=start;j<end;j++)
- {
- if(pow[i] > pow [j])
- {
- temp=pow[i];
- pow[i]=pow[j];
- pow[j]=temp;
- temp=coe[i];
- coe[i]=coe[j];
- coe[j]=temp;
- }
- }
- }
- }
- void combine(int start,int end)
- {
- int i,j;
- for(i=start;i<end;i++)
- {
- for(j=i+1;j<=end;j++)
- {
- if(pow[i] == pow[j])
- {
- coe[i]+=coe[j];
- pow[j]=0;
- coe[j]=0;
- }
- }
- }
- }
- void Print(int start,int end)
- {
- int i,j=0;
- for(i=start;i<=end;i++)
- {
- if(coe[i] != 0)//係數不為0
- {
- if(pow[i] != 0)
- {
- if(coe[i] == -1 )
- {
- printf("-x^%d",pow[i]);
- j=1;
- }
- else if(coe[i] == 1)
- {
- printf("x^%d",pow[i]);
- j=1;
- }
- else
- {
- printf("%dx^%d",coe[i],pow[i]);
- j=1;
- }
- }
- else
- {
- printf("%d",coe[i]);
- j=0;
- }
- }
- if(j == 1)//係數為0
- {
- if(coe[i] >0)
- {
- printf("+");
- }
- }
- j=1;
- }
- }
- int main()
- {
- int i,j;
- int startA=0,startB,finishA=0,finishB=0,avail;
- while(1)
- {
- printf("請輸入多項式1 (係數 次方):");
- scanf("%d %d",&coe[finishA],&pow[finishA]);
- if(coe[finishA] == 0 && pow[finishA] == 0)
- {
- finishA--;
- startB = finishA+1;
- break;
- }
- else
- {
- finishA++;
- }
- }
- combine(startA,finishA);
- sort(startA,finishA);
- printf("\n多項式1為:\n");
- Print(startA,finishA);
- finishB = startB;
- printf("\n");
- while(1)
- {
- printf("請輸入多項式2 (係數 次方):");
- scanf("%d %d",&coe[finishB],&pow[finishB]);
- if(coe[finishB] == 0 && pow[finishB] == 0)
- {
- finishB--;
- avail = finishB+1;
- break;
- }
- else
- {
- finishB++;
- }
- }
- combine(startB,finishB);
- sort(startB,finishB);
- printf("\n多項式2為:\n");
- Print(startB,finishB);
- for(i=startA;i<=finishA;i++)
- {
- for(j=startB;j<=finishB;j++)
- {
- coe[avail] = coe[i]*coe[j];
- pow[avail] = pow[i]+pow[j];
- avail++;
- }
- }
- combine(finishB+1,avail);
- sort(finishB+1,avail);
- printf("\n");
- printf("相乘過後之多項式為:\n");
- Print(finishB+1,avail);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement