c语言5版第10章答案,第10章 指 针 参考答案 c语言(1)
10.1 輸入3個整數,按由小到大的順序輸出。
解:程序如下:(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;
}
運行結果如下:
Input three integers n1,n2,n3: 34,21,25↙
Now, the order is: 21,25,34
10.2輸入3個字符串,按由小到大的順序輸出。
解:程序如下:(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)/*交換兩個字符串*/
{ char*p[20];
strcpy(p,p1); strcpy(p1,p2); strcpy(p2,p);
}
運行結果如下:
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個整數,將其中最小的數與第一個數對換,把最大的數與最后一個數對換。寫3個函數:(1)輸入10個數;(2)進行處理;(3)輸出10個數。
解:輸入輸出函數的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);
}
運行結果如下:
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個整數,使其前面各數順序向后移m個位置,最后m個數變成最前面m個數,見圖10.3。寫一函數實現以上功能,在主函數中輸入n個整數,并輸出調整后的n個數。
解:程序如下:(xt10-4.c)
#include
main()
{ intnumber[20],n,m,i;
void move(int array[20],int n,int m);
printf("How many numbers? ");/* 共有多少個數 */
scanf("%d",&n);
printf("Input %d numbers: \n",n);/* 輸入n個數 */
for(i=0;i
scanf("%d",&number[i]);
printf("How many place you want to move? "); /* 后移多少個位置 */
scanf("%d",&m);
move(number,n,m);/* 調用move 函數 */
printf("Now, they are: \n");
for(i=0;i
printf("%d",number[i]);
}
void move(int array[20],int n,int m)/* 循環后移-次的函數 */
{ 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);/* 遞歸調用,當循環次數m減至0 */
/* 時,停止調用 */
}
運行結果:
How many numbers? 8↙
Input 8 numbers:
1243656782711↙
How many place you want to move? 4↙
Now, they are:
8271112436567
10.5 有n個人圍成一圈,順序排號。從第一個人開始報數(從l到3報數),凡報到3的人退出圈子,問最后留下的是原來第幾號的那位。
解:
:(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為序給每個人編號*/
i=0;/* i為每次循環時的計數變量*/
k=0;/* k為按1、2、3報數時的計數變量 */
m=0;/* m為退出人數 */
while(m/* 當退出人數比n-1少時(即未退出人數大于1時)執行循環體 */
{ if(*(p+i)!=0) k++;
if(k==3)/* 對退出的人的編號置為0 */
{ *(p+i)=0;
k=0;
m++;
}
i++;
if(i==n) i=0;/* 報數到尾后, i恢復為0 */
}
while(*p==0) p++;
printf("The last one is NO.%d\n",*p);
}
運行結果:
Input number of person: n=8↙
The last one is NO.7
''
10.6 寫一個函數,求一個字符串的長度。在main函數中輸入字符串,并輸出其長度。
解:程序如下:(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)/* 求字符中長度函數 */
{ intn;
n=0;
while(*p!='\0')
{ n++;
p++;
}
return(n);
}
運行結果:
Input string: China↙
The length of string is 5.
10.7 有一字符串,包含n個字符。寫一個函數,將此字符串中從第m個字符開始的全部字符復制成為另一個字符串。
解:程序如下:(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)/* 字符串部分復制函數 */
{ intn;
n=0;
while(n
{ n++;
p1++;
}
while(*p1!='\0')
{ *p2=*p1;
p1++;
p2++;
}
*p2='\0';
}
運行結果:
Input string: reading-room↙
Which character that begin to copy? 9↙
result: room
10.8 輸入一行文字,找出其中大寫字母、小寫字母、空格、數字及其他字符各有多少。
解:程序如下:(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);
}
運行結果:
Input string: Today is 2000/1/1↙
upperer case:1
lowerer case:6
digit:6
space:2
other:2
10.9 寫一個函數,將一個3×3的矩陣轉置。
解:程序如下:(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;
}
}
運行結果:
Input matrix:
l 2 3↙
4 5 6↙
7 8 9↙
Now,matrix:
1 4 7
2 5 8
3 6 9
l0.l0 將一個5×5的矩陣中最大的元素放在中心,4個角分別放4個最小的元素(按從
左到右、從上到下的順序 ,依次從小到大存放),寫一個函數實現之,并用main數調用。
解:程序如下:(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);/*調用函數, 實現交換*/
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)/*交換函數*/
{ 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;
}
運行結果:
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在主函數中輸入10個等長的字符串。用另一個函數對它們排序,然后在主函數輸出這10個已排好的字符串。
解:程序如下:(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);
}
}
運行結果:
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
總結
以上是生活随笔為你收集整理的c语言5版第10章答案,第10章 指 针 参考答案 c语言(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql hbase 同步_HBase
- 下一篇: c语言 段位,C语言 段位