關於郭益銘問題的解答

問題與回答

C 語言

Java

C#

學習方法

日常生活

專題製作

訊息

相關網站

參考文獻

最新修改

簡體版

English

原始問題

下列程式有錯

#include <cstdlib>
#include <iostream>  
struct student  
{ 
 int num;  
char name[10]; 
 int score; 
 struct student*next;
};
typedef struct student node;
typedef node*link;

int main()
{
link newnode,ptr,delptr;
int i;
printf("請輸入5筆學生資料:\n");
delptr=(link)malloc(sizeof(node));

if(!delptr)
{
printf("[error!!記憶體配置失敗!]\n");

exit(1);

}
printf("請輸入座號");
scanf("%d",&delptr->num);
printf("請輸入姓名");
scanf("%s",&delptr->name);
printf("請輸入成績");
scanf("%d",&delptr->score);
ptr=delptr;
for(i=1;i<5;i++)
{
newnode=(link)malloc(sizeof(node));
if(!newnode)
{
printf("[error!!記憶體配置失敗!]\n");
exit(1);
}
printf("請輸入座號");
 scanf("%d",&newnode->num);          
printf("請輸入姓名");          
scanf("%s",&newnode->name);          
printf("請輸入成績");          
scanf("%d",&newnode->score);          
newnode->next=NULL;          
ptr->next=newnode          
ptr=ptr->next;
}
printf("\n學生成績\n");          
printf("座號\t姓名\t成績\n====================\n");           ptr=delptr;          
while(ptr!NULL)
{
printf("%3d\t%-s\t%3d\n",ptr->num,ptr->name,ptr->score);                          delptr=ptr;                          
ptr=ptr->next;                        
 free(delptr);                        
 }            
 system("PAUSE");     
 return 0;
}

修正後版本

#include <stdlib.h> // #include <cstdlib>
// #include <iostream>  

struct student  
{ 
 int num;  
 char name[10]; 
 int score; 
 struct student*next;
};
typedef struct student node;
typedef node*link;

int main()
{
link newnode,ptr,delptr;
int i;
printf("請輸入5筆學生資料:\n");
delptr=(link)malloc(sizeof(node));

if(!delptr)
{
printf("[error!!記憶體配置失敗!]\n");

exit(1);

}
printf("請輸入座號");
scanf("%d",&delptr->num);
printf("請輸入姓名");
scanf("%s",&delptr->name);
printf("請輸入成績");
scanf("%d",&delptr->score);
ptr=delptr;
for(i=1;i<5;i++)
{
newnode=(link)malloc(sizeof(node));
if(!newnode)
{
printf("[error!!記憶體配置失敗!]\n");
exit(1);
}
printf("請輸入座號");
 scanf("%d",&newnode->num);          
printf("請輸入姓名");          
scanf("%s",&newnode->name);          
printf("請輸入成績");          
scanf("%d",&newnode->score);          
newnode->next=NULL;          
ptr->next=newnode;     // ptr->next=newnode          
ptr=ptr->next;
}
printf("\n學生成績\n");          
printf("座號\t姓名\t成績\n====================\n");           ptr=delptr;          
while(ptr!=NULL) // while(ptr!NULL)
{
printf("%3d\t%-s\t%3d\n",ptr->num,ptr->name,ptr->score);                          delptr=ptr;                          
ptr=ptr->next;                        
 free(delptr);                        
 }            
 system("PAUSE");     
 return 0;
}

執行結果

D:\QA>gcc cPtr1.c

D:\QA>gcc cPtr1.c -o cPtr1

D:\QA>cPtr1
請輸入5筆學生資料:
請輸入座號1
請輸入姓名aaa
請輸入成績100
請輸入座號2
請輸入姓名bb
請輸入成績90
請輸入座號3
請輸入姓名ccc
請輸入成績80
請輸入座號4
請輸入姓名ddd
請輸入成績70
請輸入座號5
請輸入姓名eee
請輸入成績60

學生成績
座號    姓名    成績
====================
  1     aaa     100
  2     bb       90
  3     ccc      80
  4     ddd      70
  5     eee      60
請按任意鍵繼續 . . .

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License