文档视界 最新最全的文档下载
当前位置:文档视界 › 计算机二级编程题

计算机二级编程题

计算机二级编程题
计算机二级编程题

1.Cprog011.C完成函数fun1,该函数的数学表达式是fun1(x)=1sin

1

x

x e

x

++

+

如:fun1(0.76)=2.175

fun1(3.00)=5.307

#include

#include

double fun1(double x)

{

/**/

double d;

d=(1+sin(x)+exp(x))/(1+x) ;

return d;

/**/

}

void main()

{

clrscr();

printf("fun1(0.76)=%8.3lf\n",fun1(0.76));

printf("fun1(3.00)=%8.3lf\n",fun1(3.00));

getch();

}

2.Cprog012.C

完成其中的函数fun2(int a[],int n,int b[],int c[])实现:

将数组a中大于-20的元素,依次存放到数组b中

将数组b中的元素按照从小到大的顺序存放到数组c中

函数返回数组b中的元素个数。

#include

int fun2(int a[],int n,int b[],int c[])

{

/**/

int i,b_index=0,k,t;

for(i=0;i

if(a[i]>-20){

b[b_index]=a[i];

b_index++;

}

for(i=0;i

c[i]=b[i];

for(i=1;i

for( k=0;k

if(c[k]>c[k+1]){t=c[k];c[k]=c[k+1];c[k+1]=t;} return b_index;

/**/

void main()

{

int n=10,i,nb;

int aa[10]={12,-30,22,20,15,-39,11,23,-46,100};

int bb[10],cc[10];

clrscr();

printf("there are %2d elements in aa:\n",n);

printf("\nthey are:");

for(i=0;i

nb=fun2(aa,n,bb,cc);

printf("\nthere are %2d elements in bb:\n",nb);

printf("they are:\n");

for(i=0;i

printf("\n\nthere are %2d elements in cc:\n",nb);

printf("they are:\n");

for(i=0;i

getch();

}

_________________________________________________

1.Cprog021.C

完成其中的函数fun1,该函数的数学表达式是:

|6|1() 1.3x e x fun x x +-=+,如:fun1(0.76)=3.582

fun1(3.00)=5.369

fun1(3.76)=8.931

#include

#include

double fun1(double x)

{

/**/

double b=(exp(x)+fabs(x-6))/(x+1.3);

return b;

/**/

}

void main()

{

printf("fun1(0.76)=%.3lf\n",fun1(0.76));

printf("fun1(3.00)=%.3lf\n",fun1(3.00));

printf("fun1(3.76)=%.3lf\n",fun1(3.76));

getch();

2.Cprog022.C,完成其中函数fun2(char a[ ],char b[ ],char c[ ]),将三个字符串a,b,c从小到大排序后输出。注意:字符串比较函数为strcmp(str1,str2);

字符串复制函数为strcpy(str1,str2);

#include

#include

void fun2(char a[],char b[],char c[])

{

/**/

int pass,k;

char str[3][100],t[100];

strcpy(str[0],a);strcpy(str[1],b);strcpy(str[2],c);

for(pass=1;pass<3;pass++)

for(k=0;k<3-pass;k++)

if(strcmp(str[k],str[k+1])>0){

strcpy(t,str[k]);

strcpy(str[k],str[k+1]);

strcpy(str[k+1],t);

}

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

printf("%s\n",str[k]);

/**/

}

void main()

{

fun2("tigger","cat","monkey");

getch();

}

_________________________________________________________________ 1.Cprog031.C,完成函数。如:…

1.2 x<3

fun1(x)= 10 x=3

2x+1 x>3

#include

double fun1(double x)

{

/**/

double f;

if(x<3) f=1.2;

else if(x>3) f=2*x+1;

else f=10.0;

return f;

/**/

{

printf("fun1(1.5)=%.2f\n",fun1(1.5));

printf("fun1(6.5)=%.2f\n",fun1(6.5));

printf("fun1(3.0)=%.2f\n",fun1(3.0));

getch();

}

2.Cprog032.C ,完成其中函数fun(char *s),统计输入字符串中空格的个数。 #include

int fun(char* s)

{

/**/

int i=0,count=0;

while(s[i]!='\0'){

if(s[i]==' ') count++;

i++;

}

return count;

/**/

}

void main()

{

char str[255];

gets(str);

printf("%d\n ",fun(str));

getch();

}

_________________________________________________________________

1.Cprog041.C ,完成其中的fun()函数,使其计算

f(x)= 0, x ≤0 || 3.2,0sin()2x x x +>+

如:…

#include

#include

double f(double x)

{

/**/

if(x<=0) return 0;

else return ( fabs(x)+3.2 )/( sin(x)+2 ) ;

{

printf("f(-2.3)=%.4f\n ",f(-2.3));

printf("f(3.14)=%.4f\n ",f(3.14));

getch();

}

2.Cprog042.C ,完成其中的fun()函数,使程序打印出Fibonacci数列的前20个数。该数列(1,1,2,3,5,8,13,…)。

#include

void fun(int a[],int m)

{

/**/

int k;

a[0]=1;a[1]=1;

for( k=3;k<=m;k++)

a[k-1]=a[k-2]+a[k-3];

/**/

}

void main()

{

int a[20],i;

fun(a,20);

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

printf("%d ",a[i]);

printf("\n");

getch();

}

__________________________________________

1.Cprog051.C,完成函数f(),使其计算:

x<

|700

-1, |x|>=700

如:输入0.4,输出f(0.40)=0.82

#include

#include

double f(double x)

{

/**/

if(fabs(x)<700) return (sqrt(5.8+fabs(x)))/(cos(x)+2.1);

else return -1 ;

/**/

}

void main()

printf("f(0.40)=%.2f\n ",f(0.4));

getch();

}

2.Cprog052.C,完成其中f()函数,使其判断一个矩阵是否为对称矩阵,若是返回1,

不对称返回0。

#include

int fun(int a[][3],int m)

{

/**/

int row,col,flag=1;

for(row=0;row

for(col=0;col

if(a[row][col]!=a[col][row]){

flag=0;

return 0;

}

return 1;

/**/

}

void main()

{

int a[3][3],i,j;

int b;

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

}

_________________________________________

1.Cprog061.C,完成f()函数,计算:

f(x)=

3

,||300 lg(|| 2.6)

x

x

x

<

+

-1, |x|>=300

如:输入:0.8,输出f(0.8)=0.96

#include

#include

double f(double x)

{

/**/

if(fabs(x)>=300) return -1;

else return (x*x*x)/( log10( fabs(x)+2.6 ) ); /**/

void main()

{

printf("f(0.80)=%.2f\n",f(0.8));

getch();

}

2.Cprog062.C ,完成其中fun()函数,使其实现四则运算的功能。如:输入3.2 2.1 ,输出:3.20+2.10=5.30

3.20-2.10=1.10

3.20*2.10=6.72

3.20/2.10=1.52

——————————————————————————————————————

1. 打开程序Cprog071.C ,完成其中的f()函数,使其对输入的一个月工资数额,求应交税款。设应交税款的计算公式如下:

???????>-?-≤<-?-≤

%10)1600(21001600%5)1600(16000)(x x x x x x x x f

例如 输入:1825 输出:f(1825)=11.25

输入:2700 输出:f(2700)=85.00

输入:5655 输出:f(5655)=483.25

#include

#include

double f(float x)

{

/**/

double y;

if(x>2100)

if(x<=3100) y=(x-1600)*0.10-25;

else y=(x-1600)*0.15-125;

else if(x>1600)

y=(x-1600)*0.05;

else y=0;

return y;

/**/

}

void main()

{

float x;

double y;

clrscr();

printf("Please input a number:\n");

scanf("%f",&x);

y = f(x);

printf("f(%.2f)=%.2f\n",x,y);

----------------------------------------------------------------------------------------------------------------------

2. 打开程序Cprog072.c ,完成其中的fun()函数,该程序输出4阶矩阵A 中各行中0之前

的所有正数,遇到0则跳过该行,并计算这些输出正数之和。如矩阵A 为

????????????--------033

3231240232114131204321,则输出1,2,23,32,和s=58。 #include

#define ROW 4

#define COL 4

int fun(int a[][COL],int row,int b[])

{

/**/

int i,j,k=0,s=0;

for(i=0;i

{

for(j=0;j

{

if(a[i][j]<0) continue;

if(a[i][j]==0) break;

b[k++]=a[i][j];

s+=a[i][j];

}

}

return s;

/**/

}

void main()

{

int sss=0, b[16]={0};

int

a[ROW][COL]={{1,2,-3,-4},{0,-12,-13,14},{-21,23,0,-24},{-31,32,-33,0}};

clrscr();

sss=fun(a,ROW,b);

printf("Sum of positive elements is %d\n",sss);

getch(); }

1. 打开程序Cprog081.c ,完成其中的f()函数,使其计算:

???≥+<+=0

2ln )2(0)2()(x x x x e x x f x

例如 输入:-1.2 输出:f(-1.200)=0.241

输入:2.4 输出:f(2.400)=6.902

#include

#include

double f(float x)

{

/**/

float y;

if(x<0)

y=(x+2)*exp(x);

else

y=(x+2)*log(2*x);

return y;

/**/

}

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

}

---------------------------------------------------------------------------------------------------------------------- 2.打开程序Cprog082.c,完成其中的fun()函数,该函数将以指针数组形式存放的n个串升序排序。注意:字符串比较函数为strcmp(str1,str2),

字符串复制函数为strcpy(str1,str2 。

#include

#include

void fun(char p[][20],int n) ;

void main(void)

{

int i;

char p[][20]={"abc","xabdfg","abbd","dcdbe","cd"};

f(p,5);

clrscr();

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

printf("%s\n",p[i]);

getch();

}

void fun(char p[][20],int n)

{

/**/

char t[20]; int i,j;

for(i=0;i

for (j=i+1;j

if(strcmp(p[i],p[j])>0)

strcpy(t,p[i]);

strcpy(p[i],p[j]);

strcpy(p[j],t);

}

/**/ }

1.打开程序Cprog091.c ,完成其中的f ()函数,使其返回方程02

=++c bx ax 的两个根中较大的根,求根公式为: a

ac b b x 2422,1-±-= 如:a=1 b=5 c=6时 则程序输出:The bigger root is -2.00

#include

#include

double f(float a,float b,float c)

{

/**/

float x1,x2;

x1=(-b+sqrt(b*b-4*a*c))/(2*a);

x2=(-b-sqrt(b*b-4*a*c))/(2*a);

return (x1>x2)?x1:x2;

/**/

}

void main()

{

float x;

printf("The bigger root is %.2f\n",f(1,5,6));

getch();

}

----------------------------------------------------------------------------------------------------------------------

2. 打开程序Cprog092.c ,完成其中的strcmp1()函数,该函数实现判别两字符串str1

和str2的大小。

#include

int strcmp1(const char* str1,const char* str2)

{

/**/

while((*str1==*str2)&&(*str2!=0))

{

str1++;str2++;

}

return *str1-*str2;

/**/

}

void main()

{

int com;

clrscr();

com=strcmp1(ps1,ps2);

if(com>0)

printf("%s>%s",ps1,ps2);

if(com==0)

printf("%s=%s",ps1,ps2);

if(com<0)

printf("%s<%s",ps1,ps2);

getch();

} ————————————————————————————————————

相关文档