2020-12-18第三次上机作业

题目 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 << "Fraction的析构函数的调用" << endl;
    }
    //分子
    int getnumerator()
    const{
        return m_Numerator;
    }
    //获取分母
     int getdenominator()
    const{
        return m_Denominator;
    }
    //实现约分
    void reduce();
    //运用operator/
    Fraction operator/(Fraction b) {
        Fraction f(m_Numerator * b.getdenominator(), m_Denominator * b.getnumerator());
        f.reduce();
        return f;
    }
    
private:
    int gcd(int i, int j)  //计算i和j的最大公约数
{
    int z = j;
    while (i % j != 0)
    {
        z = i % j;
        i = j;
        j = z;
    }
    return z;
 }
    
};

void Fraction:: reduce() {
    int n = gcd(m_Numerator, m_Denominator);
    m_Numerator /= n;
    m_Denominator /= n;
}
Fraction makeCommon( Fraction a, Fraction b) {
    Fraction f(a.getnumerator() * b.getdenominator() + a.getdenominator() * b.getnumerator(), a.getdenominator() * b.getdenominator());
    f.reduce();
    return f;
}
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 p(2,4);
    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);

   //获取分数的分子
    cout << "分数的分子是:" << a.getnumerator() << endl;
    //获取分数的分母
    cout << "分数的分母是:" << a.getdenominator() << endl;
    //分数的约分
    p.reduce();
    cout << "分数=" << p.getnumerator() << "/" << p.getdenominator() << endl;
    //分数的通分
    Fraction f = makeCommon(d1,d2);
    cout << "分数=" << f.getnumerator()<<"/"<<f.getdenominator()<< endl;
    //运用operator/
    Fraction m = d1 / d2;
    cout << "分数=" << m.getnumerator() << "/" << m.getdenominator() << endl;
    system("pause");
    return 0;
}

题目 2、数组与函数的综合应用

#include <iostream>
#include <vector>
using namespace std;

int fun1(int n, int num[], int x) {
    int  pos = 0;
    for (int i = 0; i <= n - 1; ++i) {
        if (num[i] == x) {
            pos = i;
            break;
        }
    }
    return pos;
}
int msearch(int a[], int low, int high) {
    int pos = 0, key = 0;
    while (low <= high)
    {
        pos = (low + high) / 2;          //求中间位置
        if (key < a[pos])                //key小于中间值时
            high = pos - 1;            //确定左子表范围
        else if (key > a[pos])        //key 大于中间值时
            low = pos + 1;            //确定右子表范围
        else if (key == a[pos])      //表明查找成功,返回所在位置
        {
            return pos;
        }
    }
    if (low > high) //表明查找失败,返回-1
        return -1;
}
//找素数
bool su(int a) {
    if (a == 2) { return 1; }
    for (int i = 2; i < a; i++) {
        if (a % i == 0) {
            return 0;
            break;
        }
        if (i == a - 1) {
            return 1;
        }
    }
}
void sort(vector<int>& c, int size) {
    for (int i = 1; i < size; i++) {
        for (int j = 0; j < size - i; j++) {
            if (c[j + 1] < c[j]) {
                int t = c[j + 1];
                c[j + 1] = c[j];
                c[j] = t;
            }
        }
    }
}
int main()
{
    int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
    int* m = a;
    int* n = b;
    cout << "数组a的17:" << fun1(5, m, 17) << endl;
    cout << "数组b的17:" << fun1(5, n, 17) << endl;

    //找到数组a与b中所有的素数

    vector <int> vi;
    for (int i = 0; i < 5; i++) {
        if (su(a[i]) == 1) {
            vi.push_back(a[i]);
        }
        if (su(b[i]) == 1) {
            vi.push_back(b[i]);
        }
    }

    sort(vi, vi.size());
    for (int i = 0; i < vi.size(); i++) {
        cout << vi[i] << ends;
    }

    return 0;
}

题目 3、类的定义与基本操作

#include <iostream>
#include <cmath>
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;
	}
	double getX()
	{
		return m_x;
	}
	double getY()
	{
		return m_y;
	}
private:
	friend double dist(const Point&a, const Point& b);
};

double dist(const Point& a, const Point& b) {
	double i = sqrt(a.m_x * a.m_x + a.m_y * a.m_y);
	return i;
 }
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 area()
	{
		const double PI = 3.14;
		double s = PI * m_radius * m_radius;
		return s;
	}
	double cir()
	{
		const double PI = 3.14;
		double j = 2 * PI * m_radius;
		return j;
	}
};

int main()
{
	Circle a(2, Point(1, 1));
	cout << "end" << endl;

	Point p(2, 3);
	Point m(4, 2);
	cout << "圆的横坐标,纵坐标为:" << p.getX() << "," << p.getY() << endl;
	cout << "两点间的距离:" << dist(p, m) << endl;
	Circle k(3, p);

	cout << "圆的面积:" <<k.area() << endl;
	cout << "圆的周长:" << k.cir() << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值