1、
这道题对我来说难点主要在对类的知识点掌握不够,并且不能熟练应用,所以花了很长时间查缺补漏。
代码如下
#include<iostream>
using namespace std;
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() {//显示定义析构函数
cout << "Destructor of Frantion" << endl;
}
int getnumerator() const { return m_numerator; }//获取分子
int getdenominator() const { return m_denominator; }//获取分母
void reduce();//实现分数约分
};
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 makeCommon(const Fraction& p, const Fraction& q) {//实现通分
Fraction result(p.getnumerator() * q.getdenominator() + p.getdenominator() * q.getnumerator(), p.getdenominator() * q.getdenominator());
return result;
}
Fraction operator/(const Fraction& p, const Fraction& q) {//使用 operator/操作符重载实现两个分数的除法运算
Fraction result(p.getnumerator() * q.getdenominator(), p.getdenominator() * q.getnumerator());
return result;
}
int gcd(int a, int b)//获取最大公因数
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void Fraction::reduce() {//分数通分
int n = gcd(m_numerator, m_denominator);
m_numerator /= n;
m_denominator /= n;
}
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);
}
输出结果为:
说明执行下列语句后,分别执行的什么操作,会输出什么?
//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”
“Constructor called”
//Fraction e1 = divide1(d1, d2); “Constructor called”
//Fraction e2 = divide2(d1, d2);
Copy constructor called
Copy constructor called
Constructor called
Copy Constructor called
2、
题目 数组与函数的综合应用
已知:int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
编写程序查找数组中是否存在某个指定元素;将数组a和数组b中的素数不重不漏地合并到一个vector容器c中,然后按照下标访问的方式手动对容器c中的数据,按从小到大顺序重新排序。要求依次实现: 1) 编写顺序查找法函数和折半查找法函数,分别在数组a和数组b中查找元素17所在的下标并输出。 2) 编写判断素数函数和排序函数,并对容器c中的结果进行输出
#include<iostream>
#include<vector>
using namespace std;
//判断素数函数
int ss(int a) {
int flag = 1;//标记1为素数
if (a == 2)
return 1;
if (a > 2) {
for (int i = 2; i < a; i++) {
if (a % i == 0)
return 0;
}
return 1;
}
}
//排序函数 冒泡排序法
vector<int> vi;
void sort() {
int len = vi.size();
for (int i = 0; i < len-1;i++ )
for (int j = 0; j <len-1-i ; j++) {
if (vi[j] > vi[j + 1]) {
int temp = vi[j + 1];
vi[j + 1] = vi[j];
vi[j] = temp;
}
}
}
int main() {
//顺序查找法
int a[5] = { 19,67,24,11,17 };
for (int i = 0; i < 5; i++) {
if (a[i] == 17) {
cout << i << endl;
}
}
//折半查找法
int b[5] = { 2,3,9,17,59 };
int mid = 2, left = 0, right = 4;
while (b[mid] != 17) {
if (b[mid] < 17) { left = mid; }
else { right = mid; }
mid = (left + right) / 2;
}
cout << mid << endl;
//合并a b中为素数的元素
int num=0;
for (int i = 0; i < 5; i++) {
if (ss(a[i])) {
vi.push_back(a[i]);
num++;
}
}
for (int j = 0; j < 5; j++) {
if (ss(b[j]))
{
vi.push_back(b[j]);
num++;
}
}
//删除重复元素
for (int i = 0; i < num-1; i++) {
for (int j = i+1; j < num; j++) {
if (vi[i] == vi[j]) {
vi.erase(vi.begin() + i);
num--;
}
}
}
sort();
//遍历新的数组
for (int i = 0; i < vi.size(); i++) {
cout << vi[i] << " ";
}
}
3、
这道题涉及到友元函数,并且借助这道题学会了如何提取自定义类中的元素。
#include<iostream>
using namespace std;
class Circle;
class Point {
friend class Circle;
private:
double m_x = 0, m_y = 0; //x和y坐标
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 getX() { //获取横坐标
return m_x;
}
double getY() { //获取纵坐标
return m_y;
}
double distant(const Point& a, const Point& b) { //两点间距离
double distant= sqrt(pow(abs(a.m_x - b.m_x), 2) + pow(abs(a.m_y - b.m_y), 2));
return distant;
}
};
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 getarea() {//圆的面积
double area = 3.14*m_radius*m_radius;
return area;
}
double getC() {//圆的周长
double C = 2*3.14*m_radius ;
return C;
}
};
int main()
{
Circle a(2, Point(1, 1));
cout << "end" << endl;
Point m(2, 3); //测试
cout << m.getX() << " " << m.getY() << endl;
Circle b(3, Point (3,1));//测试
cout << "圆的面积为:" << b.getarea()
<< "圆的周长为:" << b.getC() << endl;
return 0;
}