Re: [問題] 程式的邏輯問題
看到 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
04/27 22:57, 1F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章