Class and Object Experiment 4

Experiment 4

Grahp

Declaration of class Grahp

#ifndef GRAPH_H
#define GRAPH_H

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


#endif

Implementation of class Grahp

// 类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() {
    int i,j;
    for(i=0;i<size;i++)
    {
        for(j=0;j<size+i;j++)
        {
            if(j<size-i-1)
            {
                cout<<" ";
            }
            else cout<<symbol;
        }
        cout<<endl;
    }
    // 补足代码,实现「实验4.pdf」文档中展示的图形样式 
}

Tests for class Graph: define objects of class Graph

#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; 
} 

Running results Running environment: DEV

Fraction class

Declaration of class Fraction

//类Fraction的声明 
    class Fraction {
        public:
            Fraction();//初始化 
            Fraction(int t,int b);//提供两个初始化参数 
            Fraction(int t);//只提供一个初始化参数 
            void plus(Fraction &p,Fraction &q);//加法 
            void subtract(Fraction &p,Fraction &q);//减法 
            void multiply(Fraction &p,Fraction &q);//乘法 
            void divide(Fraction &p,Fraction &q); //除法 
            void compare(Fraction &q);//两个分数比较
            void roft(Fraction &q);//分母通分
            void roaf();//分母约分 
            void show();
        private:
            int top;
            int bottom;
    };

Implementation of class Fraction

//类 Fraction 的实现

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

//构造函数的实现
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::plus(Fraction &p,Fraction &q){// 加法 
    top=p.top+q.top;
    bottom=p.bottom;
}
void Fraction::subtract(Fraction &p,Fraction &q){//减法 
    top=p.top-q.top;
    bottom=p.bottom;
}
void Fraction::multiply(Fraction &p,Fraction &q){//乘法 
    top=p.top*q.top;
    bottom=p.bottom*q.bottom;
} 
void Fraction::divide(Fraction &p,Fraction &q){//除法 
    top=p.top*q.bottom;
    bottom=p.bottom*q.top;
}
//通分 
void Fraction::roft(Fraction &q){
    bottom*=q.bottom;
    top*=q.bottom;
    q.top*=bottom/q.bottom;
    q.bottom=bottom;
}
//约分 
void Fraction::roaf(){
    int temp;
    temp=min(top,bottom);
    while(1)
    {
        if(top%temp==0&&bottom%temp==0)
        {
            break;
        }
        temp--;
    }
    top/=temp;
    bottom/=temp;
} 
//分数比较 
void Fraction::compare(Fraction &q){
    if(top>q.top)
    {
        cout<<"a>b"<<endl; 
    }
    else cout<<"a<b"<<endl;
}
void Fraction::show(){//输出 
    cout<<top<<"/"<<bottom<<endl;
}

main function implementation

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

int main() 
{
    Fraction a(1,2),b(1,3),x;//定义三个变量 
    x.multiply(a,b);//乘法 
    x.show();//输出 积 
    x.divide(a,b);//除法 
    x.show();//输出 商 
    a.roft(b);//通分 
    x.plus(a,b);//加法 
    x.roaf();//约分 
    x.show();//输出 和 
    x.subtract(a,b);//减法 
    x.roaf();//约分 
    x.show();//输出 差 
    a.compare(b);//比较大小 
    return 0;
}

Running result: Running environment: DEV

This experiment has given me a deeper understanding of classes and objects, but I have not yet reached the level of proficiency. There are many simplified operations that I can think of, but the current ability is limited and cannot be realized, such as the second experiment. , you can directly put it into addition and subtraction. There are still many defects in the function. In the future, I will work harder to learn and improve the code and improve my ability.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324643729&siteId=291194637