第三次上机
题目 1、类的定义与基本操作
class Fraction {
//数据成员,访问控制属性默认是私有
int m_numerator = 0; // 分子默认为0; C++11
int m_denominator = 1; //分母默认为1;
public://公有成员函数
Fraction(int above = 0, int below = 1) :
m_numerator(above), m_denominator(below) {
cout << "Constructor called" << endl;
}
Fraction(const Fraction& rhs) : m_numerator(rhs.m_numerator), \
m_denominator(rhs.m_denominator) {
cout << "Copy constructor called" << endl;
}
};
Fraction divide1(const Fraction& divident, const Fraction& divisor) {
return Fraction(divident.getnumerator() * divisor.getdenominator(), \
divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor) {
Fraction result(divident.getnumerator() * divisor.getdenominator(), \
divident.getdenominator() * divisor.getnumerator());
return result;
}
说明执行下列语句后,分别执行的什么操作,会输出什么?
Fraction a;执行默认构造函数,输出 Constructor called
Fraction b(a);b为对象a的副本,执行复制构造函数,输出 Copy constructor called
Fraction c = Fraction(3, 2);复制初始化,先构造一个临时对象,然后将这个临时对象以复制构造的方式来初始化对象c,输出 Constructor called Copy constructor called
c++编译器使用优化技术,直接初始化方式完成这一操作, 实际输出为Constructor called
Fraction d1(2, 3), d2(4, 5);直接初始化两个对象d1,d2,执行两次构造函数
输出Constructor called Constructor called
Fraction e1 = divide1(d1, d2);执行divide1函数返回一个临时对象,执行复制构造函数对e1进行赋值
输出 Copy constructor called
Fraction e2 = divide2(d1, d2);执行divide2函数返回一个临时对象,执行复制构造函数对e2进行赋值
输出 Copy constructor called
#include <iostream>
using namespace std;
class Fraction {
public://公有成员函数
int m_numerator = 0; // 分子默认为0;
int m_denominator = 1; //分母默认为1;
Fraction(int above = 0, int below = 1) :
m_numerator(above), m_denominator(below) {
cout << "Constructor called" << endl;
}
Fraction(const Fraction& rhs) : m_numerator(rhs.m_numerator), \
m_denominator(rhs.m_denominator) {
cout << "Copy constructor called" << endl;
}
//定义析构函数
~Fraction() {
cout << "Detructor of Fraction";
}
int getnumerator()const { return m_numerator; }//获取分子
int getdenominator() const { return m_denominator; }//获取分母
int gcd(int x, int y) {
if (y != 0)
return reduce(y, x % y);
else return x;
}//求最大公约数函数
double reduce(int x, int y) {
int i = gcd(m_numerator, m_denominator);
double n = m_numerator / i;
double d = m_denominator / i;
double result = n / d;
return result;
}//约分
Fraction tf(Fraction x, Fraction y)
{
int i = x.m_denominator * y.m_denominator;
int o = x.m_numerator * y.m_denominator;
int p = y.m_numerator * x.m_denominator;
x.m_numerator = o;
x.m_denominator = i;
y.m_denominator = i;
y.m_numerator = p;
return x, y;
}//通分
};
Fraction operator/(const Fraction& x, const Fraction& y)
{
return Fraction(x.m_numerator * y.m_denominator, x.m_denominator * y.m_numerator);
}
Fraction divide1(const Fraction& divident, const Fraction& divisor) {
return Fraction(divident.getnumerator() * divisor.getdenominator(), \
divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor) {
Fraction result(divident.getnumerator() * divisor.getdenominator(), \
divident.getdenominator() * divisor.getnumerator());
return result;
}
int main() {
Fraction a;
Fraction b(a);
Fraction c = Fraction(3, 2);
Fraction d1(2, 3), d2(4, 5);
Fraction e1 = divide1(d1, d2);
Fraction e2 = divide2(d1, d2);
return 0;
}
题目 2、数组与函数的综合应用
已知:int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
编写程序查找数组中是否存在某个指定元素;将数组a和数组b中的素数不重不漏地合并到
一个vector容器c中,然后按照下标访问的方式手动对容器c中的数据,按从小到大顺序重新
排序。要求依次实现:
- 编写顺序查找法函数和折半查找法函数,分别在数组a和数组b中查找元素17所在的下标
并输出。 - 编写判断素数函数和排序函数,并对容器c中的结果进行输出。
#include <iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
vector<int>v;
//顺序查找法函数
int sx(int x[5]) {
for (int i = 0; i < 5; ++i)
{
if (x[i] == 17)return i;
}
}
//折半查找函数
int zb(int x[5])
{
int i = 0, j = 4, h;
h = (i + j) / 2;
if (x[h] == 17) return h;
if (x[h] > 17)
{
j = h - 1;
if (x[j] == 17)return j;
else { return i; }
}
if (x[h] < 17)
{
i = h + 1;
if(x[i] == 17)return i;
else { return j; }
}
}
//排序函数
int px(int x[5]) {
for (int i = 1; i < 5; i++)
{
for (int j = 0; j < 5 - i; j++)
{
if (x[j] > x[j + 1])
{
swap(x[j], x[j + 1]);
}
}
}
return x[5];
}
//判断素数函数
int sh(int x)
{
int m = sqrt(x);
for (int i = 2; i <= m; i++)
{
if (x % i != 0)return x;
else { break; }
}
} int e[9];
int main() {
vector <int> c;
int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
cout << sx(a) << endl;
cout<< zb(b) << endl;
for (int i = 0; i < 5; i++)
{
if (sh(a[i]) != 0) { c.push_back(a[i]); }
if (sh(b[i]) != 0&&b[i]!=17){ c.push_back(b[i]); }
}
for (int i = 0; i < size(c); i++)
{
cout << c[i] << ends;
}
cout << endl;
for (int i = 0; i < size(c); i++)
{
e[i] = c[i];
}
for (int i = 1; i < 9; i++)
{
for (int j = 0; j < 9 - i; j++)
{
if (e[j] > e[j + 1])
{
swap(e[j], e[j + 1]);
}
}
}
c.clear();
for (int i = 0; i < 9; i++)
{
c.push_back(e[i]);
}
for (int i = 0; i < 9; i++)
{
cout << c[i] << ends;
}
return 0;
}
}
题目 3、类的定义与基本操作
class Point {
double m_x = 0, m_y = 0;
public:
Point(double x=0, double y=0) : m_x(x), m_y(y) {
cout << “Constructor of Point” << endl;
}
Point(const Point &p) :m_x(p.m_x), m_y(p.m_y) {
cout << “Copy constructor of Point” << endl;
}
~Point() {
cout << “Destructor of Point” << endl;
}
};
class Circle {
Point m_center; double m_radius = 1.0;
public:
Circle(double r=1, const Point &p=Point()) :m_center§, m_radius® {
cout << “Constructor of Circle” << endl;
}
~Circle() {
cout << “Destructor of Circle” << endl;
}
};
int main()
{
Circle a(2, Point(1, 1));
cout << “end” << endl;
return 0;
}
- 说明上述程序执行流程和输出结果;
- 在Point类中完善获取点的横坐标、获取点的纵坐标成员函数,并在主函数中测试;
- 通过友元函数实现平面上任意两个点的距离计算辅助函数;
- 在Circle类中完善圆的面积计算与圆的周长计算成员函数,并在主函数中测试;
1.执行流程:首先调用point构造函数、circle构造函数、再调用point析构函数、circle析构函数
输出:Constructor of Point
Copy constructor of Point
Constructor of Circle
Destructor of Point
Destructor of Circle
end
#include <iostream>
#include<cmath>
using namespace std;
double PI = 3.14;
class Point {
double m_x = 0, m_y = 0;
public:
Point(double x = 0, double y = 0) : m_x(x), m_y(y) {
cout << "Constructor of Point" << endl;
}
Point(const Point& p) :m_x(p.m_x), m_y(p.m_y) {
cout << "Copy constructor of Point" << endl;
}
~Point() {
cout << "Destructor of Point" << endl;
}
double getpointx() { return m_x;}
double getpointy() { return m_y; }
friend double len(const Point& a, const Point& b);
};
class Circle {
Point m_center; double m_radius = 1.0;
public:
Circle(double r = 1, const Point& p = Point()) :m_center(p), m_radius(r) {
cout << "Constructor of Circle" << endl;
}
~Circle() {
cout << "Destructor of Circle" << endl;
}
double S() const { //圆的面积
return PI * m_radius * m_radius;
}
double C() const { //圆的周长
return 2 * PI * m_radius;
}
};
double len(const Point& a, const Point& b)
{
double s = a.m_x - b.m_x, m = a.m_y - b.m_y;
double length = sqrt(s * s + m * m);
return length;
}
int main()
{
Circle a(2, Point(1, 1));
cout << "end" << endl;
Point b(5, 7);
cout <<"坐标为"<< b.getpointx() << ends << b.getpointy();
cout << "圆的面积为" << a.S() << endl;
cout << "圆的周长为:" << a.C() << endl;
cout <<" end "<< endl;
return 0;
}