第一题
#include<iostream>
using namespace std;
class Fraction {
int m_numerator = 0;
int m_denominator = 1;
public:
Fraction(int above = 0, int below = 1) :
m_numerator(above), m_denominator(below) {
cout << "Constructor called" << endl;
}
~Fraction(){} //1) 显示定义析构函数;
Fraction(const Fraction& rhs) : m_numerator(rhs.m_numerator), \
m_denominator(rhs.m_denominator) {
cout << "Copy constructor called" << endl;
}
~Fraction(){} //1) 显示定义析构函数;
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; //输出Constructor called
Fraction b(a); //输出Copy constructor called
Fraction c = Fraction(3, 2); //输出Constructor called
Fraction d1(2, 3), d2(4, 5); //输出Constructor called
Fraction e1 = divide1(d1, d2); //输出Constructor called
Fraction e2 = divide2(d1, d2); //输出Constructor called
cout << m_numerator << endl;
cout << m_denominator << endl;
cout << m_numerator / m_denominator << endl;
if (m_numerator > m_denominator) {
int a, b;
a = m_numerator % m_denominator;
b = (m_numerator - a) / m_denominator;
cout << b << " " << a << "/" << m_denominator;
}
return 0;
}
第二题
#include<iostream>
#include<vector>
using namespace std;
int main() {
int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
int i, n;
//a用顺序查找法
for (i = 0; i < 5; ++i) {
if (a[i] = 17) {
cout << "数组a中元素17所在下标为" << i << endl;
}
}
//b用折半查找法
if (b[3] <= 17) {
if (b[4] <= 17) {
if (b[5] = 17) {
cout << "数组b中元素17所在下标为" << 5 << endl;
}
else {
cout << "数组b中元素17所在下标为" << 4 << endl;
}
}
else {
cout << "数组b中元素17所在下标为" << 3 << endl;
}
}
else {
if (b[2] = 17) {
cout << "数组b中元素17所在下标为" << 2 << endl;
}
else {
cout << "数组b中元素17所在下标为" << 1 << endl;
}
}
vector <int> c;
for (n = 0; n < 5; ++n) {
for (i = 2; i < a[n]; ++i) {
if (a[n] % i == 0)
break;
else {
if (i + 1 == a[n]) {
c.push_back(a[n]);
}
}
}
}
for (n = 0; n < 5; ++n) {
for (i = 2; i < b[n]; ++i) {
if (b[n] % i == 0)
break;
else {
if (i + 1 == b[n]) {
c.push_back(b[n]);
}
}
}
}
c.pop_back();
c.pop_back();
c.push_back(59);
for (i = 0; i < 7; ++i) {
cout << c[i] << " ";
}
return 0;
}
第三题
#include<iostream>
using namespace std;
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;
}
friend int dis(Point& m_x, Point& m_y) {
(m_x) * (m_x)+(m_y) * (m_y);
}
};
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;
}
};
int main() {
Circle a(2, Point(1, 1)); cout << "end" << endl;
/* 输出过程:
1.输出Constructor of Circle
2.输出Destructor of Circle
3.输出Constructor of Point
4.输出Destructor of Point */
cout << m_y << endl;
return 0;
}