Re: [問題] 程式的邏輯問題

看板C_and_CPP (C/C++)作者時間16年前 (2009/04/27 22:16), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串3/3 (看更多)
看到 adrianshum大大用for簡化程式碼,所以我就把這一題 重新改寫了一下,不過感覺上還是很長,不知道還有沒有辦  法讓程式看起來更短呢?新手上路,還請各位高手多多指教  ~^_^ #include <stdlib.h> #include <iostream.h> struct node { int data; struct node *next; }; int main() { struct node *list=NULL,*head=NULL; int temp; cout << "請輸入數筆數字資料:" << endl; cin >> temp; if(temp!=0){ list=(node *)malloc(sizeof(node)); list->data=temp; head=list; cin >> temp; while(temp!=0){ list->next=(node *)malloc(sizeof(node)); list=list->next; list->data=temp; cin>>temp; } } list->next=NULL; cout << endl; cout << "請輸入欲比較大小的數字:"; cin >> temp; cout << "比 "<< temp << " 大的數字有.." << endl; for(list=head;list!=NULL;list=list->next){ if(list->data>temp) cout << list->data << endl; } for(list=head->next;list!=NULL;list=list->next){ free(head); head=list; } free(list); head=NULL; list=NULL; return 1; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.133.117.77

04/27 22:57, , 1F
do{/*...*/} while( !=0); malloc node 就要給值
04/27 22:57, 1F
文章代碼(AID): #19zRvM-i (C_and_CPP)
文章代碼(AID): #19zRvM-i (C_and_CPP)