兩個多項式相乘後合并同類項並以指數從低到高順序排序並列印C語言

來源:互聯網
上載者:User

  說成難以置信,簡直難以置信.因為我這種能力的人,居然還敢說難以置信.真的難以置信.

  這個東西,寫了大約6小時?我本想更快的,但結構卻跟我想得相差甚多.

  原本以為自己的指標使用得爐火純青了,但這麼說話確實顯得沒什麼程度.我對記憶體不熟悉,怎麼能深刻地理解指標呢?不過我還是不擔心,因為我的書就快看到記憶體部分了,呵呵.

  我想精簡操作,並沒有像寫ADT那樣寫出那麼多分工那麼明確的函數,而是強調快速,高效.無論是對機器,還是對我.

  我的思路,比較聰明吧.因為我目前還沒學很多東西,呵呵.這算是對我自己的嘲笑嗎?主要的思路就是幾個關鍵字概括吧:鏈表, 雜湊表.

  卡殼的主要地方,是在為鏈表分配空間的時候.我第一次感受到了記憶體的巨大壓力,我試圖一次分配一大塊連續的空間,這樣就能減少程序呼叫了.可事實上,老是沒有足夠的棧空間歸我調用.於是,我更改了,放棄了這個提高效率的想法.再一個,對於表的大小.我的思路是,建立一個表的大小為相乘後最大指數+1,但開始我並沒有這麼做.後來,發現了.但我確實發現,我確實浪費了很多空間.不過,沒辦法了,當初就是這麼設計的,中途更改將花費大量的工時,於是我向我的完美低頭了,為了結果,我妥協了.當然,我最終是實現了的.其次,就是一些看似不起眼的細節問題.

  最部分代碼上,我察覺到我對鏈表的理解不錯,至少沒白學.其次,最主要的我想說的是,我盲目地想要最佳化他的效能.這帶來的損失是慘痛的.最近在看最佳化程式效能的的書,於是我給用上了.對於這個問題,首先,我對程式的最佳化能力還不強.其次,我不應該在構造程式的過程中嘗試最佳化.這樣帶來的效果就是,程式難寫,分工不明確.最佳化,應該在寫完程式之後才進行.這麼做,最佳化操作簡單,而且不破壞模組性.的確是很聰明的做法.而我的做法,實在是太愚蠢了.再一個,通過一個其他的問題,我知道,我不該依賴F7來協助我檢測錯誤.這樣只會害了我,F7,應該是確認無誤之後才去做的事.養成好的習慣,有助於我成長.因為我堅信,我,要麼成功,要麼走火入魔.因為,我不會放棄這條路.這是我人生僅存的理想了.

  好吧,貼出My Code.

/*5-7-10-24-00.29 -- 第五章第七題*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include <time.h></p><p>#define USED 1<br />#define LEISURE 0</p><p>typedef int Item ;<br />typedef struct node<br />{<br />Item coefficent ;/*係數*/<br />Item power ;/*指數*/<br />struct node * next ;<br />} Node ;/*結點,可用作多項式鏈表結點和表的元素*/<br />typedef struct linkedlist<br />{<br />Node * noumenon ;/*指向結點的指標*/<br />int sum_of_power ;/*指數和*/<br />} * LinkedList ;<br />typedef struct cell<br />{<br />Item coefficent ;<br />Item power ;<br />int condition ;<br />} Cell ;<br />typedef struct hashtable<br />{<br />int size ;/*表的大小*/<br />Cell * cell ;/*指向元素的指標*/<br />} * HashTable ;</p><p>int main (void) ;<br />int hash (const int key, const int size) ;<br />int get_random_coefficent (const int max) ;<br />int get_random_power (const int max) ;<br />LinkedList create_list (const int lenth, const int c_max, const int p_max) ;<br />HashTable create_table (const int size) ;<br />void print_multinomial (Node * const multinomial) ;<br />void print_table (const HashTable * const ph) ;<br />void Release_list (const LinkedList * const pl) ;<br />void Release_table (const HashTable * const ph) ;</p><p>int main (void)<br />{<br />LinkedList m1, m2 ;<br />Node * scan_m1, * scan_m2 ;<br />Cell * temp ;<br />HashTable h ;<br />int lenth_1, lenth_2, c_max_1, c_max_2, p_max_1, p_max_2, table_size, key ;</p><p>lenth_1 = 7 ;/*多項式1項數*/<br />c_max_1 = 50 ;/*多項式1最高係數*/<br />p_max_1 = 12 ;/*多項式1最高指數*/<br />lenth_2 = 9 ;/*多項式2項數*/<br />c_max_2 = 12 ;/*多項式2最高係數*/<br />p_max_2 = 7 ;/*多項式2最高指數*/</p><p>m1 = create_list (lenth_1, c_max_1, p_max_1) ;<br />m2 = create_list (lenth_2, c_max_2, p_max_2) ;<br />table_size = p_max_1 + m2 -> sum_of_power + 1 ;<br />h = create_table (table_size) ;<br />for (scan_m1 = m1 -> noumenon; scan_m1 != NULL; scan_m1 = scan_m1 -> next)<br />{<br />for (scan_m2 = m2 -> noumenon; scan_m2 != NULL; scan_m2 = scan_m2 -> next)<br />{<br />scan_m1 -> coefficent *= scan_m2 -> coefficent ;<br />scan_m1 -> power += scan_m2 -> power ;<br />key = hash (scan_m1 -> power, table_size) ;<br />temp = &h -> cell[key] ;<br />if (LEISURE == temp -> condition)<br />{<br />temp -> coefficent = scan_m1 -> coefficent ;<br />temp -> power = scan_m1 -> power ;<br />temp -> condition = USED ;<br />}<br />else<br />temp -> coefficent += scan_m1 -> coefficent ;<br />}<br />}<br />print_table (&h) ;</p><p>Release_list (&m1) ;<br />Release_list (&m2) ;<br />Release_table (&h) ;</p><p>return 0 ;<br />}</p><p>int hash (const int key, const int size)<br />{<br />return key % size ;<br />}</p><p>int get_random_coefficent (const int max)<br />{<br />return rand () % max + 1 ;<br />}</p><p>int get_random_power (const int max)<br />{<br />return rand () % max + 1 ;<br />}</p><p>LinkedList create_list (const int lenth, const int c_max, const int p_max)<br />{<br />LinkedList list ;<br />Node * new_node, * parent = NULL ;<br />int count, sum_of_power = 0 ;</p><p>list = (struct linkedlist *) malloc (sizeof (struct linkedlist)) ;</p><p>for (count = 0; count < lenth; count++)<br />{<br />new_node = (Node *) malloc (sizeof (Node)) ;<br />if (NULL == new_node)<br />puts ("Out of space[1]") ;<br />if (parent != NULL)<br />parent -> next = new_node ;<br />else<br />list -> noumenon = new_node ;<br />new_node -> coefficent = get_random_coefficent (c_max) ;<br />new_node -> power = get_random_coefficent (p_max) ;<br />sum_of_power += new_node -> power ;<br />parent = new_node ;<br />}<br />parent -> next = NULL ;<br />list -> sum_of_power = sum_of_power ;</p><p>return list ;<br />}</p><p>HashTable create_table (const int size)<br />{<br />HashTable h ;<br />int count ;</p><p>h = (struct hashtable *) malloc (sizeof (struct hashtable)) ;<br />if (NULL == h)<br />puts ("Out of space[2]") ;<br />h -> cell = (Cell *) malloc (sizeof (Cell) * size) ;<br />if (NULL == h -> cell)<br />puts ("Out of space[3]") ;<br />h -> size = size ;<br />for (count = 0; count < size; count++)<br />h -> cell[count].condition = LEISURE ;</p><p>return h ;<br />}</p><p>void print_multinomial (Node * const multinomial)<br />{<br />Node * scan = multinomial ;</p><p>while (scan)<br />{<br />printf ("[係數:%-30d 指數:%-10d]/n", scan -> coefficent, scan -> power) ;<br />scan = scan -> next ;<br />}<br />putchar ('/n') ;<br />}</p><p>void print_table (const HashTable * const ph)<br />{<br />int count, size = (*ph) -> size ;<br />Cell temp ;</p><p>for (count = 0; count < size; count++)<br />{<br />temp = (*ph) -> cell[count] ;<br />if (USED == temp.condition)<br />printf ("[係數:%-30d指數:%-10d]/n", temp.coefficent, temp.power) ;<br />}<br />}</p><p>void Release_list (const LinkedList * const pl)<br />{<br />Node * scan = (*pl) -> noumenon, * temp ;</p><p>while (scan)<br />{<br />temp = scan ;<br />scan = scan -> next ;<br />free (temp) ;<br />}<br />free (*pl) ;<br />}</p><p>void Release_table (const HashTable * const ph)<br />{<br />free ((*ph) -> cell) ;<br />free (*ph) ;<br />}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.