計算機二級C語言機試112_第1頁
計算機二級C語言機試112_第2頁
計算機二級C語言機試112_第3頁
計算機二級C語言機試112_第4頁
免費預覽已結(jié)束,剩余1頁可下載查看

下載本文檔

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

文檔簡介

1、 模擬 計算機二級C 語言機試112一、填空題第 1 題:程序填空題給定程序中,函數(shù)fun 的功能是:將形參std 所指結(jié)構(gòu)體數(shù)組中年齡最大者的數(shù)據(jù)作為函數(shù)值返回,并在main 函數(shù)中輸出。請在程序的下劃線處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。注意:部分原程序給出如下。請勿改動主函數(shù)main 和其他函數(shù)中的任何內(nèi)容,僅在行線上填入所編寫的若干表達式或語句。 試題源程序#include <stdio.h > typedef struct char name10 ; int age ;STD;STD fun(STD std , int n) STD max; int i

2、 ;/*found*/ max= (1) ;for(i=1 ; i <n; i+) /*found*/ if(max.aged (2) )max=mtdi ;return max ;main()STD std5="aaa" , 17, "bbb" ,STD max;16, "ccc" , 18,"ddd" ,17, "ee" , 15;max=fun(std , 5);printf("nThe result : n") ;/*found*/ printf("n

3、Name : %s, Age: %dn", (3) , max.age); 1 *std 2 stdi.age 3 詳細解答:本題是從結(jié)構(gòu)體中找出年齡最大的記錄。填空1:給存放最大者 max賦初值,所以應填:*std 。 填空 2:當前最大者的年齡和結(jié)構(gòu)中所有的年齡進行比較,所以應填:stdi.age 。 填空3:輸出最大者的姓名和年齡,所以應填:。二、改錯題第 2 題:程序修改題下列給定程序是建立一個帶頭結(jié)點的單向鏈表,并用隨機函數(shù)為各結(jié)點賦值。函數(shù) fun() 的功能是:將單向鏈表結(jié)點( 不包括頭結(jié)點)數(shù)據(jù)域為偶數(shù)的值累加起來,并作為函數(shù)值返回。和

4、值通過函數(shù)值返回 main()函數(shù)。例如,若n=5,則應輸出8.391667。請改正程序中的錯誤,使它能得到正確結(jié)果。注意:不要改動main 函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。 試題源程序#include <stdio.h > #include <stdlib.h > typedef struct aaint data ;struct aa*next )NODE;int fun(NODE *h)int sum=0 ;NODE*;p /*found*/ p=h;while(p- > next)if(p- >data%2=0)sum+=P-> d

5、ata ;/*found*/ p=h- > next ;return sum ;NODE *creatlink(int n)NODE*,h *p, *s, *q;int i , x;h=p=(NODE*)malloc(sizeof(NODE) ;for(i=1 ; i <=n; i+)S=(NODE*)malloc(sizeof(NODE) ;s- >data=rand()%16 ;s- >next=p- >next ;p- >next=s ;p=p- > next ;p- >next=NULL;return h ;outlink(NODE*h

6、, FILE*pf)NODE*;pp=h- > next ;fprintf(pf , "nnTHE LIST : nn HEAD") ;while(p)fprintf(pf , "- >%d", p- > data) ; p=p->next;fprintf(pf , "n") ;outresult(int s , FILE*pf)fprintf(pf , "nThe sum of even numbers : %dn", s) ;main()NODE*hea;d int even ;head

7、=creatlink(12) ;head- >data=9000;outlink(head , stdout) ;even=fun(head) ;printf("nThe result : n") ; outresult(even , stdout) ; 參考答案:(1)錯誤:while(p- >next) 正確:while(p!=NULL)或 while(p) (2)錯誤: p=h- > next ; 正確:p=p- > next ;詳細解答:錯誤1:循環(huán)時要注意循環(huán)中止的條件是 p!=NULL(最后一個結(jié)點為空,就像 字符串的最后一個字符為結(jié)尾標

8、志符0 ' 一梯,while(p- > next)使循環(huán)提前結(jié) 束,所以應改為while(p!=NULL) 或 while(p) 。 錯誤2:當累加完后,應該把指針p指向鏈表的下一個結(jié)點,即P=P->next ,而不是把頭指針h的下一個結(jié)點賦給p,因為這樣會造成死循環(huán)。所以 p=h->next,應該改為p=p->next。三、編程題第 3 題:程序設計題請編寫函數(shù)fun() ,它的功能是:將帶頭結(jié)點單向鏈表按data 域由大小排序( 排序時不考慮頭結(jié)點) ,主函數(shù)用隨機函數(shù)為各結(jié)點data 域賦值,頭結(jié)點data 域賦值為0。注意:部分原程序給出如下。請勿改動主

9、函數(shù)main 和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun 的花括號中填入所編寫的若干語句。 試題源程序#include < stdio.h >#include < conio.h >struct aaint data ;struct aa*next ;void fun(struct aa*p) main()int i , n, m=100;struct aa*h=NULL , *s=NULL,*p=NULL;clrscr() ;S=(struct aa*)malloc(sizeof(struct aa) ;h=s; h->data=0; h->next=NULL

10、;printf("Please input n:");scanf("%d" , n) ;for(i=1 ; i <=n; i+) p=(struct aa*)malloc(sizeof(struct aa);p- >data=rand()%m; p- >next=NULL; printf("%d” , p->data);s- >next=p ; s=s- >next ; fun(h) ;printf("n") ;for(h=h- >next ; h!=NULL; h=h->nnext) printf("%d” , h->data);void fun(struct aa*p) int temp; struct aa*1st ; for(p=p- >next ; P->next!=NULL; p=p- > next) for(1st=p- >next ; 1st!=NULL; 1st=1st- > next) if(1st- >data >p->data)

溫馨提示

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

評論

0/150

提交評論