2014太原理工大学c++实验报告.docxVIP

  • 173
  • 0
  • 约1.53万字
  • 约 41页
  • 2017-05-21 发布于浙江
  • 举报
2014太原理工大学c实验报告

本科实验报告 课程名称: C++面向对象程序设计 实验项目: C++语言编程 实验地点: 明向校区 专业班级: 软件1431 学号: 201400XXXX 学生姓名: 白建兴 指导教师: 崔晓红 2015年5月10 日 实验内容 实验2.3(1)(2)(3)实验记录1.运行例题程序: #includeiostream using namespace std; void fn1(int z=5); int x=1,y=2; int main() { coutBegin. . .endl; coutx=xendl; couty=yendl; coutEvaluae x and y in main()...endl; int x=10,y=20; coutx=xendl; couty=yendl; coutStep into fn1()...endl; fn1(); fn1(10); coutBace in mainendl; coutx=xendl; couty=yendl; return 0; } void fn1(int z) { static int x=100; int y=200; coutx=xendl; couty=yendl; x=x+y+z; }运行结果: 2.编写重载函数max1可分别求取2个整数,3个整数,3个双精度数,3个双精度数的最大值 #includeiostream using namespace std; int max(int x,int y); double max(double x,double y); int max(int x,int y,int z); double max(double x,double y,double z); int main() { int a,b,c; cout请输入3个整数endl; cinabc; cout2个整数的最大值:max(a,b)endl; cout3个整数的最大值:max(a,b,c)endl; double i,j,k; cout请输入3个双精度数endl; cinijk; cout2个双精度数的最大值:max(i,j)endl; cout3个双精度数的最大值:max(i,j,k)endl; return 0; } int max(int x,int y) { return xy?x:y; } double max(double x,double y) { return xy?x:y; } int max(int x,int y,int z) { return (max(x,y)z)?max(x,y):z; } double max(double x,double y,double z) { return (max(x,y)z)?max(x,y):z; }运行结果: 3.用new操作为一个包含10个整数的数组分配内存,输入若干个值到数组中,分别统计其中正数和负数的个数后再用delete操作释放内存。 #includeiostream using namespace std; const int N=10; int main() { int *p,i,plus,minus; p=new int[N]; if(!p) { cout内存分配错误!endl; exit(1); } plus=0; minus=0; cout请任意输入N个整数endl; for(i=0;iN;i++) { cinp[i]; if(p[i]0) plus++; else if(p[i]!=0) minus++; } cout正数的个数:plusendl; cout负数的个数:minusendl; delete []p; return 0; }运行结果:  心得体会在经过多次编写实验程序后,比较了C与C++,二者在编程的思想上完全不同,但是在C++的编译器中,也会用到很多C中的东西,虽然C++后期修改程序上比C要容易了很多,但是在编写过程中要比C要麻烦。虽然编写了这么多的程序,但是对编写的程序如何变成软件还是有很多的疑惑以及更大好奇心。 实验内容 实验3.3(1)(2)(3)(4)实验记录1.程序: #includeiostream #define pi 3.14 using namespace

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档