ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验3

2019-04-21 13:38:45  阅读:200  来源: 互联网

标签:cout int Graph void 实验 Fraction include


#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; 
} 
#ifndef GRAPH_H
#define GRAPH_H

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


#endif
// 类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, m;
    for (i=1;i<=size;i++)
    {
        for (j=1;j<=size-i;j++)
            cout << " ";
        for (m=1;m<=(2*i-1);m++)
            cout << symbol;
        for (j=1;j<=size-i;j++)
            cout << " ";
        cout<<endl;
    }

}

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 fractionadd(Fraction &f1, Fraction &f2);
    void fractionmin(Fraction &f1, Fraction &f2);
    void fractionmul(Fraction &f1, Fraction &f2);
    void fractiondiv(Fraction &f1, Fraction &f2);
    void fractioncom(Fraction &f1, Fraction &f2);
    void show();
private:
    int top;
    int bottom;
};
#endif
#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 &f2) {
    int t1, b1, t2, b2, m, n, temp, x, y ,i;
    t1 = f1.top;
    t2 = f2.top;
    b1 = f1.bottom;
    b2 = f2.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 &f2) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = f1.top;
    t2 = f2.top;
    b1 = f1.bottom;
    b2 = f2.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 &f2) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = f1.top;
    t2 = f2.top;
    b1 = f1.bottom;
    b2 = f2.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 &f2) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = f1.top;
    t2 = f2.bottom;
    b1 = f1.bottom;
    b2 = f2.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 &f2) {
    int t1, t2, b1, b2, x, y;
    t1 = f1.top;
    t2 = f2.top;
    b1 = f1.bottom;
    b2 = f2.bottom;
    y = b1 * b2;
    x = t1 * b2 - t2 * b1;
    if (x < 0) cout << f1.top << "/" << f1.bottom << "<" << f2.top << "/" << f2.bottom << endl;
    else if (x > 0) cout << f1.top << "/" << f1.bottom << ">" << f2.top << "/" << f2.bottom << endl;
    else if (x == 0) cout << f1.top << "/" << f1.bottom << "=" << f2.top << "/" << f2.bottom << endl;
}
#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.fractionadd(b, d);
    a.fractionmin(b, d);
    a.fractionmul(b, d);
    a.fractiondiv(b, d);
    a.fractioncom(b, d);

    system("pause");
}

标签:cout,int,Graph,void,实验,Fraction,include
来源: https://www.cnblogs.com/Tiger-Hu22/p/10744879.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有