c++实验报告

课程名称

C++与面向对象程序设计

实验项目名称

 

实验室名称及编号

 

实验日期

 

学生姓名

 

学号

 

专业班级 

 

同组组员

机器编号或IP

***.***.***.***

一,实验目的和要求

 1.理解和使用函数重载以及带默认参数的函数;

2.使用new和delete进行动态内存管理;

3.理解和使用引用。

二、实验环境(软、硬件及条件)

一台安装有Visual C++ 6.0的计算机

三,实验步骤

1.阅读下面的程序,写出程序的运行结果。

#include <iostream.h>

int max_def(int x, int y)

{

      return (x>y?x:y);

}

int max_def(int x, int y, int z)

{

      int temp = 0;

      return (temp=(x>y?x:y))>z?temp:z;

}

double max_def(double x, double y)

{

      return (x>y?x:y);

}

int main()

{

      int x1 = 0;

      int x2 = 0;

      double d1 = 0.0;

      double d2 = 0.0;

      x1 = max_def(5,6);

      x2 = max_def(2,3,4);

      d1 = max_def(2.1,5.6);

      d2 = max_def(12.3,3.4,7.8);

      cout<<"x1="<<x1<<endl;

      cout<<"x2="<<x2<<endl;

      cout<<"d1="<<d1<<endl;

      cout<<"d2="<<d2<<endl; 

      return 0;

}

 

该程序的运行结果是      x1=6

                        x2=4

                        d1=5.6

                        d2=12                         

2. 阅读下面的程序,写出程序的运行结果。

#include <iostream.h>

void swap(int a, int b)

{

      int temp = a;

      a = b;

      b = temp;

}

void swap(int *a, int *b)

{

      int temp = *a;

      *a = *b;

      *b = temp;

}

 

int main()

{

      int i = 5;

      int j = 10;

      cout<<"Before swap: i="<<i<<",j="<<j<<endl;

      swap(i,j);

      cout<<"After the first swap: i="<<i<<",j="<<j<<endl;

      swap(&i,&j);

      cout<<"After the second swap: i="<<i<<",j="<<j<<endl;

      return 0;

}

该程序的运行结果是   Before swap: i=5,j=10

                     After the first swap: i=5,j=10

                     After the second swap: i=10,j=5                            

3.以下程序的功能是:      请输入9个整数:

                           1 2 3 4 5 6 7 8 9

                           the  product  is :362880                       

# include <iostream.h>

const int size=9;                                

int *init()                  //返回指针值的函数

{

      return  new int[size];   //分配空间将首地址返回

}

void readin(int *a)      //从a指向的存储单元开始读入数据

{

      int i;

      cout<<"请输入"<<size<<"个整数:"<<endl;

      for (i=0;i<size;i++)

        cin>>*(a+i);      //读入数据到a+i指向的存储单元

}

void product(int *arr ,int  size,int *result)

                      //计算从arr指向的存储单元开始的

                      //连续size个存储单元的乘积

{   int m,i;

      for (m=1,i=0;size>0;size--,i++)

            m=m*(*(arr+i));

      *result=m; //乘积的结果放入result指向的存储单元

}

int main()

{

      int *x,res;

      if ((x=init())==NULL)

            return 1;

      readin(x);

      product(x,size,&res);

      cout<<"the  product  is :"<<res<<endl;

      delete[ ] x;

      return 0;   

}

3.设计一个函数:exchange(float x, float y, float z),当调用exchange(a,b,c)时,将a的内容赋值给b,b的内容赋值给c,c的内容赋值给a,要求采用引用的方式来实现。

#include<iostream>

using namespace std;

float exchange(float &x,float &y, float &z)

{

float t;

t=x;

x=y;

y=t;

y=z;

z=t;

return x,y,z;

}

int main()

{

float a,b,c;

cout<<" please inputa,b,c"<<endl;

cin>>a;

cin>>b;

cin>>c;

float &i=a;

float &j=b;

float &k=c;

exchange(i,j,k);

cout<<"交换后的值"<<a<<b<<c<<endl;

return 0;

}

4.编写重载函数max1可分别求取2个整数,3个整数,2个浮点数,3个浮点数的最大值。  

 #include<iostream>

using namespace std;

int max(int a,int b)

{

 return a>b?a:b;

}

int max(int a,int b,int c)

{

 return max(a,max(b,c));

}

double max(double a,double b)

{

 return a>b?a:b;

}

double max(double a,double b,double c)

{

 return max(a,max(b,c));

}

void main()

{

 int a,b,c;

 double x,y,z;

 cout<<"input three int:"<<endl;;

 cin>>a>>b>>c;

 cout<<max(a,b,c)<<endl;

 cout<<max(a,b)<<endl;

 cout<<"input three double:" <<endl;

 cin>>x>>y>>z;

 cout<<max(x,y,z)<<endl;

 cout<<max(x,y)<<endl;

}

      

四、实验中遇到的问题及解决

:\Users\周佳丽\Desktop\Microsoft Visual Studio(3)\Microsoft Visual Studio\Common\Cpp1.cpp(7) : error C2059: syntax error : ';'

C:\Users\周佳丽\Desktop\Microsoft Visual Studio(3)\Microsoft Visual Studio\Common\Cpp1.cpp(13) : warning C4508: 'exchange' : function should return a value; 'void' return type assumed

C:\Users\周佳丽\Desktop\Microsoft Visual Studio(3)\Microsoft Visual Studio\Common\Cpp1.cpp(19) : error C2440: 'initializing' : cannot convert from 'float *' to 'float'

        There is no context in which this conversion is possible

C:\Users\周佳丽\Desktop\Microsoft Visual Studio(3)\Microsoft Visual Studio\Common\Cpp1.cpp(20) : error C2440: 'initializing' : cannot convert from 'float *' to 'float'

        There is no context in which this conversion is possible

C:\Users\周佳丽\Desktop\Microsoft Visual Studio(3)\Microsoft Visual Studio\Common\Cpp1.cpp(21) : error C2440: 'initializing' : cannot convert from 'float *' to 'float'

        There is no context in which this conversion is possible

Error executing cl.exe.

 

 

 

 

实验结果及分析

<1> 

x1=6

x2=4

d1=5.6

d2=12

<2>

Before swap: i=5,j=10

After the first swap: i=5,j=10

After the second swap: i=10,j=5

<3>

请输入9个整数:

1 2 3 4 5 6 7 8 9

the  product  is :362880

<4>

please inputa,b,c

4 7 8

交换后的值784

<5>

input three int:

1 2 3

3

2

input three double:

3.2 7.2 2.1

7.2

7.2

猜你喜欢

转载自blog.csdn.net/ccccc49813645075/article/details/80160512
今日推荐