admin 管理员组文章数量: 1086019
2024年8月27日发(作者:x 10110y 00001)
最简单的C程序设计——顺序程序设计
P037 3.1 把华氏温度转化为摄氏表示法.
#include
float F_to_C(float input_fah) //代表华转摄,input_fah是局部变量.
{
float output_cen; //这里也是局部变量.
output_cen=(5.0/9)*(input_fah-32); //函数的功能体.
return output_cen; //返回值,注意,返回值的数据类型应该和函数一致.
}
float C_to_F(float input_cen)
{
float output_fah;
output_fah=(9.0/5)*input_cen+32; //转换过程.
return output_fah;
}
int main()
{
int choice;
float input_fah,input_cen,output_fah,output_cen; //局部变量的调用及参数传
递.
printf("F_to_C press <1> and C_to_F press <2> !n");
scanf("%d",&choice);
if(choice==1)
{
printf("Please input fahrenheit :");
scanf("%f",&input_fah); //这个是主函数定义的变量,虽然和调用函数同名.
output_cen=F_to_C(input_fah);
printf("The 华氏 is %d , 摄氏 is %d .",(int)input_fah,(int)output_cen);
}
if(choice==2)
{
printf("Please input centigrade :");
scanf("%f",&input_cen);
output_fah=C_to_F(input_cen);
printf("The Centigrade is %d
is %d .",(int)input_cen,(int)output_fah);
}
return 0;
}
P038 3.2 计算存款利息(关于精度问题).
#include
, and the Fahrenheit
int main()
{
float p0=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);
printf("p1=%fnp2=%fnp3=%fn",p1,p2,p3);
return 0;
}
P055 3.3 大写转换成小写
#include
int main() //小写范围是97-122,大写范围是65-90,大写加上即得小写.26个字母.
{
char c1, c2;
c1='A';
c2=c1+32;
printf("%c %d",c2,c2);
return 0;
}
P059 3.4 给出三角形边长,算出面积.
#include
#include
int main()
{
double a=3.67, b=5.43, c=6.21, s, area;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area is %fn",area); //默认可以组成三角形.
return 0;
}
P065 3.5 求一无二次等式的根,默认两个不同根.
#include
#include
int main()
{
double a,b,c,disc,x1,x2,p,q;
scanf("%lf %lf %lf",&a,&b,&c);
disc=b*b-4*a*c;
p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a);
x1=p+q;
x2=p-q;
printf("x1=%7.2fnx2=%7.2f",x1,x2);
return 0;
}
P071 3.6 用%f输出实数,只能得到六位小数.
#include
#include
int main()
{
double a=1.0; //1是整型,1.0是浮点型,默认是double.可以是float.
printf("%.9fn",a/3);
return 0;
}
P072 3.7 float型数据的有效位数.
#include
#include
int main()
{
float a; //输出的结果是.333252,float精度6位,所以第七位后不可信.
a=10000/3.0;
printf("%fn",a);
return 0;
}
P078 3.8 使用putchar函数输出.
#include
#include
int main()
{
char a='B',b='O',c='Y';
putchar(a);
putchar(b);
putchar(c);
putchar('n');
putchar(101); //101是A的ASCII代码的缩写,因为此函数只能输出字符.
putchar(66);
return 0;
}
P079 3.9 使用getchar得到字符.
#include
#include
int main()
{
char a,b,c;
a=getchar();
b=getchar();
c=getchar();
putchar(a);
putchar(b);
putchar(c); //这也是基本回显的C程序代码.
putchar('n');
return 0;
}
P081 3.10 getchar得到大写,putchar输出小写.
#include
#include
int main()
{
char a,b;
a=getchar();
b=a+32;
putchar(b);
putchar('n');
return 0;
}
P082 0.1 国民生产总值10年后的增长倍数.
#include
#include
int main()
{
double p,r=0.09,n=10;
p=pow((1+r),n); //这是数学函数, pow(x,y)计算x的y次方.
printf("P is %lf when 10 years later .n",p);
return 0; //结果是0.36倍.
}
P082 0.2 求各种存款的利息数.
#include
#include
int main()
{
double p,r,n; //年份和当年利率均有给出.
p=1000*(1+5*0.0585);
printf("5 years is %lf !n",p); //1292.5,这是全五年期.lf输出的是double型.
p=(1000*(1+2*0.0468));
p=(p*(1+3*0.0540));
printf("5 years is %lf !n",p); //1270.76,这是先二年,再三年的.
p=(1000*(1+3*0.0540));
p=(p*(1+2*0.0468));
printf("5 years is %lf !n",p); //1270.76,这是先三年,再二年的.证明,是一样的.
p=1000*pow((1+0.0414),5);
printf("5 years is %lf !n",p); //1224.86,这难道说是,相对的存死期越久越值钱.
p=1000*pow((1+0.072/4),4*5);
printf("5 years is %lf !n",p); //1428.74.
return 0;
}
P083 0.3 求几个月要以还贷.
#include
#include
int main()
{
double m,r=0.01,d=300000,p=6000;
m=(log10(p/(p-d*r)))/(log10(1+r));
printf("%.1lf",m); //按要求只留一个小数,所以要写%.1lf.
return 0;
}
P084 0.6 字母密码转换,调用函数及临界处理.
#include
char printcode(char f)
{
if(((int)f>86&&(int)f<91)||((int)f>118&&(int)f<123))
{
return(f-26+4); //因为putchar会自动返回,所以改成return,因为这是在函数中,调
用需要返回值.
}
else
{
return(f+4);
}
}
int main()
{
char a,b,c,d,e;
printf("Please input :n");
a=getchar();
b=getchar();
c=getchar();
d=getchar();
e=getchar(); //临界问题.
printf("%c%c%c%c%c",printcode(a),printcode(b),printcode(c),printcode(d),pri
ntcode(e));
putchar(putcharcode(a));
putchar(putcharcode(b));
putchar(putcharcode(c));
putchar(putcharcode(d));
putchar(putcharcode(e));
return 0; //注意理解自定义函数的定义,使用,及形参实参的流向.
//p84的是计算问题,自己看着办,最后要求小数点后两位,用的是%.2lf 来实现,因为没
有要求实部,所以m.n格式中m不写.
//p84的是定义问题,第一问,两者都行,但是定义字母时,scanf要写%c来获取,而定义数
值时则要用%d来获取.
// 第二问,putchar貌似只能输出字符,所以用r本身就
是字符输出函数啦,字符,有木有字符啊!!尽管它的参数可以是putchar('101'),效果是输
出字符A啊.
// 第三问,出现"任何"及"无条件",那么答案明显是"否".可以转换,但
要在某此条件下,例如输出和读入时,%c是字母,而%d是数值,看着办.
}
版权声明:本文标题:C程序设计(第四版)_谭浩强_第三章_课后习题答案 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1724740277a822971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论