习 题 7
7.1 已有变量定义和函数调用语句:int a=1,b=–5,c ;c=fun(a,b);fun 函数的作用是计算
两个数之差的绝对值,并将差值返回调用函数,请编写fun 函数。
Fun(int x,int y)
{ }
7.1 求两数之差绝对值
#include
fun(int x,int y)
{ return abs(x-y); }
main()
{ int a=1,b=-5,c;
c=fun(a,b);
printf("|%d-%d|=%d\n",a,b,c);
}
7.2 已有变量定义和函数调用语句:int x=57;isprime(x);函数isprime( );用来判断
一个整型数a 是否为素数,若是素数,函数返回1,否则返回0。请编写isprime 函数。 isprime(int a)
{ }
7.3 已有变量定义和函数调用语句int a ,b ;b=sum(a);函数sum()用以求∑=n
k k 1,和数作为
函数值返回。若a 的值为10,经函数sum 的计算后,b 的值是55。请编写sum 函数。 Sum(int n)
{ }
7.3 求∑k
#include
sum(int n)
{ int k,s=0;
for(k=1;k<=n;k++)
s=s+k;
return s;
}
main()
{ int a,b;
printf("Input a: "); scanf("%d",&a);
b=sum(a);
printf("%c(1,%d)=%d\n",228,a,b);
}
7.4 一函数,输入一行字符,将此字符串中最长的单词输出。
7.4 输出最长的单词,假设只包含字母和空格,单词间以空格分隔,空格个数不限#include
void fun(char a[])
{ int n,i,j,k=0,len[80],p;
char c[80][80],max[80];
n=strlen(a);
for(i=0;i if(a[i]!=32) /*a[i]不为空格,说明单词开始了*/ { j=0; while(a[i]!=32) /*没有遇到下一个空格,说明单词没有结束*/ { c[k][j]=a[i]; j++; i++; } /*将单词放入字符串c[k]*/ c[k][j]='\0'; /*人为加串尾标志*/ k++; /*k为下一个单词存入c的行号*/ } for(i=0;i len[i]=strlen(c[i]); /*求每行字符串的长度*/ p=0; if(len[p] /*找最长字符串的行号*/ printf("The word is : %s\n",c[p]); } main() { char a[80]; printf("Input a line string: "); gets(a); fun(a); } 7.5 一函数,输入一个十六进制数,输出相应的十进制数。 7.5 十六进制数转换成十进制数 #include #include int fun(char a[]) { int i,n=0,m; for(i=0;a[i];i++) { if(isalpha(a[i])) /*如果a[i]是字母,求出相应的十进制数*/ switch(a[i]) { case 'a': case 'A': m=10; break; case 'b': case 'B': m=11; break; case 'c': case 'C': m=12; break; case 'd': case 'D': m=13; break; case 'e': case 'E': m=14; break; case 'f': case 'F': m=15; break; default: printf("Input error!\n"); exit(0); /*如果输入了不合法的字母,输出相应的信息*/ } else if(isdigit(a[i])) /*如果a[i]是数字*/ m=a[i]-'0'; else { printf("Input error!\n"); exit(0); } /*如果输入了不合法的字符,输出相应的信息*/ n=n*16+m; /*将十六进制数转换成十进制数*/ } return n; } main() { char a[80]; int n; printf("input: "); scanf("%s",a); n=fun(a); printf("output: %d\n",n); } 7.6 给出年、月、日,计算该日是该年的第几天。#include int fun(year,month,day) { int n=0,i; n=n+day; /*累加当月的天数*/ for(i=month-1;i>0;i--) /*累加前面月份的天数*/ switch(i) { case 12: n=n+31; case 11: n=n+30; case 10: n=n+31; case 9: n=n+30; case 8: n=n+31; case 7: n=n+31; case 6: n=n+30; case 5: n=n+31; case 4: n=n+30; case 3: n=n+31; case 2: if(year%4==0&&year%100!=0||year%400==0) n=n+28; else n=n+29; /*根据year判断闰年或平年,进行累加*/ case 1: n=n+31; } return n; /*返回天数*/ } main() { int year,month,day; printf("Input year,month,day: "); scanf("%d%d%d",&year,&month,&day); printf("%d.%d.%d is No.%d of a year.\n",year,month,day,fun(year,month,day)); } 7.7 定义一个函数digit(n,k),它回送整数n的从右边开始数第k个数字的值。例如: digit(15327,4)=5 digit(289,5)=0 7.7 回送第k个数字 #include int fun(long n,int k) { int i,m ; for(i=1;i<=k;i++) { m=n%10; /*从右边分离出第i个数字*/ n=n/10; /*为下一次分离作准备*/ } return m; /*返回第k次分离出的数字*/ } main() { long int n; int k; printf("Input n: "); scanf("%ld",&n); printf("Input k: "); scanf("%d",&k); printf("The No.%d number is %d.\n",k,fun(n,k)); } 7.8 计算s。已知 s=10!+7!*8! 将n!定义成函数。 #include double fun(int n) { double f=1.0; int i; for(i=1;i<=n;i++) f=f*i; /*求n!*/ return f; /*返回阶乘值*/ } main() { printf("10!+7!*8!=%g\n",fun(10)+fun(7)*fun(8)); } 7.9 定义一个函数,使给定的二维数组(3×3)转置,即行列转换,并输出。 7.9 矩阵转置 #include void convert(int a[][3]) { int i,j,t; for(i=0;i<3;i++) for(j=0;j { t=a[i][j]; a[i][j]=a[j][i]; a[j][i]=t; } } /*矩阵转置*/ main() { int a[3][3],i,j; printf("Input 3*3 array: \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); /*读入数据*/ printf("before converted, output: \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%4d",a[i][j]); printf("\n"); } /*输出原始数据*/ convert(a); /*调用函数进行处理*/ printf("after converted, output: \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%4d",a[i][j]); printf("\n"); } /*输出转置后的数据*/ } 7.10 写几个函数:①输入10个职工的性名和职工号;②按职工号由小到大的顺序排序, 姓名顺序也随之调整;③要求输入一个职工号,用折半查找法找出该职工的姓名,从主函数输入要查找的职工号,输出该职工的姓名。 7.10 编写几个函数,分别实现输入、排序、查找 #include #include #define N 10 void input(char name[N][8],char no[N][8]) { int i; printf("Input %d name and No.\n",N); for(i=0;i scanf("%s%s",name[i],no[i]); } /*输入每个人的姓名和学号*/ void sort(char name[N][8],char no[N][8]) { char t[8]; int i,j,p; for(i=0;i { p=i; for(j=p;j if(strcmp(name[p],name[j])>0) p=j; if(p!=i) { strcpy(t,name[p]); strcpy(name[p],name[i]); strcpy(name[i],t); strcpy(t,no[p]); strcpy(no[p],no[i]); strcpy(no[i],t); } } } /*按姓名进行从小到大的排序,姓名进行交换的时候, 职工号也进行交换,以保证职工号跟着姓名走,以免打乱 对应的关系*/ void find(char name[N][8],char no[N][8],char No[8],char messge[8]) { int low=0,high=N-1,mid; while(low<=high) { mid=(low+high)/2; if(strcmp(No,no[mid])>0) low=mid+1; /*向下找*/ else if(strcmp(No,no[mid])<0) high=mid-1; /*向上找*/ else { strcpy(message,name[mid]); return; } /*找到了, 就将对应的姓名存入find字符串*/ } strcpy(message,"no find"); /*找不到,find字符串存入"no find"*/ } main() { char name1[N][8],no1[N][8],No1[8],message1[8]; int i; clrscr(); input(name1,no1); /*调用输入函数*/ printf("%d alerks are: \n",N); printf("%10s%20s\n","name","No."); for(i=0;i printf("%10s%20s\n",name1[i],no1[i]); /*输出每个人的姓名和职工号*/ sort(name1,no1); /*调用排序函数*/ printf("after sorted: \n"); printf("%10s%20s\n","name","No."); for(i=0;i printf("%10s%20s\n",name1[i],no1[i]); /*输出排序后的姓名和职工号*/ printf("Input finded No. : "); scanf("%s",No1); /*输入要查找的职工号*/ find(name1,no1,No1,message1); /*调用查找函数,将找到的姓名放入find字符串*/ if(strcmp(message1,"no find")==0) printf("No find.\n"); else printf("The finded alerk is: %s\n",message1); /*根据find的内容输出相应的信息*/ } 7.11 定义一个函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。 7.11 反序存放字符串 #include void fun(char s[80]) { int i,j,n; char c; n=strlen(s); /求字符串长度,n也对应串尾标志*/ for(i=0,j=n-1;i /*i表示前面的下标,j表示后面的下标*/ { c=s[i]; s[i]=s[j]; s[j]=c; } /*首尾交换*/ } main() { char s[80]; printf("Input a string: "); gets(s); fun(s); printf("converted, output: "); puts(s); } 7.11 fun函数方法二 void fun(char s[]) { int i,n=0; char c; while(s[n]) n++; /*n对应串尾标志,同时也代表长度*/ for(i=0;i<=n/2;i++) { c=s[i]; s[i]=s[n-1-i]; s[n-1-i]=c; } /*首尾交换,注意i的上界*/ } 7.11 fun函数方法三 void fun(char s[]) { int i,n=0; char c; while(s[n++]) ; /*n是串尾后一个字符的下标*/ n--; /*n为串尾下标,同时也代表长度*/ 7.12 用递归法将一个整数N转换成字符串。例如,输入483,应输出“483”。N的位数不 确定,可以是任意位数的整数。 7.12 递归法将整数转换成字符串 main函数输入整数n。 fun(int n)函数完成: 如果n!=0, i=n/10; 作为下一步递归调用的实参。 递归调用fun(i)。 以字符形式输出n%10 程序如下: # include void conver(int n) { int i; if (( i=n/10 )!=0) conver(i); putchar( n%10+’0’); } main() { int num; printf("Input num: "); scanf(“%d”,&num); printf(“ Output: ” ); conver(num); } 7.13 已有变量定义语句double a=5.0;int n=5;和函数调用语句mypow(a,n);用以求a 的 n 次方。请编写double mypow(double x,int y)函数。 double mypow(double x,int y) { } 7.13 求an 在被调函数中输出结果,调用函数作为独立的语句。 #include void mypow(double x,int y) { int i; double z=1.0; for(i=1;i<=y;i++) z=z*x; printf("Output : %g\n",z); } main() { double a=5.0; int n=5; mypow(a,n); } 运行结果:Output: 3125 7.14 以下程序的功能是用牛顿法求解方程f(x)=cosx-x=0。已有初始值x0=3.1415/4,要求绝 对误差不超过0.001,函数f用来计算迭代公式中xn的值,请编写子函数。牛顿迭代公式是: x n+1=x n-f(x n) 即: x n+1=x n-(cosx n-x n)/(sinx n-1) #include #include #define PI 3.1415 float f(float x0) { } main() {int t=0,k=100,n=0;float x0=PI/4,x1; while(n {x1=f(x0); if(fabs(x0-x1)<0.001){t=1;break;} else{x0=x1;n=n+1;} } if(t==1)printf(“\nfangcheng gen g is %10.5f”,x1); else printf(“\nSorry,not found!”); } 方法二:在main函数输出结果,被调函数返回值 #include double mypow(double x,int y) { int i; double z=1.0; for(i=1;i<=y;i++) z=z*x; return z; } main() { double a=5.0; int n=5; printf("Output: %g\n",mypow(a,n)); } 运行结果:Output: 3125 方法三:递归调用 #include double mypow(double x,int y) { double z; if(y==0) z=1.0; else z=mypow(x,y-1)*x; return z; } main() { double a=5.0; int n=5; printf("Output : %g\n",mypow(a,n)); } 运行结果:Output: 3125 7.15 已有函数调用语句c=add(a,b);请编写add函数,计算两个实数a和b的和,并返 回和值。 Double add(double x,double y) { } 7.15 求和 #include double add(double x,double y) { double z; z=x+y; return z; } main() { double a,b,c; printf("Input a,b: "); scanf("%lf%lf",&a,&b); /*注意:必须用%lf*/ c=add(a,b); pirntf("%f+%f=%f\n",a,b,c); } 7.16 以下程序的功能是应用弦截法求方程x3-5x2+16x-80=0的根,其中f函数可根据指定 x 的值求出方程的值;函数xpoint可根据x1和x2求出f(x1)和f(x2)的连线与x轴的交点;函数root用来求区间(x1,x2)的实根,请编写root函数。 7.17 以下函数p的功能是用递归方法计算x的阶勒让德多项式的值。已有调用语句p(n,x); 请编写p函数。递归公式如下: 1 (n=0) Px(x)= x (n=1) ((2n-1)*x*Pn-1(x)-(n-1)*Pn-z(x))/n (n>1) float p(int n,int x) { } 7.17 递归法求勒让德多项式 #include float p(int n,int x) { if(n==0) return 1.0; else if(n==1) return x; else return ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n; } main() { int n,x; printf("Input n,x: "); scanf("%d%d",&n,&x); printf("Output : %g\n",p(n,x)); } 7.18 程序的功能是应用下面的近似公式计算e 的n 次方。函数f1用来计算每项分子的值, 函数f2用来计算每项分母的值。请编写f1和f2函数。 e x =1+x+ !22x +!33x +……(前20项的和) float f2(int n) { } float f1(int x,int n) { } main() {float exp=1.0;int n,x; printf(“Input a number:”); scanf(“% d”,&x);printf(“%d \n”,x); exp=exp+x; for(n=2;n<=19;n++) exp=exp+f1(x,n)/f2(n); printf(“\nThe is exp(%d)=%8.4f\n”,x,exp); } 运行结果:Input a number:3 The is exp(3)=20.0855 7.18 求en #include float f2(int n) { int i; float z=1.0; for(i=1;i<=n;i++) z=z*i; return z; } float f1(int x,int n) { int i; float z=1.0; for(i=1;i<=n;i++) z=z*x; return z; } main() { float exp=1.0; int n,x; printf("Input a number: "); scanf("%d",&x); exp=exp+x; for(n=2;n<=19;n++) exp=exp+f1(x,n)/f2(n); printf("\n The result is exp(%d)=%g\n",exp); } /*修改%8.4f为%g,以免数值太小,输出0*/ 7.19 是一个2*4的整形数组,且各元素均已赋值。函数max_value可求出其中的最大元素 值max,并将此值返回主调函数。今有函数调用语句max=max_value(a);请编写max_value函数。 max_value(int arr[][4]) { } 7.19 求二维数组的最大值 #include max_value(int arr[][4]) { int i,j,max; max=arr[0][0]; for(i=0;i<2;i++) for(j=0;j<4;j++)