实践4


#include <iostream> #include "fraction.h" using namespace std; Fraction::Fraction(int t,int b){ top=t;bottom=b; } Fraction::Fraction(int t){ top=t;bottom=1; } Fraction::Fraction(){ top=0;bottom=1; } void Fraction::divide(Fraction &f){ top = f.top*bottom; bottom = f.bottom*top; output(); } void Fraction::add(Fraction &f) { top = f.top*bottom + f.bottom*top; bottom = f.bottom*bottom; output(); } void Fraction::minus(Fraction &f) { top = f.top*bottom - f.bottom*top; bottom = f.bottom*bottom; output(); } void Fraction::multiply(Fraction &f) { top = f.top*top; bottom = f.bottom*bottom; output();} void Fraction::compare(Fraction &f) { if(f.top/f.bottom>top/bottom) cout<<f.top/f.bottom<<'>'<<top/bottom<<endl; else if(f.top/f.bottom<top/bottom) cout<<f.top/f.bottom<<'<'<<top/bottom<<endl; else if(f.top/f.bottom==top/bottom) cout<<f.top/f.bottom<<'='<<top/bottom<<endl; } void Fraction::output() { cout <<top << '/' << bottom << endl; }
#include <iostream>
#include "fraction.h"
using namespace std;
int main() {
    Fraction a;
    Fraction b(3, 4);
    Fraction c(5);
    Fraction n;
    n.add(b);
    n.output();
    n.minus(b);
    n.output();
    n.multiply(b);
    n.output();
    n.divide(b);
    n.output();
    n.compare(b);
    return 0;
}
    

1

#ifndef GRAPH_H
#define GRAPH_H

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


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


int main() {
    Graph graph1('*',5), graph2('$',7) ;  // 定义Graph类对象graph1, graph2 
    graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 
    graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    
    return 0; 
} 
// 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
//       size和symbol是类Graph的私有成员数据 
void Graph::draw() {
    // 补足代码,实现「实验4.pdf」文档中展示的图形样式 
}

猜你喜欢

转载自www.cnblogs.com/llyl/p/8922720.html