实验三

//main.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; }
//Graph.h
#ifndef GRAPH_H
#define GRAPH_H
class Graph
{
    public:
        Graph(char c,int n);
        void draw();
        
    private:
        char c;
        int n;        
};
#endif
//Garph.cpp
#include "Graph.h"
#include<iostream>
using namespace std;
Graph::Graph(char c,int n):c(c),n(n)
{
    cout<<"Construction is called"<<endl;
}
void Graph::draw()
{
    for (int i=1;i<=n;i++)
    {
        for (int j=0;j<n-i;j++)
            cout<<' ';
        for (int j=0;j<2*i-1;j++)
            cout<<c;
        for (int j=0;j<2*n-1;j++)
            cout<<' ';
        cout<<endl;
    }
}

 

//main.cpp
#include <iostream> #include"Fraction fraction.h" using namespace std; void add(Fraction a,Fraction b) { int cbottom=a.bottom*b.bottom; int ctop=a.top*b.bottom+a.bottom*b.top; cout<<ctop<<"/"<<cbottom<<endl; } void min(Fraction a,Fraction b) { int cbottom=a.bottom*b.bottom; int ctop=a.top*b.bottom-a.bottom*b.top; cout<<ctop<<"/"<<cbottom<<endl; } void mul(Fraction a,Fraction b) { int ctop=a.top*b.top; int cbottom=a.bottom*b.bottom; cout<<ctop<<"/"<<cbottom<<endl; } void div(Fraction a,Fraction b) { int ctop=a.top*b.bottom; int cbottom=a.bottom*b.top; cout<<ctop<<"/"<<cbottom<<endl; } int main () { Fraction a; Fraction b(3,4); Fraction c(5); add(c,b); min(c,b); mul(c,b); div(c,b); return 0; }
//fraction.h
#ifndef FRACTION_H
#define FRACTION_H #include <iostream> using namespace std; class Fraction { public: Fraction(int top=0,int bottom=1); friend void add(Fraction a,Fraction b); friend void min(Fraction a,Fraction b); friend void mul(Fraction a,Fraction b); friend void div(Fraction a,Fraction b); private: int top,bottom; }; #endif
//fraction.cpp
#include"Fraction fraction.h" #include<iostream> using namespace std; Fraction::Fraction(int top,int bottom):top(top),bottom(bottom){}

 

posted on 2019-04-18 12:37  happy-every-day  阅读(85)  评论(1编辑  收藏  举报