没有合适的资源?快使用搜索试试~ 我知道了~
c++语言程序设计2011版参考答案.pdf
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 16 浏览量
2022-01-04
15:26:36
上传
评论
收藏 22KB PDF 举报
温馨提示
试读
14页
c++语言程序设计2011版参考答案.pdf
资源推荐
资源详情
资源评论
第二章
1.编写程序,输出各种基本类型及其派生类型所占用的字节长度,请使用 sizeof 运算符。
解:
#include <iostream>
Using namespace std ;
Void main()
{ cout<<" 基本类型 "<<' '<<" 派生类型 "<<' '<<" 所占字长 "<<endl;
cout<<" 整型 (int)"<<' '<<" 有 (无 )符号短整数 short/short int/signed short int((unsigned
short)int)"<<' '<<"2"<<endl;
cout<<" 整型 (int)"<<' '<<" 有(无 )符号整数 int/signed int((unsigned)int)"<<' '<<"4"<<endl;
cout<<" 整型 (int)"<<' '<<" 有 ( 无 )符 号长整 数 long/long int/signed long int((unsigned
long)int)"<<' '<<"4"<<endl;
cout<<" 字符型 (char)"<<' '<<" 有(无)符号字符 (signed)char"<<' '<<"1"<<endl;
cout<<" 浮点型 "<<' '<<" 单精度数 float"<<' '<<"4"<<endl;
cout<<" 浮点型 "<<' '<<" 双精度数 double"<<' '<<"8"<<endl;
cout<<" 浮点型 "<<' '<<" 长双精度数 long double"<<' '<<"8"<<endl;
cout<<" 逻辑型 (bool)"<<' '<<' '<<endl;
cout<<" 空值型 (void)"<<' '<<endl;
}
2.根据以下函数关系,对于给定的每个 x 值,求出 y 值。请编写此程序。
y=x-1 其中 x= -5 ,1
y=x*(x+2) 其中 x= 2,10
y= x*2 其中 x= 3.5,123.456
解:
#include<iostream>
using namespace std ;
double(void ) main()
{
double x;
cin>>x;
double y;
if(x==-5||x==1)
{
y=x-1;
cout<<y;
}
if(x==2||x==10)
{
y=x*(x+2);
cout<<y;
}
if(x==3.5||x==123.456)
{
y=2*x;
cout<<y;
}
}
3. 一 直 一 个 三 角 形 中 三 条 边 的 产 度 分 别 为 a ﹑ b 和 c , 编 写 程 序 利 用 公 式 sqrt( 开
方)(s(s-a)(s-b)(s-c)) 三角形的面积 ,其中 s=(a+b+c)/2 要求边长 a﹑ b﹑c 的值由键盘输入,并
保证任意两边长度和大于等于第三条边。
解:
#include<iostream>
#include<cmath>
using namespace std ;
void main()
{
double a,b,c,y,s;
cin>>a>>b>>c;
s=(a+b+c)/2;
if(a+b>=c&&a+c>=b&&b+c>=a)
cout<<" 面积是 "<<sqrt(s*(s-a)*(s-b)*(s-c))<<endl;
}
第三章
二填空题
2. #include<iostream>
using namespace std;
void main()
{
int x=0,y=2,z=3;
switch (x){
case 0:switch (y){
case 1:cout<<'*';break;
case 2:cout<<'%';break;
}
case 1:switch (z){
case 1:cout<<'$';break;
case 2:cout<<'*';break;
default :cout<<'#';
}
}
}
输出结果为 %#
#include<iostream>
using namespace std;
void main()
{
int x=0,y=2,z=3;
switch (x){
case 0:switch (y){
case 1:cout<<'*';break;
case 2:cout<<'%';break;
}break;
case 1:switch (z){
case 1:cout<<'$';break;
case 2:cout<<'*';break;
default :cout<<'#';
}
}
}
输出结果为 %
#include<iostream>
using namespace std;
void main()
{
for(int n=1;n<=100;){
if(n%3==0)
continue ;
cout<<n<<endl;
n++;
}
}
输出结果: 1,2
#include<iostream>
using namespace std;
void main()
{
for(int n=1;n<=100;n++){
if(n%3==0)
continue ;
cout<<n<<endl;
}
}
输出结果:
比较以上两程序 考虑 coutinue 的用法
三、程序题
1.编写程序,实现输入一个整数,判断其能否被 3,5 ,7 整除,并输出以下信息:
(a)、能同时被 3,5,7 整除;
剩余13页未读,继续阅读
资源评论
BlueWatergg
- 粉丝: 3
- 资源: 11万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功