您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > C++经典练习程序代码
#includeiostream.hmain(){//用SIZEOF计算常量长度coutsizeof('$')=sizeof('$')endl;coutsizeof(1)=sizeof(1)endl;coutsizeof(1.5)sizeof(1.5)endl;coutsizeof(\Good!\)=sizeof(Good!)endl;//sizeof计算变量长度inti=100;charc='A';floatx=3.1416;doublep=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()算数组字节长charstr[]=Thisisatest;inta[10];doublexy[10];coutsizeof(str)=sizeof(str)endl;coutsizeof(a)=sizeof(a)endl;coutsizeof(xy)=sizeof(xy)endl;//用SIZEOF算自定类型字节长structst{shortnum;floatmath_grade;floatChinese_grade;floatsum_grade;};ststudent1;coutsizeof(st)=sizeof(st)endl;coutsizeof(student)=sizeof(student1)endl;}#includeiostream.hmain()//测试表达式类型转换{intn=100,m;doublex=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;//*/}-----------==================------------------------#includeiostream.hmain(){intscore;coutscore=;cinscore;if(score0||score100){coutThescoreisoutofrange!endl;}elseif(score=90)coutyougrageisaAendl;elseif(score=80)coutyougradeisaBendl;elseif(score=70)coutyougradeisaCendl;elseif(score=60)coutyougradeisaDendl;elsecoutyougradeisaEendl;}#includeiostream.hmain(){intx,y,z;charc1;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;}}---------------------------=======================================#includeiostream.hmain(){//for(inti=1;i=10;i++)couti;coutendl;//for(intj=10;j=1;j--)coutj;coutendl;//for(intk=1;k=10;k=k+2)coutk;coutendl;//for(charc='A';c='Z';c++)coutc;coutendl;for(floatx=0;x=1.0;x=x+0.1)coutx;coutendl;//for(floatx1=0;x1=1.0+0.1/2;x1=x1+0.1)coutx1;coutendl;//ints=0;for(intn=1;n=100;n++)s=s+n;couti=is=sendl;}-----==========================--------#includeiostream.hmain(){ints=0,n=0;do{n++;s+=n;}while(n100);couts=sendl;//doublex,sum=0.0;do{coutx=;cinx;sum+=x;}while(x!=0);coutsum=sumendl;}---============================-----------#includeiostream.hmain(){//打印乘法九九表for(inti=1;i=9;i++){//couti;for(intj=0;j=9;j++)cout'\t'i*j=i*j;coutendl;}}---------========================--------------------#includeiostreamusingnamespacestd;intmain(){inta[2][3],i,j;//从键盘输入数组元素for(i=0;i2;i++)for(j=0;j3;j++){couta[i][j]=;cina[i][j];}//showarraydatafor(i=0;i2;i++){for(j=0;j3;j++){couta[i][j];}coutendl;}//找出数组最大者,及下标inth,l,Max=a[0][0];for(i=0;i2;i++)for(j=0;j3;j++)if(Maxa[i][j]){Max=a[i][j];h=i;l=j;}coutMax=a[h][l]=Maxendl;return0;}---=============================----------------------#includeiostreamusingnamespacestd;#definen8intmain(){//指针与引用的测试inta,b,c,*ip;//a=100;ip=&a;couta=aendl;cout*ip=*ipendl;coutip=ipendl;ip=&b;b=200;coutb=bendl;cout*ip=*ipendl;coutip=ipendl;ip=&c;*ip=a+b;coutc=cendl;cout*iP=*ipendl;coutip=ipendl;return0;}-----------=============================---------------------#includeiostream.h//#includestring//usingnamespacestd;intmain(){//inti;structstudent{intnum;charname[20];floatmaths;floatphysics;floatchemistry;doubletotal;};studentst[3];coutnumnamemathsphysicschemistrytotalendl;for(i=0;i3;i++){couti+1;cinst[i].num;cinst[i].name;cinst[i].maths;cinst[i].physics;cinst[i].chemistry;}for(i=0;i3;i++)st[i].total=st[i].maths+st[i].physics+st[i].chemistry;for(i=0;i3;i++){coutst[i]:;coutst[i].num'\t';coutst[i].name'\t';coutst[i].maths'\t';coutst[i].physics'\t';coutst[i].chemistry'\t';coutst[i].totalendl;}return0;}----=============================--------------#includeiostream.h//#includestring//usingnamespacestd;intmain(){//structhuman{charname[10];intsex;intage;};structhumanx={WangPing,1,30},*p=&x;cout(*p).name=(*p).nameendl;cout(*p).sex=(*p).sexendl;cout(*p).age=(*p).ageendl;cout====================endl;p=newhuman;//coutp-name=;cinp-name;coutp-sex=;cinp-sex;coutp-age=;cinp-age;coutendl====================endl;coutp-name=p-nameendl;coutp-sex=p-sexendl;coutp-age=p-ageendl;deletep;return0;}--------------============================------------#includeiostream.hmain(){structdate{intyear;intmonth;intday;};structbaby{intnum;floatweight;datebirthday;;};babyb1={10001,10,{2002,12,25}};coutb1.numb1.num
本文标题:C++经典练习程序代码
链接地址:https://www.777doc.com/doc-5592311 .html