建立一個采用鏈?zhǔn)酱鎯Φ木€性表-表中元素為學(xué)生-每個學(xué)生信息包含姓名、學(xué)號和成績?nèi)糠?對該表實(shí)現(xiàn):_第1頁
建立一個采用鏈?zhǔn)酱鎯Φ木€性表-表中元素為學(xué)生-每個學(xué)生信息包含姓名、學(xué)號和成績?nèi)糠?對該表實(shí)現(xiàn):_第2頁
建立一個采用鏈?zhǔn)酱鎯Φ木€性表-表中元素為學(xué)生-每個學(xué)生信息包含姓名、學(xué)號和成績?nèi)糠?對該表實(shí)現(xiàn):_第3頁
建立一個采用鏈?zhǔn)酱鎯Φ木€性表-表中元素為學(xué)生-每個學(xué)生信息包含姓名、學(xué)號和成績?nèi)糠?對該表實(shí)現(xiàn):_第4頁
建立一個采用鏈?zhǔn)酱鎯Φ木€性表-表中元素為學(xué)生-每個學(xué)生信息包含姓名、學(xué)號和成績?nèi)糠?對該表實(shí)現(xiàn):_第5頁
已閱讀5頁,還剩19頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、云南大學(xué)物理實(shí)驗(yàn)教學(xué)中心實(shí)驗(yàn)報(bào)告課程名稱: 計(jì)算機(jī)軟件技術(shù)基礎(chǔ)實(shí)驗(yàn)項(xiàng)目: 線性表(鏈?zhǔn)酱鎯Γ┘捌鋺?yīng)用學(xué)生姓名:學(xué)號: 學(xué)院系級 專業(yè)成績指導(dǎo)教師: 實(shí)驗(yàn)時間:年 時 分至 時 分實(shí)驗(yàn)地點(diǎn):實(shí)驗(yàn)類型:教學(xué)(演示 驗(yàn)證 綜合 設(shè)計(jì))學(xué)生科研 課外開放測試其它23一、實(shí)驗(yàn)?zāi)康模赫莆真準(zhǔn)酱鎯Y(jié)構(gòu)下線性表的建立及基本操作。二、問題:建立一個采用鏈?zhǔn)酱鎯Φ木€性表,表中元素為學(xué)生,每個學(xué)生信息包含姓名、學(xué)號和成績?nèi)糠?,對該表?shí)現(xiàn):輸出、查找、插入、刪除功能,并計(jì)算出平均成績和總成績。三、程序的編寫與調(diào)試1、原程序:#include<iostream>#include<iomanip>

2、;using namespace std;struct node int num; char name10; float score; node *next;class linked_list private:node *head;public:linked_list();void prt_linked_list();void ins_num_linked_list(int i,node *b);void ins_name_linked_list(char name,node *b);int del_num_linked_list(node);int del_name_linked_list(

3、node);int sear_num_linked_list(int);void sear_name_linked_list(node);void count_linked_list(); error C2533: 'linked_list:linked_list' : constructors not allowed a return typeerror C2264: 'linked_list:linked_list' : error in function definition or declaration; function not called錯誤原因:

4、“”后缺少“;”改正:在“”后面加上“;” /*建立鏈表*/ linked_list:linked_list()node *p,*q;p=new node;q=new node;p->num=101;strcpy(p->name,"aaa");p->score=98; q->num=104;strcpy(q->name,"ddd");q->score=95; head=p;p->next=q;q->next=NULL; return ;/* 輸出*/void linked_list:prt_linked_l

5、ist()node *p;p=head;if(head=NULL)cout<<"空鏈表!”error C2001: newline in constanterror C2143: syntax error : missing '' before 'else'error C2181: illegal else without matching iferror C2601: 'ins_num_linked_list' : local function definitions are illegal error C2601: &#

6、39;ins_name_linked_list' : local function definitions are illegalerror C2601: 'del_num_linked_list' : local function definitions are illegalerror C2601: 'del_name_linked_list' : local function definitions are illegalerror C2601: 'sear_num_linked_list' : local function def

7、initions are illegalerror C2601: 'sear_name_linked_list' : local function definitions are illegalerror C2601: 'count_linked_list' : local function definitions are illegalerror C2601: 'main' : local function definitions are illegalfatal error C1004: unexpected end of file foun

8、d錯誤原因:符號錯誤,“”為中文引號。改正:把中文引號改為英文引號即可。<<endl;return; else docout<<p->num<<" "<<p->name<<" "<<p->score<<endl;p=p->next;while(p!=NULL);return;/*按學(xué)號插入*/void linked_list:ins_num_linked_list(int i,node *b) node *q;if(head=NULL)b->

9、next=NULL;head=b;return;if(head->num=i)b->next=head;head=b;return;q=head;while(q->next!=NULL)&&(q->next)->num)!=i)q=q->next;if(q->next=NULL)cout<<"無此元素"<<endl;return; b->next=q->next;q->next=b; return;/*按姓名插入*/void linked_list:ins_name_link

10、ed_list(char name,node *b)node *q;if(head=NULL)b->next=NULL;head=b;return;if(strcmp(head->name,name)=0)b->next=head;head=b return;error C2143: syntax error : missing '' before 'return'錯誤原因:return前面缺少“;”改正:在return前面加上“;”。q=head;while(q->next!=NULL)&&(strcmp(q->n

11、ext)->name,name)!=0)q=q->next;if(q->next=NULL)cout<<"無此元素"<<endl;return; b->next=q->next;q->next=b; return;/*按學(xué)號刪除*/int linked_list:del_num_linked_list( node x ) node *p,*q;if(head=NULL)cout<<"空鏈表!"<<endl;return 0;if(head->num=x.num)p=

12、head->next;delete head;head=p;return 1;q=head;while(q->next!=NULL)&&(q->next)->num)!=x.num)q=q->next;if(q->next=0)cout<<"鏈表中無此元素"<<endl;return 0;p=q->next;q->next=p->next;delete p;return 1;/*按姓名刪除*/int linked_list:del_name_linked_list( node x

13、) node *p,*q;if(head=NULL)cout<<"空鏈表!"<<endl;return 0;if(strcmp(head->name,)=0)p=head->next;delete head;head=p;return ;錯誤:error C2561: 'del_name_linked_list' : function must return a value錯誤原因:返回值出錯;必須要返回一個值。改正:把“return”改為“return 1”q=head;while(q->next!=N

14、ULL)&&(strcmp(q->next)->name,)!=0)q=q->next;if(q->next=0)cout<<"鏈表中無此元素"<<endl;return 0;p=q->next;q->next=p->next;delete p;return 1;/*按學(xué)號查找*/int linked_list:sear_num_linked_list( int i ) node *q; if(head=NULL)cout<<"空鏈表!"<&l

15、t;endl;return 0;q=head;while(q->next!=NULL)&&(q->num)!=i)q=q->next;if(q->num)!=i)cout<<"無此元素"<<endl;return 0;cout<<q->num<<" "<<q->name<<" "<<q->score<<endl;warning C4715: 'linked_list:sear

16、_num_linked_list' : not all control paths return a value警告原因:沒有控制返回值。改正:在“”前面加上返回語句“return 1;”/*按姓名查找*/void linked_list:sear_name_linked_list(node x ) node *p; if(head=NULL)cout<<"空鏈表!"<<endl;return;p=head;while(p->next!=NULL)&&(strcmp(p->name,)!=0)p=p-&

17、gt;next;if(strcmp(p->name,)!=0)cout<<"無此元素"<<endl;return 0error C2562: 'sear_name_linked_list' : 'void' function returning a valu錯誤原因:返回值出錯,不能返回0。改正:刪除return后面的0.;cout<<p->num<<" "<<p->name<<" "<<p

18、->score<<endl;/*計(jì)算*/void linked_list:count_linked_list() float sum=0,ave;int n=0;node *p;p=head;if(p=0)cout<<"空鏈表"<<endl;else while(p!=0)sum=sum+p->score; p=p->nxt;error C2039: 'nxt' : is not a member of 'node'錯誤原因:字符出錯,輸入時輸入出錯。改正:把“nxt”改為“next”。

19、n+;ave=sum/n;cout<<"總 分="<<sum<<endl;cout<<"平均分="<<ave<<endl;return ;/*主函數(shù)*/int main() int mx;linked_list s1; while(1) cout<<"1.輸出 2.插入 3.刪除 4.查找 5.計(jì)算 0.退出"<<endl; cout<<"請輸入0-5:" cin>>mx; switch(mx)

20、case 1: s1.prt_linked_list(); break;case 2: node *b=new node; int mx1;cout<<" 1.按學(xué)號插入 2.按姓名插入 0.退出"<<endl; cout<<"輸入0-2:" cin>>mx1;switch(mx1) case 1error C2065: 'i' : undeclared identifier錯誤原因:沒有對變量“i”定義。改正:對“i”進(jìn)行定義,即在case 1后面加上語句“int i;”: cout<

21、;<"請輸入要插入位置的學(xué)號(在此之前一位插入):" cin>>i; cout<<"請輸入要插入的學(xué)號、姓名、成績:"cin>>b->num>>b->name>>b->score ; s1.ins_num_linked_list(i,b); s1.prt_linked_list();break;case 2: char name10; cout<<"請輸入要插入位置的姓名(在此之前一位插入):"cin>>name;cout<

22、;<"請輸入要插入的學(xué)號、姓名、成績:" cin>>b->num>>b->name>>b->score ; s1.ins_name_linked_list(name,b); s1.prt_linked_list();break;case 0: cout<<"退出"<<endl; return 0;break;case 3: int mx2; node x;cout<<"請輸入要刪除的: 1.按學(xué)號刪除 2.按姓名刪除 0.退出"<&l

23、t;endl;cout<<"輸入0-2:" cin>>mx2;switch(mx2)case 1: cout<<"請輸入要刪除的學(xué)號:" cin>>x.num; s1.del_num_linked_list(x);s1.prt_linked_list();break;case 2: cout<<"請輸入刪除姓名:" cin>>x。error C2018: unknown character '0xa1'error C2018: unknown ch

24、aracter '0xa3'error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'struct node' (or there is no acceptable conversion)error C2146: syntax error : missing '' before identifier 'name'error C2065: 'name' : undecla

25、red identifier錯誤原因:符號錯誤。改正:把“?!备臑椤?”即可。name; s1.del_name_linked_list(x); s1.prt_linked_list();break;case 0: cout<<"退出"<<endl; return 0; break; case 4: int mx3; cout<<"1.按學(xué)號查找 2.按姓名查找 0.退出"<<endl;cout<<"輸入0-2:" cin>>mx3; switch(mx3)cas

26、e 1:int i; cout<<"請輸入要查找的學(xué)號:" cin>>i; s1.sear_num_linked_list(i) ; break;case 2:node x; cout<<"請輸入要查找的姓名:" cin>>; s1.sear_name_linked_list(x) ; break;case 0: cout<<"退出"<<endl; return 0; break; case 5:s1.count_linked_list();break

27、;case 0: cout<<"程序結(jié)束"<<endl; return 0; return 0;2、正確程序:#include<iostream>#include<iomanip>using namespace std;struct node int num; char name10; float score; node *next;class linked_list private:node *head;public:linked_list();void prt_linked_list();void ins_num_link

28、ed_list(int i,node *b);void ins_name_linked_list(char name,node *b);int del_num_linked_list(node);int del_name_linked_list(node);int sear_num_linked_list(int);void sear_name_linked_list(node);void count_linked_list(); /*建立鏈表*/ linked_list:linked_list()node *p,*q;p=new node;q=new node;p->num=101;s

29、trcpy(p->name,"aaa");p->score=98; q->num=104;strcpy(q->name,"ddd");q->score=95; head=p;p->next=q;q->next=NULL; return ;/* 輸出*/void linked_list:prt_linked_list()node *p;p=head;if(head=NULL)cout<<"空鏈表!"<<endl; return; else docout<<p-

30、>num<<" "<<p->name<<" "<<p->score<<endl;p=p->next;while(p!=NULL);return;/*按學(xué)號插入*/void linked_list:ins_num_linked_list(int i,node *b) node *q;if(head=NULL)b->next=NULL;head=b;return;if(head->num=i)b->next=head;head=b;return;q=head

31、;while(q->next!=NULL)&&(q->next)->num)!=i)q=q->next;if(q->next=NULL)cout<<"無此元素"<<endl;return; b->next=q->next;q->next=b; return;/*按姓名插入*/void linked_list:ins_name_linked_list(char name,node *b)node *q;if(head=NULL)b->next=NULL;head=b;return;i

32、f(strcmp(head->name,name)=0)b->next=head;head=b;return;q=head;while(q->next!=NULL)&&(strcmp(q->next)->name,name)!=0)q=q->next;if(q->next=NULL)cout<<"無此元素"<<endl;return; b->next=q->next;q->next=b; return;/*按學(xué)號刪除*/int linked_list:del_num_link

33、ed_list( node x ) node *p,*q;if(head=NULL)cout<<"空鏈表!"<<endl;return 0;if(head->num=x.num)p=head->next;delete head;head=p;return 1;q=head;while(q->next!=NULL)&&(q->next)->num)!=x.num)q=q->next;if(q->next=0)cout<<"鏈表中無此元素"<<endl;

34、return 0;p=q->next;q->next=p->next;delete p;return 1;/*按姓名刪除*/int linked_list:del_name_linked_list( node x ) node *p,*q;if(head=NULL)cout<<"空鏈表!"<<endl;return 0;if(strcmp(head->name,)=0)p=head->next;delete head;head=p;return 1;q=head;while(q->next!=NULL)

35、&&(strcmp(q->next)->name,)!=0)q=q->next;if(q->next=0)cout<<"鏈表中無此元素"<<endl;return 0;p=q->next;q->next=p->next;delete p;return 1;/*按學(xué)號查找*/int linked_list:sear_num_linked_list( int i ) node *q; if(head=NULL)cout<<"空鏈表!"<<en

36、dl;return 0;q=head;while(q->next!=NULL)&&(q->num)!=i)q=q->next;if(q->num)!=i)cout<<"無此元素"<<endl;return 0;cout<<q->num<<" "<<q->name<<" "<<q->score<<endl;return 1;/*按姓名查找*/void linked_list:sear_

37、name_linked_list(node x ) node *p; if(head=NULL)cout<<"空鏈表!"<<endl;return;p=head;while(p->next!=NULL)&&(strcmp(p->name,)!=0)p=p->next;if(strcmp(p->name,)!=0)cout<<"無此元素"<<endl;return;cout<<p->num<<" "

38、;<<p->name<<" "<<p->score<<endl;/*計(jì)算*/void linked_list:count_linked_list() float sum=0,ave;int n=0;node *p;p=head;if(p=0)cout<<"空鏈表"<<endl;else while(p!=0)sum=sum+p->score; p=p->next; n+;ave=sum/n;cout<<"總 分="<<

39、;sum<<endl;cout<<"平均分="<<ave<<endl;return ;/*主函數(shù)*/int main() int mx;linked_list s1; while(1) cout<<"1.輸出 2.插入 3.刪除 4.查找 5.計(jì)算 0.退出"<<endl; cout<<"請輸入0-5:" cin>>mx; switch(mx) case 1: s1.prt_linked_list(); break;case 2: node

40、 *b=new node; int mx1;cout<<" 1.按學(xué)號插入 2.按姓名插入 0.退出"<<endl; cout<<"輸入0-2:" cin>>mx1;switch(mx1) case 1: int i; cout<<"請輸入要插入位置的學(xué)號(在此之前一位插入):"cin>>i; cout<<"請輸入要插入的學(xué)號、姓名、成績:"cin>>b->num>>b->name>>b->score ; s1.ins_num_linked_list(i,b); s1.prt_linked_list();break;case 2: char name10; cout<<"請輸入要插入位置的姓名(在此之前一位插入):

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論