欢迎来到得力文库 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
得力文库 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    C++经典程序代码大全1362.pdf

    • 资源ID:79337019       资源大小:2.47MB        全文页数:134页
    • 资源格式: PDF        下载积分:20金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要20金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    C++经典程序代码大全1362.pdf

    文档/根据半径计算圆的周长和面积#include const float PI=3.1416;/声明常量(只读变量)PI 为 3.1416 float fCir_L(float);/声明自定义函数 fCir_L()的原型 float fCir_S(float);/声明自定义函数 fCir_S()的原型 /以下是 main()函数 main()float r,l,s;/声明 3 个变量 coutr;/键盘输入 l=fCir_L(r);/计算圆的周长,赋值给变量 l s=fCir_S(r);/计算圆的面积,赋值给变量 s coutl=l;/显示计算结果 coutns=0.0)/如果参数大于 0,则计算圆的周长 z=2*PI*x;return(z);/返回函数值 /定义计算圆的面积的函数 fCir_S()float fCir_S(float x)float z=-1.0;/声明局部变量 if(x=0.0)/如果参数大于 0,则计算圆的面积 z=PI*x*x;return(z);/返回函数值 /*Program:P1-2.CPP Written by:Hap Date written:02:11:10*/#include void main(void)double s1,s2,s3;s1=1.5;/*对变量 s1 赋值*/couts1=s1endl;/*对变量 s2 赋值*/s2=2.5;couts2=s2endl;s3=/*对变量 s3 赋值*/3.5;couts3=s3endl;couts1+s2+s3=s1+s2+s3endl;/计算并显示 /计算并显示 couts1+s2+s3=s1+s2+s3endl;#include main()double r=1.0;文档 coutr=rendl;double l;l=2*3.1416*r;/计算圆的周长,赋值给变量 l coutl=lendl;/显示圆的周长 double s=3.1416*r*r;/计算圆的面积,赋值给变量 s couts=sendl;/显示圆的面积 coutr;/键盘输入 l=2*3.1416*r;/计算圆的周长,赋值给变量 l coutl=lendl;/显示圆的周长 s=3.1416*r*r;couts=sendl;/显示圆的面积#include /包含 iostream.h 头文件 void main()/输出字符常量、变量和字符串 char c1=A;coutW;coutc1endl;coutThis is a test.endl;cout-endl;/输出整型常量、变量和表达式 int n=100;cout10;coutn;cout2*nendl;/输出整型表达式 cout-endl;/输出浮点型常量、变量和表达式 double pi=3.1415926,r=10.0,s=pi*r*r;coutpiendl;coutr;couts;cout2*r*piendl;/输出浮点型表达式 cout-endl;/一个 cout 可以输出多项数据 coutW c1endl;coutThis is a test.endl;coutpi=pi r=r s=sendl;#include /包含 iostream.h 头文件 main()/输入输出字符 char c;cinc;coutc=cn;coutn=nx;文档 coutx=xendl;/输入提示 coutn;coutn=nendl;/多项输入 coutc n xcnx;coutc=c n=n x=xendl;#include /包含 iostream.h 头文件 main()/声明整型变量 int a,b;/从键盘上为整型变量赋值 couta;coutb;/整型数的算术运算 couta+b=a+bendl;couta-b=a-bendl;couta*b=a*bendl;couta/b=a/bendl;couta%b=a%bendl;/测试溢出 short n=32767,m;/n 取 short 类型的最大值 coutn=nendl;m=n+1;/引起溢出 coutn+1=mendl;#include /包含 iostream.h 头文件 main()/声明变量,并初始化 int a=010,b=10,c=0X10;/以十进制形式显示数据 coutDEC:;cout a=a;cout b=b;cout c=cendl;/以八进制形式显示数据 coutOCT:;coutoct;/指定八进制输出 cout a=a;cout b=b;cout c=cendl;/以十六进制形式显示数据 coutHEX:;couthex;/指定十六进制输出 文档 cout a=a;cout b=b;cout c=cendl;/八、十和十六进制数混合运算并输出 couta+b+c=;coutdec;/恢复十进制输出 couta+b+cendl;/测试八、十和十六进制输入 couta;coutb;coutc;coutDEC:decendl;/指定十进制输出 couta=aendl;coutb=bendl;coutc=cendl;#include /包含 iostream.h 头文件#include /iomanip.h 头文件包含 setprecision()的定义 main()/float 型变量的声明、输入、计算和输出 float fx,fy;coutfx;coutfy;coutfx+fy=fx+fyendl;coutfx-fy=fx-fyendl;coutfx*fy=fx*fyendl;coutfx/fy=fx/fyendlendl;/coutfx%fy=fx%fyendl;Error!/double 型变量的声明、输入、计算和输出 float dx,dy;coutdx;coutdy;coutdx+dy=dx+dyendl;coutdx-dy=dx-dyendl;coutdx*dy=dx*dyendl;coutdx/dy=dx/dyendlendl;/coutfx%fy=fx%fyendl;Error!/测试 float 和 double 类型数据的有效位 fx=10.0;fy=6.0;float fz=fx/fy;dx=10.0;dy=6.0;double dz=dx/dy;coutfz=;coutsetprecision(20)fx/fy=fzendl;coutdz=;coutsetprecision(20)dx/dy=dzendlendl;/float 型溢出 float x=3.5e14;coutx=xendl;coutx*x=x*xendl;文档 coutx*x*x=x*x*xendl;#include /包含 iostream.h 头文件 main()/字符类型变量的声明 char c1=A;char c2;/字符数据的运算及输出 c2=c1+32;coutc1=c1endl;coutc2=c2endl;/输出字符及 ASCII 码 coutc1:int(c1)endl;coutc2:int(c2)endl;cout$:int($)endl;/输入字符 coutc1 c2c1c2;coutc1=c1 c2=c2endl;#include /包含 iostream.h 头文件 main()char c1=a,TAB=t;/阵铃一声 coutc1endl;/使用水平制表符 cout1TAB2TAB3TAB4endl;/使用双引号 coutHe said Thank you.endl;/使用回车换行 coutabcndefn;#include /包含 iostream.h 头文件 main()/声明 bool 变量,并初始化 bool flag1=false,flag2=true;/输出布尔常量和变量 coutfalse:falseendl;couttrue:trueendl;coutflag1=flag1endl;coutflag2=flag20;/存放关系运算结果 coutflag1=flag1endl;flag2=flag1;/bool 类型变量相互赋值 coutflag2=flag2endl;文档 /布尔变量超界处理 flag1=100;coutflag1=flag1endl;flag2=-100;coutflag2=flag2endl;#include const double PI=3.1416;/声明常量(const 变量)PI 为 3.1416 main()/声明 3 个变量 double r,l,s;/输入圆的半径 coutr;/计算圆的周长 l=2*PI*r;coutl=lendl;/计算圆的面积 s=PI*r*r;couts=sendl;#include main()/定义枚举类型,并指定其枚举元素的值 enum color RED=3,YELLOW=6,BLUE=9 ;/声明枚举变量 a 和 b,并为枚举变量 a 赋初值 enum color a=RED;color b;/合法,与 C 语言不同 /输出枚举常量 coutRED=REDendl;coutYELLOW=YELLOWendl;coutBLUE=BLUEendl;/枚举变量的赋值和输出 b=a;a=BLUE;couta=aendl;coutb=bendl;/a=100;错误!/a=6 也错误!/枚举变量的关系运算 b=BLUE;/枚举变量的赋值运算 coutab=(ab)endl;#include const double PI=3.1416;/声明常量(const 变量)PI 为 3.1416 文档 main()/声明 3 个变量 double r=3,l,s;/计算圆的周长 l=2*PI*r;coutl=lendl;/计算圆的面积 s=PI*r*r;couts=sendl;/验证赋值误差 int il,is;il=l;is=s;coutil=ilendl;coutis=isendl;#include main()/变量声明 char c;double x,y;/测试自增 cout+E and E+:endl;c=B;coutc=+cendl;/输出 c=C c=B;coutc=c+endl;/输出 c=B x=1.5;y=5+x;/加号后的空格不能少 couty=yendl;/输出 y=7.5 x=1.5;y=5+x+;couty=yendl;/输出 y=6.5 cout-endl;/测试自减 cout-E and E-:endl;c=B;coutc=-cendl;/输出 c=A c=B;coutc=c-endl;/输出 c=B x=1.5;y=5+-x;couty=yendl;/输出 y=5.5 x=1.5;y=5+x-;couty=yendl;/输出 y=6.5#include main()int a=3,b=2;/输出关系表达式 文档 coutabendl;cout(ab)b)=b)(a=b)(a!=b)endl;bool flag=2*ab+10;coutflag=flag;#include main()float a=3.5,b=2.1,c=0;couta=a b=b c=cendl;/与运算 couta&b=(a&b)endl;/输出 1 couta&c=(a&c)endl;/输出 0 /或运算 couta|b=(a|b)endl;/输出 1 couta|c=(a|c)endl;/输出 1 /非运算 cout!a=!aendl!c=!c=0&a=5;/变量 a 在0,5区间内 cout0&a=5=flagendl;/输出 1 /算术运算、关系运算和逻辑运算 cout2*b+2|ab+3=2*b+2|ab+3)endl;/输出 1#include main()/按位与运算 cout24&12=(24&12)endl;/按位异或运算 cout2412=(2412)endl;/按位或运算 cout24|12=(24|12)endl;/按位取反运算 cout24=(24)endl;/左移位运算 cout53=(53)endl;cout-53=(-53)endl;/右移位运算 cout3=3)endl;cout3=3)endl;#include main()int a=1,b=1,c=3;/显示 a,b,c 的值 couta=a b=b c=cendl;/计算显示(1)b+=a+2*c%5;的结果 b+=a+2*c%5;/相当于表达式语句 b=b+(a+2*c%5);cout(1)b=bendl;文档 /计算显示(2)a=c-2*b;的结果 a=1,b=1,c=3;a=c-2*b;/相当于表达式语句 a=a(c-2*b);cout(2)a=aendl;/计算显示(3)a*=b=c=3;的结果 a=1,b=1,c=3;a*=b=c=3;/相当于语句组 c=3;b=c;a=a*b;cout(3)a=a b=b c=cendl;/计算显示(4)a+=b+=c;的结果 a=1,b=1,c=3;a+=b+=c;/相当于语句组 b=b+c;a=a+b;cout(4)a=a b=b c=cendl;/计算显示(5)a-=b=+c+2;的结果 a=1,b=1,c=3;a-=b=+c+2;/相当于语句组+c;b=b+c+2;a=a-b;cout(5)a=a b=b c=cendl;#include 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;/用 sizeof 计算各类型的字节长度 coutsizeof(char)=sizeof(char)endl;coutsizeof(int)=sizeof(int)endl;coutsizeof(float)=sizeof(float)endl;coutsizeof(double)=sizeof(double)endl;/用 sizeof 计算数组的字节长度 char str=This is a test.;int a10;double xy10;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(student1)=sizeof(student1)endl;#include main()/声明变量语句中使用顺序运算 int x,y;/计算中使用顺序运算 x=50;y=(x=x-5,x/5);coutx=xendl;couty=yendl;#include 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+x=(int)1.723+xendl;coutdouble(100)=double(100)endl;#include main()float a,b,s;couta bab;/利用 cin 从键盘上为变量 a,b 赋值 s=a;if(ab)s=b;/if 语句中只有这一个语句,可省略花括号 s=s*s;/变量 s 中保存 a,b 中较大的一个数的平方 couts=s;#include main()int x,y;文档 coutx;if(x=0)/满足条件执行 y=2*x;couty=y;/输出结果 else /不满足条件执行 y=x*x;couty=y;/输出结果#include main()int a,b,c;int smallest;couta b cabc;if(a=b)/外层条件语句 if(a=c)/内层条件语句 smallest=a;else smallest=c;else if(b=c)/内层条件语句 smallest=b;else smallest=c;coutSmallest=smallestendl;#include main()int score;/从键盘上输入分数 coutscore;/用带 else if 的条件语句判断处理 if(score100)coutThe score is out of range!=90)coutYour grade is a A.=80)coutYour grade is a B.=70)coutYour grade is a C.=60)coutYour grade is a D.endl;else coutYour grade is a E.endl;文档#include main()int n;coutn;if(n=0&n=100&n%2=0)coutn=nendl;else coutThe n is out of range!endl;#include main()int a,b,Max;/输入数据 couta;coutb;/找出较大值 Max=ab?a:b;coutMax=Maxendl;#include main()int a,b;/输入数据 couta;coutb;/除法判断 if(b!=0&a%b=0)coutb divides aendl;couta/b=a/bendl;else coutb does not divide aendl;#include main()/x,y 为操作数,c 为运算符 int x,y,z;char c1;cinxc1y;/c1 /多路选择语句选择不同表达式计算语句 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;break;default:coutWrong!endl;/当不符合上述情况时执行本子句#include float x=365.5;/声明全局变量 main()int x=1,y=2;double w=x+y;double x=1.414,y=1.732,z=3.14;coutinner:x=xendl;coutinner:y=yendl;coutinner:z=zendl;coutouter:w=wendl;cout:x=:xendl;/访问重名的全局变量 coutouter:x=xendl;coutouter:y=yendl;coutouter:w=wendl;/coutinner:z=zendl;无效 cout:x=:xendl;/访问重名的全局变量#include main()/显示 1,2,3.10 for(int i=1;i=10;i+)couti;cout=1;j-)coutj;coutendl;/显示 1,3,5.9 for(int k=1;k=10;k=k+2)coutk;coutendl;/显示 ABC.Z for(char c=A;c=Z;c+)coutc;coutendl;/显示 0,0.1,0.2.1.0 for(float x=0;x=1.0;x=x+0.1)coutx;coutendl;文档 /显示 0,0.1,0.2.1.0 for(float x1=0;x1=1.0+0.1/2;x1=x1+0.1)coutx1;coutendl;/计算 s=1+2+3.+100 int s=0;for(int n=1;n=100;n+)s=s+n;couts=sendl;#include main()/计算 s=1+2+3.+100 int s=0,n=1;while(n=100)s=s+n;n+;couts=sendl;/累加键盘输入的数据 double x,sum=0.0;coutx;while(x!=0)sum+=x;coutx;coutsum=sumendl;#include main()/计算 s=1+2+3.+100 int s=0,n=0;do n+;s+=n;while(n100);couts=sendl;/累加键盘输入的数据 double x,sum=0.0;do coutx;sum+=x;while(x!=0);coutsum=sumendl;#include main()/计算和打印打印乘法九九表 for(int i=1;i=9;i+)couti;文档 for(int j=1;j=9;j+)coutti*j=i*j;coutendl;#include main()int x,sum=0;/定义标号 L1 L1:coutx;if(x=-1)goto L2;/无条件转移语句,转到 L2 语句处 else sum+=x;goto L1;/无条件转移语句,转到 L1 语句处 /定义标号 L2 L2:coutsum=sumendl;#include main()/累加键盘输入的数据 double x,sum=0.0;while(1)coutx;if(x=0)break;sum+=x;coutsum=sumendl;#include main()int i;for(i=1;i=20;i+)if(i%3=0)/能被 3 整除的整数,返回进行下次循环 continue;couti;coutendl;#include main()/声明数组和变量 int a5,i,sum;double avg;/从键盘上循环为数组赋值 for(i=0;i5;i+)coutaiai;文档 /直接显示数组元素 couta0a1a2a3a4endl;/利用 for 循环显示数组各元素的值 for(i=0;i5;i+)coutai ;coutendl;/计算数组元素之和,并显示计算结果 sum=a0+a1+a2+a3+a4;coutsum=sumendl;/利用循环计算数组的累加和 for(sum=0,i=0;i5;i+)sum+=ai;/显示累加和及平均值 coutsum=sumendl;avg=sum/5.0;coutavg=avgendl;#include main()int i,max,index,a5;/从键盘上为数组赋值 for(i=0;i=4;i+)coutaiai;/利用循环遍历数组,找出最大值的元素及其下标 max=a0;for(i=0;i=4;i+)if(maxai)max=ai;index=i;coutnMax=max index=index;#include#define size 5 main()/声明变量 int i,j;float t,asize;/从键盘上为数组赋值 for(i=0;isize;i+)coutaiai;文档 /对数组按从小到大顺序排序 for(i=0;isize-1;i+)for(j=i+1;jaj)t=ai;ai=aj;aj=t;/显示排序结果 for(i=0;isize;i+)coutai;coutendl;/输入要查找的数据 int value;int found;/找到为 1,否则为 0 int low,high,mid;for(i=1;i=3;i+)coutvalue;/二分法查找数组 a found=0;low=0;high=size-1;while(low=high)mid=(high+low)/2;if(amid=value)found=1;break;if(amidvalue)low=mid+1;else high=mid-1;if(found)coutThe valu found at:amid=amidendl;else coutThe value is not found!endl;#include main()/声明变量 int i,j;float t,a5;/从键盘上为数组赋值 for(i=0;i=4;i+)coutaiai;文档 /对数组按从大到小顺序排序 for(i=0;i=3;i+)for(j=i+1;j=4;j+)if(ai=aj)t=ai;ai=aj;aj=t;/显示排序结果 for(i=0;i=4;i+)coutai;#include main()/声明二维数组及变量 int a23,i,j;/从键盘上为数组 a 赋值 for(i=0;i2;i+)for(j=0;j3;j+)coutaijaij;/显示数组 a for(i=0;i2;i+)for(j=0;j3;j+)coutaij ;coutendl;/找出该数组的最大元素及其下标 int h,l,Max=a00;for(i=0;i2;i+)for(j=0;j3;j+)if(Maxaij)Max=aij;h=i;l=j;coutMax:ahl=ahlendl;#include main()/声明字符数组和变量 char str6;int i;文档 /从键盘上输入字符串 coutstr;coutstrendl;/按数组和下标变量两种方式显示字符数组 coutstrendl;for(i=0;i6;i+)coutstri;cout=0;i-)coutstri;coutendl;/将字符数组变成大写字母后输出 for(i=0;i=5;i+)stri-=32;/小写字母转换成大写字母 coutstrendl;/显示字符串#include main()/声明变量和指针变量 int a,b,c,*ip;/指针变量 ip 指向变量 a a=100;ip=&a;/使指针变量 ip 指向变量 a couta=aendl;cout*ip=*ipendl;coutip=ipendl;/指针变量 ip 指向变量 b ip=&b;/使指针变量 ip 指向变量 b b=200;coutb=bendl;cout*ip=*ipendl;coutip=ipendl;/指针变量 ip 指向变量 c ip=&c;/使指针变量 ip 指向变量 b *ip=a+b;coutc=cendl;cout*ip=*ipendl;coutip=ipendl;#include main()/声明数组、变量和指针变量 int a23,i,j;int*ip;/从键盘上为数组 a 赋值 for(i=0;i2;i+)/为数组 a 赋值 for(j=0;j3;j+)coutaijaij;/利用下标变量显示数组 a for(i=0;i2;i+)for(j=0;j3;j+)coutaij ;coutendl;/利用指针变量显示数组 a ip=&a00;for(i=0;i2;i+)for(j=0;j3;j+)coutaij=;coutip ;cout*ipendl;ip+;#include main()/声明数组、变量和指针变量 int a=1,2,3,4,5,6;int*ip1,*ip2;/测试指针的赋值运算 ip1=a;ip2=ip1;cout*ip1=(*ip1)endl;cout*ip2=(*ip2)endl;/测试指针的自增自减运算和组合运算 ip1+;ip2+=4;cout*ip1=(*ip1)endl;cout*ip2=(*ip2)ip1;coutip1=nendl;coutip2!=NULL=(ip2!=NULL)endl;/指针变量之间的减法 n=ip2-ip1;coutip2-ip1=nendl;#include main()/声明字符型数组和指针变量 char str10;char*strip=str;/输入输出 文档 coutstr;/用字符数组输入字符串 coutstr=strendl;coutstrip=stripendl;coutstrip;/用字符指针变量输入字符串 coutstr=strendl;coutstrip=stripendl;/利用指针变量改变其指向字符串的内容 *(strip+2)=l;coutstr=strendl;coutstrip=stripendl;/动态为字符型指针变量分配内存 strip=new char(100);coutstrip;/用字符指针变量输入字符串 coutstr=strendl;coutstrip=stripendl;#include main()/声明用于存放运动员号码的数组 int h=1001,1002,1003,1004;/声明用于存放运动员成绩的数组 float x=12.3,13.1,11.9,12.1;/声明用于存放运动姓名的字符型指针数组 char*p=Wang hua,Zhang jian,Li wei,Hua ming;/i,j,it 是用做循环控制变量和临时变量 int i,j,it;/ft 用做暂存变量 float ft;/pt 为字符型指针变量用做暂存指针变量 char*pt;/用选择法对数组 x 进行排序,并相应调整数组 h 和 p 中的数据 for(i=0;i=3;i+)for(j=i+1;j=xj)ft=xi,xi=xj,xj=ft;it=hi,hi=hj,hj=it;pt=pi,pi=pj,pj=pt;/以下打印排序结果 for(i=0;i=3;i+)couthi,pi,xiendl;#include main()/声明指针数组 char*colors=Red,Blue,Yellow,Green;/指向指针的指针变量 char*pt;/通过指向指针的变量访问其指向的内容 pt=colors;文档 for(int i=0;i=3;i+)coutpt=ptendl;cout*pt=*ptendl;cout*pt=*ptendl;pt+;#include main()/定义结构类型 struct books char title20;char author15;int pages;float price;/声明结构变量 struct books Zbk=VC+,Zhang,295,35.5;books Wbk;/对结构变量的输出 coutZbk:endl;coutZbk.title endl;coutZbk.authorendl;coutZbk.pagesendl;coutZbk.priceendl;cout-endl;/对结构成员的运算 Zbk.pages+=10;Zbk.price+=0.5;coutZbk.pages=Zbk.pagesendl;coutZbk.price=Zbk.priceendl;cout-endl;/对结构变量的输入输出 coutWbk.title;coutWbk.author;coutWbk.pages;coutWbk.price;coutWbk:endl;coutWbk.title endl;coutWbk.authorendl;coutWbk.pagesendl;coutWbk.priceendl;cout-endl;/结构变量之间的相互赋值 books temp;temp=Wbk;couttemp:endl;couttemp.titleendl;couttemp.authorendl;文档 couttemp.pagesendl;couttemp.priceendl;#include main()int i;/定义结构类型 struct student int num;char name10;float maths;float physics;float chemistry;double total;/声明结构数组 st student st3;/从键盘上为结构数组输入值 cout num name maths physics chemistry endl;for(i=0;i3;i+)couti+1sti.num;cinsti.name;cinsti.maths;cinsti.physics;cinsti.chemistry;/计算每个学生的总成绩 for(i=0;i3;i+)sti.total=sti.maths+sti.physics+sti.chemistry;/输出结构数组各元素的值 for(i=0;i3;i+)coutsti:;coutsti.numt;coutsti.namet;coutsti.mathst;coutsti.physicst;coutsti.chemistryt;coutsti.totalendl;#include main()/定义结构类型 struct human char name10;int sex;int age;/声明结构变量和结构指针变量,并初始化 文档 struct human x=WangPing,1,30,*p=NULL;/结构指针变量指向对象 p=&x;/显示结构变量的值 coutx.name=x.nameendl;coutx.sex=x.sexendl;coutx.age=x.ageendl;/利用结构指针显示结构对象中的数据 cout(*p).name=(*p).nameendl;cout(*p).sex=(*p).sexendl;cout(*p).age=(*p).ageendl;coutname=nameendl;coutsex=sexendl;coutage=ageendl;/通过结构指针为结构对象输入数据 cout(*p).name;cout(*p).sex;cout(*p).age;/显示结构变量的值 coutx.name=x.nameendl;coutx.sex=x.sexendl;coutx.age=x.ageendl;include main()/定义结构类型 struct human char name10;int sex;int age;/声明结构变量和结构指针,并初始化 struct human x=WangPing,1,30,*p=&x;/利用结构指针显示结构中的数据 cout(*p).name=(*p).nameendl;cout(*p).sex=(*p).sexendl;cout(*p).age=(*p).ageendl;cout-endl;/利用 new 运算符为 p 分配内存 p=new human;/从键盘上为 p 指向的结构对象赋值 coutname=;cinp-name;coutsex=;cinp-sex;coutage=;cinp-age;文档 cout-endl;/显示 p 所指结构对象的值 coutname=nameendl;coutsex=sexendl;coutage=ageendl;cout-endl;/显示结构变量的值 coutx.name=x.nameendl;coutx.sex=x.sexendl;coutx.age=x.ageendl;/释放 p 指向

    注意事项

    本文(C++经典程序代码大全1362.pdf)为本站会员(得****3)主动上传,得力文库 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知得力文库 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于得利文库 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

    © 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

    黑龙江省互联网违法和不良信息举报
    举报电话:0468-3380021 邮箱:hgswwxb@163.com  

    收起
    展开