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

实验4

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

标签:接口   代码   显示   成员   http   tput   字符   技术分享   png   

 1.实验内容2

  (1)size代表行数,i为当前行,j用来控制要输出的空格,k用来控制要输出的符号的个数,用for循环逐行输出

  (2)

#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() {
    int i, j, k, s;
    for (i = 1; i<=size; i++)
    {
        for (j = 1; j<=size - i; j++)
        {
            cout <<  ;
        }
        s = 1+2 * (i- 1);
        for (k = 1; k<= s; k++)
        {
            cout << symbol;
        }
        cout << endl;
    }
    // 补足代码,实现「实验4.pdf」文档中展示的图形样式 
}
#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)运行环境:vs2017

    技术分享图片

  2.实验内容3

class Fraction {
public:
    Fraction();
    Fraction(int a);
    Fraction(int a, int b);
    void plus(Fraction &p);
    void minus(Fraction &p);
    void multiply(Fraction &p);
    void divide(Fraction &p);
    void compare(Fraction &p);
    void output();
private:
    int top;
    int bottom;

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


Fraction::Fraction()  {
    top = 0;
    bottom = 1;
}
Fraction::Fraction(int a) {
    top = a;
    bottom = 1;
}
Fraction::Fraction(int a, int b) {
    top = a;
    bottom = b;
}

void Fraction::plus(Fraction &p) {
    top = p.top*bottom + p.bottom*top;
    bottom = p.bottom*bottom;
    output();
}
void Fraction::minus(Fraction &p) {
    top = p.top*bottom - p.bottom*top;
    bottom = p.bottom*bottom;
    output();
}
void Fraction::multiply(Fraction &p) {
    top = p.top*top;
    bottom = p.bottom*bottom;
    output();
}
void Fraction::divide(Fraction &p) {
    top = p.top*bottom;
    bottom = p.bottom*top;
    output();
}
void Fraction::compare(Fraction &p) {
    if (p.top / p.bottom > top / bottom)
        cout << "t>b" << endl;
    else if (p.top / p.bottom < top / bottom)
        cout << "t<b" << endl;
    else if (p.top / p.bottom == top / bottom)
        cout << "t=b" << endl;
}
void Fraction::output() {
        cout << "The result is:"<< top << / << bottom << endl;
    }
#include <iostream>
#include "fraction.h"
using namespace std;

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

运行结果截图:技术分享图片

实验4

标签:接口   代码   显示   成员   http   tput   字符   技术分享   png   

原文地址:https://www.cnblogs.com/breadbaek/p/8910324.html

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