C語言 陣列結構 問題 有大大幫忙一下的嗎?

2009-03-31 5:43 pm
資料結構的題目如下:

題目:使用C語言的陣列結構儲存二個多項式,以建立副程式執行多項式相加的運算,最後將結果輸出。例如:A(X)=7X5+3X2+8,B(X)=5X6+2X5+9X2+X+12。輸出:C(X)=5X6+9X5+12X2+X+20
更新1:

bean_bottom_2 抱歉我忘了打上平方 ˊˋ"....因為我是直接複製的 題目:A(X)=7X^5+3X^2+8,B(X)=5X^6+2X^5+9X^2+X+12。輸出:C(X)=5X^6+9X^5+12X^2+X+20 你的答案是正確的^^

回答 (1)

2009-03-31 6:33 pm
✔ 最佳答案
#include <stdio.h>

int main()
{
int a[15], b[15], c[15];
int i,j;

for (i = 0; i < 15; i++) {
a[i] = b[i] = c[i] = 0;
}
a[5] = 7;
a[2] = 3;
a[0] = 8;
b[6] = 5;
b[5] = 2;
b[2] = 9;
b[0] = 12;

for (i = 0; i < 15; i++) {
c[i] = b[i] + a[i];
}

printf("C(X) = ");
i=14;
while (c[i] == 0) {
i--;
}
printf("%dX%d ",c[i],i);

for (j = i-1; j >= 0; j--) {
if (c[j] != 0) {
if (j != 0) {
printf(" + %dX%d",c[j],j);
} else {
printf(" + %d",c[j]);
}
}
}
printf("\n");
}

2009-03-31 22:35:13 補充:
The program has a limit of X^14 due to the declaration of integer array with size 15. You can increase that if you want or use link-list to implement it.


收錄日期: 2021-04-30 12:53:56
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090331000016KK02099

檢視 Wayback Machine 備份