• c++第四次实验报告


    一·

    1.draw()函数算法简介:利用for的嵌套,外层for用来换行,内层第一个for语句是用来打印空格,第二个for语句用来打印符号。每行空格数为行数减去行的序数,每行符号个数为行序数乘以2减1。

    以下分别为(graph.h, graph.cpp, main.cpp)三个文件里的源码,其中类的定义文件和主函数实现文件与课件相比未做改动。

    #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() {
        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size - i; j++) {
                cout << " ";
            }
            for (int k = 1; k <= 2*i-1; k++) {
                cout << symbol;
            }
            cout << endl;
        }
    }
    #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


    二.

    1.

    Fraction

    -top:int

    -bottom:int

    +Fraction()

    +Fraction(top0:int,bottom:int)

    +Fraction(top0:int)

    +Fraction(f0:Fraction)

    +Add(f0:Fraction):void

    +Subtract(f0:Fraction) :void

    +Multiply(f0:Fraction) :void

    +Divide(f0:Fraction):void

    +Compare(f0:Fraction):void

    +set_fraction():void

    +get_fraction():void

    +~Fraction()

    2.Fracton.h

    class Fraction {
    public:
        Fraction();
        Fraction(int top0, int bottom0);
        Fraction(int top0);
        Fraction(Fraction&f0);
        void Add(Fraction&f0);
        void Subtract(Fraction&f0);
        void Multiply(Fraction&f0);
        void Divide(Fraction&f0);
        void Compare(Fraction&f0);
        void set_fraction();
        void get_fraction();
        ~Fraction();
    
    private:
        int top;
        int bottom;
    };

     Fraction.cpp

    #include"Fraction.h"
    #include<iostream>
    using namespace std;
    Fraction::Fraction():top(0),bottom(1) {}
    Fraction::Fraction(int top0) : top(top0), bottom(1){}
    Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){}
    Fraction::Fraction(Fraction&f0):top(f0.top),bottom(f0.bottom){}
    Fraction::~Fraction() {}
    void::Fraction::set_fraction() {
        cin >> top>> bottom;
    }
    void::Fraction::get_fraction() {
        cout << top << "/" << bottom << endl;
    }
    void::Fraction::Add(Fraction&f0) {
        int a=0, b=0;
        b = f0.bottom*bottom;
        a = f0.top*bottom + top * f0.bottom;
        cout << a << "/" << b << endl;
    }
    void::Fraction::Subtract(Fraction&f0) {
        int a=0, b=0;
        b = f0.bottom*bottom;
        a = top * f0.bottom - f0.top*bottom;
        cout << a << "/" << b << endl;
    }
    void::Fraction::Multiply(Fraction&f0) {
        int a=0, b=0;
        a = top * f0.top;
        b = f0.bottom;
        cout << a << "/" << b << endl;
    }
    void::Fraction::Divide(Fraction&f0) {
        int a=0, b=0;
        a = top * f0.bottom;
        b = bottom * f0.top;
        cout << a << "/" << b << endl;
    }
    void::Fraction::Compare(Fraction&f0) {
        if((top * f0.bottom - f0.top*bottom)>0)
            cout << top << "/" << bottom << ">" << f0.top << "/" << f0.bottom << endl;
        else if((top * f0.bottom - f0.top*bottom)<0)
            cout << top << "/" << bottom << "<" << f0.top << "/" << f0.bottom << endl;
        else
            cout << top << "/" << bottom << "=" << f0.top << "/" << f0.bottom << endl;
    }

    main.cpp

    #include<iostream>
    #include"Fraction.h"
    using namespace std;
    int main() {
        Fraction f1;
        Fraction f2(1,2);
        Fraction f3(5);
        f1.Add(f2);
        f1.Subtract(f2);
        f1.Multiply(f2);
        f1.Divide(f2);
        f2.get_fraction();
        f3.get_fraction();
        f1.Compare(f2);
        f1.set_fraction();
        f1.get_fraction();
        return 0;
    }

    运行环境为vs2017

  • 相关阅读:
    洛谷P1762 偶数
    复习1
    zoj3329 One Person Game
    poj2096 Collecting Bugs
    hdu4035 Maze
    Cogs 2856. [洛谷U14475]部落冲突
    洛谷P2474 [SCOI2008]天平
    洛谷P3802 小魔女帕琪
    清北刷题冲刺 11-03 p.m
    清北刷题冲刺 11-03 a.m
  • 原文地址:https://www.cnblogs.com/miaorui1314/p/8922244.html
Copyright © 2020-2023  润新知