SiriBlog

siriyang的个人博客


  • 首页

  • 排行榜

  • 标签115

  • 分类37

  • 归档320

  • 关于

  • 搜索

《C语言程序设计(第五版)谭浩强》课后习题答案源码

发表于 2020-03-17 更新于 2021-10-29 分类于 计算机 , 算法题 阅读次数: Valine:
本文字数: 36k 阅读时长 ≈ 33 分钟

《C语言程序设计:学习辅导(第五版)谭浩强》

第三章 顺序程序设计

1、

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <math.h>
int main(){
float p,r,n;
r=0.07;
n=10;
p=pow(1+r,n);
printf("p=%f\n",p);
return 0;
}

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <math.h>
int main(){
float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;
p=1000;
r5=0.03;
r3=0.0275;
r2=0.021;
r1=0.015;
r0=0.0035;

p1=p*(1+r5*5);
p2=p*(1+2*r2)*(1+3*r3);
p3=p*(1+3*r3)*(1+2*r2);
p4=p*pow(1 +r1,5);
p5=p*pow(1+r0/4,4*5);
printf("pl= %f\n",p1);
printf("p2= %f\n",p2);
printf("p3= %f\n",p3);
printf("p4= %f\n",p4);
printf("p5= %f\n",p5);
return 0;
}

3、

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <math.h>
int main(){
float d=300000,p= 6000,r=0.01,m;
m=log10(p/(p-d*r))/log10(1+r);
printf("m=%6.1f\n",m);
return 0;
}

4、

1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main(){
char c1,c2;
c1=97;
c2=98;
printf("c1=%c,c2=%c\n",c1,c2);
printf("c1=%d,c2=%d\n",c1,c2);
return 0;
}

5、

输入数据

1
2
a=3 b=7
8.5 71.82Aa

代码:

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main(){
int a,b;
float x,y;
char c1,c2;
scanf("a= %d b= %d",&a,&b);
scanf("%f %e",&x,&y);
scanf("%c%c",&c1,&c2);
printf("a=%d,b=%d,x=%f,y=%f,cl=%c,c2=%c\n" ,a,b,x,y,c1,c2);
return 0;
}

6、

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main(){
char c1='C',c2='h',c3='i',c4='n',c5='a';
c1=c1+4;
c2=c2+4;
c3=c3+4;
c4=c4+4;
c5=c5+4;
printf("password is %c%c%c%c%c\n",c1,c2,c3,c4,c5);
return 0;
}

7、

输入数据

1
1.5,3

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main(){
float h,r,l,s,sq,vq,vz;
float pi=3.141526;
printf("请输人圆半径r,圆柱高h:");
scanf("%f,%f",&r,&h);//要求输人圆半径r和圆柱高h
l=2*pi*r;//计算圆周长l
s=r*r*pi;//计算圆面积s
sq=4*pi*r*r;//计算圆球表面积sq
vq=3.0/4.0*pi*r*r*r;//计算圆球体积vq
vz=pi*r*r*h;//计算圆柱体积vz
printf("圆周长为:l= %6.2f\n",l);
printf("圆面积为:s=%6.2f\n",s);
printf("圆球表面积为:sq=%6.2f\n",sq);
printf("圆球体积为:v=%6.2f\n",vq) ;
printf("圆柱体积为:vz=%6.2f\n",vz);
return 0;
}

第四章 选择结构程序设计

4、

输入数据

1
12,34,9

代码:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main(){
int a,b,c,max;
printf("请输入3个整数:");
scanf("%d,%d,%d",&a,&b,&c);
max=a>b?a:b;
max=max>c?max:c;
printf("3个整数的最大数是%d\n",max);
return 0;
}

5、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <math.h>
#define M 1000
int main(){
int i,k;
printf("请输入一个小于%d的整数i:",M);
scanf("%d",&i);
while (i> M){
printf("输入的数据不符合要求,请重新输入一个小于%d的整数i:" ,M);
scanf("%d",&i);
k= sqrt(i);
}
printf("%d的平方根的整数部分是%d\n",i,k);
return 0;
}

6、

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(){
int x;
printf("输入x:");
scanf("%d",&x);
if(x<1)
printf("x=%d, y=x=%d\n",x,x);
else if(x<10)
printf("x=%d, y=2*x-1=%d\n",x,2*x-1);
else
printf("x=%d, y=2*x-11=%d\n",x,2*x-11);
return 0;
}

7、

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(){
int x;
printf("输入x:");
scanf("%d",&x);
if(x<0)
printf("x=%d, y=-1\n",x);
else if(x==0)
printf("x=%d, y=0\n",x);
else
printf("x=%d, y=1\n",x);
return 0;
}

8、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main(){
float score;
char grade;
printf("请输入学生成绩:");
scanf("%f",&score);
while(score<0 || score>100){
printf("\n输入有误,请重输");
scanf("%f",&score);
}
switch((int)(score/10)){
case 10:
case 9: grade='A';break;
case 8: grade='B';break;
case 7: grade='C';break;
case 6: grade='D';break;
default: grade='E';break;
}
printf("成绩是%5.1f,相应的等级是%c\n",score,grade);
return 0;
}

9、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
int main(){
int num,len;
int vers[5]={0};
printf("请输人一个整数(0~ 9999):");
scanf("%d",&num);
if(num==0){
len=1;
}
else{
while(num>0){
vers[len]=num%10;
num/=10;
len++;
}
}
printf("位数:%d\n",len);
printf("每位数字为:");
for(int i=len-1;i>=0;i--)printf("%d,",vers[i]);
printf("\n反序数字为:");
for(int i=0;i<len;i++)printf("%d",vers[i]);


return 0;
}

10、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
int main(){
int i;
double bonus,bon1,bon2,bon4,bon6,bon10;
int branch;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
printf("请输人利润i:");
scanf("%d",&i);
branch=i/100000;
if (branch>10) branch=10;
switch( branch){
case 0:bonus=i*0.1;break;
case 1:bonus=bon1+(i-100000)*0.075;break;
case 2:
case 3:bonus=bon2+(i-200000)*0.05;break;
case 4:
case 5:bonus=bon4+(i-400000)*0.03;break;
case 6:
case 7:
case 8:
case 9:bonus=bon6+(i-600000)*0.015;break;
case 10:bonus=bon10+(i-1000000)*0.01;
}
printf("奖金是%10.2f\n",bonus);
return 0;
}

11、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
int main(){
int t,a,b,c,d;
printf("请输入4个数:");
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);
if (a>b)
{ t=a;a=b;b=t;}
if (a>c)
{ t=a;a=c;c=t;}
if (a>d)
{ t=a;a=d;d=t;}
if (b>c)
{ t=b;b=c;c=t;}
if (b>d)
{ t=b;b=d;d=t;}
if (c>d)
{ t=c;c=d;d=t;}
printf("排序结果如下: \n");
printf("%d %d %d %d \n",a,b,c,d);
return 0;
}

12、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main(){
int h=10;
float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4;
printf("请输人一个点(x,y):");
scanf("%f,%f",&x,&y);
d1=(x-x4)*(x-x4)+(y-y4)*(y-y4);//求该点到各中心点距离
d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);
d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);
d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);
if (d1>1 && d2>1 && d3>1 && d4>1) h=0; //判断该点是否在塔外
printf("该点高度为%d\n" ,h);
return 0;
}

第五章 循环结构程序设计

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <math.h>
int main(){
int sign=1,count=0;
double pi=0.0,n=1.0,term=1.0;
while(fabs(term)>=1e-8){
pi=pi+term;
n=n+2;
sign=-sign;
term=sign/n;
count++;
}
pi=pi*4;
printf("pi=%10.8f\n",pi);
printf("count=%d\n",count);
return 0;
}

3、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
int main(){
int p,r,n, m, temp;
printf("请输人两个正整数n,m:");
scanf("%d,%d,",&n,&m);
if(n<m){
temp=n;
n=m;
m=temp;
}
p=n*m;
while(m!=0){
r=n%m;
n=m;
m=r;
}
printf("它们的最大公约数为:%d\n" ,n);
printf("它们的最小公倍数为:%d\n",p/n);
return 0;
}

4、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main(){
char c;
int letters=0,space=0,digit=0,other=0;
printf("请输入一行字符:\n");
while((c=getchar())!='\n'){
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if (c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
other++;
}
printf("字母数:%d\n空格数:%d\n数字数:%d\n其他字符数: %d\n",letters,space,digit,other);
return 0;
}

5、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main(){
int a,n,i=1,sn=0,tn=0;
printf("a,n=");
scanf("%d,%d",&a,&n);
while (i<=n){
tn=tn+a;
sn=sn+tn;
a=a*10;
++i;
}
printf("a+aa+aaa+...=%d\n",sn);
return 0;
}

6、

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main(){
double s=0,t=1;
int n;
for(n=1;n<=20;n++){
t=t*n;
s=s+t;
}
printf("1!+2!+...+20!=%22.15e\n",s);
return 0;
}

7、

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(){
int n1=100,n2=50,n3=10;
double k,s1=0,s2=0,s3=0;
for(k=1;k<=n1;k++)
{s1=s1+k;}
for(k=1;k<=n2;k++)
{s2=s2+k*k;}
for(k=1;k<=n3;k++)
{s3=s3+1/k;}
printf("sum=%15.6f\n",s1+s2+s3);
return 0;
}

8、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main(){
int i,j,k,n;
printf("parcissus numbers are ");
for(n=100;n<1000;n++){
i=n/100;
j=n/10-i*10;
k=n%10;
if (n==i*i*i+j*j*j+k*k*k)
printf("%d ",n);
}
printf("\n");
return 0;
}

9、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
int main()
{
for(int i=2;i<1000;i++){
int s=0;
for(int j=1;j<i;j++){
if((i%j)==0)s+=j;
}
if(i==s){
printf("%d its factors are",i);
for(int j=1;j<i;j++)
if(i%j==0)
printf(" %d",j);
printf("\n");
}
}
return 0;
}

10、

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
int main(){
int i,n=20;
double a=2,b=1,s=0,t;
for (i=1;i<=n;i++){
s=s+a/b;
t=a;
a=a+b;
b=t;
}
printf("sum=%16.10f\n",s);
return 0;
}

11、

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main(){
double sn=100,hn=sn/2;
int n;
for(n=2;n<=10;n++){
sn=sn+2*hn;//第n次落地时共经过的米数
hn=hn/2;//第n次反跳高度
}
printf("第10次落地时共经过%f米\n",sn);
printf("第10次反弹%f米\n",hn);
return 0;
}

12、

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main() {
int n=10;
int res=1;
for(int i=1;i<n;i++){
res=(res+1)*2;
}
printf("total=%d",res);
return 0;
}

13、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main(){
double X;
printf("enter a positive number:");
scanf("%lf",&X);
double x0,x1;
x0=X/2;
x1=(x0+X/x0)/2;
do{
x0=x1;
x1=(x0+X/x0)/2;
}while(x0-x1>=1e-5);
printf("The square root of %5.2f is %8.5f",X,x1);
return 0;
}

14、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <math.h>
int main(){
double x1,x0,f,f1;
x1=1.5;
do{
x0=x1;
f=((2*x0-4)*x0+3)*x0-6;
f1=(6*x0-8)*x0+3;
x1=x0-f/f1;
} while(fabs(x1-x0)>=1e-5);
printf("The root of equation is %5.2f\n",x1);
return 0;
}

15、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <math.h>
int main(){
float x0,x1,x2,fx0,fx1,fx2;
do{
printf("enter x1 & x2:");
scanf("%f,%f",&x1,&x2);
fx1=x1*((2*x1-4)*x1+3)-6;
fx2=x2*((2*x2-4)*x2+3)-6;
}while(fx1*fx2>0);
do{
x0=(x1+x2)/2;
fx0=x0*((2*x0-4)*x0+3)-6;
if((fx0*fx1)<0){
x2=x0;
fx2=fx0;
}
else{
x1=x0;
fx1=fx0;
}
} while(fabs(fx0)>=1e-5);
printf("x=%6.2f\n",x0);
return 0;
}

16、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>
int main(){
int i,j,k;
for(i=0;i<=3;i++){
for(j=0;j<=2-i;j++)
printf(" ");
for(k=0;k<=2*i;k++)
printf("*");
printf("\n");
}
for (i=0;i<=2;i++){
for (j=0;j<=i;j++)
printf(" ");
for (k=0;k<=4-2*i;k++)
printf("*");
printf("\n");
}
return 0;
}

17、

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main(){
char i,j,k;
for(i='x';i<='z';i++)
for(j='x';j<='z';j++)
if(i!=j)
for(k='x';k<='z';k++ )
if (i!=k&&j!=k)
if(i!='x'&&k!='x'&&k!='z')
printf("A——%c\nB——%c\nC——%c\n",i,j,k);
return 0;
}

第六章 利用数组处理批量数据

1、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <math.h>

int main() {

int num[10000]={0};
int n=100;
int i,j;

for(i=2;i<=n;i++)
num[i]=i;

for(i=2;i<sqrt(n);i++){
for(j=i+1;j<=n;j++){
if(num[i]!=0 && num[j]!=0)
if(num[j]%num[i]==0)
num[j]=0;
}
}

int c=0;
for(i=2;i<=n;i++)
if(num[i]!=0){
printf("%5d",i);
if(++c%10==0) printf("\n");
}


return 0;
}

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>

int main() {

int num[10]={0};
int n=10,min,t;
int i,j;

printf("enter data:\n");
for(i=0;i<n;i++){
printf("a[%d]=",i+1);
scanf("%d",num+i);
}

printf("The original numbers:\n");
for(i=0;i<n;i++)
printf("%5d ",num[i]);
printf("\n");

for(i=0;i<n;i++){
min=i;
for(j=i;j<n;j++){
if(num[j]<num[min]){
min=j;
}
}
t=num[i];
num[i]=num[min];
num[min]=t;
}

printf("The sorted numbers:\n");
for(i=0;i<n;i++)
printf("%5d ",num[i]);
printf("\n");

return 0;
}

3、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main() {

int num[10]={0};
int sum=0;

printf("enter data:\n");

for(int i=0;i<9;i++)
scanf("%d",num+i);

sum=num[1]+num[4]+num[7];

printf("sum=%6d",sum);


return 0;
}

4、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

int main() {

int a[11]={1,4,6,9,13,16,19,28,40,100};
int num;

printf("array a:\n");
for(int i=0;i<10;i++)printf("%5d",a[i]);
printf("\n");

printf("insert data:");
scanf("%d",&num);

int f=0;
while(f<10 && a[f]<num)f++;

if(f<10)
for(int i=10;i>f;i--)a[i]=a[i-1];
a[f]=num;

printf("Now array a:\n");
for(int i=0;i<11;i++)printf("%5d",a[i]);
printf("\n");

return 0;
}

5、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#define N 5

int main() {

int a[10]={0};
int num;

printf("enter array a:\n");
for(int i=0;i<N;i++)scanf("%d",a+i);

printf("array a:\n");
for(int i=0;i<N;i++)printf("%4d",a[i]);
printf("\n");

for(int i=0;i<N/2;i++){
int t=a[i];
a[i]=a[N-i-1];
a[N-i-1]=t;
}

printf("Now,array a:\n");
for(int i=0;i<N;i++)printf("%4d",a[i]);
printf("\n");

return 0;
}

6、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int main() {

int num[1000]={0};
int n=10;

for(int i=1;i<=n;i++){
for(int j=i-1;j>=0;j--){
if(j==0)
num[j]=1;
else if(j==i-1)
num[j]=1;
else
num[j]=num[j]+num[j-1];
}

for(int j=0;j<i;j++)
printf("%6d ",num[j]);
printf("\n");
}
printf("\n");

return 0;
}

7、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
int main() {

int a[15][15]={0},i,j,k,n;
while(1){
printf("enter n(n=1--15):");
scanf("%d",&n);
if(n!=0&&n<=15&&n%2!=0)break;
}

i=0;
j=n/2;
a[0][j]=1;
for(k=2;k<=n*n;k++){
if(i==0&&j==n-1){
i=1;
}
else{
i-=1;
j+=1;
}
if(i<0)i=n-1;
if(j>=n)j=0;

if(a[i][j]==0){
a[i][j]=k;
}
else{
i+=2;
j-=1;
a[i][j]=k;
}
}

for(i=0;i<n;i++){
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}

return 0;
}

8、

输入样例:

样例1
1
2
3
4
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
样例2(无鞍点)
1
2
3
4
1 2 3 4 11
2 4 6 8 12
3 6 9 10 15
4 8 12 16 7

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
int main() {

int mat[4][5]={0},i,j,t,max;
int f=0;

printf("please input matrix:\n");
for(i=0;i<4;i++)
for(j=0;j<5;j++)
scanf("%d",&mat[i][j]);

for(i=0;i<4&&f==0;i++){
f=1;
j=0;
for(t=0;t<5;t++){
if(mat[i][t]>mat[i][j])j=t;
}

for(t=0;t<4;t++){
if(mat[i][j]>mat[t][j])f=0;
}
if(f)
break;
}

if(f)
printf("a[%d][%d]=%d\n",i,j,mat[i][j]);
else
printf("It is not exist!\n");

return 0;
}

9、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#define N 15
int main() {

int i,number,top,bott,mid,loca,a[N],flag=1,sign;
char c;
printf("enter data:\n");
scanf("%d",&a[0]);
i=1;
while(i<N){
scanf("%d",&a[i]);
if(a[i]>=a[i-1])i++;
else
printf("enter this data again:\n");
}
printf("\n");
for(i=0;i<N;i++)
printf("%5d",a[i]);
printf("\n");
while(flag){
printf("input number to look for:");
scanf("%d",&number);
sign=0;
top=0;
bott=N-1;
if((number<a[0])||(number>a[N-1]))loca=-1;
while((!sign)&&(top<=bott)){
mid=(bott+top)/2;
if(number==a[mid]){
loca=mid;
printf("Has found %d,its position is %d\n",number,loca+1);
sign=1;
}
else if(number<a[mid]) bott=mid-1;
else top=mid+1;
}
if(!sign || loca==-1)
printf("cannot find %d.\n",number);
printf("continue or not(Y/N)?");
scanf(" %c",&c);
if(c=='N'||c=='n')flag=0;
}

return 0;
}

10、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
int main() {

int i,j,upp,low,dig,spa,oth;
char text[80];
upp=low=dig=spa=oth=0;

for(i=0;i<3;i++){
printf("please input line %d:\n",i+1);
gets(text);
for(j=0;j<80 && text[j]!=0;j++){
if(text[j]>='A'&&text[j]<='Z')upp++;
else if(text[j]>='a'&&text[j]<='z')low++;
else if(text[j]>='0'&&text[j]<='9')dig++;
else if(text[j]==' ')spa++;
else oth++;
}
}

printf("\nupper case:%d\n",upp);
printf("\nlower case:%d\n",low);
printf("\ndigit:%d\n",dig);
printf("\nspace:%d\n",spa);
printf("\nother:%d\n",oth);

return 0;
}

11、

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main() {

for(int i=0;i<5;i++){
for(int j=0;j<i;j++)printf(" ");
for(int j=0;j<5;j++)printf("*");
printf("\n");
}

return 0;
}

12、

输入样例:

1
R droo erhrg Xsrmz mvcg dvvp.

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
int main() {

char ch[80];

printf("input cipher code:");
gets(ch);
printf("cipher code:%s\n",ch);

for(int i=0;ch[i]!=0;i++){
if(ch[i]>='a'&&ch[i]<='z')ch[i]=219-ch[i];
else if(ch[i]>='A'&&ch[i]<='Z')ch[i]=155-ch[i];
else ch[i]=ch[i];
}

printf("oringal text:%s",ch);

return 0;
}

13、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
int main() {

char s1[80],s2[40];

printf("input string1:");
scanf("%s",s1);
printf("input string2:");
scanf("%s",s2);

char *p1,*p2;
for(p1=s1;*p1!=0;p1++);
for(p2=s2;*p2!=0;p1++,p2++)*p1=*p2;
*p1=0;

printf("The new string is:%s",s1);

return 0;
}

14、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main() {

char s1[100],s2[100];

printf("input string1:");
gets(s1);
printf("\ninput string2:");
gets(s2);

int i=0,res;
while((s1[i]==s2[i])&&(s1[i]!=0))i++;
if(s1[i]==0&&s2[i]==0)
res=0;
else
res=s1[i]-s2[i];

printf("result:%d",res);

return 0;
}

15、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
int main() {

char s1[100],s2[100];

printf("input string2:");
gets(s2);

char *p1=s1,*p2=s2;
for(;*p2!=0;p1++,p2++)*p1=*p2;
*p1=*p2;

printf("s1:%s",s1);

return 0;
}

第七章 用函数实现模块化程序设计

1、

方法一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>

int hcf(int u,int v){
int t,r;
if(v>u){
t=u;u=v;v=t;
}
while((r=u%v)!=0){
u=v;
v=r;
}
return v;
}

int lcd(int u,int v,int h){
return u*v/h;
}

int main() {
int u,v,h,l;
scanf("%d,%d",&u,&v);
h=hcf(u,v);
printf("H.C.F=%d\n",h);
l=lcd(u,v,h);
printf("L.C.D=%d\n",l);

return 0;
}
方法二
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
int Hcf,Lcd; //Hef和Lced是全局变量
int main( ){
void hcf(int,int);
void lcd(int,int);
int u,v;
scanf("%d,%d",&u,&v);
hcf(u,v); //调用hef函数
lcd(u,v); //调用lcd函数
printf("H.C.F= %d\n" ,Hcf);
printf("L.C.D= %d\n" ,Lcd);
return 0;
}

void hcf(int u,int v){
int t,r;
if(v>u){
t=u;u=v;v=t;
}
while((r= u%v)!=0){
u=v;
v=r;
}

Hcf=v; //把求出的最大公约数赋给全局变量Hef
}

void lcd(int u,int v){
Lcd=u*v/Hcf; //把求出的最小公倍数赋给全局变量Led
}

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <math.h>
float x1,x2,disc,p,q;
int main( ){
void greater_than_zero( float, float);
void equal_to_zero( float, float);
void smaller_than_zero(float, float);
float a,b,c;
printf("input a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
printf("equation: %5.2f*x*x+%5.2f*x+%5.2f=0\n",a,b,c);
disc=b*b-4*a*c;
printf("root:\n");
if (disc>0){
greater_than_zero(a,b);
printf("x1= %f\t\tx2= %f\n",x1,x2);
}
else if (disc==0){
equal_to_zero(a,b);
printf("x1= %f\t\tx2=%f\n",x1,x2);
}
else{
smaller_than_zero(a,b);
printf("x1=%f+%fi\tx2=%f-%fi\n",p,q,p,q);
}
return 0;
}

void greater_than_zero(float a,float b){
x1=(-b+sqrt(disc))/(2* a);
x2=(-b-sqrt(disc))/(2* a);
}

void equal_to_zero(float a,float b){
x1=x2=(-b)/(2*a);
}

void smaller_than_zero( float a,float b){
p=-b/(2*a);
q=sqrt(-disc)/(2*a);
}

3、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
int main( ){
int prime(int);
int n;
printf("input an integer:");
scanf(" %d",&n);
if (prime(n))
printf(" %d is a prime. \n" ,n);
else
printf("%d is not a prime. \n" ,n);
return 0;
}

int prime(int n){
int flag=1,i;
for (i=2;i<n/2 && flag==1;i++)
if (n%i==0)
flag=0;
return(flag);
}

4、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#define N 3
int array[N][N];
int main( ){
void convert(int array[][3]);
int i,j;
printf("input array:\n");
for (i=0;i<N;i++)
for (j=0;j<N;j++)
scanf("%d" ,&array[i][j]);
printf("\noriginal array :\n");
for (i=0;i<N;i++){
for (j=0;j<N;j++)
printf("%5d" ,array[i][j]);
printf("\n");
}

convert(array) ;
printf("convert array:\n");
for (i=0;i<N;i++ ){
for (j=0;j<N;j++)
printf("%5d" ,array[i][j]);
printf("\n");
}
return 0;
}

void convert(int array[][3]){ //定义转置数组的函数
int i,j,t;
for (i=0;i<N;i++)
for (j=i+1;j<N;j++){
t= array[i][j];
array[i][j]= array[j][i];
array[j][i]=t;
}
}

5、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <string.h>
int main( ){
void inverse(char str[ ]);
char str[100];
printf("input string:");
scanf("%s",str);
inverse(str);
printf("inverse string: %s\n",str);
return 0;
}

void inverse(char str[]){
char t;
int i,j;
for(i=0,j=strlen(str);i<(strlen(str)/2);i++,j--){
t=str[i];
str[i]=str[j-1];
str[j-1]=t;
}
}

6、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main( ){
void concatenate(char string1[ ],char string2[ ],char string[ ]);
char s1[100],s2[100],s[100];
printf("input string1:");
scanf("%s",s1);
printf("input string2:");
scanf("%s",s2);
concatenate(s1,s2,s) ;
printf("\nThe new string is %s\n",s);
return 0;
}

void concatenate(char string1[ ],char string2[ ],char string[ ]){
int i,j;
for(i=0;string1[i]!='\0';i++)
string[i]= string1[i];
for(j=0;string2[j]!='\0';j++)
string[i+j]= string2[j];
string[i+j]='\0';
}

7、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
int main( ){
void cpy(char [],char []);
char str[80],c[80];
printf("input string:");
gets(str);
cpy(str,c);
printf("The vowel letters are: %s\n",c);
return 0;
}

void cpy(char s[ ],char c[ ]){
int i,j;
for (i=0,j=0;s[i]!='\0';i++)
if (s[i]=='a' || s[i]=='A' || s[i]=='e' || s[i]=='E' || s[i]=='i' || s[i]=='I' || s[i]=='o' || s[i]=='O' ||s[i]=='u' || s[i]=='U') {
c[j]=s[i];
j++;
}
c[j]='\0';
}

8、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <string.h>
int main( ){
void insert(char [ ]);
char str[80];
printf("input four digits:");
scanf(" %s" ,str);
insert(str) ;
return 0;
}

void insert(char str[ ]){
int i;
for (i= strlen(str);i>0;i--){
str[2*i]= str[i];
str[2*i-1]=' ';
}
printf("output:\n%s\n" ,str);
}

9、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
int letter,digit,space,others;
int main( ){
void count(char [ ]) ;
char text[80];
printf("input string:\n");
gets(text);
printf("string:");
puts(text);
letter= 0;
digit=0;
space= 0;
others= 0;
count(text);
printf("Inetter: %d\ndigit: %d\nspace: % d\nothers: %d\n",letter,digit,space,others);
return 0;
}

void count(char str[]){
int i;
for (i=0;str[i]!='\0';i++ )
if ((str[i]>='a'&& str[i]<= 'z')||(str[i]>='A' && str[i]<='Z'))
letter++;
else if (str[i]>='0' && str[i]<='9')
digit++;
else if (str[i]==32)
space++;
else
others++;
}

10、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <string.h>
int main( ){
int alphabetic(char);
int longest(char []);
int i;
char line[100];
printf("input one line:\n");
gets(line);
printf("The longest word is :");
for (i=longest(line) ; alphabetic(line[i]);i++ )
printf("%c" ,line[i]);
printf("\n");
return 0;
}

int alphabetic(char c){
if((c>='a' &&c<='z')||(c>='A'&&c<='Z'))
return(1);
else
return(0);
}

int longest(char string[]){
int len=0,i,length=0,flag=1,place= 0,point;
for (i=0;i<= strlen(string);i++ )
if (alphabetic( string[i]))
if (flag){
point=i;
flag=0;
}
else
len++;
else{
flag=1;
if (len>= length){
length= len;
place= point;
len= 0;
}
}
return(place);
}

11、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <string.h>
#define N 10
char str[N];
int main( ){
void sort(char []);
int i,flag;
for(flag= 1;flag==1;){
printf("input string:\n");
scanf("%s" ,&str);
if (strlen(str)> N)
printf("string too long ,input again!");
else
flag=0;
}
sort(str);
printf("string sorted:\n");
for (i=0;i<N;i++)
printf("%c",str[i]);
printf("\n");
return 0;
}

void sort(char str[]){
int i,j;
char t;
for(j=1;j<N;j++)
for (i=0;(i<N-j)&&(str[i]!='\0');i++)
if(str[i]>str[i+1]){
t= str[i];
str[i]=str[i+1];
str[i+1]=t;
}
}

12、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <math.h>
int main( ){
float solut(float a,float b, float e,float d) ;
float a,b,c,d;
printf("input a,b,c,d:");
scanf("%f, %f,%f,%f" ,&a,&b,&c,&d);
printf("x= % 10.7f\n",solut(a,b,c,d));
return 0;
}

float solut( float a, float b,float c,float d){
float x= 1,x0,f,f1;
do{
x0= x;
f=((a* x0+b)* x0+c) * x0+d;
f1=(3*a* x0+2* b)* x0+c;
x=x0-f/f1;
} while(fabs(x-x0)>=1e-3);
return(x);
}

13、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
int main() {
int x,n;
float p(int,int);
printf("\ninput n & x:");
scanf("%d,%d" ,&n,&x);
printf("n= %d,x= %d\n" ,n,x);
printf("P%d(%d) = %6.2f\n" ,n,x,p(n,x));
return 0;
}

float p(int n,int x){
if(n==0)
return (1);
else if(n==1)
return(x);
else
return(2*n-1)*x* p((n-1),x)- (n-1)*p((n- 2),x)/n;
}

14、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#define N 10
#define M 5
float score[N][M];//全局数组
float a_stu[N],a_cour[M];//全局数组
int r,c;//全局变量
int main( ){
int i,j;
float h;
float s_var(void);//函数声明
float highest( );//函数声明
void input_stu( void) ;//函数声明
void aver_stu( void) ;//函数声明
void aver_cour( void) ;//函数声明
input_stu( );//函数调用,输人10个学生成绩
aver_stu( );//函数调用,计算10个学生平均成绩
aver_cour( );
printf("\n. NO. courl cour2 cour3 cour4 cour5 aver\n");
for(i=0;i<N;i++){
printf("\n NO %2d ",i+1);//输出一个学生号
for(j=0;j<M;j++ )
printf("%8.2f" ,score[i][i]) ;//输出一个学生各门课的成绩
printf("%8.2f\n",a_stu[i]);//输出一个学生的平均成绩
}
printf("\naverage:");//输出5门课平均成绩
for(j=0;j<M;j++)
printf("%8.2f",a_cour[j]);
printf("\n");
h=highest( );//调用函数,求最高分和它属于哪个学生、哪门课.
printf("highest:%7.2f NO. %2d course %2d\n" ,h,r,c);//输出最高分和学生号、课程号
printf("variance %8.2f\n",s_var( ));//调用函数,计算并输出方差
return 0;
}

void input_stu( void){//输人10个学生成绩的函数
int i,j;
for (i=0;i<N;i++ ){
printf("\ninput score of student%2d:\n",i+1); //学生号从 1开始
for(j=0;j<M;j++)
scanf("%f" , &score[i][j]);
}
}

void aver_stu( void){//计算10个学生平均成绩的函数
int i,j;
float s;
for(i=0;i<N;i++){
for(j=0,s=0;j<M;j++ )
s+= score[i][j];
a_stu[i]=s/5.0;
}
}

void aver_cour( void){//计算5门课平均成绩的函数
int i,j;
float s;
for (j= 0;j<M;j++){
s=0;
for (i=0;i<N;i++)
s+= score[i][j];
a_cour[i]= s/ (float)N;
}
}

float highest( ){//求最高分和它属于哪个学生、哪门课的函数
float high;
int i,j;
high= score[0][0];
for (i=0;i<N;i++ )
for (j=0;j<M;j++ )
if (score[i][j]> high){
high= score[i][j];
r=i+1;//数组行号i从0开始,学生号τ从1开始,故r=i+1
c=j+1;//数组列号j从0开始,课程号c从1开始,故c=j+1
}
return(high) ;
}

float s_var( void){//求方差的函数
int i;
float sumx , sumxn;
sumx=0.0;
sumxn=0.0;
for(i=0;i<N;i++ ){
sumx+=a_stu[i]*a_stu[i];
sumxn+=a_stu[i];
}
return(sumx/N- (sumxn/N) * (sumxn/N));
}

15、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <string.h>
#define N 10
int main( ){
void input(int [ ],char name[ ][8]);
void sort(int [ ],char name[ ][8]) ;
void search(int ,int [ ],char name[ ][8]);
int num[N], number,flag= 1,c;
char name[N][8];
input( num , name) ;
sort( num, name) ;
while (flag== 1){
printf("\ninput number to look for:");
scanf("%d" , &number);
search( number , num , name) ;
printf("continue ot not(Y/N)?");
getchar( );
c= getchar( );
if (c=='N'||c=='n')
flag=0;
}
return 0;
}

void input(int num[ ],char name[N][8]){//输人数据的函数
int i;
for (i=0;i<N;i++){
printf("input NO.: ");
scanf("%d" , &num[i]);
printf("input name: ");
getchar( );
gets(name[i]);
}
}

void sort(int num[ ],char name[N][8]){//排序的函数
int i,j, min, temp1;
char temp2[8];
for (i=0;i<N-1;i++){
min= i;
for (j=i;j<N;j++)
if (num[ min]> num[j]) min=j;
temp1= num[i];
strcpy( temp2 ,name[i]);
num[i]= num[ min];
strcpy(name[i] ,name[ min]);
num[ min]= temp1;
strcpy( name[ min], temp2) ;
}
printf("\n result:\n");
for (i=0;i<N;i++)
printf("\n % 5d% 10s" , num[i] ,name[i]) ;
}

void search(int n, int num[ ],char name[N][8]){//折半查找的函数
int top, bott, mid,loca , sign;
top= 0;
bott=N- 1;
loca= 0;
sign=1;
if ((n< num[0])||(n> num[N- 1]))
loca=-1;
while((sign==1) && (top<= bott)){
mid= (bott+ top)/2;
if (n== num[ mid]){
loca= mid;
printf("NO. %d,his name is %s. \n" ,n,name[loca]) ;
sign=-1;
}
else if (n< num[ mid])
bott= mid- 1;
else
top= mid+1;
}
if (sign==1 || loca==-1)
printf(" %d not been found. \n" ,n);
}

16、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#define MAX 1000
int main( ){
int htoi(char s[ ]);
int c,i,flag,flag1;
char t[MAX];
i=0;
flag= 0;
flag1=1;
printf("input a HEX number:");
while((c= getchar( ))!='\0' && i<MAX&& flag1){
if (c>='0' && c<='9'||c>='a' &&c<='f'||c>='A' && c<='F'){
flag=1;
t[i++]=c;
}
else if (flag){
t[i]='\0';
printf("decimal number %d\n" ,htoi(t));
printf("continue or not?" );
c= getchar( );
if (c=='N'||c=='n')
flag1=0;
else{
flag=0;
i=0;
printf("\ninput a HEX number:");
}
}
}
return 0;
}

int htoi(char s[ ]){
int i,n;
n=0;
for (i=0;s[i]!= '\0';i++){
if (s[i]>='0'&& s[i]<='9')
n=n* 16+s[i]-'0';
if (s[i]>='a' && s[i]<='f')
n=n* 16+s[i]-'a'+ 10;
if (s[i]>='A' && s[i]<='F')
n=n*16+s[i]-'A'+10;
}
return(n);
}

17、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
int main( ){
void convert(int n) ;
int number;
printf("input an integer: ");
scanf("%d" , & number);
printf("output: ");
if (number<0){
putchar('-');putchar(' ');
number =-number ;
}
convert(number);
printf("\n");
return 0;
}

void convert(int n){
int i;
if ((i=n/10)!=0)
convert(i) ;
putchar(n%10+'0');
putchar(32);
}

18、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
int main( ){
int sum_day(int month,int day) ;
int leap(int year) ;
int year , month,day,days;
printf("input date( year , month,day):");
scanf("%d, %d, %d" , &year, & month, &day);
printf(" %d/%d/%d ", year ,month,day);
days= sum_day( month,day) ;//调用函数sum_day
if(leap( year) && month>= 3)//调用函数leap
days= days+1;
printf("is the %dth day in this year. \n" ,days) ;
return 0;
}

int sum_day(int month,int day){//函数sum_day:计算日期
int day_tab[13]= {0,31 ,28,31 ,30,31 ,30,31,31,30,31 ,30,31};
int i;
for (i=1;i< month;i++)
day+= day_tab[i];//累加所在月之前天数
return(day);
}

int leap(int year){//函数leap:判断是否为闰年
int leap;
leap=year%4==0&&year%100!=0||year%400==0;
return(leap);
}

第八章 善于利用指针

1、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main( ){
void swap(int *p1,int *p2);
int n1,n2,n3;
int *p1, *p2, *p3;
printf("input three integer 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);
return 0;
}

void swap(int *p1,int *p2){
int p;
p=*p1;*p1=*p2;*p2=p;
}

2、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h> 
#include <string.h>
int main( ){
void swap(char*,char*);
char str1[20],str2[31],str3[20];
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);
return 0;
}
void swap(char *p1,char *p2){
char p[20];
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
}

3、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
int main( ){
void input(int *);
void max_min_value(int *);
void output(int *);
int number[10];
input(number); //调用输人10个数的函数
max_min_value(number) ; //调用交换函数
output(number) ; //调用输出函数
return 0;
}

void input(int* number)//输人10个数的函数
{
int i;
printf("input 10 numbers:");
for(i=0;i<10;i++)
scanf("%d",&number[i]);
}

void max_min_value(int *number)//交换函数
{
int *max, *min, *p,temp;
max=min=number;//开始时使max和min都指向第1个数
for(p=number+1;p<number+10;p++)
if(*p>*max)max=p;//若p指向的数大于max指向的数,就使max指向p指向的大数
else if(*p<*min)min=p;//若p指向的数小于min指向的数,就使min指向p指向的小数
temp=number[0];number[0]= *min; *min= temp; //将最小数与第 1个数number[0]交换
if(max==number) max= min;//如果max和number相等,表示第1个数是最大数,则使max指向当前的最大数
temp=number[9];number[9]= *max; *max= temp; //将最大数与最后一个数交换
}

void output(int *number)//输出函数
{
int *p;
printf("Now ,they are:");
for(p=number;p<number+10;p++)
printf("%d ", *p);
printf("\n");
}

4、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
int main( ){
void move(int [20],int ,int);
int number[20],n,m,i;
printf("how many numbers?");
scanf("%d",&n);
printf("input %d numbers:\n",n);
for(i=0;i<n;i++)
scanf("%d",&number[i]);
printf("how many place you wantmove?");
scanf("%d",&m);
move(number,n,m) ;
printf("Now,they are:\n");
for(i=0;i<n;i++)
printf("%d ", number[i]);
printf("\n");
return 0;
}

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);
}

5、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
int main( ){
int i,k,m,n,num[50],*p;
printf("\ninput number of person: n=");
scanf("%d",&n);
p=num;
for(i=0;i<n;i++)
*(p+i)=i+1; //以1至n为序给每个人编号
i=0; //i为每次循环时计数变量
k=0; //k为按1,2,3报数时的计数变量
m=0; //m为退出人数
while (m<n-1) //当退出人数比n-1少时(即未退出人数大于1时)执行循环体
{
if(*(p+i)!=0) k++ ;
if (k==3){
*(p+i)=0; //对退出的人的编号置为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);
return 0;
}

6、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main( ){
int length(char *p);
int len;
char str[20];
printf("input string: ");
scanf("%s" ,str);
len= length(str);
printf("The length of string is %d. \n",len);
return 0;
}

int length(char *p){
int n;
n=0;
while(*p!='\0'){
n++;
p++;
}
return(n);
}

7、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <string.h>
int main( ){
void copystr(char *,char *,int);
int m;
char str1[20],str2[20];
printf("input string:");
gets(str1);
printf("which character that begin to copy?" );
scanf("%d", &m);
if(strlen(str1)< m)
printf("input error!");
else{
copystr( str1,str2,m) ;
printf("result: %s\n" ,str2);
}
return 0;
}

void copystr(char *p1,char *p2,int m) //字符串部分复制函数
{
int n;
n=0;
while (n<m-1){
n++;
p1++;
}
while (*p1!='\0'){
*p2= *p1;
p1++;
p2++;
}
*p2='\0';
}

第九章 用户自己建立数据类型


10、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)

struct student{
long num;
int score;
struct student * next;
};

struct student lista, listb;
int n,sum= 0;

int main()
{
struct student * creat( void); //函数声明
struct student * insert(struct student * ,struct student* ); //函数声明
void print(struct student * ); //函数声明
struct student *ahead, *bhead, *abh;
printf("input list a:\n");
ahead= creat(); //调用creat函数建立表A,返回头地址
sum= sum+n;
printf("input list b;\n");
bhead = creat(); //调用creat丽数建立表B,返回头地址
sum= sum+n;
abh = insert( ahead, bhead); //调用insert函数,将两表合并
print(abh); //输出合并后的链表
return 0;
}

//建立链表的函数
struct student *creat(void){
struct student *p1, *p2, *head;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("input number & scores of student;\n");
printf("if number is O,stop inputing.\n");
scanf(" %ld,%d",&p1->num,&p1->score) ;
head= NULL;
while(p1->num!= 0){
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%d",&p1->num,&p1->score);
}
p2->next= NULL;
return(head);
}


//定义insert函数,用来合并两个链表
struct student * insert(struct student *ah,struct student *bh)
{
if(!ah)
return bh;
else if(!bh)
return ah;

struct student *pa1,*pa2,*pb1,*pb2;
pa2=pa1=ah;
pb2=pb1=bh;
do{
//找到下比bh大的ah节点,也有可能因为遍历ah都没有找到而结束
while((pb1->num > pa1->num) && (pa1->next != NULL)){
pa2= pa1;
pa1= pa1->next;
}

//如果此时bh节点小于等于ah节点,则将其插入ah。
//(理论上来说id不应该重复,不会出现相等的情况)
if(pb1->num <= pa1->num){
if(ah==pa1)//如果此时pa1是第一个节点
ah=pb1;//则将ah指向pb1
else
pa2->next=pb1;//否则将pa2(pa1的上一个节点)的下一个节点指向pb1
pb1=pb1->next;//pb1向后移动一位
pb2->next=pa1;//此时pb2仍然指向之前pb1的位置,将其next指向pa1
pa2=pb2;//pa2移动到pb2的位置,同时也是pa1的上一个节点
pb2=pb1;//pb2移动到pb1的位置
}

//如果pa1还没有移动到ah的最后一个节点
//或者pa1移动到ah的最后一个节点但bh中还有比pa1更小的节点则继续循环
}while(pb1!=NULL&& (pa1->next!=NULL || (pa1->next==NULL && pb1->val<pa1->val)));
if(pb1!=NULL)//如果pb1及之后还有节点没插入完则直接将其连接到ah尾部
pa1->next=pb1;
return(ah);
}

//输出函数
void print(struct student *head){
struct student *p;
printf("There are %d records: \n" ,sum) ;
p=head;
if(p!=NULL)
do{
printf("%ld %d\n",p->num,p->score) ;
p=p->next;
}while(p!=NULL);
}

-------- 本文结束 感谢阅读 --------
相关文章
  • 算法常用模板:并查集
  • 寻找无向图最小着色方案
  • C语言基础编程练习题
  • OpenCV3使用中遇到的一些问题
  • Qt使用中遇到的一些问题
觉得文章写的不错的话,请我喝瓶怡宝吧!😀
SiriYang 微信支付

微信支付

SiriYang 支付宝

支付宝

  • 本文标题: 《C语言程序设计(第五版)谭浩强》课后习题答案源码
  • 本文作者: SiriYang
  • 创建时间: 2020年03月17日 - 20时03分
  • 修改时间: 2021年10月29日 - 18时10分
  • 本文链接: https://blog.siriyang.cn/posts/20200317205721id.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
C/C++ 算法题
Pythonista中文文档
C/C++数据输入练习
  • 文章目录
  • 站点概览
SiriYang

SiriYang

努力搬砖攒钱买镜头的摄影迷
320 日志
33 分类
88 标签
RSS
GitHub E-Mail
Creative Commons
Links
  • 友情链接
  • 作品商铺

  1. 第三章 顺序程序设计
  2. 第四章 选择结构程序设计
  3. 第五章 循环结构程序设计
  4. 第六章 利用数组处理批量数据
  5. 第七章 用函数实现模块化程序设计
  6. 第八章 善于利用指针
  7. 第九章 用户自己建立数据类型
蜀ICP备19008337号 © 2019 – 2025 SiriYang | 1.7m | 25:41
0%