結構體實驗參考答案_第1頁
結構體實驗參考答案_第2頁
結構體實驗參考答案_第3頁
結構體實驗參考答案_第4頁
結構體實驗參考答案_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

實驗九構造體1.有5個學生,每個學生有3門課的成績,從鍵盤輸入以上數(shù)據(jù)(涉及學號、姓名、3門課成績),計算出每個學生的總成績,并將總成績最高的學生信息(涉及學號、姓名、3門課成績、總成績)輸出。#include<stdio.h>structSTU{ charnumber[7]; charname[10]; intscore[3]; inttotal;}student[5];intmain(){ inti,j,n; printf("請輸入5個學生的學號、姓名、3門課成績:\n"); for(i=0;i<5;i++) { student[i].total=0; scanf("%s%s",student[i].number,student[i].name); for(j=0;j<3;j++){ scanf("%d",&student[i].score[j]); student[i].total+=student[i].score[j]; } } printf("\n--------------------成績表-----------------------\n"); printf("學號姓名數(shù)學語文英語總分\n");for(i=0;i<5;i++) { printf("%8s%8s",student[i].number,student[i].name); for(j=0;j<3;j++) printf("%8d",student[i].score[j]);printf("%8d\n",student[i].total); } printf("\n----------------成績最高的學生信息----------------\n"); printf("學號姓名數(shù)學語文英語總分\n"); n=0; for(i=1;i<5;i++) if(student[i].total>student[n].total) n=i; printf("%8s%8s",student[n].number,student[n].name); for(j=0;j<3;j++) printf("%8d",student[n].score[j]);printf("%8d\n",student[n].total); return0;}2.某單位進行先進個人選舉,有5位候選人:zhang、wang、li、zhao、liu,20人參加投票。編寫一種統(tǒng)計每位候選人得票數(shù)的程序,輸出每位候選人的得票數(shù)以及得票數(shù)最高的候選人姓名。#include<stdio.h>#include<string.h>structPIAO{ charname[10]; inttotal;}people[5]={"zhang",0,"wang",0,"li",0,"zhao",0,"liu",0};intmain(){ inti,j,n=0; charxuan[10]; printf("請輸入20個選票(zhang,wang,li,zhao,liu):\n"); for(i=0;i<20;i++) { scanf("%s",xuan); for(j=0;j<5;j++){ if(strcmp(xuan,people[j].name)==0) people[j].total++; } } printf("\n-------候選人得票統(tǒng)計表--------\n"); printf("姓名得票數(shù)\n");for(i=0;i<5;i++) printf("%8s%8d\n",people[i].name,people[i].total); for(i=1;i<5;i++) if(people[i].total>people[n].total) n=i; printf("得票數(shù)最高的候選人是:%s\n",people[n].name); return0;}3.將如表21.1所示的學生信息建立成一種按學號升序有序的鏈表,每個結點涉及:學號、姓名、性別、年紀。統(tǒng)計鏈表中年紀不大于20的學生個數(shù)并將對應的學生信息(涉及學號、姓名、性別、年紀)輸出。表21.1學生信息表學號姓名性別年紀305001ZhangM18305002WangF20305004LiF19305005ZhaoM21#include<stdio.h>#include<malloc.h>typedefstructSTU{ charno[7]; charname[10]; charsex[7]; intage;structSTU*next;}NODE;NODE*create(intnum);NODE*create(intnum)//創(chuàng)立包含num個節(jié)點的鏈表{ NODE*head=NULL,*p,*tail;/*定義現(xiàn)在指針、頭指針和尾指針*/ inti; for(i=0;i<num;i++) { p=(NODE*)malloc(sizeof(NODE));/*創(chuàng)立第一種結點*/ printf("請輸入第%d個學生的學號、姓名、性別、年紀:\n",i+1); scanf("%s%s%s%d",p->no,&p->name,p->sex,&p->age); p->next=NULL; if(i==0) head=tail=p; else { tail->next=p; tail=p; } }returnhead;}intmain(){ intn=0; NODE*head,*p;head=create(4); p=head;while(p!=NULL)/*判斷與否是尾結點*/{ if(p->age<20) { printf("%s\t%s\t%s\t%d\n",p->no,p->name,p->sex,p->age); n++; } p=p->next;/*p指向下一種結點*/ } printf("年紀不大于20的學生共有%d個。\n",n); return0;}4.在第3題建立的鏈表中插入表21.2所示的結點,規(guī)定生成的新鏈表按照學號升序排列,并將生成的新鏈表輸出。表21.2結點信息學號姓名性別年紀305003LiuMale19#include<stdio.h>#include<malloc.h>#include<string.h>NODE*create(intnum);voidprint(NODE*head);NODE*insert(NODE*head,NODE*s);typedefstructSTU{ charno[7]; charname[10]; charsex[7]; intage;structSTU*next;}NODE;NODE*create(intnum)//創(chuàng)立包含num個節(jié)點的鏈表{ NODE*head=NULL,*p,*tail;/*定義現(xiàn)在指針、頭指針和尾指針*/ inti; for(i=0;i<num;i++) { p=(NODE*)malloc(sizeof(NODE));/*創(chuàng)立第一種結點*/ printf("請輸入第%d個學生的學號、姓名、性別、年紀:\n",i+1); scanf("%s%s%s%d",p->no,p->name,p->sex,&p->age); p->next=NULL; if(i==0) head=tail=p; else { tail->next=p; tail=p; } }returnhead;}voidprint(NODE*head){NODE*p;p=head;/*p指向表頭*/if(head==NULL)printf("Listisempty!\n"); printf("學號\t姓名\t性別\t年紀\n");while(p!=NULL)/*判斷與否是尾結點*/{printf("%s\t%s\t%s\t%d\n",p->no,p->name,p->sex,p->age);p=p->next;/*p指向下一種結點*/}}NODE*insert(NODE*head,NODE*s){NODE*p=head,*p1=head;if(p==NULL){head=s;s->next=NULL;}/*鏈表為空的狀況*/else{/*查找插入位置*/while(strcmp(p->no,s->no)<0&&p->next!=NULL){ p1=p; p=p->next;}if(strcmp(p->no,s->no)>=0){if(p==head)head=s;/*插入結點作為表頭的狀況*/elsep1->next=s;/*插入結點在鏈表中間的狀況*/s->next=p;}else{/*插入結點作為表尾的狀況*/ p->next=s; s->next=NULL;}}returnhead;}intmain(){ NODE*head,*s;head=create(4); s=(NODE*)malloc(sizeof(NODE));/*創(chuàng)立插入的結點*/ printf("請輸入要插入學生的學號、姓名、性別、年紀:\n"); scanf("%s%s%s%d",s->no,s->name,s->sex,&s->age); s->next=NULL; head=insert(head,s); print(head); return0;}5.在第4題建立的鏈表中刪除姓名為“Wang”的結點,并將生成的新鏈表輸出。#include<stdio.h>#include<malloc.h>#include<string.h>NODE*create(intnum);voidprint(NODE*head);NODE*insert(NODE*head,NODE*s);NODE*del(NODE*head,char*del_name);typedefstructSTU{ charno[7]; charname[10]; charsex[7]; intage;structSTU*next;}NODE;NODE*create(intnum)//創(chuàng)立包含num個節(jié)點的鏈表{ NODE*head=NULL,*p,*tail;/*定義現(xiàn)在指針、頭指針和尾指針*/ inti; for(i=0;i<num;i++) { p=(NODE*)malloc(sizeof(NODE));/*創(chuàng)立第一種結點*/ printf("請輸入第%d個學生的學號、姓名、性別、年紀:\n",i+1); scanf("%s%s%s%d",p->no,p->name,p->sex,&p->age); p->next=NULL; if(i==0) head=tail=p; else { tail->next=p; tail=p; } }returnhead;}voidprint(NODE*head){NODE*p;p=head;/*p指向表頭*/if(head==NULL)printf("Listisempty!\n"); printf("學號\t姓名\t性別\t年紀\n");while(p!=NULL)/*判斷與否是尾結點*/{printf("%s\t%s\t%s\t%d\n",p->no,p->name,p->sex,p->age);p=p->next;/*p指向下一種結點*/}}NODE*insert(NODE*head,NODE*s){NODE*p=head,*p1=head;if(p==NULL){head=s;s->next=NULL;}/*鏈表為空的狀況*/else{/*查找插入位置*/while(strcmp(p->no,s->no)<0&&p->next!=NULL){ p1=p; p=p->next;}if(strcmp(p->no,s->no)>=0){if(p==head)head=s;/*插入結點作為表頭的狀況*/elsep1->next=s;/*插入結點在鏈表中間的狀況*/s->next=p;}else{/*插入結點作為表尾的狀況*/ p->next=s; s->next=NULL;}}returnhead;}NODE*del(NODE*head,char*del_name){NODE*p,*p1;if(head==NULL)printf("鏈表為空,不能進行刪除結點操作!");else{p=head;while(strcmp(p->name,del_name)!=0&&p->next!=NULL){

溫馨提示

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

評論

0/150

提交評論