码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 实验3 类和对象

时间:2019-04-22 00:42:49      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:hide   eve   efi   sys   app   void   char   div   return   

Part 2

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

 技术图片技术图片

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 fractionadd(Fraction &f, Fraction &p);
    void fractionmin(Fraction &f, Fraction &p);
    void fractionmul(Fraction &f, Fraction &p);
    void fractiondiv(Fraction &f, Fraction &p);
    void fractioncom(Fraction &f, Fraction &p);
    void show();
private:
    int top;
    int bottom;
};
#endif // !FRACTION_H#pragma once
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::fractionadd(Fraction &f, Fraction &p) {
    int t1, b1, t2, b2, m, n, temp, x, y, z;
    t1 = f.top;
    t2 = p.top;
    b1 = f.bottom;
    b2 = p.bottom;
    y = b1 * b2;
    x = t1 * b2 + t2 * b1;
    m = x;
    n = y;
    if (m < n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for (z = n;z >= 1;z--)
    {
        if (x%z == 0 && y%z == 0) break;
    }
    x = x / z;
    y = y / z;
    cout << x << "/" << y << endl;
}

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

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

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

void Fraction::fractioncom(Fraction &f, Fraction &p) {
    int t1, t2, b1, b2, x, y;
    t1 = f.top;
    t2 = p.top;
    b1 = f.bottom;
    b2 = p.bottom;
    y = b1 * b2;
    x = t1 * b2 - t2 * b1;
    if (x < 0) cout << f.top << "/" << f.bottom << "<" << p.top << "/" << p.bottom << endl;
    else if (x > 0) cout << f.top << "/" << f.bottom << ">" << p.top << "/" << p.bottom << endl;
    else if (x == 0) cout << f.top << "/" << f.bottom << "=" << p.top << "/" << p.bottom << endl;
}
fraction.app
技术图片
#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");
}
mian.cpp

技术图片

 

C++ 实验3 类和对象

标签:hide   eve   efi   sys   app   void   char   div   return   

原文地址:https://www.cnblogs.com/wyy0204/p/10744290.html

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