码迷,mamicode.com
首页 > 其他好文 > 详细

【实验4】类与对象2

时间:2018-04-23 00:23:27      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:iostream   top   ret   分享图片   通过   src   bottom   body   margin   

1、Graph

1)用for循环内部定义ijk三个变量,i表示当前行,j控制空格输出,k控制符号输出。在第i行,输出size-i个空格,然后输出i*2-1个符号,最后换行后i++进行下一行的输出。

(2)

 

//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

 

//graph.cpp
// 类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」文档中展示的图形样式 
    for(int i = 1; i <= size; i++)
    {
        for(int j = 1; j <= size - i; j++)
        {
            cout << " ";
        }
        for(int k = 1; k <= i*2-1; k++)
        {
            cout << symbol;
        }
        cout << endl;
    } 
}
//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; 
} 

(3)dev C++运行结果截图:

技术分享图片

 

 

2、Faction

(1)

Fraction

-top      : int

-bottom   : int

+Faction()

+Faction( t0 : int )

+Faction(int t0,int b0)

+plus( &f2 : Faction ) : void

+minus( &f2 : Faction ) : void

+multiply( &f2 : Faction ) : void

+divide( &f2 : Faction ) : void

+compare( &f2 : Faction ) : void

+show() : void

 

(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;
    }
}
//main.cpp 

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

(3)在Dev C++环境下运行截图:

技术分享图片

 

 

思考:Graph问题中,一开始还思考怎么能用一个参数实现符号两边有相同数量空格,结果忽然发现输出符号之后直接换行就可以了……感觉我的思维还不是代码思维……

   Faction问题中,中途发生了很多问题……对于类的概念可能还不深刻。

 

【实验4】类与对象2

标签:iostream   top   ret   分享图片   通过   src   bottom   body   margin   

原文地址:https://www.cnblogs.com/Werol714/p/8910132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!