当前位置:文档之家› C程序设计第四版谭浩强_习题&例题第7章

C程序设计第四版谭浩强_习题&例题第7章

第7章函数例7.1#include<stdio.h>int main(){void print_star();void print_message();print_star();print_message();print_star();return 0;}void print_star(){printf("***********************************\n"); }void print_message(){printf("How do you do!\n");}例7.2int max(int x,int y){int z;z=x>y?x:y;return(z);}#include<stdio.h>int main(){int max(int x,int y);int a,b,c;printf("please enter two integer number:");scanf("%d,%d",&a,&b);c=max(a,b);printf("max is %d\n",c);return 0;}例7.3#include<stdio.h>int main(){int max(float x,float y);float a,b;int c;printf("please enter two integer number:");scanf("%f,%f",&a,&b);c=max(a,b);printf("max is %d\n",c);return 0;}int max(float x,float y){float z;z=x>y?x:y;return(z);}例7.4#include<stdio.h>int main(){float add(float x,float y);float a,b,c;printf("please enter a and b:");scanf("%f,%f",&a,&b);c=add(a,b);printf("sum is %f\n",c);return 0;}float add(float x,float y){float z;z=x+y;return(z);}例7.5#include<stdio.h>int main(){int max4(int a,int b,int c,int d);int a,b,c,d,max;printf("please enter 4 interger number:");scanf("%d %d %d %d",&a,&b,&c,&d);max=max4(a,b,c,d);printf("max=%d\n",max);return 0;}int max4(int a,int b,int c,int d){int max2(int a,int b);int m;m=max2(a,b);m=max2(m,c);m=max2(m,d);return(m);}int max2(int a,int b){if(a>=b) return a;else return b;}#include<stdio.h>int main(){int max4(int a,int b,int c,int d);int a,b,c,d,max;printf("please enter 4 interger number:");scanf("%d %d %d %d",&a,&b,&c,&d);max=max4(a,b,c,d);printf("max=%d\n",max);return 0;}int max4(int a,int b,int c,int d)int max2(int a,int b);return(max2(max2(max2(a,b),c),d)); }int max2(int a,int b){return(a>b?a:b);}例7.6#include<stdio.h>int main(){int age(int n);printf("NO.5,age:%d\n",age(5));return 0;}int age(int n){int c;if(n==1) c=10;else c=age(n-1)+2;return(c);}例7.7递归方法求n!#include<stdio.h>int main(){int fac(int n);int n,y;printf("input a interger number:");scanf("%d",&n);y=fac(n);printf("%d!=%d\n",n,y);return 0;}int fac(int n){int f;if(n<0) printf("n<0,data error!");else if(n==0||n==1) f=1;else f=fac(n-1)*n;return(f);例7.8汉诺塔#include<stdio.h>int main(){void hanoi(int n,char one,char two,char three);int m;printf("input the number of diskes:");scanf("%d",&m);printf("The step to move %d diskes:\n",m);hanoi(m,'A','B','C');}void hanoi(int n,char one,char two,char three)//将N-1个盘子从一座移到另一座。

{void move(char x,char y);//用到这个函数,提前声明if(n==1) move(one,three);//最后一步1-3else{hanoi((n-1),one,three,two);//小和尚工作n-1个盘子借用3由1搬到2move(one,three);//老和尚工作最大盘子由1搬到3hanoi((n-1),two,one,three);//小和尚工作n-1个盘子借用2由1搬到3,完成工作}}void move(char x,char y)//将一个盘子从一座移到另一座{printf("%c-->%c\n",x,y);}例7.9#include<stdio.h>int main(){int max(int x,int y);int a[10],m,n,i;printf("enter 10 interger numbers:");for(i=0;i<10;i++)scanf("%d",&a[i]);printf("\n");for(i=1,m=a[0],n=0;i<10;i++){if (max(m,a[i])>m){m=max(m,a[i]);n=i;}}printf("The largest number is %d\nit is the %dth number.\n",m,n+1); }int max(int x,int y){return(x>y?x:y);}例7.10#include<stdio.h>int main(){float average(float array[10]);float score[10],aver;int i;printf("enter 10 scores:\n");for(i=0;i<10;i++)scanf("%f",&score[i]);printf("\n");aver=average(score);printf("average score is %5.2f\n",aver);return 0;}float average(float array[10]){int i;float aver,sum=array[0];for (i=1;i<10;i++)sum=sum+array[i];aver=sum/10;return(aver);}例7.11#include<stdio.h>int main(){float average(float array[10],int n);float score1[5]={98.5,97,91.5,60,55};float score2[10]={67.5,89.5,99,69.5,77,89.5,76.5,54,60,99.5};printf("The average of class A is %6.2f\n",average(score1,5));printf("The average of class B is %6.2f\n",average(score2,10));return 0;}float average(float array[10],int n){int i;float aver,sum=array[0];for (i=1;i<n;i++)sum=sum+array[i];aver=sum/n;return(aver);}例7.12#include<stdio.h>int main(){void sort(int array[],int n);int a[10],i;printf("enter array:\n");for(i=0;i<10;i++)scanf("%d",&a[i]);sort(a,10);printf("The sorted array:\n");for(i=0;i<10;i++)printf("%d ",a[i]);printf("\n");return 0;}void sort(int array[],int n){int i,j,k,t;for(i=0;i<n-1;i++){k=i;for(j=i+1;j<n;j++)if(array[j]<array[k])k=j;t=array[k];array[k]=array[i];array[i]=t;}}例7.13#include<stdio.h>int main(){int max_value(int array[][4]);int a[3][4]={{1,3,5,7},{2,4,6,8},{15,17,34,12}};printf("Max value is %d\n",max_value(a));return 0;}int max_value(int array[][4]){int i,j,max;max=array[0][0];for(i=0;i<3;i++)for(j=0;j<4;j++)if(array[i][j]>max) max=array[i][j];return(max);}例7.14#include<stdio.h>float Max=0,Min=0;int main(){float average(float array[],int n);float ave,score[10];int i;printf("Please enter 10 score:");for(i=0;i<10;i++)scanf("%f",&score[i]);ave=average(score,10);printf("max =%6.2f\nmin=%6.2f\naverage=%6.2f\n",Max,Min,ave);return 0;}float average(float array[],int n){int i;float aver,sum=array[0];Max=Min=array[0];for(i=1;i<n;i++){if(array[i]>Max) Max=array[i];else if(array[i]<Min) Min=array[i];sum+=array[i];}aver=sum/n;return(aver);}例7.15#include<stdio.h>int a=3,b=5;int main(){int max(int a,int b);int a=8;printf("max=%d\n",max(a,b));return 0;}int max(int a,int b){int c;c=a>b?a:b;return(c);}例7.16#include<stdio.h>int main(){int f(int);int a=2,i;for (i=0;i<3;i++)printf("%d\n",f(a));return 0;}int f(int a){auto int b=0;static c=3;b=b+1;c=c+1;return(a+b+c);}例7.17#include<stdio.h>int main(){int fac(int n);int i;for (i=1;i<=5;i++)printf("%d!=%d\n",i,fac(i));return 0;}int fac(int n){static int f=1;f=f*n;return(f);}例7.18#include<stdio.h>int main(){int max();extern int A,B,C;printf("Please enter three interger numbers\n");scanf("%d %d %d",&A,&B,&C);printf("max is %d\n",max());return 0;}int A,B,C;int max(){int m;m=A>B?A:B;if(C>m)m=C;return(m);}例7.19file1.c#include<stdio.h>int A;int main(){int power(int);int b=3,c,d,m;printf("enter the number a and its power m:\n");scanf("%d,%d",&A,&m);c=A*b;printf("%d*%d=%d\n",A,b,c);d=power(m);printf("%d**%d=%d\n",A,m,d);return 0;}file2.cextern A;int power(int n){int i,y=1;for(i=1;i<=n;i++)y*=A;return(y);}例7.20file1.c#include<stdio.h>int main(){extern void enter_string(char str[]);extern void delete_string(char str[],char ch);extern void print_string(char str[]);char c,str[80];enter_string(str);scanf("%c",&c);delete_string(str,c);print_string(str);return 0;}file2.cvoid enter_string(char str[80]){gets(str);}file3.cvoid delete_string(char str[],char ch){int i,j;for(i=j=0;str[i]!='\0';i++)if(str[i]!=ch)str[j++]=str[i];str[j]='\0';}file4.cvoid print_string(char str[]){printf("%s\n",str);}习题1.#include<stdio.h>int divisor(x,y){int r;if(x<y){r=x;x=y;y=r;}while(r){r=x%y;x=y;y=r;}return(x);}int multiple(int x,int y){int z;z=x*y/divisor(x,y);return(z);}int main(){int a,b;printf("请输入两个整数a和b:");scanf("%d,%d",&a,&b);printf("a和b的最大公约数为%d.\n",divisor(a,b));printf("a和b的最小公倍数为%d.\n",multiple(a,b));return 0;}2.#include<stdio.h>#include<math.h>float d;void disc1(float a,float b,float c){printf("方程有两不等实根:x1=%5.2f,x2=%5.2f\n",(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a)); }void disc2(float a,float b,float c){printf("方程有两相等实根:x1=x2=%5.2f\n",-b/(2*a));}void disc3(float a,float b,float c){printf("方程有两不等实根:x1=%5.2f+%5.2fi,x2=%5.2f-%5.2fi\n",-b/(2*a),sqrt(-d)/(2*a),-b/(2*a),sqrt(-d)/(2*a)); }int main(){float a,b,c;printf("请输入a,b,c:");scanf("%f,%f,%f",&a,&b,&c);d=b*b-4*a*c;if(d>0) disc1(a,b,c);else if(fabs(d)<=1e-6) disc2(a,b,c);else disc3(a,b,c);return 0;}3.#include<stdio.h>#include<math.h>void f(int a){int i,n=(int)sqrt(a),t;for(i=2,t=1;i<=n;i++){if(a%i==0){t=0;printf("这是一个合数.\n");break;}}if(t) printf("这是一个素数.\n");}int main(){int a;printf("请输入一个正整数a:");scanf("%d",&a);f(a);return 0;}4.#include<stdio.h>void f(int a[3][3]){int i,j,t;for(i=0;i<=2;i++)for(j=0;j<i;j++){t=a[i][j];a[i][j]=a[j][i];a[j][i]=t;}}void main(){int i,j,a[3][3]={1,2,3,4,5,6,7,8,9};for(i=0;i<=2;i++){for(j=0;j<=2;j++)printf("%5d",a[i][j]);printf("\n");}printf("\n");f(a);for(i=0;i<=2;i++){for(j=0;j<=2;j++)printf("%5d",a[i][j]);printf("\n");}}5.#include<stdio.h>#include<string.h>void f(char a[]){int i,t;char b;t=strlen(a);for(i=0;i<=t/2;i++){b=a[i];a[i]=a[t-i-1];a[t-i-1]=b;}}void main(){char ch[80];gets(ch);f(ch);puts(ch);}6.#include<stdio.h>void f(char a[],char b[]){int i,j;for(i=0;a[i]!='\0';i++);for(j=0;b[j]!='\0';i++,j++)a[i]=b[j];}void main(){char ch1[80]="How do you do!",ch2[]="Fine,thank you,and you?";f(ch1,ch2);puts(ch1);}7.#include<stdio.h>void f(char a[80],char b[40]){int i,j;for(i=0,j=0;a[i]!='\0';i++)if(a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U'||a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o' ||a[i]=='u'){b[j]=a[i];j++;}b[j]='\0';}int main(){char ch1[80]="How are you?Fine,thank you,and you?",ch2[40];f(ch1,ch2);puts(ch2);return 0;}8.#include<stdio.h>void f(int n,int ch[]){ch[0]=n/1000;ch[1]=(n-ch[0]*1000)/100;ch[2]=(n-ch[0]*1000-ch[1]*100)/10;ch[3]=n%10;}void main(){int n,i,ch[4];printf("输入一个四位整数:\n");scanf("%d",&n);f(n,ch);for(i=0;i<=3;i++)printf("%c ",ch[i]+48);}9.#include<stdio.h>int upp,low,num,space,other;void f(char c[]){int i;for(upp=0,low=0,num=0,space=0,other=0,i=0;c[i]!='\0';i++){if(c[i]>='A'&&c[i]<='Z') upp++;else if(c[i]>='a'&&c[i]<='z') low++;else if(c[i]>='0'&&c[i]<='9') num++;else if(c[i]==' ') space++;else other++;}}void main(){char ch[80];printf("输入一句英文,限80字符内:\n");gets(ch);f(ch);printf("大写字母%d个,小写字母%d个,数字%d个,空格%d个,其他%d个\n",upp,low,num,space,other);}10.#include<stdio.h>void f(char a[100],char b[40][30]){int i,m,n,k,g;for(i=m=n=k=g=0;a[i]!='\0';i++){if (a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z'){b[m][n]=a[i];//m单词位置n单词长度n++;}else{if(k<n){k=n;//k单词最长数g最长单词位置g=m;b[m][n]='\0';}n=0;m++;}}printf("最长的单词为:\n");puts(b[g]);}void main(){char a[100],b[40][30];printf("请输入一句英文:\n");gets(a);f(a,b);}11.#include<stdio.h>void f(char a[10]){int i,j;char ch;for(i=0;i<=8;i++)for(j=i+1;j<=9;j++)if(a[i]>a[j]){ch=a[i];a[i]=a[j];a[j]=ch;}}void main(){char a[10];printf("请输入10个字符:\n");gets(a);f(a);puts(a);}12.#include<stdio.h>float f(float a,float b,float c,float d,float x){float z;z=x-(a*x*x*x+b*x*x+c*x+d)/(3*a*x*x+2*b*x+c);return (z);}void main(){float a,b,c,d,x;printf("请输入a,b,c,d:\n");scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("方程为%f*x*x*x+%f*x*x+%f*x+%f=0\n",a,b,c,d);x=1.0;while(a*x*x*x+b*x*x+c*x+d>1e-6)x=f(a,b,c,d,x);printf("方程在1附近的根为:%f\n",x);}13.#include<stdio.h>float p(int n,float x){float y;if(n==0)y=1;else if(n==1)y=x;else y=((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n;return (y);}void main(){float x,y;int n;printf("请输入正整数n和实数x:\n");scanf("%u,%f",&n,&x);y=p(n,x);printf("%f\n",y);}14.#include<stdio.h>void f0(double a[10][5]){int i,j;double sum,aver;for(i=0;i<=9;i++){sum=0,aver=0;for(j=0;j<=4;j++)sum+=a[i][j];aver=sum/5.0;printf("第%d名学生的平均分为:%5.1lf分\n",i+1,aver);}}void f1(double a[10][5]){int i,j;double sum,aver;for(j=0;j<=4;j++){sum=0,aver=0;for(i=0;i<=9;i++)sum+=a[i][j];aver=sum/10.0;printf("第%d门学科的平均分为:%5.1lf分\n",j+1,aver);}}void f2(double a[10][5]){int i,j,s,t;double max;for(j=0;j<=4;j++){max=0;for(i=0;i<=9;i++)if(max<a[i][j]){max=a[i][j];s=i;t=j;}printf("第%d门学科的最高分为第%d学生:%5.1lf分\n",t+1,s+1,max);}}void f3(double a[10][5]){int i,j;double s,t,y;for(i=0;i<=9;i++){s=t=y=0;for(j=0;j<=4;j++){s+=a[i][j]*a[i][j];t+=a[i][j]/5.0;}y=s/5.0-t*t;printf("第%d名学生的平均分方差为:%5.1lf\n",i+1,y);}}void main(){doublea[10][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,28,29 ,30},{31,32,33,34,35},{36,37,38,39,40},{41,42,43,44,45},{46,47,48,49,50}};f0(a);f1(a);f2(a);f3(a);}15.#include<stdio.h>#include<string.h>void f(int a[10],char b[10][20]){int i,j,t;char ch[20];for(i=0;i<=8;i++)for(j=i+1;j<=9;j++)if(a[i]>a[j]){t=a[i];a[i]=a[j];a[j]=t;strcpy(ch,b[i]);strcpy(b[i],b[j]);strcpy(b[j],ch);}}void g(int k,int a[10],char b[10][20]){int i,m=1;if(k>a[4]){for(i=9;i>=5;i--){if(k==a[i])printf("%d %s\n",a[i],b[i]);}m=0;}else{for(i=0;i<=4;i++){if(k==a[i])printf("%d %s\n",a[i],b[i]);}m=0;}if (m) printf("输入职工号错误\n");}void main(){int i,k,a[10]={2411,2123,2135,2147,2169,2172,2184,2196,2108,2110};char b[10][20]={"马云","李娜","张靓颖","吴彦祖","刘德华","孙燕姿","田震","那英","梁朝伟","刘烨"};for(i=0;i<=9;i++)printf("%d %s\n",a[i],b[i]);printf("\n");f(a,b);for(i=0;i<=9;i++)printf("%d %s\n",a[i],b[i]);printf("please enter a number:\n");scanf("%d",&k);g(k,a,b);}16.#include<stdio.h>#include<string.h>#include<math.h>int f(char ch[]){int i,y=0,n;n=strlen(ch);for (i=0;i<n;i++){if(ch[i]>=48&&ch[i]<=57) y+=(ch[i]-48)*pow(16,i);else if(ch[i]>=65,ch[i]<=90) y+=(ch[i]-65+10)*pow(16,i);else if(ch[i]>=97,ch[i]<=102) y+=(ch[i]-97+10)*pow(16,i);}return (y);}void main(){int t;char ch[4];printf("输入一个十六进制数(4字节):\n");scanf("%s",&ch);t=f(ch);printf("十六进制数%s转化为十进制数为%d:\n",ch,t);17.#include<stdio.h>void f(int n){int i;if (i=n/10)f(i);putchar(n%10+'0');}main(){int n;printf("输入一个整数:\n");scanf("%d",&n);printf("输出字符串:");if (n<0){putchar('-');n=-n;}f(n);printf("\n");}18.#include <stdio.h>#include <stdlib.h>int leap(int y){ int l;if(y%400==0||y%4==0&&y%100!=0)//判断闰年,&&优先级高于|| l=1;elsel=0;return(l);}void main()int y,m,d,a=0,i,s;//年y月m日n,求和s,循环增量iint b[12]={31,28,31,30,31,30,31,31,30,31,30,31};scanf("%d,%d,%d",&y,&m,&d);for (i=1;i<m;i++)a=a+b[i-1];//求前几个月的天数和s=(m>2?leap(y):0)+d+a;//闰年加一天printf("%d年%d月%d日是%d年第%d天\n",y,m,d,y,s); }。

相关主题