C编程题.docx
《C编程题.docx》由会员分享,可在线阅读,更多相关《C编程题.docx(18页珍藏版)》请在冰豆网上搜索。
C编程题
1.Cprog011.C完成函数fun1,该函数的数学表达式是fun1(x)=
如:
fun1(0.76)=2.175
fun1(3.00)=5.307
#include
#include
doublefun1(doublex)
{
/**/
doubled;
d=(1+sin(x)+exp(x))/(1+x);
returnd;
/**/
}
voidmain()
{
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(inta[],intn,intb[],intc[])实现:
将数组a中大于-20的元素,依次存放到数组b中
将数组b中的元素按照从小到大的顺序存放到数组c中
函数返回数组b中的元素个数。
#include
intfun2(inta[],intn,intb[],intc[])
{
/**/
inti,b_index=0,k,t;
for(i=0;iif(a[i]>-20){
b[b_index]=a[i];
b_index++;
}
for(i=0;ic[i]=b[i];
for(i=1;ifor(k=0;kif(c[k]>c[k+1]){t=c[k];c[k]=c[k+1];c[k+1]=t;}
returnb_index;
/**/
}
voidmain()
{
intn=10,i,nb;
intaa[10]={12,-30,22,20,15,-39,11,23,-46,100};
intbb[10],cc[10];
clrscr();
printf("thereare%2delementsinaa:
\n",n);
printf("\ntheyare:
");
for(i=0;inb=fun2(aa,n,bb,cc);
printf("\nthereare%2delementsinbb:
\n",nb);
printf("theyare:
\n");
for(i=0;iprintf("\n\nthereare%2delementsincc:
\n",nb);
printf("theyare:
\n");
for(i=0;igetch();
}
_________________________________________________
1.Cprog021.C
完成其中的函数fun1,该函数的数学表达式是:
,如:
fun1(0.76)=3.582
fun1(3.00)=5.369
fun1(3.76)=8.931
#include
#include
doublefun1(doublex)
{
/**/
doubleb=(exp(x)+fabs(x-6))/(x+1.3);
returnb;
/**/
}
voidmain()
{
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(chara[],charb[],charc[]),将三个字符串a,b,c从小到大排序后输出。
注意:
字符串比较函数为strcmp(str1,str2);
字符串复制函数为strcpy(str1,str2);
#include
#include
voidfun2(chara[],charb[],charc[])
{
/**/
intpass,k;
charstr[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]);
/**/
}
voidmain()
{
fun2("tigger","cat","monkey");
getch();
}
_________________________________________________________________
1.Cprog031.C,完成函数。
如:
…
1.2x<3
fun1(x)=10x=3
2x+1x>3
#include
doublefun1(doublex)
{
/**/
doublef;
if(x<3)f=1.2;
elseif(x>3)f=2*x+1;
elsef=10.0;
returnf;
/**/
}
voidmain()
{
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
intfun(char*s)
{
/**/
inti=0,count=0;
while(s[i]!
='\0'){
if(s[i]=='')count++;
i++;
}
returncount;
/**/
}
voidmain()
{
charstr[255];
gets(str);
printf("%d\n",fun(str));
getch();
}
_________________________________________________________________
1.Cprog041.C,完成其中的fun()函数,使其计算
f(x)=0,x≤0
如:
…
#include
#include
doublef(doublex)
{
/**/
if(x<=0)return0;
elsereturn(fabs(x)+3.2)/(sin(x)+2);
/**/
}
voidmain()
{
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
voidfun(inta[],intm)
{
/**/
intk;
a[0]=1;a[1]=1;
for(k=3;k<=m;k++)
a[k-1]=a[k-2]+a[k-3];
/**/
}
voidmain()
{
inta[20],i;
fun(a,20);
for(i=0;i<20;i++)
printf("%d",a[i]);
printf("\n");
getch();
}
__________________________________________
1.Cprog051.C,完成函数f(),使其计算:
f(x)=
-1,|x|>=700
如:
输入0.4,输出f(0.40)=0.82
#include
#include
doublef(doublex)
{
/**/
if(fabs(x)<700)return(sqrt(5.8+fabs(x)))/(cos(x)+2.1);
elsereturn-1;
/**/
}
voidmain()
{
printf("f(0.40)=%.2f\n",f(0.4));
getch();
}
2.Cprog052.C,完成其中f()函数,使其判断一个矩阵是否为对称矩阵,若是返回1,不对称返回0。
#include
intfun(inta[][3],intm)
{
/**/
introw,col,flag=1;
for(row=0;rowfor(col=0;colif(a[row][col]!
=a[col][row]){
flag=0;
return0;
}
return1;
/**/
}
voidmain()
{
inta[3][3],i,j;
intb;
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");
elseprintf("No\n");
getch();
}
_________________________________________
1.Cprog061.C,完成f()函数,计算:
f(x)=
-1,|x|>=300
如:
输入:
0.8,输出f(0.8)=0.96
#include
#include
doublef(doublex)
{
/**/
if(fabs(x)>=300)return-1;
elsereturn(x*x*x)/(log10(fabs(x)+2.6));
/**/
}
voidmain()
{
printf("f(0.80)=%.2f\n",f(0.8));
getch();
}
2.Cprog062.C,完成其中fun()函数,使其实现四则运算的功能。
如:
输入3.22.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()函数,使其对输入的一个月工资数额,求应交税款。
设应交税款的计算公式如下:
例如输入:
1825输出:
f(1825)=11.25
输入:
2700输出:
f(2700)=85.00
输入:
5655输出:
f(5655)=483.25
#include
#include
doublef(floatx)
{
/**/
doubley;
if(x>2100)
if(x<=3100)y=(x-1600)*0.10-25;
elsey=(x-1600)*0.15-125;
elseif(x>1600)
y=(x-1600)*0.05;
elsey=0;
returny;
/**/
}
voidmain()
{
floatx;
doubley;
clrscr();
printf("Pleaseinputanumber:
\n");
scanf("%f",&x);
y=f(x);
printf("f(%.2f)=%.2f\n",x,y);
getch();
}----------------------------------------------------------------------------------------------------------------------
2.打开程序Cprog072.c,完成其中的fun()函数,该程序输出4阶矩阵A中各行中0之前的所有正数,遇到0则跳过该行,并计算这些输出正数之和。
如矩阵A为
,则输出1,2,23,32,和s=58。
#include
#defineROW4
#defineCOL4
intfun(inta[][COL],introw,intb[])
{
/**/
inti,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];
}
}
returns;
/**/
}
voidmain()
{
intsss=0,b[16]={0};
inta[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("Sumofpositiveelementsis%d\n",sss);
getch();
}
1.打开程序Cprog081.c,完成其中的f()函数,使其计算:
例如输入:
-1.2输出:
f(-1.200)=0.241
输入:
2.4输出:
f(2.400)=6.902
输入:
6输出:
f(6.000)=19.879
#include
#include
doublef(floatx)
{
/**/
floaty;
if(x<0)
y=(x+2)*exp(x);
else
y=(x+2)*log(2*x);
returny;
/**/
}
voidmain()
{
floatx;
doubley;
printf("Pleaseinputanumber:
\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
voidfun(charp[][20],intn);
voidmain(void)
{
inti;
charp[][20]={"abc","xabdfg","abbd","dcdbe","cd"};
f(p,5);
clrscr();
for(i=0;i<5;i++)
printf("%s\n",p[i]);
getch();
}
voidfun(charp[][20],intn)
{
/**/
chart[20];inti,j;
for(i=0;ifor(j=i+1;jif(strcmp(p[i],p[j])>0)
{
strcpy(t,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],t);
}
/**/
}
1.打开程序Cprog091.c,完成其中的f()函数,使其返回方程
的两个根中较大的根,求根公式为:
如:
a=1b=5c=6时则程序输出:
Thebiggerrootis-2.00
#include
#include
doublef(floata,floatb,floatc)
{
/**/
floatx1,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;
/**/
}
voidmain()
{
floatx;
printf("Thebiggerrootis%.2f\n",f(1,5,6));
getch();
}
----------------------------------------------------------------------------------------------------------------------
2.打开程序Cprog092.c,完成其中的strcmp1()函数,该函数实现判别两字符串str1和str2的大小。
#include
intstrcmp1(constchar*str1,constchar*str2)
{
/**/
while((*str1==*str2)&&(*str2!
=0))
{
str1++;str2++;
}
return*str1-*str2;
/**/
}
voidmain()
{
intcom;
char*ps1="uvwx",*ps2="uvwxyz";
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();
}
————————————————————————————————————
|
|