11.打开考生文件夹中的Cprog011.C ,完成其中的函数
fun1,该函数的数学表达式是:
x e x x fun x
+++=1sin 1)(1
例如:fun1(0.76)=2.175
fun1(3.00)=5.307
fun1(3.76)=9.111
#include
#include
double fun1(double x)
{ /**/
double f;
f=(1+sin(x)+exp(x))/(1+x);
return f;
/**/
}
void main()
{
clrscr();
printf("fun1(0.76) = %8.3lf\n", fun1(0.76));
printf("fun1(3.00) = %8.3lf\n", fun1(3.00));
printf("fun1(3.76) = %8.3lf\n", fun1(3.76));
}
12.打开考生文件夹中的Cprog012.C,完成其中的函数fun2(int a[ ],int n,int b[ ],int c[ ]),实现:
1)将数组a中大于-20的元素依次存放在数组b中;
2)将数组b中的元素按照从小到大的顺序依次存放到数组c中;
3)函数返回数组b中的元素个数。
#include
#include
#include
#include
int fun2(int a[],int n,int b[],int c[])
{ /**/
int nb=0;
int i,j=0,temp;
for(i=0;i if(a[i]>-20) {b[j]=a[i];nb++;j++;} for(i=0;i for(i=0;i for(j=0;j if(c[j]>c[j+1]) { temp=c[j]; c[j]=c[j+1]; c[j+1]=temp; } return nb; /**/ } void main() { int n = 10, i, nb; int aa[10] = {12, -10, -31, -18, -15, 50, 17, 15, -20, 20}; int bb[10], cc[10]; clrscr(); printf("There are %2d elements in aa.\n", n); printf("They are: "); for(i=0; i printf("\n"); nb = fun2(aa, n, bb, cc); printf("Elements in bb are: "); for (i=0; i printf("\n"); printf("Elements in cc are: "); for(i=0; i printf("\n"); printf("There are %2d elements in bb.\n", nb); } 21.打开考生文件夹中的Cprog021.C,完成其中的函数fun1,该函数的数学表达式使: 3.16 ) (1 +- + = x x e x fun x 例如:fun1(0.76)=3. 582 fun1(3.00)=5.369 fun(3.76)=8.931 #include #include double fun1(double x) { /**/ double f; f=(exp(x) +fabs(x-6)) / (x+1.3); return f; /**/ } void main() { clrscr(); printf("fun1(0.76) = %8.3lf\n", fun1(0.76)); printf("fun1(3.00) = %8.3lf\n", fun1(3.00)); printf("fun1(3.76) = %8.3lf\n", fun1(3.76)); } 22.打开考生文件夹中的Cprog022.C,完成其中的函数fun2(char a[ ],char b[ ],char c[ ]),实现:将三个字符串a、b、c从小到大排序后输出。 注意:字符串比较函数为strcmp(str1,str2), 字符串赋值函数为strcpy(str1,str2)。 #include #include #include #include void fun2(char a[],char b[],char c[]) { /* */ char temp[15]; if(strcmp(a,b)>0) {strcpy(temp,a);strcpy(a,b); strcpy(b,temp);} if(strcmp(a,c)>0) {strcpy(temp,a);strcpy(a,c); strcpy(c,temp);} if(strcmp(b,c)>0) {strcpy(temp,b);strcpy(b,c); strcpy(c,temp);} /**/ } void main() { char str1[15]="Fuzhou",str2[15]="Fujian",str3[15]="China"; clrscr(); fun2(str1,str2,str3); printf("The ordered strings is : %s, %s, %s\n",str1,str2,str3); getch(); } 31.打开cprog031.c完成其中的函数fun1,该函数的数学表达式是: 1.2 当x<3时 fun1(x)= 10 当x=3时 2x+1 当x>3时 例如: fun1(0.76)=1.200 fun1(3.00)=10.000 fun1(3.76)=8.520 #include #include double fun1(double x) { /**/ double f; if(x<3) f=1.2; else if(x==3) f=10; else f=2*x+1; return f; /**/ } void main() { clrscr(); printf("fun1(0.76) = %8.3lf\n", fun1(0.76)); printf("fun1(3.00) = %8.3lf\n", fun1(3.00)); printf("fun1(3.76) = %8.3lf\n", fun1(3.76)); } 32.打开cprog032.c完成其中的函数fun(char *s),使程序实现统计输入字符串中空格的个数。 #include int fun(char *s) { /**/ int i,n=0; for(i=0 ; s[i]!='\0';i++) if(s[i]==' ') n++; return n; /**/ } void main() { char str[255]; gets(str); printf("%d\n",fun(str)); } 41.打开程序Cprog041.C ,完成其中的f( )函数,使其计算: ?????=≤>++0002)sin(2.3)(x x x x x f 如 输入:12 输出:f(12.000)=10.387 输入:32.25 输出:f(32.250)=12.935 输入:0.113 输出:f(0.113)=1.568 #include #include double f(float x) { /**/ double f; if(x<=0) f=0; else f=(fabs(x)+3.2)/(sin(x)+2); return f; /**/ } void main() { float x; double y; printf("Please input a number: \n"); scanf("%f",&x); y = f(x); printf("f(%.3f)=%.3f\n",x,y); getch(); } 42.打开程序Cprog042.C,完成其中的fun( )函数,使函数打印出Fibonacci数列的前20个数。该数列(1,1,2,3,5,8,13,…)的第1、第2个数为1,从第3个数开始每个数等于前2个数之和。 #include #include void fun(int a[],int m) { /**/ int i; a[0]=a[1]=1; for(i=2;i<20;i++) a[i]=a[i-1]+a[i-2]; /**/ } void main() { int a[20],i; fun(a,20); for(i=0; i<20; i++) printf("%d ",a[i]); printf("\n"); getch(); } 51.打开程序Cprog051.C ,完成其中的f( )函数,使其计算: ?? ???=<++>=-7001.2)cos(8.5700;1)(x x x x x f 如 输入:0.4 输出:f(0.40)=0.82 输入:1.5 输出:f(1.50)=1.24 输入:780 输出:f(780.00)=-1.00 #include #include double f(float x) { /**/ double f; if(fabs(x)<700) f=(sqrt(5.8+fabs(x)))/(cos(x)+2.1); else f=-1.0; return f; /**/ } void main() { float x; double y; printf("Please input a number: \n"); scanf("%f",&x); y = f(x); printf("f(%0.2f)=%0.2f\n",x,y); getch(); } 52.打开程序Cprog052.C,完成其中的fun( )函数,使其判断一个矩阵是否为对称矩阵,若矩阵对称返回1,不对称返回0。说明:矩阵a使一个二维数组,若其中的第k行第j列的元素与第j行第k列的元素相同,则称其为对称矩阵,否则为非对称矩阵。 如输入:6 3 12 如输入:6 9 12 3 18 8 3 18 8 12 8 7 34 8 22 输出:Yes 输出:No #include #include int fun(int a[][3],int m) { /**/ int b=1; int i,j; for(i=0;i { for(j=0;j if(b==0) break; } return b; /**/ } void main() { int a[3][3],i,j; int b; fun(a,20); for(i=0; i<3; i++) for(j=0; j<3; j++) scanf("%d",&a[i][j]); b = fun(a,3); if( b == 1 ) printf("Yes\n"); else printf("No\n"); getch(); } 61.打开程序Cprog061.C ,完成其中的f( )函数,使其计算: ?????=<+>=-300;)6.2|lg(|300;13)(x x x x x f 如:输入:0.8 输出:f(0.80)=0.96 输入:4.5 输出:f(4.50)=107.05 输入:725 输出:f(725.00)=-1.00 #include #include double f(float x) { /**/ double f; if(fabs(x)<300) f=pow(x,3) /log10(fabs(x)+2.6); else f=-1.0; return f; /**/ } void main() { float x; double y; printf("Please input a number: \n"); scanf("%f",&x); y = f(x); printf("f(%0.2f)=%0.2f\n",x,y); getch(); } 62.打开程序Cprog062.C,完成其中的fun( )函数,使其实现四则运算的功能。 如:输入:3.2 2.1 输出:3.20+2.10=5.3 3.2-2.10=1.10 3.20*2.10=6.72 3.20/2.10=1.52 #include #include float fun(float a,char flag,float b) { /**/ float f; switch(flag) { c ase ‘+’: f=a+b; break; c ase ‘-’: f=a-b; break; c ase ‘*’: f=a*b; break; c ase ‘/’: f=a/b; break; } return f; /**/ } void main() { float a,b; printf("Please input two numbers:\n"); scanf("%f%f",&a,&b); printf("%.2f+%.2f=%.2f\n",a,b,fun(a,'+',b)); printf("%.2f-%.2f=%.2f\n",a,b,fun(a,'-',b)); printf("%.2f*%.2f=%.2f\n",a,b,fun(a,'*',b)); printf("%.2f/%.2f=%.2f\n",a,b,fun(a,'/',b)); getch(); }