实验三

Graph类的实现:

源代码;

Graph.h:

#ifndef GRAPH_H
#define GRAPH_H
class Graph{
    public:
        Graph(char ch,int n);
        void draw();
    private:
        char symbol;
        int size;
};
#endif

Graph.cpp:

#include "graph.h"
#include <iostream>
using namespace std;
Graph::Graph(char ch,int n):symbol(ch),size(n){
}
void Graph::draw(){
    for(int i=1;i<=size;i++)
    {
        for(int j=1;j<=size-i;j++)
        {
            cout<<" ";
        }
        for(int j=size-i;j<=size+i-1;j++)
        {
            cout<<symbol;
        }
        cout<<endl;
    }
}

main.cpp:

#include <iostream>
#include "graph.h"
using namespace std;
int main()
{
    Graph graph1('*',5);
    graph1.draw();
    system("pause");
    Graph graph2('$',7);
    graph2.draw();
    return 0;
}

运行截图:

分数类fraction:

源代码:

fraction.h:

#ifndef FRACTION_H
#define FRACTION_H
class Fraction
{
    public:
        Fraction(int top0=0,int bottom0=1):top(top0),bottom(bottom0) {}
        friend void add(Fraction a,Fraction b);
        friend void subtract(Fraction a,Fraction b);
        friend void multiply(Fraction a,Fraction b);
        friend void divide(Fraction a,Fraction b);
        friend void compare(Fraction a,Fraction b);
    private:
        int top;
        int bottom;
};
#endif

fraction.cpp:

#include "fraction.h"
#include <iostream>
using namespace std;
void add(Fraction a,Fraction b)
{
    int x,y;
    x=a.top*b.bottom+a.bottom*b.top;
    y=a.bottom*b.bottom;
    cout<<x<<'/'<<y<<endl;
}
void subtract(Fraction a,Fraction b)
{
    int x,y;
    x=a.top*b.bottom-a.bottom*b.top;
    y=a.bottom*b.bottom;
    cout<<x<<'/'<<y<<endl;
}
void multiply(Fraction a,Fraction b)
{
    int x,y;
    x=a.top*b.top;
    y=a.bottom*b.bottom;
    cout<<x<<'/'<<y<<endl;
}
void divide(Fraction a,Fraction b)
{
    int x,y;
    x=a.top*b.bottom;
    y=a.bottom*a.top;
    cout<<x<<'/'<<y<<endl;
}
void compare(Fraction a,Fraction b)
{
    int x;
    x=a.top*b.bottom-a.bottom*b.top;
    if(x>0)
    {
        cout<<'>'<<endl;
    }
    else if(x<0)
    {
        cout<<'<'<<endl;
    }
    else if(x=0)
    {
        cout<<'='<<endl;
 }
}

main.cpp:

#include "fraction.h"
#include <iostream>
int main()
{
    Fraction a;
    Fraction b(3,4);
    Fraction c(5);
    add(b,c);
    subtract(b,c);
    multiply(b,c);
    divide(b,c);
    compare(b,c);
    return 0; 
}

运行结果:

觉得本项目还需要加入化简等函数进行优化

转载于:https://www.cnblogs.com/csl-40/p/10740370.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值