人人范文网 范文大全

C++编程例题个人总结0706

发布时间:2020-03-02 09:51:10 来源:范文大全 收藏本文 下载本文 手机版

VS2008通过编译

1、交换两个数值 #include using namespace std; void swap(int x,int y); int main() {int a=2,b=3;swap(a,b);printf(\"a=%d,b=%d\",a,b);return 0;}

void swap(int x,int y) {int temp;temp=x;x=y;y=temp; }

显示结果 a=2,b=3无法交换两个数值 #include using namespace std;

void swap(int &x,int &y); int main() {int a=2,b=3;swap(a,b);printf(\"a=%d,b=%d\",a,b);return 0;}

void swap(int &x,int &y) {int temp;temp=x;x=y;y=temp; }

显示结果 a=3,b=2 通过了引用,实现了两个数值交换 函数的定义为int类型,返回值有两个 #include using namespace std; int swap(int &x,int &y); int main() {int a=2,b=3;swap(a,b);printf(\"a=%d,b=%d\",a,b);return 0;}

int swap(int &x,int &y) {int temp;temp=x;x=y;y=temp;return x,y; }

显示结果 a=3,b=

22、指针

#include using namespace std; int main() {int a=5;int *p1=&a;int **p2=&p1;printf(\"a的存储地址为:%d\\n\",p1);printf(\"a的值%d\\n\",*p1);printf(\"%d\\n\",p2);printf(\"%d\\n\",*p2);printf(\"%d\\n\",**p2);return 0; }

显示结果:

a的存储地址为:1245024 a的值5 1245012 1245024 5

请按任意键继续...

#include using namespace std; int main() {int a=5;int *p1=NULL;p1=&a;int **p2=&p1;int ***p3=&p2;printf(\"a的存储地址为:%d\\n\",p1);printf(\"a的值%d\\n\",*p1);printf(\"\\n\");printf(\"%d\\n\",p2);printf(\"%d\\n\",*p2);printf(\"%d\\n\",**p2);printf(\"\\n\");printf(\"%d\\n\",p3);printf(\"%d\\n\",*p3);printf(\"%d\\n\",**p3);return 0; }

a的存储地址为:1245024 a的值5

1245012 1245024 5

1245000 1245012 1245024

请按任意键继续...

#include using namespace std; int main() {int a=5;int *p1=&a;int **p2=p1;

printf(\"a的存储地址为:%d\\n\",p1);printf(\"a的值%d\\n\",*p1);printf(\"%d\\n\",p2);printf(\"%d\\n\",*p2);printf(\"%d\\n\",**p2);return 0; }

1>------ 已启动生成: 项目: 55, 配置: Debug Win32 ------ 1>正在编译...1>55.cpp

1>c:\\documents and settings\\administrator\\my documents\\visual studio 2008\\projects\\55\\55\\55.cpp(7) : errorprintf(\"a的存储地址为:%d\\n\",p1);

printf(\"a的值%d\\n\",*p1);printf(\"\\n\");printf(\"%d\\n\",p2);printf(\"%d\\n\",*p2);printf(\"%d\\n\",**p2);printf(\"\\n\");printf(\"%d\\n\",p3);printf(\"%d\\n\",*p3);printf(\"%d\\n\",**p3);return 0; }

C2440: “初始化”: 无法从“int *”转换为“int **”

1>与指向的类型无关;转换要求reinterpret_cast、C 样式转换或函数样式转换

1>生成日志保存在“file://c:\\Documents and

Settings\\Administrator\\My Documents\\Visual Studio 2008\\Projects\\55\\55\\Debug\\BuildLog.htm” 1>551 个错误,个警告

========== 生成: 成功0 个,失败1 个,最新0 个,跳过0 个==========

#include using namespace std; int main() {int a=5;int *p1=&a;int **p2=&p1;int ***p3=&p1;printf(\"a的存储地址为:%d\\n\",p1);printf(\"a的值%d\\n\",*p1);printf(\"\\n\");printf(\"%d\\n\",p2);printf(\"%d\\n\",*p2);printf(\"%d\\n\",**p2);printf(\"\\n\");printf(\"%d\\n\",p3);printf(\"%d\\n\",*p3);printf(\"%d\\n\",**p3);return 0; }

1>------ 已启动生成: 项目: 55, 配置: Debug Win32 ------ 1>正在编译...1>55.cpp

1>c:\\documents and settings\\administrator\\my documents\\visual studio 2008\\projects\\55\\55\\55.cpp(8) : error C2440: “初始化”: 无法从“int **”转换为“int ***”

1>与指向的类型无关;转换要求reinterpret_cast、C 样式转换或函数样式转换

1>生成日志保存在“file://c:\\Documents and

Settings\\Administrator\\My Documents\\Visual Studio 2008\\Projects\\55\\55\\Debug\\BuildLog.htm” 1>551 个错误,个警告

========== 生成: 成功0 个,失败1 个,最新0 个,跳过0 个==========

#include using namespace std; int main() {int a=5,b=9;int *p1=&a;printf(\"%d\\n\",p1);printf(\"%d\\n\",a);*p1=6;

printf(\"%d\\n\",a);p1=&b;printf(\"%d\\n\",p1);return 0; }

1245024 5 6

1245012

请按任意键继续...

4、指针常量

#include using namespace std; int main() {int a=5,b=9;int *const p1=&a;printf(\"%d\\n\",p1);printf(\"%d\\n\",a);*p1=6;

printf(\"%d\\n\",a);return 0; }

1245024 5 6

请按任意键继续...

5、指针函数

#include using namespace std;

int *max(int *array,int size); int main() {int array[]={5,3,6,1,2,7,9,10};printf(\"数组最大值

=%d\\n\",*max(array,sizeof(array)/sizeof(int)));return 0; }

int *max(int *array,int size) {int *max=array;for(int i=0;i*max)max=&array[i];}return max; }

1245024 5 6

1245012

请按任意键继续...

#include using namespace std;

int *max(int *array,int size); int main() {int array[]={5,3,46,19,2,52,9,10};printf(\"%d\\n\",max(array,sizeof(array)/sizeof(int)));return 0; }

int *max(int *array,int size) {int *m=array;for(int i=0;i*m)m=&array[i];}return m; }

1245016

请按任意键继续...

#include using namespace std;

int *max(int *a,int size); int main() {int array[]={5,3,46,19,2,52,9,10};printf(\"%d\\n\",max(array,sizeof(array)/sizeof(int)));return 0; }

int *max(int *a,int size) {int *m=a;for(int i=0;i*m)m=&a[i];}return m; }

1245016

请按任意键继续...

6、枚举类型

#include using namespace std; int main() {enum

day{sunday,monday,tuesday,wednesday,thursday,friday,saturday};day today=sunday;printf(\"%d\",today);return 0; }

0请按任意键继续...

6、结构体

#include struct student {int idnumber;char name[15];int age;char department[20];float gpa; };

void display(student *arg); int main() {student s1={001,\"李明\",20,\"自动化\",90};student *slp=&s1;

display(slp);return 0; }

void display(student *arg) {printf(\"学号=%d,姓名=%s,年龄=%d,专业=%s,成绩

=%1.0f\",arg->idnumber,arg->name,arg->age,arg->department,arg->gpa); }

学号=1,姓名=李明,年龄=20,专业=自动化,成绩=90请按任意键继续...

#include struct student {int idnumber;char name[15];int age;char department[20];float gpa; };

int main() {student s1[2]={{001,\"李明\",20,\"自动化\",90},{002,\"梁雷\",22,\"电子工程\",92}};printf(\"第一个学生:学号=%d,姓名=%s,年龄=%d,专业=%s,成绩

=%2.0f\\n\",s1[0].idnumber,s1[0].name,s1[0].age,s1[0].department,s1[0].gpa);

printf(\"第二个学生:学号=%d,姓名=%s,年龄=%d,专业=%s,成绩

=%2.0f\\n\",s1[1].idnumber,s1[1].name,s1[1].age,s1[1].department,s1[1].gpa);return 0; }

第一个学生: 学号=1,姓名=李明,年龄=20,专业=自动化,成绩=90

第二个学生: 学号=2,姓名=梁雷,年龄=22,专业=电子工程,成绩=92

请按任意键继续...

C++编程心得

C++编程思想读后感

C++多线程编程简单实例

C++ 面试题(服务器编程、网络编程方便)

C++编程思想读后感1(推荐)

C语言C++程序员编程必备

C++编程复习题(华北电力 华北电力科技学院)(材料)

测绘程序设计与C++编程课程设计心得体会

中南大学C++实践报告MFC编程

C、C++总结

C++编程例题个人总结0706
《C++编程例题个人总结0706.doc》
将本文的Word文档下载到电脑,方便编辑。
推荐度:
点击下载文档
点击下载本文文档