实验三

 

part 2

#include <iostream>
#include "graph.h"
using namespace std;

int main() {
    Graph graph1('*',5);
    graph1.draw();
    
    system("pause");
    system("cls");
    
    Graph graph2('$',7);
    graph2.draw();
    
    return 0; 
} 
main
#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明 
class Graph {
    public:
        Graph(char ch, int n);   // 带有参数的构造函数 
        void draw();     // 绘制图形 
    private:
        char symbol;
        int size;
};


#endif
graph
// 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
void Graph::draw(){
     int i,j,k;
     for(i=0;i<size;i++)
    {   for(j=0;j<size-i;j++)
            cout<<" ";
        for(k=1;k<=2*i-1;k++)
            cout<<symbol;
            cout<<endl;
    }
    // 补足代码
    // ...
}
graph.h

part 3

#ifndef FRACTION_H
#define FRACTION_H

class Fraction {
public:
    Fraction(int t = 0, int b = 1) : top(t), bottom(b) {
    }
    Fraction(const Fraction &fr) : top(fr.top), bottom(fr.bottom) {
    }
    void add(Fraction &f1, Fraction &f);
    void min(Fraction &f1, Fraction &f);
    void mul(Fraction &f1, Fraction &f);
    void div(Fraction &f1, Fraction &f);
    void com(Fraction &f1, Fraction &f);
    void show();
private:
    int top;
    int bottom;
};
#endif
Fraction
#include"fraction.h"
#include<iostream>
using namespace std;
void Fraction::show()  {
    if (top == 0) cout << 0<<endl;
    else if (bottom == 1) cout << top << endl;
    else if (top / bottom < 0) cout << "-" << top << "/" << bottom << endl;
    else cout << top << "/" << bottom << endl;
}

void Fraction::fractionadd(Fraction &f1, Fraction &f) {
    int t1, b1, t2, b2, m, n, temp, x, y ,i;
    t1 = f1.top;
    t2 = f.top;
    b1 = f1.bottom;
    b2 = f.bottom;
    y = b1 * b2;
    x = t1 * b2 + t2 * b1;
    m = x;
    n = y;
    if(m<n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for(i=n;i>=1;i--)
    {
        if (x%i == 0 && y%i == 0) break;
    }
    x = x / i;
    y = y / i;
    cout << x << "/" << y << endl;
}

void Fraction::fractionmin(Fraction &f1, Fraction &f) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = f1.top;
    t2 = f.top;
    b1 = f1.bottom;
    b2 = f.bottom;
    y = b1 * b2;
    x= t1 * b2 - t2 * b1;
    m = x;
    n = y;
    if (m < n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for (i = n; i >= 1; i--)
    {
        if (x%i == 0 && y%i == 0) break;
    }
    x = x / i;
    y = y / i;
    cout << x << "/" << y << endl;
}

void Fraction::fractionmul(Fraction &f1, Fraction &f) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = f1.top;
    t2 = f.top;
    b1 = f1.bottom;
    b2 = f.bottom;
    y = b1 * b2;
    x = t1 * t2;
    m = x;
    n = y;
    if (m < n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for (i = n; i >= 1; i--)
    {
        if (x%i == 0 && y%i == 0) break;
    }
    x = x / i;
    y = y / i;
    cout << x << "/" << y << endl;
}

void Fraction::fractiondiv(Fraction &f1, Fraction &f) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = f1.top;
    t2 = f.bottom;
    b1 = f1.bottom;
    b2 = f.top;
    y = b1 * b2;
    x = t1 * t2;
    m = x;
    n = y;
    if (m < n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for (i = n; i >= 1; i--)
    {
        if (x%i == 0 && y%i == 0) break;
    }
    x = x / i;
    y = y / i;
    cout << x << "/" << y << endl;
}

void Fraction::fractioncom(Fraction &f1, Fraction &f) {
    int t1, t2, b1, b2, x, y;
    t1 = f1.top;
    t2 = f.top;
    b1 = f1.bottom;
    b2 = f.bottom;
    y = b1 * b2;
    x = t1 * b2 - t2 * b1;
    if (x < 0) cout << f1.top << "/" << f1.bottom << "<" << f.top << "/" << f.bottom << endl;
    else if (x > 0) cout << f1.top << "/" << f1.bottom << ">" << f.top << "/" << f.bottom << endl;
    else if (x == 0) cout << f1.top << "/" << f1.bottom << "=" << f.top << "/" << f.bottom << endl;
}
Fraction.h
#include"fraction.h"
#include<iostream>
using namespace std;

int main() {
    Fraction a;
    a.show();
    Fraction b(3, 4);
    b.show();
    Fraction c(5);
    c.show();
    int x, y;
    cin >> x >> y;
    Fraction d(x, y);
    d.show();
    a.add(b, d);
    a.min(b, d);
    a.mul(b, d);
    a.div(b, d);
    a.com(b, d);

    system("pause");
}
main

 

实验总结:

1.对类的使用还是不太熟悉,掌握的不够深,容易忘记,还要翻书或者参考别人的才能写。

2.课后还是要多了解了解这方面的知识

 

posted @ 2019-04-21 14:05  小眉毛  阅读(124)  评论(0编辑  收藏  举报