2020-12-25
說明執行下列語句後,分別執行的什麼操作,會輸出什麼?
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);
在上述類的定義基礎上,完善下列操作:
1)顯示定義解構函式;
2)獲取分數的分子;
3)獲取分數的分母;
4)實現分數的約分;
5)實現對兩個分數物件進行通分;
6)使用operator/操作符過載實現兩個分數的除法運算。
#include <iostream>
using namespace std;
class Fraction{
//資料成員,訪問控制屬性預設是私有
int m_numerator = 0; // 分子預設為0; C++11
int m_denominator = 1; //分母預設為1;
int gcd(int x, int y);
public://公有成員函式
int numerator()const { return m_numerator; }
int denominator()const { return m_denominator; }
int getnumerator()const { return m_numerator; }
int getdenominator()const { return m_denominator; }
double value()const;
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;//複製建構函式,複製優化
}
void makeCommon(Fraction& a, Fraction& b)
;~Faction() {
cout << "Destory"<<endl;
}
};
//宣告,定義makeCommon函式
void makeCommom(Fraction& a, Fraction& b) {
a.m_numerator *= b.m_denominator;
b.m_numerator *= a.m_denominator;
b.m_denominator = a.m_denominator *= b.m_denominator;
}
//定義gcd 函式
int Fraction::gcd(int x, int y) {
if (y != 0)
gcd(y, x % y);
else
return x;
}
//定義value函式
double Fraction::value()const {
return static_cast<double>(m_numerator) / m_denominator;
}
//定義divide函式
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 operator/(const Fraction& left, const Fraction& right) {
Fraction result(left.numerator() *= right.denominator(),
left.denominator() *= right.numerator());
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中的資料,按從小到大順序重新排序。要求依次實現:
1)編寫順序查詢法函式和折半查詢法函式,分別在陣列a和陣列b中查詢元素17所在的下標並輸出。
2)編寫判斷素數函式和排序函式,並對容器c中的結果進行輸出。
#include <iostream>
#include <vector>
using namespace std;
//定義順序查詢函式
int shunxu(int a[], int high, int key) {
for (int i = 0; i < high; i++)
if (a[i] == key)
cout << i << endl;
else
cout << "陣列中無此數;" << endl;
return 0;
}
//定義折半查詢函式low,high,middle
int zheban(int a[], int number, int key) {
int low = 0;
int middle = 0;
int high = number - 1;
//在low<=high的時候才可以查詢
while (low <= high)
{
middle = (low + high) / 2;
if (a[middle] == key)
cout << middle << endl;
if (a[middle] < key)
{
low = middle + 1;
//更改搜素的範圍,中間值小於目標值,目標值在中間的下一位到末尾;
}
else
{
//中間值大於目標值,則目標值在中間的上一位到最前;
high = middle - 1;
}
cout << "無此數:";
}
return 0;
}
//定義判斷素數的函式
bool is_sushu(int shuzi) {
for (int i = 2; i < shuzi; i++)
{
if ((shuzi % i) != 0)
{
if (i == shuzi - 1)
return true;
}
else
{
return false;
}
}
}
//定義新增素數vector容器,並將重複的素數刪除
vector <int> xinzeng(int *a,int a_number,int* b,int b_number)
{
vector <int> add;
for (int i = 0; i < a_number; i++)
{
if (is_sushu(a[i]))
{
add.push_back(a[i]);
}
}
//將陣列a中的素數從尾部輸入到vector add中
for (int i = 0; i < b_number; i++)
{
if (is_sushu(b[i]))
{
add.push_back(b[i]);
}
//將陣列b中的素數從尾部輸入到vector add 中;
for (int j = 0; j < add.size(); j++)
{
if (add[j] == b[i])
{
add.pop_back();
}
//b是從尾部輸入的,可以從尾部開始刪除與a輸入到vector add中的重複的;
}
}
return add;
}
vector<int>change(vector<int>& add)
{
int high = add.size();
for (int i = 0; i < high; i++)
{
for (int j = 1; j < high; j++)
{
if (add[i] > add[j])
{
int x = add[i];
add[i] = add[j];
add[j] = x;
}
}
}
return add;
}
int main()
{
int a[5] = { 19,67,24,11,17 };
int b[5] = { 2,3,9,17,59 };
vector<int> add;
add = xinzeng(a, 5, b, 5);
//呼叫新增函式
add = change(add);
for (int i = 0; i < add.size(); i++)
{
cout << add[i] << endl;
}
}
題目 3、類的定義與基本操作
1)說明上述程式執行流程和輸出結果;
2)在Point類中完善獲取點的橫座標、獲取點的縱座標成員函式,並在主函式中測試;
3)通過友元函式實現平面上任意兩個點的距離計算輔助函式;
4)在Circle類中完善圓的面積計算與圓的周長計算成員函式,並在主函式中測試;
#include<iostream>
using namespace std;
class Point {
private:
double m_x = 0, m_y = 0;
friend double distance(Point& A, Point& B);//友元函式,宣告求距離的函式;
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 get_x()const { return m_x; }//宣告得到橫座標函式
double get_y()const { return m_y; }//宣告獲取縱座標函式
};
double distance(Point& A, Point& B)
{
double x1 = A.m_x, y1 = A.m_y;
double x2 = B.m_x, y2 = B.m_y;
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}//定義求距離函式
class Circle {
private:
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();//宣告面積函式
double L();//宣告周長函式
};
double Circle::S()
{
return m_radius * m_radius * 3.1415;
}//定義面積函式;
double Circle::L()
{
return 2 * 3.1415 * m_radius;
}//定義周長函式;
int main()
{
Circle a(2, Point(1, 1));
cout << a.S() << endl;//.訪問和呼叫面積函式
cout << a.L() << endl;//.訪問和呼叫面積函式
cout << "end" << endl;
Point A(2, 2);
Point B(1, 1);
cout << distance(A, B) << endl;
return 0;
}