線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:968
推到 Plurk!
推到 Facebook!

請問有關兩種鏈結型態寫多項式相加問題~

尚未結案
victor0060
一般會員


發表:3
回覆:0
積分:0
註冊:2005-04-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-25 07:34:06 IP:210.66.xxx.xxx 未訂閱
教授丟給我們兩個課題請用circular list和chain撰寫多項式相加程式 但我找到的範例好像都是circular list如下 { /* 多項式相加--使用降冪排列輸入兩個格式為ax^b的多項式相加 */ #include  #include void input(struct poly **); /* 輸入函數 */ void poly_add(void); /* 多項式相加函數 */ void show_ans(void); /* 顯示多項式相加結果函數 */ struct poly { int coef; /* 多項式係數 */ int exp; /* 多項式指數 */ struct poly *next; }; struct poly *ptr, *eq_h1, *eq_h2, *ans_h; void main(void) { printf("****************************************\n"); printf(" -- Polynomial add using format ax^b --\n"); printf("****************************************\n"); printf("Please enter the first equation: "); input(&eq_h1); printf("Please enter the second equation: "); input(&eq_h2); poly_add(); show_ans(); } void input(struct poly **eq_h) { struct poly *prev = NULL; char symbol = ' '; do { ptr = (struct poly *) malloc(sizeof(struct poly)); ptr->next = NULL; scanf("%dx^%d", &ptr->coef, &ptr->exp); if(*eq_h == NULL) *eq_h = ptr; else { if(symbol == '-') ptr->coef = -(ptr->coef); prev->next = ptr; } prev = ptr; scanf("%c", &symbol); } while(symbol != '\n'); } void poly_add(void) { struct poly *this_n1, *this_n2, *prev; this_n1 = eq_h1; this_n2 = eq_h2; prev = NULL; while(this_n1 != NULL || this_n2 != NULL) /* 當兩個多項式皆相加完畢則結束 */ { ptr = (struct poly *) malloc(sizeof(struct poly)); ptr->next = NULL; /* 第一個多項式指數大於第二個多項式 */ if(this_n1 != NULL && (this_n2 == NULL || this_n1->exp > this_n2->exp)) { ptr->coef = this_n1->coef; ptr->exp = this_n1->exp; this_n1 = this_n1->next; } else /* 第一個多項式指數小於第二個多項式 */ if(this_n1 == NULL || this_n1->exp < this_n2->exp) { ptr->coef = this_n2->coef; ptr->exp = this_n2->exp; this_n2 = this_n2->next; } else /* 兩個多項式指數相等,進行相加 */ { ptr->coef = this_n1->coef this_n2->coef; ptr->exp = this_n1->exp; if(this_n1 != NULL) this_n1 = this_n1->next; if(this_n2 != NULL) this_n2 = this_n2->next; } if(ptr->coef != 0) /* 當相加結果不等於0,則放入答案多項式中 */ { if(ans_h == NULL) ans_h = ptr; else prev->next = ptr; prev = ptr; } else free(ptr); } } void show_ans(void) { struct poly *this_n; this_n = ans_h; printf("The answer equation: "); while(this_n != NULL) { printf("%dx^%d", this_n->coef, this_n->exp); if(this_n->next != NULL && this_n->next->coef >= 0) printf(" "); this_n = this_n->next; } printf("\n"); } 同樣都是在鏈結的章節中,chain和circular list的表示方式到底有何不同請各位高手解答~
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-04-25 09:07:04 IP:210.68.xxx.xxx 未訂閱
您好: PO程式碼的方式請參考版規說明,煩請修改謝謝您的配合 < href="http://delphi.ktop.com.tw/topic.php?TOPIC_ID=58991">http://delphi.ktop.com.tw/topic.php?TOPIC_ID=58991
系統時間:2024-06-01 23:06:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!