实验四

项目1.(1)类的定义  graph.h

#ifndef GRAPH_H
#define GRAPH_H

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


#endif

 

  (2)类的实现 graph.cpp

// 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(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=1;j<=2*size-1;j++)
        {
            if(j>=size-i&&j<=size+i)
            {
                cout<<symbol;
            }
            else
                cout<<" ";
        }
        cout<<endl;
    }
    // 补足代码,实现「实验4.pdf」文档中展示的图形样式 
}

(3)主函数 main.cpp

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

 

2.

//Faction.h 

#ifndef FACTION_H
#define FACTION_H

class Faction
{
    public:
        Faction();                           //构造函数 
        Faction(int t0);                     //构造函数的重载 
        Faction(int t0,int b0);              //构造函数的重载 
        void plus(Faction &f2);              //加法函数 
        void minus(Faction &f2);             //减法函数 
        void multiply(Faction &f2);          //乘法函数 
        void divide(Faction &f2);            //除法函数 
        void compare(Faction &f2);           //比较大小函数 
        void show();                         //输出函数 
    private:
        int top;     //分子 
        int bottom;     //分母 
};

#endif

//Faction.cpp 

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

//构造函数 
Faction::Faction():top(0), bottom(1) {
}

//构造函数的重载        
Faction::Faction(int t0):top(t0), bottom(1) {
}

//构造函数的重载        
Faction::Faction(int t0,int b0):top(t0), bottom(b0){
}
        
//加法函数
void Faction::plus(Faction &f1){
    Faction f2;
    f2.top = top * f1.bottom + f1.top*bottom;
    f2.bottom = bottom * f1.bottom;
    f2.show();
} 

//减法函数
void Faction::minus(Faction &f1){
    Faction f2;
    f2.top = top * f1.bottom - f1.top*bottom;
    f2.bottom = bottom * f1.bottom;
    f2.show();
} 

//乘法函数
void Faction::multiply(Faction &f1){
    Faction f2;
    f2.top =top*f1.top;
    f2.bottom =bottom*f1.bottom;
    f2.show();
} 

//除法函数
void Faction::divide(Faction &f1) {
    Faction f2;
    f2.top =top*f1.bottom;
    f2.bottom = bottom * f1.top;
    f2.show();
}

//输出函数
void Faction::show(){
    cout << top << "/" << bottom <<endl;
} 

//比较大小函数
void Faction::compare(Faction &f1) {
    Faction f2;
    f2.top = top * f1.bottom - f1.top*bottom;
    f2.bottom = bottom * f1.bottom;
    if(f2.bottom*f2.top > 0){
        cout << top << "/" << bottom << ">" << f1.top << "/" << f1.bottom <<endl;
    }
    else {
        cout << top << "/" << bottom << "<=" << f1.top << "/" << f1.bottom <<endl;
    }
}
#include <iostream>
#include "Faction.h"
using namespace std;

int main() {
    Faction a;
    Faction b(3,4);
    Faction c(5);
    a.show();
    b.show();
    c.show();
    b.plus(c);
    b.minus(c);
    b.multiply(c);
    b.divide(c);
    b.compare(c);
    return 0;
}

 运行截图

这次的实验涉及的知识点比较多,在写的时候遇到了一些难题。我的程序中还有一些小错误(解决不了唉   o(╯□╰)o   ),希望各位同学指正。

 

转载于:https://www.cnblogs.com/baiyixin/p/8922347.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值