经典练习程序代码.doc
文本预览下载声明
#include iostream.h
main()
{
//用SIZEOF计算常量长度
coutsizeof($)=sizeof($)endl;
coutsizeof(1)=sizeof(1)endl;
coutsizeof(1.5)sizeof(1.5)endl;
coutsizeof(\Good!\)=sizeof(Good!)endl;
//sizeof计算变量长度
int i=100;
char c=A;
float x=3.1416;
double p=0.1;
coutsizeof(i)=sizeof(i)endl;
coutsizeof(c)=sizeof(c)endl;
coutsizeof(x)=sizeof(x)endl;
coutsizeof(p)=sizeof(p)endl;
//用SIZEOF()计算表达式长度
coutsizeof(x+1.732)=sizeof(x+1.732)endl;
//
coutsizoef(char)=sizeof(char)endl;
coutsizoef(int)=sizeof(int)endl;
coutsizeof(float)=sizeof(float)endl;
coutsizeof(double)=sizeof(double)endl;
//用SIZEOF()算数组字节长
char str[]=This is a test;
int a[10];
double xy[10];
coutsizeof(str)=sizeof(str)endl;
coutsizeof(a)=sizeof(a)endl;
coutsizeof(xy)=sizeof(xy)endl;
//用SIZEOF 算自定类型字节长
struct st
{
short num;
float math_grade;
float Chinese_grade;
float sum_grade;
};
st student1;
coutsizeof(st)=sizeof(st)endl;
coutsizeof(student)=sizeof(student1)endl;
}
#include iostream.h
main()//测试表达式类型转换
{
int n=100,m;
double x=3.791,y;
coutn*x=n*xendl;
//赋值类型转换
m=x;
y=n;
coutm=mendl;
couty=yendl;
//强制转换
coutint(x)=int(x)endl;
cout(int)x=(int)xendl;
coutint(1.732+x)=int(1.732+x)endl;
cout(int)1.732=(int)1.732endl;
coutdouble(100)=double(100)endl;
//*/
}
-----------==================------------------------
#include iostream.h
main()
{
int score;
coutscore=;
cinscore;
if(score0||score100)
{
coutThe score is out of range!endl;
}
else if(score=90)
coutyou grage is a Aendl;
else if(score=80)
coutyou grade is a Bendl;
else if(score=70)
coutyou grade is a Cendl;
else if(score=60)
coutyou grade is a Dendl;
else
coutyou grade is a Eendl;
}
#include iostream.h
main()
{
int x,y,z;
char c1;
cinxc1y;
switch(c1)
{
case +:coutx+y=x+yendl;
break;
case -:coutx-y=x-yendl;
break;
case *:coutx*y=x*yendl;
break;
case /:coutx/y=x/yendl;
break;
case %:coutx%y=x%yendl;
default:coutWrong !endl;
}
}
---------------------------=========
显示全部