《C程序设计(第三版)》习题(编程题)解答

更新时间:2023-12-16 21:26:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《C程序设计(第三版)》习题(编程题)解答

1.5

#include void main ( )

{ printf (\ printf(\ Very Good!\\n\\n\

printf (\} 1.6

include void main()

{int a,b,c,max;

printf(\ scanf(\ max=a; if (max

max=c;

printf(\}

3.7

#include

void 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(\}

4.7

#include void main() {int a,b;

float x,y,z; char c1,c2;

scanf(\,&z);

1

printf(\,z); } 4.8

#include void main () {float h,r,l,s,sq,vq,vz;

float pi=3.141526;

printf(\请输入圆半径r,圆柱高h∶\

scanf(\ //要求输入圆半径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\ printf(\圆面积为: s=%6.2f\\n\ printf(\圆球表面积为: sq=%6.2f\\n\ printf(\圆球体积为: v=%6.2f\\n\ printf(\圆柱体积为: vz=%6.2f\\n\ }

4.9

#include void main()

{float c,f;

printf(\请输入一个华氏温度:\

scanf(\

c=(5.0/9.0)*(f-32); /*注意5和9要用实型表示,否则5/9值为0*/ printf(\摄氏温度为:%5.2f\\n\}

4.10

#include void main() {

char c1,c2;

printf(\请输入两个字符c1,c2:\c1=getchar();

c2=getchar();

printf(\用putchar语句输出结果为:\putchar(c1); putchar(c2); printf(\

2

printf(\用printf语句输出结果为:\printf(\}

1) #include void main()

{

int c1,c2; /*整型定义*/

printf(\请输入两个整数c1,c2:\ scanf(\ printf(\按字符输入结果:\\n\

printf(\

printf(\按ASCII码输入出结果为:\\n\ printf(\}

2) #include void main()

{

char c1,c2; /*定义字符型变量*/ int i1,i2; /*定义整型变量*/ printf(\请输入两个整数c1,c2:\ scanf(\

i1=c1; /*赋值给整型变量*/ i2=c2;

printf(\按字符输入结果:\\n\ printf(\

printf(\按整数输入出结果:\\n\ printf(\}

3) #include void main()

{

char c1,c2; /*定义为字符型*/ int i1,i2; /*定义为整型*/ printf(\请输入两个整数i1,i2:\

scanf(\

c1=i1; /*将整数赋值给字符变量*/ c2=i2;

printf(\按字符输入结果:\\n\ printf(\

printf(\按整数输入出结果:\\n\ printf(\}

3

5.4

方法一: #include void main() {

int a,b,c;

printf(\请输入三个整数:\ scanf(\ if (a

printf(\ else

printf(\ else if (a

printf(\ else

printf(\}

方法二: #include void main()

{ int a,b,c,temp,max;

printf(\请输入三个整数:\

scanf(\

temp=(a>b)?a:b; /*将a和b中的大者存入temp中*/ max=(temp>c)?temp:c; /*将a和b中的大者与c比较,取最大者*/ printf(\三个整数的最大数是%d\\n\} 5.5

#include void main() { int x,y;

printf(\输入x:\

scanf(\

if(x<1) /* x<1 */ { y=x;

printf(\ y=x=%d\\n\ }

else if(x<10) /* 1=

{ y=2*x-1;

printf(\ y=2*x-1=%d\\n\}

else /* x>=10 */

4

} 5.6

{ y=3*x-11;

printf(\ y=3*x-11=%d\\n\}

#include void main() { float score;

char grade;

printf(\请输入学生成绩:\ scanf(\

while (score>100||score<0) {printf(\输入有误,请重输\

scanf(\}

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; case 5: case 4: case 3: case 2: case 1:

case 0: grade='E'; }

printf(\成绩是 %5.1f,相应的等级是%c.\\n \}

5.7

#include #include void main() {

long int num;

int indiv,ten,hundred,thousand,ten_thousand,place; /*分别代表个位,十位,百位,千位,万位和位数*/

printf(\请输入一个整数(0-99999):\ scanf(\ if (num>9999) place=5;

5

else if (num>999) place=4; else if (num>99) place=3; else if (num>9) place=2;

else place=1;

printf(\位数:%d\\n\ printf(\每位数字为:\ ten_thousand=num/10000;

thousand=(int)(num-ten_thousand*10000)/1000;

hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;

ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;

indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10); switch(place)

{case 5:printf(\ printf(\反序数字为:\

printf(\ break;

case 4:printf(\ printf(\反序数字为:\ printf(\ break;

case 3:printf(\ printf(\反序数字为:\

printf(\ break;

case 2:printf(\ printf(\反序数字为:\

printf(\ break;

case 1:printf(\ printf(\反序数字为:\ printf(\ break; } } 5.8

(1) #include void main()

{

long i;

double bonus,bon1,bon2,bon4,bon6,bon10;

6

bon1=100000*0.1;

bon2=bon1+100000*0.075; bon4=bon2+100000*0.05; bon6=bon4+100000*0.03; bon10=bon6+400000*0.015; printf(\请输入利润i:\

scanf(\ if (i<=100000) bonus=i*0.1; else if (i<=200000)

bonus=bon1+(i-100000)*0.075; else if (i<=400000)

bonus=bon2+(i-200000)*0.05; else if (i<=600000)

bonus=bon4+(i-400000)*0.03; else if (i<=1000000)

bonus=bon6+(i-600000)*0.015; else

bonus=bon10+(i-1000000)*0.01; printf(\奖金是: .2f\\n\ }

(2) #include void main() {

long 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(\

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:

7

case 7:

case 8:

case 9: bonus=bon6+(i-600000)*0.015;break; case 10: bonus=bon10+(i-1000000)*0.01; }

printf(\奖金是 .2f\\n\ } 5.9

#include

void main() {int t,a,b,c,d;

printf(\请输入四个数:\

scanf(\ printf(\ 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 \\n\ ,a,b,c,d); } 5.10

#include void 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(\

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; /*判断该点是否在塔外*/

8

printf(\该点高度为 %d\\n\ } 6.1

#include void main() {

int p,r,n,m,temp;

printf(\请输入两个正整数n,m:\ scanf(\ if (n

p=n*m; while(m!=0) { r=n%m; n=m;

m=r; }

printf(\它们的最大公约数为:%d\\n\ printf(\它们的最小公约数为:%d\\n\ }

6.2

#include void 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++;

9

}

printf(\字母数:%d\\n空格数:%d\\n数字数:%d\\n其它字符数:%d\\n\ } 6.3

#include void main()

{

int a,n,i=1,sn=0,tn=0; printf(\ scanf(\ while (i<=n)

{

tn=tn+a; /*赋值后的tn为i个 a组成数的值*/ sn=sn+tn; /*赋值后的sn为多项式前i项之和*/ a=a*10;

++i; }

printf(\ }

6.4

#include void main()

{double s=0,t=1; int n;

for (n=1;n<=20;n++) {

t=t*n; s=s+t; }

printf(\} 6.5

#include void main() {

int n1=100,n2=50,n3=10;

double k,s1=0,s2=0,s3=0;

for (k=1;k<=n1;k++) /*计算1到100的和*/ {s1=s1+k;}

for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/

10

{s2=s2+k*k;}

for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/ {s3=s3+1/k;}

printf(\ } 6.6

#include void main() {

int i,j,k,n;

printf(\ for (n=100;n<1000;n++) {

i=n/100; j=n/10-i*10;

k=n;

if (n==i*i*i + j*j*j + k*k*k) printf(\ } printf(\ } 6.7

方法一:

#define M 1000 /*定义寻找范围*/ #include

void main() {

int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;

int i,a,n,s;

for (a=2;a<=M;a++) /* a是2-1000之间的整数,检查它是否完数 */ {n=0; /* n用来累计a的因子的个数 */

s=a; /* s用来存放尚未求出的因子之和,开始时等于a */ for (i=1;i

{n++; /* n加1,表示新找到一个因子 */

s=s-i; /* s减去已找到的因子,s的新值是尚未求出的因子之和 */ switch(n) /* 将找到的因子赋给k1...k9,或k10 */ {case 1:

k1=i; break; /* 找出的笫1个因子赋给k1 */ case 2:

k2=i; break; /* 找出的笫2个因子赋给k2 */ case 3:

11

k3=i; break; /* 找出的笫3个因子赋给k3 */ case 4:

k4=i; break; /* 找出的笫4个因子赋给k4 */ case 5:

k5=i; break; /* 找出的笫5个因子赋给k5 */ case 6:

k6=i; break; /* 找出的笫6个因子赋给k6 */ case 7:

k7=i; break; /* 找出的笫7个因子赋给k7 */ case 8:

k8=i; break; /* 找出的笫8个因子赋给k8 */ case 9:

k9=i; break; /*找出的笫9个因子赋给k9 */ case 10:

k10=i; break; /* 找出的笫10个因子赋给k10 */ }

}

if (s==0) {

printf(\

if (n>1) printf(\ /* n>1表示a至少有2个因子 */ 因子 因子

if (n>2) printf(\ /* n>2表示至少有3个因子,故应再输出一个*/

if (n>3) printf(\ /* n>3表示至少有4个因子,故应再输出一个*/

if (n>4) printf(\ /* 以下类似 */ if (n>5) printf(\ if (n>6) printf(\ if (n>7) printf(\ if (n>8) printf(\ if (n>9) printf(\ printf(\ } } }

方法二: #include void main() {int m,s,i;

for (m=2;m<1000;m++) {s=0;

for (i=1;i

if ((m%i)==0) s=s+i; if(s==m)

12

{printf(\ for (i=1;i

#include void 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(\ } 6.9

#include void 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\ printf(\第10次反弹%f米\\n\ } 6.10

#include void main() {

int day,x1,x2;

13

day=9;

x2=1;

while(day>0)

{x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1; day--; }

printf(\ }

6.11

#include #include void main() {

float a,x0,x1;

printf(\ scanf(\ x0=a/2; x1=(x0+a/x0)/2; do

{x0=x1;

x1=(x0+a/x0)/2;

}while(fabs(x0-x1)>=1e-5);

printf(\ is %8.5f\\n\ }

6.12

#include #include void 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(\ }

6.13

#include

14

#include void main()

{float x0,x1,x2,fx0,fx1,fx2; do

{printf(\ scanf(\ 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(\ } 6.14

#include void 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(\ }

for (i=0;i<=2;i++) {for (j=0;j<=i;j++) printf(\

for (k=0;k<=4-2*i;k++) printf(\ printf(\ } }

15

maxj=j; /* 将最大数所在的列号存放在maxj中 */

}

flag=1; /* 先假设是鞍点,以flag为1代表 */ for (k=0;ka[k][maxj]) /* 将最大数和其同列元素相比 */

{flag=0; /* 如果max不是同列最小,表示不是鞍点令flag1为0 */ continue;}

if(flag) /* 如果flag1为1表示是鞍点 */ {printf(\ /* 输出鞍点的值和所在行列号 */

break;

} }

if(!flag) /* 如果flag为0表示鞍点不存在 */ printf(\ }

7.9

#include #define N 15 void main()

{ int i,number,top,bott,mid,loca,a[N],flag=1,sign; char c;

printf(\ scanf(\ i=1;

while(i

{scanf(\ if (a[i]>=a[i-1]) i++; else

printf(\ }

printf(\

for (i=0;i

{printf(\ scanf(\

sign=0;

top=0; //top是查找区间的起始位置

bott=N-1; //bott是查找区间的最末位置

if ((numbera[N-1])) //要查的数不在查找区间内 loca=-1; // 表示找不到

21

while ((!sign) && (top<=bott))

{mid=(bott+top)/2; if (number==a[mid]) {loca=mid;

printf(\

sign=1;

}

else if (number

if(!sign||loca==-1)

printf(\ printf(\ scanf(\ if (c=='N'||c=='n') flag=0; } }

7.10

#include void main()

{int i,j,upp,low,dig,spa,oth; char text[3][80];

upp=low=dig=spa=oth=0;

for (i=0;i<3;i++)

{ printf(\ gets(text[i]);

for (j=0;j<80 && text[i][j]!='\\0';j++) {if (text[i][j]>='A'&& text[i][j]<='Z') upp++;

else if (text[i][j]>='a' && text[i][j]<='z') low++;

else if (text[i][j]>='0' && text[i][j]<='9') dig++;

else if (text[i][j]==' ') spa++; else

oth++;

} }

printf(\

22

printf(\ printf(\ : %d\\n\ printf(\ : %d\\n\ printf(\ : %d\\n\} 7.11

#include void main()

{ char a[5]={'*','*','*','*','*'}; int i,j,k;

char space=' '; for (i=0;i<5;i++) { printf(\ printf(\ \ for (j=1;j<=i;j++) printf(\ for (k=0;k<5;k++) printf(\ } printf(\} 7.12

方法一: #include void main()

{ int j,n;

char ch[80],tran[80];

printf(\ gets(ch);

printf(\ :%s\ j=0;

while (ch[j]!='\\0')

{ if ((ch[j]>='A') && (ch[j]<='Z')) tran[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z')) tran[j]=219-ch[j]; else

tran[j]=ch[j]; j++;

} n=j;

printf(\

23

for (j=0;j

方法二: #include void main() {int j,n;

char ch[80];

printf(\ gets(ch);

printf(\ j=0;

while (ch[j]!='\\0')

{ if ((ch[j]>='A') && (ch[j]<='Z')) ch[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z')) ch[j]=219-ch[j]; else

ch[j]=ch[j]; j++; } n=j;

printf(\

for (j=0;j

putchar(ch[j]); printf(\ } 7.13

#include void main() { char s1[80],s2[40]; int i=0,j=0;

printf(\ scanf(\ printf(\ scanf(\ while (s1[i]!='\\0') i++;

while(s2[j]!='\\0')

s1[i++]=s2[j++]; s1[i]='\\0';

printf(\

24

}

7.14

#include void main() { int i,resu;

char s1[100],s2[100]; printf(\ gets(s1);

printf(\ gets(s2); i=0;

while ((s1[i]==s2[i]) && (s1[i]!='\\0'))i++; if (s1[i]=='\\0' && s2[i]=='\\0') resu=0; else

resu=s1[i]-s2[i];

printf(\ } 7.15

#include #include void main()

{ char s1[80],s2[80]; int i;

printf(\ scanf(\

for (i=0;i<=strlen(s2);i++) s1[i]=s2[i]; printf(\ } 8.1

#include void main() {int hcf(int,int); int lcd(int,int,int); int u,v,h,l;

scanf(\ h=hcf(u,v);

printf(\ l=lcd(u,v,h);

printf(\

25

}

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

8.2

#include #include float x1,x2,disc,p,q;

void main()

{void greater_than_zero(float,float); void equal_to_zero(float,float); void smaller_than_zero(float,float); float a,b,c;

printf(\

scanf(\

printf(\ disc=b*b-4*a*c; printf(\ if (disc>0)

{

greater_than_zero(a,b);

printf(\ }

else if (disc==0)

{equal_to_zero(a,b);

printf(\ } else

{smaller_than_zero(a,b);

printf(\ }

26

}

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

#include void main()

{int prime(int); int n;

printf(\ scanf(\

if (prime(n))

printf(\ else

printf(\ }

int prime(int n)

{int flag=1,i;

for (i=2;i

#include #define N 3 int array[N][N]; void main()

27

{ void convert(int array[][3]); int i,j;

printf(\ for (i=0;i

scanf(\ printf(\ for (i=0;i

convert(array);

printf(\ for (i=0;i

printf(\ printf(\ } }

void convert(int array[][3]) {int i,j,t;

for (i=0;i

array[i][j]=array[j][i]; array[j][i]=t; } } 8.5

#include #include void main()

{void inverse(char str[]); char str[100]; printf(\ scanf(\ inverse(str);

printf(\}

void inverse(char str[])

28

{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; } } 8.6

#include void main()

{void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100]; printf(\ scanf(\ printf(\ scanf(\

concatenate(s1,s2,s);

printf(\ }

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'; } 8.7

#include void main()

{void cpy(char [],char []); char str[80],c[80]; printf(\ gets(str); cpy(str,c);

printf(\ }

void cpy(char s[],char c[]) { int i,j;

29

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.8

#include #include void main() {char str[80];

void insert(char []);

printf(\ scanf(\ insert(str); }

void insert(char str[]) {int i;

for (i=strlen(str);i>0;i--) {str[2*i]=str[i]; str[2*i-1]=' ';

}

printf(\} 8.9

#include

int letter,digit,space,others; void main()

{void count(char []); char text[80];

printf(\ gets(text); printf(\ puts(text); letter=0; digit=0; space=0; others=0; count(text);

30

printf(\}

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

8.10

#include #include void main() {int alphabetic(char); int longest(char []); int i;

char line[100];

printf(\ gets(line);

printf(\

for (i=longest(line);alphabetic(line[i]);i++) printf(\ printf(\}

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)

31

{point=i; flag=0; } else

len++; else {flag=1; if (len>=length) {length=len; place=point; len=0;

}

}

return(place); }

8.11

#include #include #define N 10 char str[N];

void main()

{void sort(char []); int i,flag;

for (flag=1;flag==1;) {printf(\ scanf(\

if (strlen(str)>N)

printf(\ else flag=0; }

sort(str);

printf(\ for (i=0;i

void sort(char str[]) {int i,j;

char t;

for(j=1;j

for (i=0;(i

32

if(str[i]>str[i+1]) {t=str[i]; str[i]=str[i+1]; str[i+1]=t; } }

8.12

#include #include

void main()

{float solut(float a,float b,float c,float d); float a,b,c,d;

printf(\

scanf(\ printf(\}

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

8.14

#include #define N 10 #define M 5

float score[N][M];

float a_stu[N],a_cour[M]; int r,c;

void main() { int i,j; float h;

float s_var(void); float highest();

void input_stu(void);

33

void aver_stu(void);

void aver_cour(void); input_stu(); aver_stu(); aver_cour();

printf(\ NO. cour1 cour2 cour3 cour4 cour5 aver\\n\ for(i=0;i

{printf(\ for(j=0;j

printf(\ printf(\ }

printf(\ for (j=0;j

printf(\ printf(\

h=highest();

printf(\ NO. - course -\\n\ printf(\}

void input_stu(void) {int i,j;

for (i=0;i

{printf(\ for (j=0;j

scanf(\ } }

void aver_stu(void) {int i,j; float s;

for (i=0;i

{for (j=0,s=0;j

void aver_cour(void) {int i,j; float s;

34

for (j=0;j

for (i=0;i

float highest() {float high; int i,j;

high=score[0][0]; for (i=0;ihigh) {high=score[i][j]; r=i+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

{sumx+=a_stu[i]*a_stu[i]; sumxn+=a_stu[i];

}

return(sumx/N-(sumxn/N)*(sumxn/N)); }

8.15

#include #include #define N 10 void 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];

35

input(num,name);

sort(num,name); while (flag==1)

{printf(\ scanf(\

search(number,num,name); printf(\ getchar(); c=getchar(); if (c=='N'||c=='n')

flag=0; } }

void input(int num[],char name[N][8]) {int i;

for (i=0;i

{printf(\ scanf(\ printf(\ getchar(); gets(name[i]); } }

void sort(int num[],char name[N][8]) { int i,j,min,templ; char temp2[8]; for (i=0;i

for (j=i;j

if (num[min]>num[j]) min=j; templ=num[i];

strcpy(temp2,name[i]); num[i]=num[min];

strcpy (name[i],name[min]); num[min]=templ; strcpy(name[min],temp2); }

printf(\ for (i=0;i

printf(\ }

36

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 ((nnum[N-1])) loca=-1;

while((sign==1) && (top<=bott))

{mid=(bott+top)/2; if (n==num[mid]) {loca=mid;

printf(\ sign=-1; }

else if (n

top=mid+1; }

if (sign==1 || loca==-1)

printf(\ } 8.16

#include #define MAX 1000 void main()

{ int htoi(char s[]); int c,i,flag,flag1; char t[MAX]; i=0; flag=0; flag1=1;

printf(\

while((c=getchar())!='\\0' && i

{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(\ number %d\\n\ printf(\

37

c=getchar(); if (c=='N'||c=='n') flag1=0; else

{flag=0;

i=0;

printf(\

} } } }

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

#include void main()

{ void convert(int n); int number;

printf(\ scanf(\ printf(\

if (number<0)

{putchar('-');putchar(' '); /* 先输出一个‘-’号和空格 */ number=-number; }

convert(number); printf(\}

void convert(int n) { int i;

38

if ((i=n/10)!=0) convert(i);

putchar(n+'0'); putchar(32); } 8.18

#include void main()

{int sum_day(int month,int day); int leap(int year);

int year,month,day,days;

printf(\

scanf(\

printf(\

days=sum_day(month,day); /* 调用函数sum_day */ if(leap(year)&&month>=3) /* 调用函数leap */ days=days+1;

printf(\}

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

day+=day_tab[i]; /* 累加所在月之前天数 */ return(day);

} /* 函数leap:判断是否为闰年 */

int leap(int year)

{int leap;

leap=year%4==0&&year0!=0||year@0==0; return(leap); } 10.1

#include

void main()

{ void swap(int *p1,int *p2); int n1,n2,n3; int *p1,*p2,*p3;

printf(\ scanf(\ p1=&n1;

39

p2=&n2;

p3=&n3;

if(n1>n2) swap(p1,p2); if(n1>n3) swap(p1,p3); if(n2>n3) swap(p2,p3);

printf(\ }

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

p=*p1; *p1=*p2; *p2=p; } 10.2

#include #include

void main()

{void swap(char *,char *);

char str1[20],str2[20],str3[20]; printf(\ 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(\ printf(\ }

void swap(char *p1,char *p2) {char p[20];

strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); } 10.3

方法一: #include void main() { void input(int *);

void max_min_value(int *);

void output(int *); int number[10];

input(number);

40

max_min_value(number); output(number); }

void input(int *number) {int i;

printf(\ for (i=0;i<10;i++)

scanf(\ }

void max_min_value(int *number) { int *max,*min,*p,temp; max=min=number;

for (p=number+1;p*max) max=p; else if (*p<*min) min=p;

temp=number[0];number[0]=*min;*min=temp; if(max==number) max=min;

temp=number[9];number[9]=*max;*max=temp; }

void output(int *number) {int *p;

printf(\ \ for (p=number;p

方法二: #include void main()

{ void input(int *);

void max_min_value(int *); void output(int *); int number[10];

input(number); max_min_value(number); output(number); }

void input(int *number) {int i;

printf(\

41

for (i=0;i<10;i++)

scanf(\ }

void max_min_value(int *number) /* 交换函数 */

{ int *max,*min,*p,temp;

max=min=number; /* 开始时使max和min都指向第一个数 */ for (p=number+1;p

if (*p<*min) min=p; /*若p指向的数小于min指向的数,就使min指向p指向的小数 */ temp=number[0];number[0]=*min;*min=temp; /* 将最小数与第一个数number[0]交换 */ for (p=number+1;p

if (*p>*max) max=p; /* 若p指向的数大于max指向的数,就使max指向p指向的大数 */

temp=number[9];number[9]=*max;*max=temp; /* 将最大数与最后一个数交换 */ }

void output(int *number) {int *p;

printf(\ \ for (p=number;p

#include void main()

{void move(int [20],int,int); int number[20],n,m,i;

printf(\ scanf(\

printf(\ for (i=0;i

scanf(\

printf(\ scanf(\ move(number,n,m); printf(\ for (i=0;i

printf(\ \ printf(\

}

void move(int array[20],int n,int m)

42

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

#include void main()

{int i,k,m,n,num[50],*p;

printf(\ scanf(\ p=num;

for (i=0;i

*(p+i)=i+1; i=0; k=0; m=0;

while (m

if (k==3) {*(p+i)=0; k=0; m++;

} i++;

if (i==n) i=0; }

while(*p==0) p++;

printf(\} 10.6

#include void main()

{int length(char *p); int len; char str[20];

printf(\ \scanf(\len=length(str);

43

printf(\}

int length(char *p) {int n; n=0;

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

#include #include

void main()

{void copystr(char *,char *,int); int m;

char str1[20],str2[20]; printf(\

gets(str1);

printf(\ scanf(\ if (strlen(str1)

{copystr(str1,str2,m);

printf(\ } }

void copystr(char *p1,char *p2,int m) {int n; n=0;

while (n

while (*p1!='\\0') {*p2=*p1; p1++; p2++; }

44

*p2='\\0'; } 10.8

#include void main()

{int upper=0,lower=0,digit=0,space=0,other=0,i=0; char *p,s[20];

printf(\ \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 ((*p<='9') && (*p>='0')) ++digit; else

++other; p++; }

printf(\ lower case:%d\

printf(\ space:%d digit:%d other:%d\\n\}

10.9

#include void main()

{void move(int *pointer); int a[3][3],*p,i;

printf(\

for (i=0;i<3;i++)

scanf(\ p=&a[0][0];

move(p);

printf(\ for (i=0;i<3;i++)

printf(\ }

void move(int *pointer)

45

{int i,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; } }

10.10 方法一: #include void main()

{void change(int *p); int a[5][5],*p,i,j;

printf(\ for (i=0;i<5;i++) for (j=0;j<5;j++)

scanf(\ p=&a[0][0]; change(p);

printf(\ for (i=0;i<5;i++) {for (j=0;j<5;j++)

printf(\ printf(\ }

}

void change(int *p) {int i,j,temp;

int *pmax,*pmin; pmax=p; pmin=p;

for (i=0;i<5;i++) for (j=i;j<5;j++)

{if (*pmax<*(p+5*i+j)) pmax=p+5*i+j; if (*pmin>*(p+5*i+j)) pmin=p+5*i+j; }

temp=*(p+12); *(p+12)=*pmax;

*pmax=temp;

temp=*p; *p=*pmin;

46

*pmin=temp;

pmin=p+1;

for (i=0;i<5;i++) 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++)

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

方法二: #include void main()

{void change(int *); int a[5][5],*p,i,j;

printf(\

for (i=0;i<5;i++) //输入矩阵

for (j=0;j<5;j++)

scanf(\

p=&a[0][0]; //使p指向0行0列元素 change(p); //调用函数,实现交换 printf(\

for (i=0;i<5;i++) //输出已交换的矩阵 {for (j=0;j<5;j++) printf(\ printf(\ } }

47

&&

void change(int *p) //交换函数

{int i,j,temp;

int *pmax,*pmin; pmax=p; pmin=p;

for (i=0;i<5;i++) //找最大值和最小值的地址,并赋给 pmax,pmin for (j=i;j<5;j++)

{if (*pmax<*(p+5*i+j)) pmax=p+5*i+j; 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;

//将a[0][1]的地址赋给pmin,从该位置开始找最小的元素 for (i=0;i<5;i++) //找第二最小值的地址赋给 pmin for (j=0;j<5;j++) {if(i==0 && j==0) continue;

if (*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((i==0 && j==0) ||(i==0 && j==4)) continue; if(*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 ((i==0 && j==0) ||(i==0 && j==4)||(i==4 && j==0)) continue; if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;

48

}

temp=*pmin; //将第四最小值与右下角元素互换 *pmin=*(p+24); *(p+24)=temp; }

10.11 方法一:

#include #include void main()

{void sort(char (*p)[6]); int i;

char str[10][6];

char (*p)[6];

printf(\ for (i=0;i<10;i++) scanf(\ p=str; sort(p);

printf(\ for (i=0;i<10;i++)

printf(\ }

void sort(char (*s)[6]) {int i,j;

char temp[6],*t=temp; for (i=0;i<9;i++) for (j=0;j<9-i;j++) if (strcmp(s[j],s[j+1])>0) {strcpy(t,s[j]); strcpy(s[j],s[+j+1]); strcpy(s[j+1],t); } }

方法二: #include #include void main()

{void sort(char s[][6]); int i;

char str[10][6];

printf(\

49

for (i=0;i<10;i++)

scanf(\ sort(str);

printf(\ for (i=0;i<10;i++) printf(\}

void sort(char s[10][6]) {int i,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); } }

方法三:

#include #include void main()

{void sort(char (*p)[6]); int i;

char str[10][6];

char (*p)[6];

printf(\ for (i=0;i<10;i++) scanf(\ p=str;

sort(p);

printf(\ for (i=0;i<10;i++)

printf(\ }

void sort(char (*s)[6]) {int i,j;

char temp[6],*t=temp; for (i=0;i<9;i++) for (j=0;j<9-i;j++)

if (strcmp(s[j],s[j+1])>0)

50

本文来源:https://www.bwwdw.com/article/zod5.html

Top