作者shadown ()
看板C_and_CPP
标题Re: [问题] 程式的逻辑问题
时间Mon Apr 27 22:16:53 2009
看到 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
1F:→ softwind:do{/*...*/} while( !=0); malloc node 就要给值 04/27 22:57