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

实验三

时间:2019-04-23 23:55:45      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:实现   比较大小   hid   显示   图片   col   add   tom   eve   

part1

技术图片 

Part2 

技术图片
#include <iostream>
#include "graph.h"
using namespace std;
int main() {
    Graph graph1(*, 5);
    graph1.draw();
    system("pause");
    Graph graph2($, 7);
    graph2.draw();
    system("pause");
    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.h
技术图片
#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 << endl;

    }
}
graph.cpp

技术图片

part3

技术图片
#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();
    Fraction e;
    e.add(b, c);
    e.minus(b, c);
    e.multiply(b, c);
    e.division(b, c);
    e.compare(b, c);
}
main
技术图片
#include"Fraction.h"
#include<iostream>
#include<cmath>
using namespace std;
Fraction::Fraction(int top0, int bottom0) :top(top0), bottom(bottom0) {
}


int gongyueshu(int m, int n)
{
    int  x, y, i, t;
    x = abs(m);
    y = abs(n);
    for (i = 1;i <= x;i++)
        if (x%i == 0 && y%i == 0)
            t = i;
    return t;
}


int gongbeishu(int m, int n)
{
    int  x, y, i, t;
    x = abs(m);
    y = abs(n);
    for (i = 1;i <= x;i++)
        if (x%i == 0 && y%i == 0)
            t = i;
    return m * n / t;
}


void Fraction::judge() {
    if (bottom < 0 && top>0) {
        bottom = 0 - bottom;
        top = 0 - top;
    }
    else if (bottom == 0) {
        cout << "This score makes no sense." << endl;
    }
    else if (bottom < 0 && top < 0) {
        bottom = 0 - bottom;
        top = 0 - top;
    }
}

void Fraction::add(Fraction a, Fraction b) {
    bottom = gongbeishu(a.bottom, b.bottom);
    top = (bottom / a.bottom)*a.top + (bottom / b.bottom)*b.top;
    judge();
    cout << "相加:";
    cout << top / gongyueshu(abs(top), abs(bottom)) << "/" << bottom / gongyueshu(abs(top), abs(bottom)) << endl;
}


void Fraction::minus(Fraction a, Fraction b) {
    bottom = gongbeishu(a.bottom, b.bottom);
    top = (bottom / a.bottom)*a.top - (bottom / b.bottom)*b.top;
    judge();
    cout << "相减:";
    cout << top / gongyueshu(abs(top), abs(bottom)) << "/" << bottom / gongyueshu(abs(top), abs(bottom)) << endl;
}

void Fraction::multiply(Fraction a, Fraction b) {
    top = a.top*b.top;
    bottom = a.bottom*b.bottom;
    judge();
    cout << "相乘:";
    cout << top / gongyueshu(abs(top), abs(bottom)) << "/" << bottom / gongyueshu(abs(top), abs(bottom)) << endl;

}

void Fraction::division(Fraction a, Fraction b) {
    top = a.top*b.bottom;
    bottom = a.bottom*b.top;
    judge();
    cout << "相除:";
    cout << top / gongyueshu(abs(top), abs(bottom)) << "/" << bottom / gongyueshu(abs(top), abs(bottom)) << endl;

}

void Fraction::compare(Fraction a, Fraction b) {
    double m, n;
    m = a.top / a.bottom;
    n = b.top / b.bottom;
    if (m > n) {
        cout<<"比较大小:"<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
    }
    else if (m < n) {
        cout<<"比较大小:"<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
    }
    else if (m == n) {
        cout<<"比较大小:"<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl;
    }
}

void Fraction::show() {
    judge();
    cout << top << "/" << bottom << endl;
}
fraction.cpp
技术图片
#ifndef FRACTION_H
#define FRACTION_H
class Fraction {
public:
    Fraction(int top0 = 0, int bottom0 = 1);
    void judge();
    void add(Fraction a, Fraction b);
    void minus(Fraction a, Fraction b);
    void multiply(Fraction a, Fraction b);
    void division(Fraction a, Fraction b);
    void compare(Fraction a, Fraction b);
    void show();


private:
    int top;
    int bottom;
};
#endif
fraction.h

技术图片

总结:程序总是出错,可能是dev或者vs的问题,也有可能是我写的程序不够严谨出错了。第三题花了很多时间也请教了别人,

总感觉不够细心老是漏掉条件,感觉类的知识掌握得不是很好。

实验三

标签:实现   比较大小   hid   显示   图片   col   add   tom   eve   

原文地址:https://www.cnblogs.com/jackyayue/p/10747659.html

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