首页 c++经典代码大全

c++经典代码大全

举报
开通vip

c++经典代码大全#include//包含iostream.h头文件main(){//声明变量,并初始化inta=010,b=10,c=0X10;//以十进制形式显示数据cout>a;cout>b;cout>c;cout>dx;cout>dy;cout<

c++经典代码大全
#include//包含iostream.h头文件main(){//声明变量,并初始化inta=010,b=10,c=0X10;//以十进制形式显示数据cout<<"DEC:";cout<<"a="< 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 达式计算语句switch(c1){case'+':cout<floatx=365.5;//声明全局变量main(){intx=1,y=2;doublew=x+y;{doublex=1.414,y=1.732,z=3.14;cout<<"inner:x="<=1;j--)cout<main(){//计算s=1+2+3...+100ints=0,n=1;while(n<=100){s=s+n;n++;}cout<<"s="<>x;while(x!=0){sum+=x;cout<<"x=";cin>>x;}cout<<"sum="<main(){//计算s=1+2+3...+100ints=0,n=0;do{n++;s+=n;}while(n<100);cout<<"s="<>x;sum+=x;}while(x!=0);cout<<"sum="<main(){//计算和打印打印乘法九九表for(inti=1;i<=9;i++){cout<main(){intx,sum=0;//定义标号L1L1:cout<<"x=";cin>>x;if(x==-1)gotoL2;//无条件转移语句,转到L2语句处elsesum+=x;gotoL1;//无条件转移语句,转到L1语句处//定义标号L2L2:cout<<"sum="<main(){//累加键盘输入的数据doublex,sum=0.0;while(1){cout<<"x=";cin>>x;if(x<=0)break;sum+=x;}cout<<"sum="<main(){inti;for(i=1;i<=20;i++){if(i%3==0)//能被3整除的整数,返回进行下次循环continue;cout<main(){//声明数组和变量inta[5],i,sum;doubleavg;//从键盘上循环为数组赋值for(i=0;i<5;i++){cout<<"a["<>a[i];}//直接显示数组元素cout<main(){inti,max,index,a[5];//从键盘上为数组赋值for(i=0;i<=4;i++){cout<<"a["<>a[i];}//利用循环遍历数组,找出最大值的元素及其下标max=a[0];for(i=0;i<=4;i++){if(max#definesize5main(){//声明变量inti,j;floatt,a[size];//从键盘上为数组赋值for(i=0;i>a[i];}//对数组按从小到大顺序排序for(i=0;ia[j]){t=a[i];a[i]=a[j];a[j]=t;}//显示排序结果for(i=0;i>value;//二分法查找数组afound=0;low=0;high=size-1;while(low<=high){mid=(high+low)/2;if(a[mid]==value){found=1;break;}if(a[mid]main(){//声明变量inti,j;floatt,a[5];//从键盘上为数组赋值for(i=0;i<=4;i++){cout<<"a["<>a[i];}//对数组按从大到小顺序排序for(i=0;i<=3;i++)for(j=i+1;j<=4;j++)if(a[i]<=a[j]){t=a[i];a[i]=a[j];a[j]=t;}//显示排序结果for(i=0;i<=4;i++)cout<main(){//声明二维数组及变量inta[2][3],i,j;//从键盘上为数组a赋值for(i=0;i<2;i++)for(j=0;j<3;j++){cout<<"a["<>a[i][j];}//显示数组afor(i=0;i<2;i++){for(j=0;j<3;j++){cout<main(){//声明字符数组和变量charstr[6];inti;//从键盘上输入字符串cout<<"str=";cin>>str;cout<=0;i--)cout<main(){//声明变量和指针变量inta,b,c,*ip;//指针变量ip指向变量aa=100;ip=&a;//使指针变量ip指向变量acout<<"a="<main(){//声明数组、变量和指针变量inta[2][3],i,j;int*ip;//从键盘上为数组a赋值for(i=0;i<2;i++)//为数组a赋值for(j=0;j<3;j++){cout<<"a["<>a[i][j];}//利用下标变量显示数组afor(i=0;i<2;i++){for(j=0;j<3;j++){cout<main(){//声明数组、变量和指针变量inta[]={1,2,3,4,5,6};int*ip1,*ip2;//测试指针的赋值运算ip1=a;ip2=ip1;cout<<"*ip1="<<(*ip1)<ip1;cout<<"ip2>ip1="<main(){//声明字符型数组和指针变量charstr[10];char*strip=str;//输入输出cout<<"str=";cin>>str;//用字符数组输入字符串cout<<"str="<>strip;//用字符指针变量输入字符串cout<<"str="<=x[j]){ft=x[i],x[i]=x[j],x[j]=ft;it=h[i],h[i]=h[j],h[j]=it;pt=p[i],p[i]=p[j],p[j]=pt;}//以下打印排序结果for(i=0;i<=3;i++)cout<main(){//声明指针数组char*colors[]={"Red","Blue","Yellow","Green"};//指向指针的指针变量char**pt;//通过指向指针的变量访问其指向的内容pt=colors;for(inti=0;i<=3;i++){cout<<"pt="<main(){//定义结构类型structbooks{chartitle[20];charauthor[15];intpages;floatprice;};//声明结构变量structbooksZbk={"VC++","Zhang",295,35.5};booksWbk;//对结构变量的输出cout<<"Zbk:"<>Wbk.title;cout<<"Wbk.author=";cin>>Wbk.author;cout<<"Wbk.pages=";cin>>Wbk.pages;cout<<"Wbk.price=";cin>>Wbk.price;cout<<"Wbk:"<main(){inti;//定义结构类型structstudent{intnum;charname[10];floatmaths;floatphysics;floatchemistry;doubletotal;};//声明结构数组ststudentst[3];//从键盘上为结构数组输入值cout<<"numnamemathsphysicschemistry"<>st[i].num;cin>>st[i].name;cin>>st[i].maths;cin>>st[i].physics;cin>>st[i].chemistry;}//计算每个学生的总成绩for(i=0;i<3;i++)st[i].total=st[i].maths+st[i].physics+st[i].chemistry;//输出结构数组各元素的值for(i=0;i<3;i++){cout<<"st["<main(){//定义结构类型structhuman{charname[10];intsex;intage;};//声明结构变量和结构指针变量,并初始化structhumanx={"WangPing",1,30},*p=NULL;//结构指针变量指向对象p=&x;//显示结构变量的值cout<<"x.name="<name="<name<sex="<sex<age="<age<>(*p).name;cout<<"sex:";cin>>(*p).sex;cout<<"age:";cin>>(*p).age;//显示结构变量的值cout<<"x.name="<main(){//定义结构类型structhuman{charname[10];intsex;intage;};//声明结构变量和结构指针,并初始化structhumanx={"WangPing",1,30},*p=&x;//利用结构指针显示结构中的数据cout<<"(*p).name="<<(*p).name<name=";cin>>p->name;cout<<"p->sex=";cin>>p->sex;cout<<"p->age=";cin>>p->age;cout<<"-------------------------"<name="<name<sex="<sex<age="<age<name<<'\t';cout<sex<<'\t';cout<age<main(){//定义一个包含指针成员的结构类型structtest{char*str;int*ip;}x;//使用结构变量x中的整型指针ipx.ip=newint;//分配1个单元*(x.ip)=100;cout<<"x.ip:"<main(){//定义date结构structdate{intyear;intmonth;intday;};//定义baby结构structbaby{intnum;floatweight;datebirthday;//date为结构类型};//声明baby结构变量并初始化babyb1={10001,10,{2002,12,25}};//下列是baby结构变量b1的引用。cout<<"b1.num="<main(){//定义名为list的递归结构structlist{charname[10];intsex;intage;list*next;//成员next为指向其自身结构的指针};//使用递归结构变量listL1={"WeiPing",1,35.5,NULL};cout<<"L1:"<main(){inti;//定义名为student的递归结构structstudent{charname[10];intmath;intcomputer;floatsum;student*next;//next成员是指向自身的结构指针};//用student声明3个结构指针变量structstudent*head,*tail,*temp;//申请第1块数据,并设置各结构指针的初值temp=newstructstudent;//申请内存head=temp;//头指针tail=head;//尾指针//循环为链表输入数据cout<<"\tnameMathComputer"<>temp->name;if(temp->name[0]!='*'){cin>>temp->math>>temp->computer;temp->sum=temp->math+temp->computer;temp->next=NULL;tail=temp;//设置链表尾指针}else{//以下是输入结束处理deletetemp;tail->next=NULL;break;}//为下一个学生申请内存temp->next=newstructstudent;temp=temp->next;//使处理指针temp指向新内存块}//将链表数据从头到尾打印出来cout<<"--------------------"<name<<","<math<<",";cout<computer<<","<sum<next;}}#includemain(){inti;//定义名为student的递归结构structstudent{charname[10];intmath;intcomputer;floatsum;student*forw;//forw成员是前指针student*next;//next成员是后指针};//用student声明3个结构指针变量structstudent*head,*tail,*temp;//申请第1块数据,并设置各结构指针的初值temp=newstructstudent;//申请内存head=temp;//头指针tail=head;//尾指针head->forw=NULL;//循环为链表 记录 混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载 输入数据cout<<"\tnameMathComputer"<>temp->name;if(temp->name[0]!='*'){cin>>temp->math>>temp->computer;temp->sum=temp->math+temp->computer;temp->next=NULL;tail=temp;//设置链表尾指针}else{//以下是输入结束处理deletetemp;tail->next=NULL;break;}//为下一个学生申请内存temp->next=newstructstudent;temp->next->forw=temp;//设置前指针temp=temp->next;//使处理指针temp指向新内存块}//将链表数据从头到尾打印出来cout<<"head------>tail:"<name<<","<math<<",";cout<computer<<","<sum<next;}//将链表数据从尾到头打印出来cout<<"tail------>head:"<name<<","<math<<",";cout<computer<<","<sum<forw;}}#includemain(){inti;//定义联合类型unionutag{charc;intk;floatx;};//声明联合变量unionutagu;//使用联合变量中的字符型成员u.c='*';cout<<"u.c="<main(){//定义结构类型,并为声明的结构变量赋初值structs_tag{shorti;floatx;}sx={100,3.1416};//定义联合类型,并为声明的联合变量赋初值unionu_tag{shorti;floatx;}ux={1000};//输出结构类型和结构变量的有关信息cout<<"sizeof(structs_tag)="<//定义结构类型structstudent{intnum;charname[20];floatgrade;};voidmain(void){//声明数组inti,size;charstr[]="Thisisastring.";intint_values[]={51,23,2,44,45,0,11};floatfloat_values[]={15.1,13.3,22.2,10.4,1.5};studentst_arr[]={101,"WangLin",92,102,"LiPing",85,103,"ZhaoMin",88};//显示char类型数组元素及其大小size=sizeof(str)/sizeof(char);cout<<"Numberofelementsinstr:";cout<//add()函数的定义,其有返回值doubleadd(doublex,doubley){doublez;z=x+y;cout<//定义符号函数sgn(),其返回值为int类型intsgn(doublex){if(x>0)return(1);//返回出口1if(x<0)return(-1);//返回出口2return(0);//返回出口3}//main()函数定义main(){doublex;inti;for(i=0;i<=2;i++){cout<<"x=";cin>>x;cout<<"sgn("<//函数原型语句可以在这里//定义main()函数main(){//max()函数原型声明语句floatmax(float,float);//变量声明语句floata,b,Max;//输入参数并计算cout<<"a=";cin>>a;cout<<"b=";cin>>b;Max=max(a,b);//调用max()函数cout<<"max("<y)?x:y;return(z);}#include//定义f()函数f(intx,inty)//f()的参数以值方式传递{++x;--y;cout<<"x="<//定义公共结构类型structstudent{intnum;charname[10];floatmaths;floatphysics;floatchemistry;doubletotal;};//定义结构输入函数input_Rec(structstudent*p)//参数为student类型的结构指针变量{cin>>p->num;cin>>p->name;cin>>p->maths;cin>>p->physics;cin>>p->chemistry;}//定义结构数据交换函数swap_Rec(structstudent*p1,structstudent*p2){structstudentx;//交换两个记录的数据x=*p1;*p1=*p2;*p2=x;}//输出结构的值put_Rec(structstudent*p){cout<num<<'\t';cout<name<<'\t';cout<maths<<'\t';cout<physics<<'\t';cout<chemistry<<'\t';cout<total<total=p1->maths+p1->physics+p1->chemistry;}//对3个学生的数据排序for(i=0;i<=2;i++)for(j=i+1;j<=2;j++)if(a[i].total//定义结构structstudent{charname[10];floatgrade;};//交换student类型的数据voidswap(student&x,student&y)//swap的参数为引用传递方式{studenttemp;temp=x;x=y;y=temp;}//返回student类型的引用,求优者student&max(student&x,student&y)//swap的参数为引用传递方式{return(x.grade>y.grade?x:y);}//显示student类型的数据voidshow(student&x)//show的参数为引用传递方式{cout<//参数带有默认值的函数disp(intx=1,inty=1,intz=1){cout<<"参数1:"<//计算字符串长度的函数intstr_len
本文档为【c++经典代码大全】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
机构认证用户
夕夕资料
拥有专业强大的教研实力和完善的师资团队,专注为用户提供合同简历、论文写作、PPT设计、计划书、策划案、各类模板等,同时素材和资料部分来自网络,仅供参考.
格式:doc
大小:483KB
软件:Word
页数:0
分类:理学
上传时间:2021-03-26
浏览量:16