第三章课后习题(1)
1、表达式&表达式语句
表达式后面加 ; 即可成为表达式语句。
赋值表达式加分号成为赋值语句,大多数的语句是表达式语句(包括函数调用语句)
表达式是程序的基本要素,它可以组成关系表达式,逻辑表达式等多种表达式以完成需要的功能。(见课本P45)
2、编写程序求圆周长、面积,圆柱体表面积、体积,圆球表面积、体积
#include<iostream>
#include<iomanip>
using namespace std;
#define PI 3.14159
int main(){
double r,h;
cout<<"please input r,h:"<<endl;
cin>>r>>h;//输入1.5 3
cout<<setiosflags(ios::fixed)<<setprecision(2);//对后面输出都有效
cout<<"circle:"<<endl;
cout<<"the circumference of the circle: "<<(2*PI*r)<<endl;
cout<<"the area of the circle: "<<(PI*r*r)<<endl;
cout<<"sphere:"<<endl;
cout<<"the surface area of the sphere: "<<(4*PI*r*r)<<endl;
cout<<"the volume of the sphere: "<<(4/3*PI*r*r*r)<<endl;
cout<<"cylinder:"<<endl;
cout<<"the surface area of the cylinder:"<<(2*PI*r*r+2*PI*r*h)<<endl;
cout<<"the volume of the cylinder: "<<(PI*r*r*h)<<endl;
return 0;
}
3、华氏摄氏温度转换
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
double f,c;
cout<<"please input F:"<<endl;
cin>>f;
c=5*(f-32)/9;//c=5.0/9.0*(f-32);
cout<<"the tranformed temperture C is:";
cout<<setiosflags(ios::fixed)<<setprecision(2)<<c;
return 0;
}
4、输入输出字符
#include<iostream>
using namespace std;
int main(){
char c1,c2;//定义为字符型输出为字符
//int c1,c2;//定义为整型输出的是ASCII码
c1=getchar();//注意输入时不需要空格间隔
c2=getchar();
putchar(c1);
putchar(' ');
putchar(c2);
cout<<endl<<c1<<" "<<c2<<endl;
return 0;
}
5、整型和字符变量是否任何条件可替代?
一般情况下,对无符号整型,范围0~255,该范围内ASCII码和字符一一对应,可以转换,不同类型可互相赋值。(课本P24)
6、算术运算&关系运算&逻辑运算
算术运算:对常量、变量、函数等进行加减乘除求模等算术运算,结果一

这篇博客主要涵盖了C++编程的基础练习,包括表达式和表达式语句的使用,数学计算如圆的周长、面积以及几何体的表面积、体积,温度转换,字符与整型变量的关系,算术、关系和逻辑运算的规则,布尔值的表示,逻辑表达式的计算,以及一些实际应用问题如函数、数据处理、排序、利润计算、最大公约数和最小公倍数等。
&spm=1001.2101.3001.5002&articleId=109741016&d=1&t=3&u=fc0b3017ac1b45fe8112c8085a260510)
1万+

被折叠的 条评论
为什么被折叠?



