当前位置:文档之家› 2015C 实验报告讲解

2015C 实验报告讲解

{return(x>y?x:y)>z?(x>y?x:y):z;}
doublemax1(doublex,doubley)
{returnx>y?x:y;}
doublemax1(doublex,doubley,doublez)
{return(x>y?x:y)>z?(x>y?x:y):z;}
运行结果:
3.用new操作为一个包含10个整数的数组分配内存,输入若干个值到数组中,分别统计其中正数和负数的个数后再用delete操作释放内存。
程序:
#include"stdafx.h"
#include<iostream>
#include<iomanip>
usingnamespacestd;
classComplex
{
private:
intReal;
intImage;
public:
Complex(intr,inti);
Complex(Complex &c);
}
Circle::Circle(doubler):radius(r)
{
}
doubleCircle::Area()
{
returnPI*radius*radius;
}
运行结果:
2.定义一个复数类Complex,复数的实部Real与虚部Image定义为私有数据成员。用复数类定义复数对象c1、c2、c3,用默认构造函数将c1初始化为c1=20+40i,将c2初始化为c2=0+0i,用拷贝构造函数将c3初始化为c3=20+40i。用公有成员函数Dispaly()显示复数c1、c2与c3的内容。
}
return0;
}
voidScore::write()
{
cin>&th>>Phi>>Data;
}
floatScore::Average()
{
Ave=(Math+Phi+Data)/3;
returnAve;
}
voidScore::Display()
{
cout<<No<<" "<<Name<<" "<<Math<<" "<<Phi<<" "<<Data<<" "<<Ave<<endl;
#include"stdafx.h"
#include<iostream>
usingnamespacestd;
constintN=10;
int_tmain(intargc, _TCHAR* argv[])
{
int*p,i,plus,minus;
p=newint[N];
if(!p)
{
cout<<"内存分配错误!"<<endl;
实验数据:
No Name Math Phi Data Ave
1001Zhou 8070 60
1002Chen9080 85
1003Wang70 75 89
程序:
#include"stdafx.h"
#include<iostream>
usingnamespacestd;
constintN =3;
classScore
{
doublelen,wid;
cout<<"intput length and width"<<endl;
cin>>len>>wid;
Rectangle r2(len,wid);
cout<<" rectangle area = "<<r2.Area()<<endl;
break;
}
case's':
{
doublelen;
cout<<"Input No Name Mth Phi Data :"<<endl;
for(i=0;i<N;i++)
{
stu[i].write();
stu[i].Average();
}
cout<<"No Name Mth Phi Data Ave"<<endl;
for(i=0;i<N;i++)
{
stu[i].Display();
}
运行结果:
3.定义一个学生成绩类Score,描述学生成绩的私有数据成员为学号(No)、姓名(Name[8])、数学(Math)、物理(Phi)、数据结构(Data)、平均分(ave)。定义能输入学生成绩的公有成员函数Write(),能计算学生平均分的公有成员函数Average(),能显示学生成绩的公有成员函数Display()。在主函数中用Score类定义学生成绩对象数组s[3]。用Write()输入学生成绩,用Average()计算每个学生的平均分,最后用Display()显示每个学生的成绩。
cout<<"input length"<<endl;
cin>>len;
Square s(len);
cout<<" Square area = "<<s.Area()<<endl;
break;
}
default:
cout<<"input error ! "<<endl;
break;
}
return0;
}
}
运行结果:
4.定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最后用delete运算符回收为矩形动态分配的存储空间。
cout<<A<<" and "<<B<<" max: "<<D<<endl;
cout<<A<<" and "<<B<<" and "<<C<<" max: "<<E<<endl;
return0;
}
intmax1(intx,inty)
{returnx>y?x:y;}
intmax1(intx,inty,intz)
intx=10,y=20;
cout<<"x="<<x<<endl;cout<<"y="<<y<<endl;
cout<<"Step into fnl()..."<<endl;
fnl();fnl(10);
cout<<"Back in main"<<endl;
cout<<"x="<<x<<endl;cout<<"y="<<y<<endl;
constdoublePI = 3.14;
classRectangle
{
private:
doublelen,wid;
public:
Rectangle(doublex,doubley);
doubleArea();
};
classSquare
{
private:
doublelen;
public:
Square(doublex);
floatDiagonal();
return0;
}
Complex::Complex(intr,inti)
{
Real =r;
Image=i;
}
Complex::Complex(Complex &c)
{
Real=c.Real;
Image=c.Image;
}
voidComplex::Display()
{
cout<<setw(2)<<Real<<" + "<<setw(2)<<Image<<" i "<<endl;
cout<<"负数的个数:"<<minus<<endl;
delete[]p;
return0;
}
运行结果:
遇到的问题和解决方法
心得体会
实验内容
实验3.3(1)(2)(3)(4)
实验记录
1.补充程序:(加粗为补充内容)
#include"stdafx.h"
#include<iostream>
相关主题