C++编程例题个人总结2014-07-06

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) : 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>55 - 1 个错误,个警告

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

==========

#include

using namespace std;

int main()

{

int a=5;

int *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;

int ***p3=&a;

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>55 - 1 个错误,个警告

========== 生成: 成功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>55 - 1 个错误,个警告

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

==========

#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);

printf("%d\n",***p3);

return 0;

}

a的存储地址为:1245024

a的值5

1245012

1245024

5

1245000

1245012

1245024

5

请按任意键继续. . .

#include

using namespace std;

int main()

{

int a=5;

int *p1=NULL;

p1=&a;

int **p2=NULL;

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);

printf("%d\n",***p3);

return 0;

}

a的存储地址为:1245024

a的值5

1245012

1245024 5

1245000

1245012

1245024

5

请按任意键继续. . .

3、常量指针

常量指针只能从内存中读取数据,不能修改内存中的数据,也就是说常量指针不能通过间接引用来修改指向地址内的数据,但指针的指向可以改变。

#include

using namespace std;

int main()

{

int a=5,b=9;

const int *p1=&a;

printf("%d\n",p1);

p1=&b;

printf("%d\n",p1);

return 0;

}

1245024

1245012

请按任意键继续. . .

#include

using namespace std;

int main()

{

int a=5,b=9;

const int *p1=&a;

printf("%d\n",p1);

*p1=6;

p1=&b;

printf("%d\n",p1);

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 C3892: “p1”: 不能给常量赋值

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

Settings\Administrator\My Documents\Visual Studio

2008\Projects\55\55\Debug\BuildLog.htm”

1>55 - 1 个错误,个警告

========== 生成: 成功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

{

if(array[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

{

if(array[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

{

if(a[i]>*m)

m=&a[i];

}

return m;

}

1245016

请按任意键继续. . .

6、枚举类型

#include

using namespace std;

int main()

{

enum

day{sunday,monday,tuesday,wednesday,thursday,friday,saturda y};

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,ar g->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].departm ent,s1[0].gpa);

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

=%2.0f\n",s1[1].idnumber,s1[1].name,s1[1].age,s1[1].departm ent,s1[1].gpa);

return 0;

}

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

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

请按任意键继续. . .

相关主题
相关文档
最新文档