c语言5版第10章答案,第10章 指 针 参考答案 c语言(1)
10.1 輸入3個(gè)整數(shù),按由小到大的順序輸出。
解:程序如下:(xt10-1.c)
#include
main()
{ intn1,n2,n3;
int *p1,*p2,*p3;
printf("Input three integers n1,n2,n3: ");
scanf("%d,%d,%d",&n1,&n2,&n3);
p1=&n1;
p2=&n2;
p3=&n3;
if(n1>n2) swap(p1,p2);
if(n1>n3) swap(p1,p3);
if(n2>n3) swap(p2,p3);
printf("Now, the order is: %d,%d,%d\n",n1,n2,n3);
}
swap(int *p1,int *p2)
{ int p;
p=*p1;*p1=*p2;*p2=p;
}
運(yùn)行結(jié)果如下:
Input three integers n1,n2,n3: 34,21,25↙
Now, the order is: 21,25,34
10.2輸入3個(gè)字符串,按由小到大的順序輸出。
解:程序如下:(xt10-2.c)
#include
#include
main()
{ char*str1[20],*str2[20],*str3[20];
char swap();
printf("Input three line:\n");
gets(str1);
gets(str2);
gets(str3);
if(strcmp(str1,str2)>0) swap(str1,str2);
if(strcmp(str1,str3)>0) swap(str1,str3);
if(strcmp(str2,str3)>0) swap(str2,str3);
printf("Now, the order is:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}
char swap (char *p1,char*p2)/*交換兩個(gè)字符串*/
{ char*p[20];
strcpy(p,p1); strcpy(p1,p2); strcpy(p2,p);
}
運(yùn)行結(jié)果如下:
Input three lines:
I study very hard.↙
C language is very interesting.↙
He is a professor.↙
Now, the order is:
C language is very interesting.
He is a professor.
I study very hard.
10.3 輸入10個(gè)整數(shù),將其中最小的數(shù)與第一個(gè)數(shù)對(duì)換,把最大的數(shù)與最后一個(gè)數(shù)對(duì)換。寫3個(gè)函數(shù):(1)輸入10個(gè)數(shù);(2)進(jìn)行處理;(3)輸出10個(gè)數(shù)。
解:輸入輸出函數(shù)的N-S圖見圖10.1。。
序如下:(xt10-3.c)
#include
main()
{ intnumber[10];
input(number);
max_min_value(number);
output(number);
}
input(int number[10])
{ inti;
printf("Input 10 numbers: ");
for(i=0;i<10;i++)
scanf("%d",&number[i]);
}
max_min_value(int array[10])
{ int*max,*min,*p,*array_end;
array_end=array+10;
max=min=array;
for(p=array+1;p
if(*p>*max) max=p;
else if(*p
*p=array[0]; array[0]=*min; *min=*p;
*p=array[9];array[9]=*max;*max=*p;
return;
}
output(int array[10])
{ int*p;
printf("Now, they are: ");
for(p=array;p<=array+9;p++)
printf("%d ",*p);
}
運(yùn)行結(jié)果如下:
Input 10 numbers: 32 24 56 78 1 98 36 44 29 6↙
Now, they are: 1 24 56 78 32 6 36 44 29 98
10.4 有n個(gè)整數(shù),使其前面各數(shù)順序向后移m個(gè)位置,最后m個(gè)數(shù)變成最前面m個(gè)數(shù),見圖10.3。寫一函數(shù)實(shí)現(xiàn)以上功能,在主函數(shù)中輸入n個(gè)整數(shù),并輸出調(diào)整后的n個(gè)數(shù)。
解:程序如下:(xt10-4.c)
#include
main()
{ intnumber[20],n,m,i;
void move(int array[20],int n,int m);
printf("How many numbers? ");/* 共有多少個(gè)數(shù) */
scanf("%d",&n);
printf("Input %d numbers: \n",n);/* 輸入n個(gè)數(shù) */
for(i=0;i
scanf("%d",&number[i]);
printf("How many place you want to move? "); /* 后移多少個(gè)位置 */
scanf("%d",&m);
move(number,n,m);/* 調(diào)用move 函數(shù) */
printf("Now, they are: \n");
for(i=0;i
printf("%d",number[i]);
}
void move(int array[20],int n,int m)/* 循環(huán)后移-次的函數(shù) */
{ int*p,array_end;
array_end=*(array+n-1);
for(p=array+n-1;p>array;p--)
*p=*(p-1);
*array=array_end;
m--;
if(m>0) move(array,n,m);/* 遞歸調(diào)用,當(dāng)循環(huán)次數(shù)m減至0 */
/* 時(shí),停止調(diào)用 */
}
運(yùn)行結(jié)果:
How many numbers? 8↙
Input 8 numbers:
1243656782711↙
How many place you want to move? 4↙
Now, they are:
8271112436567
10.5 有n個(gè)人圍成一圈,順序排號(hào)。從第一個(gè)人開始報(bào)數(shù)(從l到3報(bào)數(shù)),凡報(bào)到3的人退出圈子,問最后留下的是原來(lái)第幾號(hào)的那位。
解:
:(xt10-5.c)
#include
main()
{ int i,k,m,n,num[50],*p;
printf("Input number of person: n=");
scanf("%d",&n);
p=num;
for(i=0;i
*(p+i)=i+1;/*以l至n為序給每個(gè)人編號(hào)*/
i=0;/* i為每次循環(huán)時(shí)的計(jì)數(shù)變量*/
k=0;/* k為按1、2、3報(bào)數(shù)時(shí)的計(jì)數(shù)變量 */
m=0;/* m為退出人數(shù) */
while(m/* 當(dāng)退出人數(shù)比n-1少時(shí)(即未退出人數(shù)大于1時(shí))執(zhí)行循環(huán)體 */
{ if(*(p+i)!=0) k++;
if(k==3)/* 對(duì)退出的人的編號(hào)置為0 */
{ *(p+i)=0;
k=0;
m++;
}
i++;
if(i==n) i=0;/* 報(bào)數(shù)到尾后, i恢復(fù)為0 */
}
while(*p==0) p++;
printf("The last one is NO.%d\n",*p);
}
運(yùn)行結(jié)果:
Input number of person: n=8↙
The last one is NO.7
''
10.6 寫一個(gè)函數(shù),求一個(gè)字符串的長(zhǎng)度。在main函數(shù)中輸入字符串,并輸出其長(zhǎng)度。
解:程序如下:(xt10-6.c)
#include
main()
{ intlen;
char *str[20];
int length(char *p);
printf("Input string: ");
scanf("%s",str);
len=length(str);
printf("The length of string is %d.\n",len);
}
int length(char *p)/* 求字符中長(zhǎng)度函數(shù) */
{ intn;
n=0;
while(*p!='\0')
{ n++;
p++;
}
return(n);
}
運(yùn)行結(jié)果:
Input string: China↙
The length of string is 5.
10.7 有一字符串,包含n個(gè)字符。寫一個(gè)函數(shù),將此字符串中從第m個(gè)字符開始的全部字符復(fù)制成為另一個(gè)字符串。
解:程序如下:(xt10-7.c)
#include
#include
main()
{ intm;
char *str1[20],*str2[20];
void copystr(char *p1,char *p2,int m);
printf("Input string: ");
gets(str1);
printf("Which character that begin to copy? ");
scanf("%d",&m);
if(strlen(str1)
printf("Input error!");
else
{ copystr(str1,str2,m);
printf("resut: %s\n",str2);
}
}
void copystr(char *p1,char *p2,int m)/* 字符串部分復(fù)制函數(shù) */
{ intn;
n=0;
while(n
{ n++;
p1++;
}
while(*p1!='\0')
{ *p2=*p1;
p1++;
p2++;
}
*p2='\0';
}
運(yùn)行結(jié)果:
Input string: reading-room↙
Which character that begin to copy? 9↙
result: room
10.8 輸入一行文字,找出其中大寫字母、小寫字母、空格、數(shù)字及其他字符各有多少。
解:程序如下:(xt10-8.c)
#include
main()
{ intupper=0,lower=0,digit=0,space=0,other=0,i=0;
char *p,s[80];
printf("Input string: ");
while((s[i]=getchar())!='\n') i++;
p=&s[0];
while(*p!='\n')
{ if(('A'<=*p)&&(*p<='Z')) upper++;
else if(('a'<=*p)&&(*p<='z')) lower++;
else if(*p==' ')space++;
else if(('0'<=*p)&&(*p<='9')) digit++;
elseother++;
p++;
}
printf("upperer case:%d\n",upper);
printf("lowerer case:%d\n",lower);
printf("digit:%d\n",digit);
printf("space:%d\n",space);
printf("other:%d\n",other);
}
運(yùn)行結(jié)果:
Input string: Today is 2000/1/1↙
upperer case:1
lowerer case:6
digit:6
space:2
other:2
10.9 寫一個(gè)函數(shù),將一個(gè)3×3的矩陣轉(zhuǎn)置。
解:程序如下:(xt10-9.c)
#include
main()
{ inta[3][3],*p,i;
void move(int *pointer);
printf("Input matrix: \n");
for(i=0;i<3;i++)
scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);
p=&a[0][0];
move(p);
printf("Now,matrix: \n");
for(i=0;i<3;i++)
printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);
}
voidmove(int *pointer)
{ inti,j,t;
for(i=0;i<3;i++)
for(j=i;j<3;j++)
{ t=*(pointer+3*i+j);
*(pointer+3*i+j)=*(pointer+3*j+i);
*(pointer+3*j+i)=t;
}
}
運(yùn)行結(jié)果:
Input matrix:
l 2 3↙
4 5 6↙
7 8 9↙
Now,matrix:
1 4 7
2 5 8
3 6 9
l0.l0 將一個(gè)5×5的矩陣中最大的元素放在中心,4個(gè)角分別放4個(gè)最小的元素(按從
左到右、從上到下的順序 ,依次從小到大存放),寫一個(gè)函數(shù)實(shí)現(xiàn)之,并用main數(shù)調(diào)用。
解:程序如下:(xt10-10.c)
#include
main()
{ inta[5][5],*p,i,j;
void change(int *p);
printf("Input matrix:\n");
for(i=0;i<5;i++)/*輸入矩陣*/
for(j=0;j<5;j++)
scanf("%d",&a[i][j]);
p=&a[0][0];/*使p指向0行0列元素*/
change(p);/*調(diào)用函數(shù), 實(shí)現(xiàn)交換*/
printf("Now, matrix: \n");
for(i=0;i<5;i++)/*輸出已交換的矩陣*/
{ for(j=0;j<5;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
void change(int *p)/*交換函數(shù)*/
{ inti,j,temp;
int *pmax,*pmin;
pmax=p;
pmin=p;
for(i=0;i<5;i++)/*找最大值和最小值的地址,并賦給pmax,pmin*/
for(j=0;j<5;j++)
{ if(*pmax
if(*pmin>*(p+5*i+j)) pmin=p+5*i+j;
}
temp=*(p+12);/*將最大值換給中心元素*/
*(p+12)=*pmax;
*pmax=temp;
temp=*p;/*將最小值換給左上角元素*/
*p=*pmin;
*pmin=temp;
pmin=p+1;
for(i=0;i<5;i++)/*找第二最小值的地址賦給pmin*/
for(j=0;j<5;j++)
if(((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;
temp=*pmin;/*將第二最小值換給右上角元素*/
*pmin=*(p+4);
*(p+4)=temp;
pmin=p+1;
for(i=0;i<5;i++)/*找第三最小值的地址賦給pmin*/
for(j=0;j<5;j++)
if(((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))
pmin=p+5*i+j;/*將第三最小值換給左下角元素*/
temp=*pmin;
*pmin=*(p+20);
*(p+20)=temp;
pmin=p+1;
for(i=0;i<5;i++)/*找第四最小值的地址賦給pmin*/
for(j=0;j<5;j++)
if(((p+5*i+j)!=p)&&((p+5*i+j)!=(p+4))&&((p+5*i+j)!=
(p+20))&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;
temp=*pmin;/*將第四最小值換給右下角元素*/
*pmin=*(p+24);
*(p+24)=temp;
}
運(yùn)行結(jié)果:
Input matrix:
35 34 33 32 31↙
3O 29 28 27 26↙
25 24 23 22 21↙
15 14 13 12 ll↙
Now, matrix:
11 34 33 32 12
30 29 28 27 26
25 24 35 22 21
20 l9 18 17 16
13 23 15 31 14
10.11在主函數(shù)中輸入10個(gè)等長(zhǎng)的字符串。用另一個(gè)函數(shù)對(duì)它們排序,然后在主函數(shù)輸出這10個(gè)已排好的字符串。
解:程序如下:(xt10-11.c)
#include
#include
main()
{ voidsort(char s[10][6]);
int i;
char str[10][6];
printf("Input 10 strings:\n");
for(i=0;i<10;i++)
scanf("%s",str[i]);
sort(str);
printf("Now, the sequence is:\n");
for(i=0;i<10;i++)
printf("%s\n",str[i]);
}
voidsort(char s[10][6])
{ inti,j;
char *p,temp[10];
p=temp;
for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
if(strcmp(s[j],s[j+1])>0 )
{ strcpy(p,s[j]);
strcpy(s[j],s[+j+1]);
strcpy(s[j+1],p);
}
}
運(yùn)行結(jié)果:
Input 10 strings:
China↙
Japan↙
Korea↙
Egypt↙
Nepal↙
Burma↙
Ghana↙
Sudan↙
Italy↙
Libya↙
Now, the sequence is:
Burma
China
Egypt
Ghana
Italy
Japan
Korea
Libya
Nepal
Sudan
總結(jié)
以上是生活随笔為你收集整理的c语言5版第10章答案,第10章 指 针 参考答案 c语言(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql hbase 同步_HBase
- 下一篇: python argparse_Pyth