![]() |
|
論壇說明 |
歡迎您來到『史萊姆論壇』 ^___^ 您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的! 請點擊這裡:『註冊成為我們的一份子!』 |
![]() ![]() |
|
主題工具 | 顯示模式 |
![]() |
#1 |
|
![]() 小弟不才...向大家求救
請問各位高手!要如何將下面的程式(多項式相加)改成(多項式相減) 請大家救救我吧! #include <stdlib.h> struct plist /* 多項式結構宣告 */ { int coef; /* 多項式的係數 */ int exp; /* 多項式的指數 */ struct plist *next; /* 指向下一節點的指標 */ }; typedef struct plist pnode; /* 多項式新型態 */ typedef pnode *plink; /* 多項式指標新型態 */ /* ---------------------------------------- */ /* 多項式含開頭節點的環狀鍵結串列的列印 */ /* ---------------------------------------- */ void print_poly(plink poly) { plink ptr; ptr = poly->next; /* 指向串列開始 */ while ( poly != ptr ) /* 串列走訪迴路 */ { /* 列印節點資料 */ printf("%dX^%d",ptr->coef,ptr->exp); ptr = ptr->next; /* 指向下一個節點 */ if ( poly != ptr) printf("+"); } printf("\n"); /* 換行 */ } /* ---------------------------------------- */ /* 使用陣列值建立多項式 */ /* ---------------------------------------- */ plink create_poly(int *array,int len) { plink head; /* 環狀串列的指標 */ plink before; /* 前一節點的指標 */ plink new_node; /* 新節點的指標 */ int i; /* 建立開頭節點 */ /* 配置節點記憶體 */ head = ( plink ) malloc(sizeof(pnode)); if ( !head ) /* 檢查記憶體指標 */ return NULL; head->exp = -1; /* 建立節點內容 */ before = head; /* 指向第一個節點 */ for ( i = len - 1; i >= 0; i-- ) /* 用迴路建立其它節點 */ if ( array[i] != 0 ) { /* 配置節點記憶體 */ new_node = ( plink ) malloc(sizeof(pnode)); if ( !new_node ) /* 檢查記憶體指標 */ return NULL; new_node->coef = array[i]; /* 建立係數內容 */ new_node->exp = i; /* 建立指數內容 */ new_node->next = NULL; /* 設定指標初值 */ before->next = new_node; /* 將前節點指向新節點 */ before = new_node; /* 新節點成為前節點 */ } new_node->next = head; /* 建立環狀鏈結 */ return head; /* 傳回串列起始指標 */ } /* ---------------------------------------- */ /* 多項式相加 */ /* ---------------------------------------- */ plink poly_add(plink poly1,plink poly2) { plink head1; /* 多項式1的開始 */ plink head2; /* 多項式2的開始 */ plink result; /* 多項式的結果 */ plink before; /* 前一節點的指標 */ plink new_node; /* 新節點的指標 */ head1 = poly1->next; /* 指向多項式1的開始 */ head2 = poly2->next; /* 指向多項式2的開始 */ /* 建立開頭節點且配置節點記憶體 */ result = ( plink ) malloc(sizeof(pnode)); if ( !result ) /* 檢查記憶體指標 */ return NULL; result->exp = -1; /* 建立節點內容 */ before = result; /* 指向第一個節點 */ while ( poly1 != head1 || poly2 != head2 ) { /* 配置節點記憶體 */ new_node = ( plink ) malloc(sizeof(pnode)); if ( !new_node ) /* 檢查記憶體指標 */ return NULL; if ( head1->exp < head2->exp ) /* 多項式2的指數大 */ { new_node->coef = head2->coef; /* 設定係數 */ new_node->exp = head2->exp; /* 設定指數 */ head2 = head2->next; /* 指向下一節點 */ } else if ( head1->exp > head2->exp ) /*多項式1的指數大 */ { new_node->coef = head1->coef; /* 設定係數 */ new_node->exp = head1->exp; /* 設定指數 */ head1 = head1->next; /* 指向下一節點 */ } else /* 多項式的指數相等 */ { /* 係數相加 */ new_node->coef = head1->coef + head2->coef; new_node->exp = head1->exp; /* 設定指數 */ head1 = head1->next; /* 指向下一節點 */ head2 = head2->next; /* 指向下一節點 */ } before->next = new_node; /* 將前節點指向新節點 */ before = new_node; /* 新節點成為前節點 */ } new_node->next = result; /* 建立環狀鏈結 */ return result; /* 傳回多項式的指標 */ } /* ---------------------------------------- */ /* 主程式: 多項式相加 */ /* ---------------------------------------- */ void main() { plink poly1; /* 多項式1的指標 */ plink poly2; /* 多項式2的指標 */ plink result; /* 多項式結果的指標 */ int list1[6] = { 4, 0, 3, 0, 7, 0 }; /* 陣列1內容 */ int list2[6] = { 9, 7, 1, 0, 5, 6 }; /* 陣列2內容 */ poly1 = create_poly(list1,6); /* 建立多項式1 */ printf("多項式1內容: "); print_poly(poly1); /* 印出多項式1 */ poly2 = create_poly(list2,6); /* 建立多項式2 */ printf("多項式2內容: "); print_poly(poly2); /* 印出多項式2 */ result = poly_add(poly1,poly2); /* 多項式相加 */ printf("多項式相加結果: "); print_poly(result); /* 印出多項式結果 */ } |
送花文章: 0,
![]() |