c++经典代码大全[共125页]

上传人:gfy****yf 文档编号:155672360 上传时间:2022-09-24 格式:DOC 页数:65 大小:1.73MB
收藏 版权申诉 举报 下载
c++经典代码大全[共125页]_第1页
第1页 / 共65页
c++经典代码大全[共125页]_第2页
第2页 / 共65页
c++经典代码大全[共125页]_第3页
第3页 / 共65页
资源描述:

《c++经典代码大全[共125页]》由会员分享,可在线阅读,更多相关《c++经典代码大全[共125页](65页珍藏版)》请在装配图网上搜索。

1、#include //包含 iostream.h 头文件 //float 型溢出 main() float x=3.5e14; { cout<<"x="<

3、

6、="<

8、y<<"="<=1;j--) }while(n<100); cout<

10、for (int i=1;i<=9;i++) { cout< for(int n=1;n<=100;n++) main() s=s+n; { cout<

11、<"s="< L1: cout<<"x="; main() cin>>x; { if (x==-1) //计算 s=1+2+3...+100 goto L2; //无条件转移语句,转到 L2 语句处 int s=0,n=1; else while(n<=100) { sum+=x; s=s+n; goto L1; //无条件转移语句,转到 L1 语句处 n++; //定义标号 L2 } L2: cout<<"sum="<

13、sum="< max=a[0]; main() for (i=0;i<=4;i++) { { int i; if (max

16、="<

19、]; } h=i; } l=j; #include } main() } { } //声明变量 cout<<"Max:"<<"a["< main() //从键盘上为数组赋值 { for (i=0;i<=4;i++) //声明字符数组和变量 { char str[6]; cout<<"a["<>a[i]; } //从键盘上输入字符串 cout<

20、<"str="; //对数组按从大到小顺序排序 cin>>str; for (i=0;i<=3;i++) cout<=0;i--) for (i=0;i<=4;i++) cout<

21、]; cout< //将字符数组变成大写字母后输出 main() for (i=0;i<=5;i++) { str[i]-=32; //小写字母转换成大写字母 //声明二维数组及变量 cout< //从键盘上为数组 a 赋值 main() for (i=0;i<2;i++) { for (j=0;j<3;j++) //声明变量和指针变量 { int a,b,c,*

22、ip; cout<<"a["<>a[i][j]; //指针变量 ip 指向变量 a } a=100; ip=&a; //使指针变量 ip 指向变量 a //显示数组 a cout<<"a="<

23、ip2="<<(*ip2)<ip1="< //指针变量之间的减法 main() n=ip2-ip1; { cout<<"ip2-ip1="< int* ip; main() { //从键盘上为数组 a 赋值 //声明

25、字符型数组和指针变量 for (i=0;i<2;i++) //为数组 a 赋值 char str[10]; for (j=0;j<3;j++) char *strip=str; { cout<<"a["<>a[i][j]; cout<<"str="; } cin>>str; //用字符数组输入字符串 cout<<"str="<

29、oat ft; cout<<"*ip1="<<(*ip1)<

31、 ,"< cout<

32、*pt; temp=Wbk; cout<<"temp:"<>st[i].num; cout<

35、endl; cin>>st[i].name; cout<<"--------------------"<>st[i].maths; cin>>st[i].physics; //对结组成员的运算 cin>>st[i].chemistry; Zbk.pages+=10; } Zbk.price+=0.5; 6 //计算每个学生的总成绩 { for (i=0;i<3;i++) //定义结构类型 st[i].total=st[i].maths+st[i].physics+st[i].chemistry; struct human { char name[10]

36、; //输出结构数组各元素的值 int sex; for (i=0;i<3;i++) int age; { }; cout<<"st["<

37、"(*p).name="<<(*p).name< main() //利用 new 运算符为 p 分配内存 { p=new human; //定义结构类型 struct human { //从键盘上为 p 指向的结构对象赋值 char name[10

38、]; cout<<"p->name="; int sex; cin>>p->name; int age; cout<<"p->sex="; }; cin>>p->sex; cout<<"p->age="; //声明结构变量和结构指针变量 ,并初始化 cin>>p->age; struct human x={"WangPing",1,30},*p=NULL; cout<<"-------------------------"<name="<name<

39、ut<<"p->sex="<sex<age="<age<name="<name<sex="<sex<age="<age< main() //通过结构指针为结构

41、对象输入数据 { cout<<"name:"; //定义结构类型 cin>>(*p).name; struct human { cout<<"sex:"; char name[10]; cin>>(*p).sex; int sex; cout<<"age:"; int age; cin>>(*p).age; }; //显示结构变量的值 //声明结构数组和结构指针变量 ,并初始化 cout<<"x.name="<

42、5},{"LiuMin",0,23}},*p=NU cout<<"x.age="< //用下标变量的输出结构数组的元素 main() for (int i=0;i<3;i++) 7 { main() cout<

43、; //用结构指针输出结构数组的元素 int day; for (p=x;p<=&x[2];p++) }; { cout<name<<'\t'; //定义 baby 结构 cout<sex<<'\t'; struct baby { cout<age< }; main() { //声明 baby 结构变量并初始化 //定义一个包含指针成员的结构类型 baby b1={10001,10,

44、{2002,12,25}}; struct test { char *str; //下列是 baby 结构变量 b1 的引用。 int *ip; cout<<"b1.num="<

46、<<"temp.num="<

47、ay.day="< main() //使用结构变量 x 中的字符型指针 str { x.str=new char('A'); //分配 1 个单元 //定义名为 list 的递归结构 cout<<"x.str:"<<(*x.str)<

48、t sex; x.str=new char[5]; //分配多个单元 int age; *x.str='G'; list *next; //成员 next 为指向其自身结 *(x.str+1)='o'; 构的指针 *(x.str+2)='o'; }; *(x.str+3)='d'; *(x.str+4)='\0'; //使用递归结构变量 cout<<"x.str:"<

49、endl; cout<<"name\t"< } main() #include { 8 int i; int

50、 math; //定义名为 student 的递归结构 int computer; struct student { float sum; char name[10]; student *forw; //forw 成员是前指针 int math; student *next; //next 成员是后指针 int computer; }; float sum; student *next; //next 成员是指向自身的结构指 //用 student 声明 3 个结构指针变量 针 struct student *head,*tail,*temp; }; //申请第 1 块数据

51、,并设置各结构指针的初值 //用 student 声明 3 个结构指针变量 temp=new struct student; //申请内存 struct student *head,*tail,*temp; head=temp; // 头指针 tail=head; // 尾指针 //申请第 1 块数据,并设置各结构指针的初值 head->forw=NULL; temp=new struct student; //申请内存 head=temp; // 头指针 //循环为链表记录输入数据 tail=head; // 尾指针 cout<<"\tname Math Computer"<<

52、endl; for (i=1;;i++) { //循环为链表输入数据 cout<>temp->name; for (i=1;;i++) { if (temp->name[0]!='*') cout<>temp->name; cin>>temp->math>>temp->computer; if (temp->name[0]!='*') temp->sum=temp->math+temp->computer; { temp->next=NULL;

53、cin>>temp->math>>temp->computer; tail=temp; //设置链表尾指针 temp->sum=temp->math+temp->computer; } temp->next=NULL; else tail=temp; //设置链表尾指针 { } // 以下是输入结束处理 else delete temp; { tail->next=NULL; // 以下是输入结束处理 break; delete temp; } tail->next=NULL; //为下一个学生申请内存 break; temp->next=new struct studen

54、t; } temp->next->forw=temp; //设置前指针 //为下一个学生申请内存 temp=temp->next; //使处理指针 temp 指向新 temp->next=new struct student; 内存块 temp=temp->next; // 使处理指针 temp 指向新内存 } 块 } // 将链表数据从头到尾打印出来 cout<<"head------>tail:"<

55、) { temp=head; cout<name<<","<math<<","; while (temp!=NULL) { cout<computer<<","<sum<name<<","<math<<","; temp=temp->next; cout<computer<<","<sum<next; } // 将链表数据从尾到头打印出来 } cout<<"tail------>head:"<

56、 #include temp=tail; main() while (temp!=NULL) { { cout<name<<","<math<<","; int i; cout<computer<<","<sum<forw; struct student { } char name[10]; } 9 #include ux.x=123.456; main() cout<<"ux.x="<

57、< int int_values[] = {51, 23, 2, 44, 45,

60、0,11}; main() float float_values[] = {15.1, 13.3, 22.2, 10.4, 1.5}; { student //定义结构类型,并为声明的结构变量赋初值 st_arr[]={101,"WangLin",92,102,"LiPing",85,103,"ZhaoMin",88}; struct s_tag { short i; //显示 char 类型数组元素及其大小 float x; size=sizeof(str) / sizeof(char); } sx={100,3.1416}; cout<<"Number of elements

61、 in str: "; cout<

62、Number of elements in int_values: "; cout<<"sx.i="<

63、n u_tag)="<

64、for (i=0;i<=2;i++) { } cout<<"x="; cout<>x; cout<<"sgn("< cout<

65、ut< //变量声明语句 //add()函数的定义,其有返回值 float a,b,Max; double add(double x,double y) { //输入参数并计算 double z; cout<<"a="; z=x+y; cin>>a; cout<

66、< //以表达式方式调用函数 add() //定义 f() 函数 double c

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

相关资源

更多
正为您匹配相似的精品文档
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


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