第二章
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)
{
评论0
最新资源