实验3:类与对象

part2:

#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
// 类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 << endl;
    }
    
}
graph.cpp
#include <iostream>
#include "graph.h"
using namespace std;

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

截图

 

part3:

#ifndef FRACTION_H
#define FRACTION_H
//声名类
class fraction {
public:
    fraction(int a=0, int b=1);
    void add(fraction x, fraction y);
    void min(fraction x, fraction y);
    void mul(fraction x, fraction y);
    void div(fraction x, fraction y);
    void com(fraction x, fraction y);
    void show();
private:
    int top;
    int bottom;
};
#endif
fraction.h
#include "fraction.h" 
#include <iostream>
using namespace std;
fraction::fraction(int a, int b) :top(a), bottom(b) {

}
//
void fraction::add(fraction x, fraction y) {
    int t, r, a, b;
    top = x.top *y.bottom + y.top*x.bottom;
    bottom = x.bottom*y.bottom;
    if (top < bottom) { t = top; top = bottom; bottom = t; }
    a = top; b = bottom;
    r = top % bottom;
    while (r != 0)
        {
        top = bottom; bottom = r; r = top % bottom;
    }
    top = a / bottom;
    bottom = b / bottom;
}
//
void fraction::min(fraction x, fraction y) {
    int t, r, a, b;
    top = x.top *y.bottom - y.top*x.bottom;
    bottom = x.bottom*y.bottom;
    a = top; b = bottom;
    if (top < bottom) { t = top; top = bottom; bottom = t; }
    r = top % bottom;
    while (r != 0)
    {
        top = bottom; bottom = r; r = top % bottom;
    }
    top = a / bottom;
    bottom = b / bottom;
}
//
void fraction::mul(fraction x, fraction y) {
    int t, r, a, b;
    top = x.top *y.top;
    bottom = x.bottom*y.bottom;
    a = top; b = bottom;
    if (top < bottom) { t = top; top = bottom; bottom = t; }
    r = top % bottom;
    while (r != 0)
    {
        top = bottom; bottom = r; r = top % bottom;
    }
    top = a / bottom;
    bottom = b / bottom;
}
//
void fraction::div(fraction x, fraction y) {
    int t, r, a, b;
    top = x.top *y.bottom;
    bottom = x.bottom*y.top;
    a = top; b = bottom;
    if (top < bottom) { t = top; top = bottom; bottom = t; }
    r = top % bottom;
    while (r != 0)
    {
        top = bottom; bottom = r; r = top % bottom;
    }
    top = a / bottom;
    bottom = b / bottom;
}
//比较
void fraction::com(fraction x, fraction y) {
    top = x.top*y.bottom;
    bottom = y.top*y.bottom;
    if (top > bottom)
        cout<<'>';
    else
        cout<<'<';
}
//输出
void fraction::show() {
    if (bottom == 0)
        cout << "分母不能为零!" << endl;
    else if (bottom == 1)
        cout << top << endl;
    else
        cout << top << "/" << bottom << endl;
}
fraction.cpp
#include <iostream>
#include <cstdlib>
#include "fraction.h"
using namespace std;
int main() {
    fraction a;
    cout << "a=";
    a.show();
    
    fraction b1(3, 4);
    cout << "b1=";
    b1.show();
    
    fraction b2(1, 4);
    cout << "b2=";
    b2.show();
    
    fraction c(5);
    cout << "c="; 
    c.show();
    
    fraction c1;
    c1.add(b1, b2);
    cout << "b1+b2=";
    c1.show();
    
    c1.min(b1, b2);
    cout << "b1-b2=";
    c1.show();

    c1.mul(b1, b2);
    cout << "b1*b2=";
    c1.show();

    c1.div(b1,b2);
    cout << "b1/b2="; 
    c1.show();
    
    cout << "b1";
    c1.com(b1, b2);
    cout << "b2"<<endl;
    system("pause");
    return 0;
}
main.cpp

截图

猜你喜欢

转载自www.cnblogs.com/zxz2425405395/p/10745209.html