《2022年《C语言程序设计》习题答案 .pdf》由会员分享,可在线阅读,更多相关《2022年《C语言程序设计》习题答案 .pdf(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1、选择题(1)A(2)C(3)A(4)B(5)B(6)D(7)D(8)B(9)D(10)B 2、填空题(1)a=10,b=20 a=20,b=10(2)*pp=60 3、程序设计题(1)#include char*month_name(int n);void main()int n;printf(nPlease enter 1 integer:);scanf(%d,&n);printf(%d month:%sn,n,month_name(n);char*month_name(int n)static char*name=illegal month,Jan,Feb,Mar,Apr,May,Ju
2、n,July,Aug,Sept,Oct,Nov,Dec;return(n12)?name0:namen);(2)#include#define N 10 sort(int data)int i,j,min_a,temp;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 7 页 -for(i=0;iN;i+)min_a=i;for(j=i+1;jN;j+)if(*(data+j)*(data+min_a)min_a=j;if(min_a!=i)temp=*(data+min_a);*(data+min_a)=*(data+i);*(data+i)=temp;main()int i,j
3、,dataN,temp;int min_a;printf(nPlease input%d int:n,N);for(i=0;iN;i+)scanf(%d,&datai);sort(data);printf(After sorted:n);for(i=0;iN;i+)printf(%d,datai);(3)#include void reverse(char*c);void main()名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 7 页 -char str80;puts(Please enter 1 stringn);gets(str);reverse(str);puts(Af
4、ter reversedn);puts(str);void reverse(char*c)char*p,*q,temp;int size=0;for(p=c;*p!=0;p+)size+;size=size/2;for(q=c,p-;qc+size;q+,p-)temp=*q;*q=*p;*p=temp;(4)#include#include void sort(char*keyword,int size);void print(char*keyword,int size)void main()char*keyword=if,else,case,switch,do,whlie,名师资料总结-精
5、品资料欢迎下载-名师精心整理-第 3 页,共 7 页 -for,break,continue;sort(keyword,9);print(keyword,9);void sort(char*keyword,int size)int i,j,min_location;char*temp;for(i=0;isize-1;i+)min_location=i;for(j=i+1;j0)min_location=j;if(min_location!=i)temp=keywordi;keywordi=keywordmin_location;keywordmin_location=temp;void pri
6、nt(char*keyword,int size)int i;for(i=0;isize;i+)printf(n%s,*(keyword+i);(5)名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 7 页 -#include void fun_char(char str1,char str2,char str3);void main()char str180,str280,str380,c,i;printf(nPlease enter 2 string:);scanf(%s%s,str1,str2);fun_char(str1,str2,str3);printf(Third st
7、ring is%s.,str3);void fun_char(char*str1,char*str2,char*str3)int i,j,k,flag;i=0,k=0;while(*(str1+i)!=0)j=0;flag=1;while(*(str2+j)!=0&flag=1)if(*(str2+j)=*(str1+i)flag=0;j+;if(flag)*(str3+k)=*(str1+i);k+;i+;*(str3+k)=0;(6)#include int count_word(char*str);void main()char str180,c,res;puts(nPlease ent
8、er a string:);gets(str1);printf(There are%d words in this sentence,count_word(str1);int count_word(char*str)int count,flag;char*p;count=0;名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 7 页 -flag=0;p=str;while(*p!=0)if(*p=)flag=0;else if(flag=0)flag=1;count+;p+;return count;(7)#include#include char*encrypt(char*stri
9、ng);char*decrypt(char*string);main()char item80;char*point;char*pEncrypted;char*pDecrype;printf(Please enter the string need to encrypt:n);gets(item);point=item;pEncrypted=encrypt(point);printf(nThe string after encrypted is:n%sn,pEncrypted);pDecrype=decrypt(pEncrypted);printf(nThe string after decr
10、ypted is:n%sn,pDecrype);free(pEncrypted);free(pDecrype);char*encrypt(char*string)char*q,*t;q=(char*)malloc(sizeof(char)*80);if(!q)printf(No place to malloc!);return 0;名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 7 页 -t=q;while(*string!=0)*q=*string-2;string+;q+;*q=0;return t;char*decrypt(char*string)char*q,*t;q=(char*)malloc(sizeof(char)*80);if(!q)printf(No place to malloc!);return 0;t=q;while(*string!=0)*q=*string+2;string+;q+;*q=0;return t;名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 7 页 -