## 2010年9月11日 星期六

### [ 資料結構 小學堂 ] 陣列結構 : 矩陣與多項式

* ch02.h 代碼 :
1. #include
2. using namespace std
3.
4. /*
5. * As ch02_07.cpp
6. * 將多項式矩陣 polyA 與 polyB 進行相加並將結果回存 polyC. 其中參數 itimes 為多項式的最高次項.
7. */
8. void polySum(int* polyA, int* polyB, int* polyC, int itimes);
* ch02.cpp 代碼 :
1. void polySum(int* polyA, int* polyB, int* polyC, int itimes) {
2.     for(int i=1; i<=(itimes+1); i++) {
3.         polyC[i] = polyA[i] + polyB[i];
4.     }
5. }
* 呼叫演算碼範例代碼 :
1. #include "ch02.h"
2.
3. /*
4. * 顯示傳入的多項式矩陣.
5. */
6. void _printPoly(char* message, int *poly) {
7.     printf("[  %s ]->", message);
8.     int maxItem = poly[0];
9.     for(int i=1; i<=(poly[0] + 1); i++) {
10.         if(poly[i]!=0) {
11.             if(maxItem>0) {
12.                 printf("%dX^%d", poly[i], maxItem);
13.             } else {
14.                 printf("%d", poly[i]);
15.             }
16.             for(int j=i+1; j<=(poly[0]+1); j++)
17.                 if(poly[j]>0) {
18.                     cout << " + ";
19.                     break;
20.                 }
21.         }
22.         maxItem--;
23.     }
24.     cout << endl;
25. }
26.
27. void ch02_07(bool b) {
28.     if(b) {
29.         const int ITEMS = 4;
30.         int polyA[ITEMS + 2] = {437062};
31.         _printPoly("多項式A", polyA);
32.         int polyB[ITEMS + 2] = {415209};
33.         int polyC[ITEMS + 2] = {0};
34.         polyC[0] = polyA[0];
35.         _printPoly("多項式B", polyB);
36.         printf("[  多項式相加結果(polyA+polyB=polyC) ]\n");
37.         polySum(polyA, polyB, polyC, polyA[0]);
38.         _printPoly("多項式C", polyC);
39.     }
40. }

[ 多項式A ]->3X^4 + 7X^3 + 6X^1 + 2
[ 多項式B ]->1X^4 + 5X^3 + 2X^2 + 9
[ 多項式相加結果(polyA+polyB=polyC) ]
[ 多項式C ]->4X^4 + 12X^3 + 2X^2 + 6X^1 + 11
This message was edited 1 time. Last update was at 21/03/2010 01:10:42

### [ Python 常見問題 ] python generator “send” function purpose?

Source From  Here   Question   Can someone give me an example of why the "send" function associated with Python generator function...