码迷,mamicode.com
首页 > 其他好文 > 详细

实验3

时间:2019-04-21 17:23:12      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:帮助   one   style   rac   top   temp   cti   show   lse   

技术图片
// 类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=1;i<=size;i++) 
    {for(j=1;j<=size-i;j++)
    cout<<" ";
    for(k=1;k<=2*i-1;k++)
       cout<<symbol;
       cout<<" ";
       cout<<endl; 
    }
}
garph.cpp
技术图片
#ifndef GRAPH_H
#define GRAPH_H

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


#endif
graph.h
技术图片
#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.cpp

技术图片

技术图片

3.设计定义并实现分数类fraction

 

技术图片
#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 &f2);
    void min(Fraction &f1, Fraction &f2);
    void mul(Fraction &f1, Fraction &f2);
    void div(Fraction &f1, Fraction &f2);
    void com(Fraction &f1, Fraction &f2);
    void show();
private:
    int top;
    int bottom;
};
#endif
fraction.h

 

技术图片
#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::add(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::min(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::mul(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::div(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::com(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;
}
fraction.cpp
技术图片
#include <iostream>
#include "Fraction.h"
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");
return 0;
}
main.cpp

技术图片

总结:1.对于第三题,自己花了很长的时间都没有思路,还是靠了舍友的帮助,参考了别人,一点点让别人教我呜呜呜,太菜了。

           2.还是自己掌握的不太到位,需要在方面多花点心思去看书,去理解。

           3.希望自己能一点点进步吧。

 

实验3

标签:帮助   one   style   rac   top   temp   cti   show   lse   

原文地址:https://www.cnblogs.com/charlotte00/p/10745672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!