ch3例题习题实验.docx
《ch3例题习题实验.docx》由会员分享,可在线阅读,更多相关《ch3例题习题实验.docx(16页珍藏版)》请在冰豆网上搜索。
ch3例题习题实验
第三章简单的C程序设计
例题
3.1
main()
{
inta=0;
{
inta=1;
printf("In:
a=%d\n",a);
}
printf("Out:
a=%d\n",a);
}
3.2
#include
main()
{
charch;
/*空行增加程序的可读性、结构的清晰性*/
printf("PressakeyandthenpressEnter:
");
ch=getchar();/*从键盘输入一个字符,并按回车键结束输入,该字符被存入变量ch中*/
printf("Youpressed");
putchar(ch);/*在屏幕上显示变量ch中的字符*/
putchar('\n');/*输出一个回车换行控制符*/
}
3.3
#include
main()
{
intvar;
printf("PleaseenteranumberandthenpressEnter:
");
scanf("%d",&var);
printf("Thenumberyouenteredwas%d\n",var);
}
PleaseenteranumberandthenpressEnter:
6
Thenumberyouenteredwas6
3.4
#include
main()
{
floatvar;
printf("PleaseenteranumberandthenpressEnter:
");
scanf("%f",&var);
printf("Thenumberyouenteredwas%f\n",var);
}
PleaseenteranumberandthenpressEnter:
6.2
Thenumberyouenteredwas6.200000
3.5
#include
main()
{
doublef1=1.0e+10;
doublef2=3.14;
printf("%%f:
%f\n",f1);
printf("%%e:
%e\n",f1);
printf("%%g:
%g\n",f1);
printf("%%f:
%f\n",f2);
printf("%%e:
%e\n",f2);
printf("%%g:
%g\n",f2);
}
%f:
10000000000.000000
%e:
1.00000e+10
%g:
1e+10
%f:
3.140000
%e:
3.14000e+00
%g:
3.14
3.6
#include
main()
{
intvar=12;
printf("%%p:
%p\n",&var);
printf("%%x:
%x\n",&var);
printf("%%X:
%X\n",&var);
}
%p:
FFD8
%x:
ffd8
%X:
FFD8
3.7
#include
main()
{
intcount;
printf("Thisis%natestprogram\n",&count);
printf("count=%d\n",count);
}
Thisisatestprogram
count=7
3.8
#include
main()
{
floatf1=100.15799,f2=12.55,f3=1.7;
intn1=123,n2=45,n3=6;
printf("printfWITHOUTwidthorprecisionspecifications:
\n");
printf("%f\n%f\n%f\n",f1,f2,f3);
printf("%d\n%d\n%d\n",n1,n2,n3);
printf("printfWITHwidthandprecisionspecifications:
\n");
printf("%5.2f\n%6.1f\n%3.0f\n",f1,f2,f3);
printf("%5d\n%6d\n%3d\n\n",n1,n2,n3);
}
3.9
#include
main()
{
inta,b;
printf("Pleaseinputaandb:
");
scanf("%2d%*2d%2d",&a,&b);
printf("a=%d,b=%d,a+b=%d\n",a,b,a+b);
}
3.10
main()
{
inta,b;
scanf("%d%d",&a,&b);
printf("a=%d,b=%d\n",a,b);
}
3.10(6)
#include
main()
{
inta,b;
scanf("%d%*c%d",&a,&b);
printf("a=%d,b=%d\n",a,b);
}
3.11
#include
main()
{
intdata1,data2,sum;
charop;
printf("Pleaseentertheexpressiondata1+data2\n");
scanf("%d%c%d",&data1,&op,&data2);
printf("%d%c%d=%d\n",data1,op,data2,data1+data2);
}
3.11
(1)
#include
main()
{
intdata1,data2,sum;
charop;
printf("Pleaseentertheexpressiondata1+data2\n");
scanf("%d%1s%d",&data1,&op,&data2);
printf("%d%c%d=%d\n",data1,op,data2,data1+data2);
}
3.12
#include
main()
{
inta;
charb;
floatc;
printf("Pleaseinputaninteger:
");
scanf("%d",&a);
printf("integer:
%d\n",a);
printf("Pleaseinputacharacter:
");
scanf("%c",&b);
printf("character:
%c\n",b);
printf("Pleaseinputafloatnumber:
");
scanf("%f",&c);
printf("float:
%f\n",c);
}
3.12
(1)
#include
main()
{
inta;
charb;
floatc;
printf("Pleaseinputaninteger:
");
scanf("%d",&a);
printf("integer:
%d\n",a);
printf("Pleaseinputacharacter:
");
scanf("%1s",&b);
printf("character:
%c\n",b);
printf("Pleaseinputafloatnumber:
");
scanf("%f",&c);
printf("float:
%f\n",c);
}
习题
习题答案
3.1
3.2
3.3
3.4
3.5
#include
#include
main()
{
floata,b,c;/*a,b,c为三边变量*/
floats,area;
printf("\n\n\nInputa,b,c:
");
scanf("%f%f%f",&a,&b,&c);
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%.2f\n",area);
}
3.6
#include
#definePI3.14
main()
{
floatr;/*r为半径变量*/
floatcircum,area;
printf("Inputr:
");
scanf("%f",&r);
circum=2*PI*r;
area=PI*r*r,
printf("circum=%.2f,area=%.2f\n",circum,area);
}
实验
实验结果
实验1-1
(1)
#include
main()
{
chara,b;
intc;
scanf("%c%c%d",&a,&b,&c);
printf("%-2c%-2c%d\n",a,b,c);
}
实验1-1
(2)
#include
main()
{
chara,b;
intc;
scanf("%c,%c,%d",&a,&b,&c);
printf("%c,%c,%d\n",a,b,c);
}
实验1-1(3)
#include
main()
{
chara,b;
intc;
scanf("%c,%c,%d",&a,&b,&c);
printf("\'%c\',\'%c\',%d\n",a,b,c);
}
实验1-1(4)
#include
main()
{
chara,b;
intc;
scanf("%c%*c%c%*c%d",&a,&b,&c);
printf("\'%c\',\'%c\',%d\n",a,b,c);
}
实验1-1(5)
#include
main()
{
chara,b;
intc;
scanf("%c%*c%c%*c%d",&a,&b,&c);
printf("\'%c\',\'%c\',%d\n",a,b,c);
}
实验1-2
#include
#include
main()/*主函数首部*/
{
intn;/*存款期变量声明*/
doublerate;/*存款年利率变量声明*/
doublecapital;/*存款本金变量声明*/
doubledeposit;/*本利之和变量声明*/
printf("Pleaseenterrate,year,capital:
");/*打印用户输入的提示信息*/
scanf("%lf,%d,%lf",&rate,&n,&capital);/*输入数据*/
deposit=capital*pow(1+rate,n);/*计算存款利率之和,pow为幂函数*/
printf("deposit=%f\n",deposit);/*打印存款利率之和*/
}