C語言程序設計填空題及答案復習用(最新整理)_第1頁
C語言程序設計填空題及答案復習用(最新整理)_第2頁
C語言程序設計填空題及答案復習用(最新整理)_第3頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、.導讀:在程序填空題中,已經給出了程序的主干,讀者首先要理解程序的思路, 再選擇正確的內容填入空白處,使程序完成既定的功能。這類習題的設計就是要引導讀者逐步掌握編程的方法。本節(jié)習題的難度適中,可能有些典型的程序在課堂上已經有所接觸,讀者一定要獨立完成它,這樣就可以逐步提高自己的編程能力。在程序設計語言學習的中期,讀者對程序設計已經有了初步的了解, 而自己編寫程序又不知從何處入手,此時解答此類題目可以避免盲目性,從而提高學習的效率?!?.1】下面程序的功能是不用第三個變量,實現兩個數的對調操作。#include main() int a,b; scanf(%d%d,&a,&b); printf(

2、a=%d,b=%dn,a,b); a= ;b= ;a= ;printf(a=%d,b=%dn,a,b);【3.2】下面程序的功能是根據近似公式:2/6 1/12+1/22+1/32+ +1/n2,求 值。#include double pi(long n) double s=0.0; long i; for(i=1;i=n;i+) s=s+ ;return( );【3.3】下面的程序的功能是求一維數組中的最小元素。findmin(int *s,int t,int *k) int p; for(p=0,*k=p;pt;p+) if(sps*k) ;main() int a10,i,*k=&i;

3、for(i=0;i10;i+) scanf(%d,&ai); findmin(a,10,k); printf(%d,%dn,*k,a*k);【3.4】下面程序的功能是計算 1-3+5-7+ -99+101的值。main() int i,t=1,s=0; for(i=1;i0?t=-1:1;printf(%dn,s);【3.5】有以下程序段: s=1.0; for(k=1;kamax) amax=x; if( ) amin=x; scanf(%f,&x);printf(namax=%fnamin=%fn,amax,amin);【3.7】下面程序的功能是將形參x 的值轉換為二進制數, 所得的二進制

4、數放在一個一維數組中返回,二進制數的最低位放在下標為 0 的元素中。fun(int x,int b) int k=0,r; do r=x% ; bk+=r; x/= ;while(x);【3.8】下面程序的功能是輸出 1 到 100 之間每位數的乘積大于每位數的和的數。例如數字 26,數位上數字的乘積 12 大于數字之和 8。main() int n,k=1,s=0,m; for(n=1;ns) printf(%d,n);【3.9】下面程序的功能是統計用 0 至 9 之間的不同的數字組成的三位數的個數。main() int i,j,k,count=0; for(i=1;i=9;i+) for(

5、j=0;j=9;j+) if( ) continue;else for(k=0;k=9;k+) if( ) count+; printf(%d,count);【3.10】下面程序的功能是輸出 100 以內的個位數為 6、且能被 3 整除的所有數。main() int i,j; for(i=0; ;i+) j=i*10+6; r=m; ;n=r;r=m%n; while( ) m=n; n=r; r=m%n; ;【3.12】下面程序的功能是使用冒泡法對輸入的 10 個浮點數從小到大進行排序。排好序的 10 個數分兩行輸出。程序如下:#include main() ;int i,j;printf(

6、input 10 numbers pleasen); for(i=0; ;i+ )scanf(%f, &ai); printf(n); for(i=2; ;i+ ) for(j=0; ;j+ ) if( )printf(n);【3.13】下面程序的功能是讀入 20 個整數,統計非負數個數,并計算非負數之和。#include stdio.h main() int i,a20,s,count; s=count=0; for(i=0;i20;i+ ) scanf(%d, ); for(i=0;i20;i+) if(ai0) ; s+=ai; count+;printf(s=%dt count=%dn

7、,s,count);【3.14】下面程序的功能是刪除字符串 s 中的空格。#include main() char *s=beijing ligong daxue; int i,j; for(i=j=0;si!=0;i+) if(si!= ) ;if( ) countinue; x=aj;else ;printf(%d,j); ;sj= 0;aj+1=x;printf(%s,s);【3.11】下面程序的功能是用輾轉相除法求兩個正整數printf(thesorted10numbers;n);【3.15】下面程序的功能是將字符串 s 中所有的字符cm 和 n 的最大公約數。hcf(int m,in

8、t n) int r; if(mn)for(i=0; ;i+ ) if( )printf(n); printf(%ft,ai);刪除。請選擇填空。#include main( ) char s80;int i,j; gets(s);for(i=j=0;si!= 0;i+ ) if(si!= c) ;sj= 0; puts(s);【3.16】下面程序的功能是輸出兩個字符串中對應相等的字符。請選擇填空。#include puts(a);【3.18】下面程序的功能是對鍵盤輸入的兩個字符串進行比較,然后輸出兩個字符串中第一個不相同字符的ascii 碼之差。例如:輸入的兩個字符串分別為abcdefg 和

9、abceef,則輸出為-1。#include main() char str1100,str2100,c; int i,s;tj = ;【3.20】下面的函數 index(char s, char t)檢查字符串 s 中是否包含字符串 t,若包含,則返回 t 在 s 中的開始位置(下標值),否則送回-1。index(char s, char t) int i,j,k; for(i=0;si!= 0;i+ ) for(j=i,k=0; & sj=tk;j+,k+) ;if( ) return (i);char x=programming;char y=fortran;printf(enterpr

10、intf(enterstringstring1:2:););gets(str1);gets(str2);return(-1);main()i=0; int i=0;while(xi!= 0 & yi!= 0) if(xi=yi)printf(%c, ); elsei+;【3.17】下面程序的功能是將字符串 s 中的每個字符按升序的規(guī)則插到數組 a 中, 字符串 a 已排好序。#include main()while(str1i = str2i & str1i!= ) i+;s= ;printf(%dn, s);【3.19】下面的函數expand 在將字符串s 復制到字符串t 時, 將其中的換行

11、符和制表符轉換為可見的轉義字符表示,即用n表示換行符,用t表示制表符。expand(char s,char t) int i,j;for(i=j=0;si!= 0;i+ )n【3.21】下面程序的功能是計算 s= k! 。k=0long fun(int n) int i; long s;for(i=1;i ;i+) s*=i;return( );main() char a20=cehiknqtw;switch(si) int k,n;char s=fbla; casen: t = ;long s;int i,k,j;tj+= n;scanf(%d,&n);for(k=0;sk!= 0;k+ )

12、 j=0;while(sk=aj & aj!= 0 ) j+;break;case t: t = ;tj+ = t; break;s= ; for(k=0;k=n;k+) s+= ; printf(%ldn,s);for( )default:t=si; ;break;【3.22】下面程序的功能是顯示具有 n 個元素的數組 saj=sk;中的最大元素。#define n 20 main() int i,an; for(i=0;in;i+) scanf(%d,&ai); printf(%dn, );fmax(int s,int n) int k,p; for(p=0,k=p;psk) ; retu

13、rn(k);【3.23】下面程序的功能是由鍵盤輸入 n,求滿足下述條件的 x、y:nx 和 ny 的末 3 位數字相同,且 xy,x、y、n 均為自然數,并使 x+y 為最小。#include pow3(int n,int x) int i, last; for(last=1,i=1;i=x;i+ ) last= ;return(last);main() int x,n,min,flag=1; scanf(%d, &n); for(min=2;flag;min+) for(x=1;xmin & flag;x+ )if( & pow3(n,x)=pow3(n,min-x) printf(x=%d

14、,y=%dn, x, min-x ); ;【3.24】下面的程序是用遞歸算法求 a 的平方根。求平方根的迭代公式如下:#include double mysqrt( double a, double x0 ) double x1, y; x1 = ;if( fabs(x1-x0)0.00001 ) y = mysqrt( );else y = x1; return( y );main() double x; printf(enter x: ); scanf(%lf, &x);printf(the sqrt of %lf=%lfn, x, mysqrt( x, 1.0);【3.25】以下程序是計

15、算學生的年齡。已知第一位最小的學生年齡為 10 歲,其余學生的年齡一個比一個大 2 歲, 求第 5 個學生的年齡。#include age( int n ) int c;if( n=1 ) c=10; else c= ; return(c);main() int n=5; printf(age:%dn, );【3.26】下面的函數 sum(int n)完成計算 1n 的累加和。sum(int n) if(n=0)printf(data errorn); if(n=1) ;else ;【3.27】下面的函數是一個求階乘的遞歸調用函數。facto(int n) if( n = 1 ) ;else

16、return( );【3.28】組合問題,由組合的基本性質可知:(1) c(m,n)=c(n-m,n)(2) c(m,n+1)=c(m,n)+c(m-1,n)公式(2)是一個遞歸公式,一直到滿足 c(1,n)=n 為止。當 n2*m 時,可先用公式(1) 進行簡化,填寫程序中的空白,使程序可以正確運行。#includestdio.h main() int m,n; printf(input m,n=); scanf(%d%d, &m, &n);printf(the combination numbeers is %dn, combin(m,n);combin( int m, int n) in

17、t com;if( n2*m ) m=n-m; if( m=0 ) com=1; else if(m=1) ; else ; return(com);【3.29】下列函數是求一個字符串 str 的長度。int strlen( char *str ) if( ) return (0); else return ( );【3.30】用遞歸實現將輸入小于 32768 的整數按逆序輸出。如輸入 12345,則輸出 54321。#includestdio.hmain() int n; printf(input n : ); scanf(%d, ); r(n);printf(n);r( int m ) p

18、rintf(%d, ); m = ;if( ) ;【3.31】輸入 n 值,輸出高度為 n 的等邊三角形。例如當 n=4 時的圖形如下:*#include void prt( char c, int n ) if( n0 ) printf( %c, c ); ;main() int i, n;scanf(%d, &n);for( i=1; i=n; i+ ) ; ;printf(n);【3.32】下面的函數實現 n 層嵌套平方根的計算。double y(double x, int n) if( n=0 ) return(0);else return ( sqrt(x+( ) );【3.33】函

19、數 revstr(s)將字符串 s 置逆,如輸入的實參 s 為字符串abcde, 則返回時 s 為字符串edcba。遞歸程序如下:revstr( char *s ) char *p=s, c; while(*p) p+; ;if(sp) c=*s;*s=*p; ;revstr(s+1); ;如下是由非遞歸實現的 revstr(s)函數: revstr (s)char *s; char *p=s, c; while( *p ) p+; ;while( s2) invent ( ,n-2);else ;【3.35】從鍵盤上輸入 10 個整數,程序按降序完成從大到小的排序。#include int

20、array10;sort( int *p, int *q ) int *max, *s; if( ) return;max=p; for( s=p+1; s *max ) ; swap( ); sort( ); swap( int *x, int *y ) int temp; temp=*x;*x=*y;*y=temp;main() int i; printf(enter data :n); for( i=0; i10; i+)scanf(%d, &arrayi); sort( );printf(output:); for( i=0; i10; i+)printf(%d , arrayi);【

21、3.36】下面函數的功能是將一個整數存放到一個數組中。存放時按逆序存放。例如:483 存放成384。#include void convert(char *a, int n) int i; if(i=n/10) !=0 ) convert( , i );*a = ;char str10= ; main() int number; scanf(%d, &number); convert( str, number ); puts(str);【3.37】下面程序的功能是實現數組元素中值的逆轉。#include main() int i,n=10,a10=1,2,3,4,5,6,7,8,9,10;in

22、vert(a,n-1); for(i=0;i10;i+) printf(%4d,ai); printf(n);invert(int *s,int num) int *t,k; t=s+num; while( ) k=*s;*s=*t;*t=k; ; ;【3.38】下面程序通過指向整型的指針將數組 a34 的內容按行列的格式輸出,請給 printf( )填入適當的參數,使之通過指針 p 將數組元素按要求輸出。#include int a34=1,2,3,4,5,6,7,8,9,10,11,12,*p=a; main() int i,j; for(i=0;i3;i+ ) for(j=0;j4;j+

23、 ) printf(%4d , );【3.39】下面程序的功能是:從鍵盤上輸入一行字符, 存入一個字符數組中,然后輸出該字符串。#include main ( ) char str81, *sptr; int i; for(i=0;iwp) ; for(i=*n;i=p;i-) ; wp=x;+*n;【3.41】下面程序的功能是從鍵盤上輸入兩個字符串, 對兩個字符串分別排序;然后將它們合并,合并后的字符串按 ascii 碼值從小到大排序,并刪去相同的字符。#include strmerge(a,b,c) /* 將已排好序的字符串 a、b 合并到 c*/char *a,*b,*c; char t

24、,*w; w=c;while( *a!= 0 *b!=0 ) t= ?*a+:*b*a ? *b+ : ( ); /* 將*a、*b 的小者存入 t */if( *w 0 ) *w=t;else if( t *w) *+w=t; /* 將與*w 不相同的 t 存入 w*/while( *a != 0 ) /* 以下將a或b中剩下的字符存入w*/if( *a != *w ) *+w=*a+; else a+;while( *b != 0)if( *b != *w ) *+w=*b+; else b+;*+w = ;strsort( char *s ) /* 將字符串 s 中的字符排序 */ in

25、t i,j,n; char t,*w; ;for( n=0;*w != 0; ) w+;for( i=0;in-1;i+ ) for( j=i+1;jsj ) main( ) char s1100,s2100,s3200; printf(nplease input first string:); scanf(%s,s1);printf(nplease input second string:); scanf(%s,s2);strsort(s1); strsort(s2); = 0; strmerge(s1,s2,s3); printf(nresult:%s,s3);【3.42】已知某數列前兩項

26、為 2 和 3,其后繼項根據前面最后兩項的乘積,按下列規(guī)則生成: 若乘積為一位數,則該乘積即為數列的后繼項; 若乘積為二位數,則該乘積的十位上的數字和個位上的數字依次作為數列的兩個后繼項。下面的程序輸出該數列的前項及它們的和,其中,函數 sum(n,pa) 返回數列的前 n 項和,并將生成的前 n 項存入首指針為 pa 的數組中,程序中規(guī)定輸入的 n 值必須大于 2,且不超過給定的常數值 maxnum。例如:若輸入的值為 10,則程序輸出如下內容: sum(10)=442 3 6 1 8 8 6 4 2 4#include stdio.h #define maxnum 100 int sum(

27、n, pa)int n, *pa; int count, total, temp;*pa = 2; =3; total=5; count=2;while( count+n ) temp = *(pa-1) * *pa; if( temp10 ) total += temp;*(+pa) = temp;else = temp/10; total += *pa; if( countn ) count +; pa+; = temp%10; total += *pa; ;main() int n, *p, *q, nummaxnum; do printf(input n=? (2n%d):, maxn

28、um+1); scanf(%d, &n);while( );printf(nsum(%d)=%dn, n, sum(n, num); for( p=num, q = ; pq; p+ ) printf(%4d, *p);printf(n);【3.43】下面程序的功能是輸入學生的姓名和成績,然后輸出。#include struct stuinf char name20; /* 學生姓名 */ int score; /* 學生成績 */ stu, *p; main ( ) p=&stu; printf(enter name:); gets( );printf(enter score: ); sca

29、nf(%d, );printf(output: %s, %dn, , );【3.44】下面程序的功能是按學生的姓名查詢其成績排名和平均成績。查詢時可連續(xù)進行,直到輸入0 時才結束。#include #include #define num 4struct student int rank; char *name; float score; stu = 3,liming,89.3, 4,zhanghua,78.2, 1,anli,95.1, 2,wangqi,90.6;main() char str10; int i;do printf(enter a name);scanf(%s,str);

30、for( i=0;i=num ) printf(not foundn);while( strcmp(str,0)!=0 );【3.45】下面程序的功能是從終端上輸入個人的年齡、性別和姓名,然后輸出。#include stdio.h struct man char name20; unsigned age; char sex7;main ( ) struct man person5; data_in(person,5); data_out(person,5);data_in(struct man *p, int n ) struct man *q = ;for( ;page, p-sex); ;

31、data_out( struct man *p, int n) struct man *q = ; for( ;pname, p-age, p-sex);【3.46】輸入 n 個整數,儲存輸入的數及對應的序號, 并將輸入的數按從小到大的順序進行排列。要求:當兩個整數相等時,整數的排列順序由輸入的先后次序決定。例如:輸入的第 3 個整數為 5,第 7 個整數也為 5,則將先輸入的整數 5 排在后輸入的整數 5 的前面。程序如下: #include stdio.h#define n 10 struct int no; int num; arrayn; main( ) int i,j,num; fo

32、r( i=0;i=0&arrayj.num num; ) arrayj+1=arrayj;array .num=num; array .no=i;for( i=0;in;i+ ) printf(%d=%d,%dn,i,arrayi.num,arrayi.no);【3.47】以下程序的功能是:讀入一行字符(如:a、.y、z),按輸入時的逆序建立一個鏈接式的結點序列,即先輸入的位于鏈表尾(如下圖),然后再按輸入的相反順序輸出,并釋放全部結點。#include main( ) struct node char info; struct node *link; *top,*p; char c; top

33、=null;while(c= getchar( ) ) p=(struct node *)malloc(sizeof(struct node); p-info=c;p-link=top; top=p;while( top ) ;top=top-link; putchar(p-info); free(p);【3.48】下面函數將指針 p2 所指向的線性鏈表,串接到 p1 所指向的鏈表的末端。假定 p1 所指向的鏈表非空。#define null 0struct link float a;struct link *next;concatenate ( p1,p2 ) struct list *p1

34、,*p2; if( p1-next=null ) p1-next=p2;elseconcatenate( ,p2);【3.49】下面程序的功能是從鍵盤輸入一個字符串,然后反序輸出輸入的字符串。#include struct node char data; struct node *link;*head; main() char ch; struct node *p; head = null;while( ch=getchar()!=n ) p = (struct node *)malloc(sizeof(struct node); p-data = ch;p-link = ;head = ;

35、;while( p!=null ) printf(%c , p-data); p = p-link;【3.50】下面程序的功能是從鍵盤上順序輸入整數,直到輸入的整數小于 0 時才停止輸入。然后反序輸出這些整數。#include struct data int x;struct data *link;*p; input() int num; struct data *q;printf(enter data:); scanf(%d, &num);if( numx = num; q-link = p; p=q; ;main() printf(enter data until datax); ;【3.

36、51】下面函數的功能是創(chuàng)建一個帶有頭結點的鏈表, 將頭結點返回給主調函數。鏈表用于儲存學生的學號和成績。新產生的結點總是位于鏈表的尾部。struct student long num;int score;struct student *next;struct student *creat() struct student *head=null,*tail; long num; int a;tail= malloc(len); do scanf(%ld,%d,&num,&a); if(num!=0) if(head=null) head=tail; else ;tail-num=num; tai

37、l-score=a;tail-next=(struct student *)malloc(len);else tail-next=null;while(num!=0); return( );【3.52】下面 create 函數的功能是建立一個帶頭結點的單向鏈表,新產生的結點總是插入在鏈表的末尾。單向鏈表的頭指針作為函數值返回。#include #define len sizeof(struct student) struct student long num; int score;struct student *next;struct student *creat() struct stude

38、nt *head=null,*tail; long num;int a;tail=( )malloc(len);do scanf(%ld,%d,&num,&a); if(num!=0) if(head=null) head=tail; else tail=tail-next;tail-num=num; tail-score=a;tail-next=( )malloc(len);else tail-next=null;while(num!=0); ;【3.53】下面程序的功能是統計文件中的字符的個數。#include main()file *fp; main() char ch;if(fp=fo

39、pen( )=null) exit(0); while(ch=getchar()!=) fputc(ch,fp); ;【3.55】下面程序的功能是將磁盤上的一個文件復制到另一個文件中,兩個文件名在命令行中給出(假定給定的文件名無誤)。#include main(int argc,char *argv) file &f1,*f2; if(argc ) int n;void sum(),mult(); void (*funcp)(); n=atoi(argv2); if(argc!=3 | n=0) dispform( ); switch ( ) case +: funcp=sum; break;

40、case -: funcp=mult; break;default: dispform( ); ;void sum(int m) int i,s=0; long num=0; printf(thecommandlineerror!);for(i=1;im;i+ ) *fp;exit(0); ;if(fp=fopen(fname.dat,r)=null)printf(sum=%dn,s); printf(cantopenthefile!);f1=fopen(argv1,r);exit(0);f2=fopen(arhv2,w);void mult(int m)while( ) long int i

41、, s=1;while( )fputs(fgetc(f1), );for(i=1;i0)n);個正整數的累加或階乘。例如:如果可執(zhí)行文件的文件exit (0);【3.54】下面程序的功能是把從鍵盤輸入的文件(用名是 sm,則執(zhí)行該程序時輸入:sm+10,可以實現10 的累加;輸入:sm - 10,可以實現求 10 的階乘。作為文件結束標志)復制到一個名為 second.txt 的新文件中。#include #include #include main (int argc,char *argv)【3.57】下面程序的功能是鍵盤上輸入一個字符串,把該字符串中的小寫字母轉換為大寫字母,輸出到文件te

42、st.txt 中,然后從該文件讀出字符串并顯示出來。#include s=p;main()fclose(fp);while( *s & (*s=*q) ) char str100; int i=0;file *fp;if(fp=fopen(test.txt, )=null) printf(cant open the file.n); exit(0);printf(input a string:n); gets(str);while(stri) if(stri= a&stri= z) stri= ;fputc(stri,fp);【3.59】以字符流形式讀入一個文件,從文件中檢索出六種語言的關鍵字

43、,并統計、 輸出每種關鍵字在文件中出現的次數。本程序中規(guī)定:單詞是一個以空格或t、 n結束的字符串。#include #include file *cp;char fname20, buf100; int num;struct key char word10;if( ) keywordi.count+; break;return;main() int i; char *word;printf(input file name:);i+;int count;scanf(%s, fname);keyword= if,0, char,0,int,0,if(cp=fopen(fname, r) =nul

44、l)fclose(fp); fp=fopen(test.txt, ); fgets(str,strlen(str)+1,fp); printf(%sn,str); fclose(fp);else, 0, while, 0, return, 0; char *getword (file *fp) int i=0; char c;while(c=getc(fp) != eof & (c= printf(file open error: %sn, fname); exit(0);num = sizeof(keyword) / sizeof(struct key); while( )|c=t|c=n) ;lookup(word);if( c=eof ) return (null);fclose(cp);【3.58】下面程序的功能是將從終端上讀入的 10 個整數else bufi+=c;for(i=0;inum;i+)以二進制方式寫入名為bi.dat的新文件中。while(c = & c!= &c!= t & c!

溫馨提示

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

評論

0/150

提交評論