C++ 实验四

void Graph::draw() {
    int i, j;
    for(i=0;i<size+1;i++){                     //第一层循环控制层数
        for (j = 0; j < size - i; j++) {     //第二层循环控制空格数
            cout << ' ';
        }
        for (j = 0; j < 2 * i- 1;j++ ) {     //第二层循环控制输出符号数
            cout << symbol;
        }
        cout << endl;
}
}

project1.h

#include<iostream>
using namespace std;
class Fraction{
public:
    Fraction(int t,int b);
    Fraction(int t);
    Fraction() {
        top = 0;
        bottom = 1;
    }
    void show() { cout << top << "/" << bottom << endl; };
    void add(Fraction x);
    void min(Fraction x);
    void mul(Fraction x);
    void div(Fraction x);
    void com(Fraction x);

private:
    int top;
    int bottom;
};

project1.cpp

#include "project1.h"
#include <iostream>
using namespace std;


Fraction::Fraction(int t,int b):top(t),bottom(b){}
Fraction::Fraction(int t):top(t),bottom(1){}
void Fraction::add(Fraction x) {
    bottom *= x.bottom;
    top = top * x.bottom + x.top*bottom;
}
void Fraction::min(Fraction x) {
    bottom *= x.bottom;
    top = top * x.bottom - x.top*bottom;
}
void Fraction::mul(Fraction x) {
    top *= x.bottom;
    bottom *= x.bottom;
}
void Fraction::div(Fraction x) {
    top *= x.bottom;
    bottom *= x.bottom;
}
void Fraction::com(Fraction x) {
    if (top*x.bottom > bottom*x.top)
        cout << top << "/" << bottom << ">" << x.top << "/" << x.bottom << endl;
    else cout << top << "/" << bottom << "<" << x.top << "/" << x.bottom << endl;
}

main.cpp

#include <iostream>
#include "project1.h"
using namespace std;

int main() {
    Fraction a;
    Fraction b(3, 4);
    Fraction c(5);
    a.show();
    b.show();
    c.show();
    system("pause");
    return 0;
}

总结

这次实验第一题还行,就是金字塔显示我想了一会,如何循环我注释出来了

第二题和上次实验有题有点类似,我参考了一下上次实验的题写的代码--,运行是没错,要是前面有错可以点一下,我觉得我是看不出来了

原文地址:https://www.cnblogs.com/nuo26/p/8922355.html