第四次

#ifndef GRAPH_H
#define GRAPH_H

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


#endif
// 类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」文档中展示的图形样式 
    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;
        }
        for(j=1;j<=size-i;j++)
        {
            cout<<' ';
        }
        cout<<endl;
    }    
}
#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; 
} 

// 类Fraction的声明 
class Fraction {
    public:    
        Fraction();            //构造函数
        Fraction(int t, int b);//重载
        Fraction(int t);       //重载
        void show();           //show函数
        void add(Fraction &f1);//
        void min(Fraction &f1);//
        void mul(Fraction &f1);//
        void div(Fraction &f1);//
        void compare(Fraction f1, Fraction f2);//比较
    private:
        int top;   //分子 
        int bottom;//分母 
};
#include "Fraction.h" 
#include <iostream>
using namespace std;

//Fraction函数
Fraction::Fraction() {     
    top = 0; bottom = 1;
}
Fraction::Fraction(int t, int b) { 
    top = t; bottom = b;
}
Fraction::Fraction(int t) {        
    top = t; bottom = 1;
}
 void Fraction::add(Fraction &f1) {   //加法
     Fraction f2;
     f2.top = top * f1.bottom + f1.top*bottom;
     f2.bottom = bottom * f1.bottom;
     f2.show();
}
 void Fraction::min(Fraction &f1) {    //减法
     Fraction f2;
     f2.top = top * f1.bottom - f1.top*bottom;
     f2.bottom = bottom * f1.bottom;
     f2.show();
 }
 void Fraction::mul(Fraction &f1) {    //乘法
     Fraction f2;
     f2.top =top*f1.top;
     f2.bottom =bottom*f1.bottom;
     f2.show();
 }
 void Fraction::div(Fraction &f1) {    //除法
     Fraction f2;
     f2.top =top*f1.bottom;
     f2.bottom = bottom * f1.top;
     f2.show();
 }
 void Fraction::show(){          //show函数
    cout<<top<<"/"<<bottom<<endl;
}    
 void Fraction::compare(Fraction f1, Fraction f2) {    //比较
     float a,b;
     a = f1.top / f1.bottom; 
     b = f2.top / f2.bottom;
     if (a <= b) { 
     cout << f1.top << "/" << f1.bottom << "<="; 
     f2.show(); cout << endl; 
     }
     if (a > b) { 
     cout << f1.top << "/" << f1.bottom << ">"; 
     f2.show(); cout << endl;
     }
 }
#include <iostream>
#include "Fraction.h"
using namespace std;

int main()
{
    Fraction f1;
    Fraction f2(5);
    Fraction f3(3, 4);
    f1.show(); 
    f2.show(); 
    f3.show();
    f2.min(f3);   //
    f2.add(f3);   //
    f2.mul(f3);   //
    f2.div(f3);   //
    f2.compare(f2, f3);  //比较
    return 0;
}

第一题:共有n行,第i行前后各有n-i个空格、2i-1个符号。

       这次实验我还不能做到单独结题,对类的应用还不熟练,还需要多练习。

猜你喜欢

转载自www.cnblogs.com/Fgw520/p/8907433.html