《C语言程序设计》第6章习题答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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 :%s\n",n,month_name(n));
}
char *month_name(int n)
{
static char*name[]={"illegal month",
"Jan","Feb","Mar","Apr","May","Jun",
"July","Aug","Sept","Oct","Nov","Dec"};
return ((n<1||n>12)?name[0]:name[n]);
}
(2)
#include
#define N 10
sort(int data[])
{
int i,j,min_a,temp;
for(i=0;i { min_a=i; for(j=i+1;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,data[N],temp; int min_a; printf("\nPlease input %d int:\n",N); for(i=0;i scanf("%d",&data[i]); sort(data); printf("After sorted:\n"); for(i=0;i printf(" %d",data[i]); } (3) #include void reverse(char *c); void main() {char str[80]; puts("Please enter 1 string\n"); gets(str); reverse(str) ; puts("After reversed\n"); 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--;q { 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", "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;i { min_location=i; for(j=i+1;j if(strcmp(keyword[min_location],keyword[j])>0) min_location=j; if(min_location!=i) { temp=keyword[i]; keyword[i]=keyword[min_location]; keyword[min_location]=temp; } } } void print(char *keyword[],int size) { int i; for(i=0;i printf("\n%s",*(keyword+i)); } (5) #include void fun_char(char str1[],char str2[],char str3[]); void main() { char str1[80],str2[80],str3[80],c,i; printf("\nPlease enter 2 string:"); scanf("%s%s",str1,str2); fun_char(str1,str2,str3); printf("Third string 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 str1[80],c,res; puts("\nPlease enter 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;