实验2 C++对C 的扩充

开发工具及环境:PC机一套 Visual Studio 2010

实验要求:
1.硬件基本配置:Intel PentiumIII以上级别的CPU,大于64MB的内存。
2.软件要求:Window 7操作系统,Visual Studio 2010或更高版本开发环境。
3.实验学时:2学时
4.实现实验内容中的题目。
5.写实验报告

实验目的:
1.进一步了解和熟悉Visual Studio 2010开发环境,学会在Visual Studio 2010环境下调试程序;
2.熟悉C++中简单的标准输入输出函数的实用;
3.理解const修饰符的作用,并学会应用const修饰符;
4.理解内置(内联)函数的优缺点并学会使用内置函数;
5.理解和使用函数重载以及带默认参数的函数;
6.使用new和delete进行动态内存管理;
7.理解和使用引用。

实验内容:
(一) 程序阅读
1.理解下面的程序,并在Visual Studio 2010下运行查看结果,回答程序后面的问题。

#include 
using namespace std;
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="<>a>>b>>c;
float &i=a;
float &j=b;
float &k=c;
exchange(i,j,k);
cout<<"交换后的值"<
  1. 编写一个函数,实现两个字符串变量的交换,要求参数用引用。

    #include
    #include
    using namespace std;
    void swap(string &x,string &y);

    int main()
    {
    string str1,str2;
    cout<<“输入str1:”;
    cin>>str1;
    cout<<“输入str2:”;
    cin>>str2;
    swap(str1,str2);
    cout<<“str1=”< cout<<“str2=”< return 0;
    }

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

4.编写几个计算面积的函数,分别计算圆、矩形、梯形和三角形的面积。
要求:采用重载函数实现。
【提示】:函数原型如下:
double area(double radius=0);
// 圆面积,参数为半径,缺省参数0,表示点面积
double area(double a, double b);
// 计算矩形面积,参数为长和宽
double area(double a, double b, double h);
// 计算梯形面积,参数为两底和高
double area(double a, double b, double c, int);
// 三角形,参数为三边长,int型参数起表示作用,以区别于梯形,不参加计算。

#include
#include
using namespace std;
double area(double radius=0)
// 圆面积,参数为半径,缺省参数0,表示点面积
{
	double s1;
	s1 = 3.14*radius*radius;
	return s1;
}

double area(double a, double b)
// 计算矩形面积,参数为长和宽
{
	double s2;
	s2 = a * b;
	return s2;
}

double area(double a, double b, double h)
// 计算梯形面积,参数为两底和高
{
	double s3;
	s3 = ((a + b)*h)/2;
	return s3;
}

double area(double a, double b, double c, int)  
// 三角形,参数为三边长,int型参数起表示作用,以区别于梯形,不参加计算。
{
	double p,s4;
	p = (a+b+c)/2;
	s4 = sqrt(p*(p-a)*(p-b)*(p-c));
	return s4;
}

int main()
{
double area(double radius=0);
double area(double a, double b); 
 double area(double a, double b, double h); 
 double area(double a, double b, double c, int);  
 double a,b,h,c,radius;
 double s1,s2,s3,s4;
 cin>>a>>b>>c>>h>>radius;
 s1 = area(radius);
 s2 = area(a,b);
 s3 = area(a,b,h);
 s4 = area(a,b,c);
 cout<

5.建立一个头文件area.h,在其中声明第4题中的所有area()函数,然后再在area.cpp文件中
实现4题中的4个面积函数。另外建立一个实现文件mainTest.cpp,在其中定义主函数。通
过包含area.h,输入数据并输出各种图形面积。

area.h

#include 
#include
#define PI 314159
double area(double radius=0);
double area(double a,double b);
double area(double a,double b,double h);
double area(double a,double b,double c,int);

area.cpp

#include 
#include "area.h"
double area(double radius){
return PI*radius*radius;
}
double area(double a,double b){
return a*b;
}
double area(double a,double b,double h){
return (0.5*(a+b)*h);
}
double area(double a,double b,double c,int){
double s=0.5*(a+b+c);
return sqrt(s*(s-a)*(s-b)*(s-c));
}

mainTest.cpp

#include 
#include "area.h"
using namespace std;
int main(){
cout<<"Area of point is:"<

6.编写一个程序,用同一个函数名对n个数据进行求平均值,数据类型可以是整形、单精
度型、双精度型。
(1)用重载函数实现。
(2)用函数模板实现。
比较这两种方法各有什么特点,什么情况下可以用函数模板代替重载函数?什么情况下不
可以用函数模板代替重载函数?
(1)用重载函数实现。

#include
using namespace std;
int ave(int *a, int n);
float ave(float *a, int n);
double ave(double *a, int n);
int main() 
{
	int n, num;
	double *a;
	cout<<"请输入参数个数并依次输入各参数"<>n;
	a = new double[n];
	for (int i=0;i>a[i];
	cout<

(2)用函数模板实现。

#include
using namespace std;
template
M ave(M *a, int n) 
{
	M total = 0;
	for (int i = 0; i < n; i++)
		total += a[i];
	return(total / n);
}
int main() 
{
	int n;
	double *a;
	cout<<"请输入参数个数并依次输入各参数"<> n;
	a = new double[n];
	for (int i = 0; i < n; i++) cin >> a[i];
	cout << ave(a, n) << endl;
	system("pause");
	return 0;
}

学艺不精,如有错误,还请指出,多谢。

你可能感兴趣的:(C++)