c语言程序代码Word文档下载推荐.docx
《c语言程序代码Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《c语言程序代码Word文档下载推荐.docx(16页珍藏版)》请在冰豆网上搜索。
y)z=x;
elsez=y;
return(z);
4.有人用温度计测量出华氏发表示的温度(如69°
F),今要求把她转换成以摄氏法表示的温度(如20℃)。
公式:
c=5(f-32)/9.
其中f代表华氏温度,c代表摄氏温度。
floatf,c;
f=64.0;
c=(5.0/9)*(f-32);
f=%f\nc=%f\n"
f,c);
5.计算存款利息。
有1000元,想存一年。
有一下三种方法可选:
(1)活期:
年利率为r1;
(2)一年定期:
年利率为r2;
(3)存两次半年定期:
年利率为r3。
分别计算一年后按三种方法所得到的本息和。
floatp0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;
p1=p0*(1+r1);
p2=p0*(1+r2);
p3=p0*(1+r3/2)*(1+r3/2);
p1=%f\np2=%f\np3=%f\n"
p1,p2,p3);
6.给定一个大写字母,要求以小写字母输出。
charc1,c2;
c1=’A’;
c2=c1+32;
printf(“%c\n”,c2);
printf(“%d\n”,c2);
7.给出三角形的三边长,求三角形的面积。
若给定三角形的三边长,且任意两边之长大于第三边。
则:
area=
其中s=(a+b+C)/2.
math.h>
doublea,b,c,area;
a=3.67;
b=5.43;
c=6.21;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“a=%f\tb=%f\tc=%f\n”,a,b,c);
printf(“area=%f\n”,area);
8.求ax2+bx+c=0方程的根。
a,b,c由键盘输入,设b2-4ac>
0.
doublea,b,c,disc,x1,x2,p,q;
scanf(“%lf%lf%lf”,&
b,&
c);
disc=b*b-4*a*c;
if(disc<
0)
printf(“Thisquestionhasnorealroots\n”);
else
p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a);
x1=p+q;
x2=p-q;
printf(“x1=%7.2f\nx2=%7.2f\n”,x1,x2);
9.先后输出BOY三个字符。
chara=’B’,b=’O’,c=’Y’;
putchar(a);
putchar(b);
putchar(c);
putchar(‘\n’);
10.用三个getchar函数先后向计算机输入BOY三个字符,然后用putchar函数输出。
chara,b,c;
a=getchar();
b=getchar();
c=getchar();
或
putchar(getchar());
11.用getchar函数从键盘读入一个大写字母,把它转换成小写字母,然后用getchar函数输出对应的小写字母。
c1=getchar();
putchar(c2);
12.输入两个实数,按代数值由小到大的顺序输出这两个数。
(参照将两个杯子中的水互换,必须借助第三个杯子)。
floata,b,t;
scanf(“%f,%f”,&
if(a>
b)
t=a;
a=b;
b=t;
printf(“%5.2f,%5.2f\n”,a,b);
13.输入a,b,c三个数,要求由小到大的顺序输出。
floata,b,c,t;
scanf("
%f,%f,%f"
if(a>
{
t=a;
a=b;
b=t;
}
c)
a=c;
c=t;
if(b>
t=b;
b=c;
printf("
%5.2f,%5.2f,%5.2f\n"
a,b,c);
return0;
14.输入一个字符,判断它是否为大写字母,如果是,将它转换成小写字母,如果不是,不转换。
然后输出最后得到的字符。
charch;
scanf(“%c”,&
ch);
ch=(ch>
=’A’&
&
ch<
=’Z’)?
(ch+32):
ch;
printf(“%c\n”,ch);
charch;
%c"
if(ch>
='
A'
Z'
)
printf("
%c\n"
ch+32);
else
ch);
15.有一个函数:
y=
编一程序。
输入一个x的值,要求输出相应的y值。
intx,y;
%d"
x);
if(x<
y=-1;
if(x==0)
y=0;
else
y=1;
x=%d\ny=%d\n"
x,y);
return0;
16.要求按照考试成绩的等级输出百分制分数段,A等为85分以上,B等为70-84分。
C等为60-69分,D等为60分以下。
成绩的等级由键盘输入。
chargrade;
grade);
Youscore:
\n"
);
switch(grade)
case'
:
85~100\n"
break;
B'
70~84\n"
C'
60~69\n"
D'
<
60\n"
default:
enterdateerror\n"
17.写一程序,判断某一年是否为闰年。
intleap,year;
pleaseenteryear:
"
year);
if(year%4==0)
if
(year%100==0)
{
if(year%400==0)
leap=1;
else
leap=0;
}
else
leap=1;
elseleap=0;
if(leap)
%disaleapyear\n"
year);
%disnotaleapyear\n"
if(year%4!
=0)
leap=0;
elseif(year%100!
leap=1;
elseif(year%400!
if(leap==1)
if((year%4==0&
year%100!
=0)||(year%400==0))
18.求ax2+bx+c=0方程的根。
a,b,c由键盘输入。
(完整版)
doublea,b,c,disc,x1,x2,x3,realpart,imagepart;
%lf,%lf,%lf"
Theequation"
if(fabs(a)<
=1e-6)
isnotaquadratic"
disc=b*b-4*a*c;
if(fabs(disc)<
printf("
hastwoequalroots%8.4f\n"
-b/2*a);
if(disc>
1e-6)
{
printf("
hastwodistinctrealroots\n%8.4f,%8.4f"
x1,x2);
x1=(-b+sqrt(disc))/2*a;
x2=(-b-sqrt(disc))/2*a;
}
realpart=-b/2*a;
imagepart=sqrt(-disc)/2*a;
hastwocomplexroots:
%8.4f+%8.4fi\n"
realpart,imagepart);
%8.4f-%8.4fi\n"
//注释:
由于b*b-4*a*c(disc)是实数,而实数在计算和存储时会有一些微小的误差,因此不能直接进行如下判断:
//“if(disc==0)"
因为这样可能出现本来是零的量,由于上述误差而判别为不等于零而导致结果错误,
//所以采取的办法是判别disc的绝对值(fabs(disc))是否小于一个很小的数(例如:
1e-6)。
如果小于此数,则认为disc=0.
19.给出一个不多出5位的正整数,要求:
(1):
求出它是几位数;
(2):
分别输出每一位数字;
(3):
按逆序输出各位数字,例如原数为321,输出123。
intnum,indiv,ten,hundred,thousand,ten_thousand,place;
请输入一个正整数(0~99999):
num);
if(num>
9999)
place=5;
elseif(num>
999)
place=4;
99)
place=3;
9)
place=2;
place=1;
位数为:
%d\n"
place);
ten_thousand=num/10000;
thousand=(num-ten_thousand*10000)/1000;
hundred=(num-ten_thousand*10000-thousand*1000)/100;
ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
每一个数字分别为:
%d,%d,%d,%d,%d\n"
ten_thousand,thousand,hundred,ten,indiv);
switch(place)
case5:
反序数字为:
%d%d%d%d%d\n"
indiv,ten,hundred,thousand,ten_thousand);
case4:
%d%d%d%d\n"
indiv,ten,hundred,thousand);
case3:
%d%d%d\n"
indiv,ten,hundred);
case2:
%d%d\n"
indiv,ten);
case1:
indiv);
20.求1+2+3+4+5+……+100。
inti=1,sum=0;
while(i<
=100)
sum=sum+i;
i++;
sum=%d\n"
sum);
do
=100);
21.输出1-100,100个数。
inti=1;
i++);
22.