• c++简单程序设计-4


    一 编程实验部分
    1.graph类
     ① 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) {
    }
    
    void Graph::draw() {
        int i, j;
        for (i = 1; i <= size; i++){
            for (j = 1; j <= size-i; j++)    //每行字符前的空
                cout << " ";
            for (j = 1; j <= 2*i-1; j++)       //每行的字符
                cout << symbol;
            for (j = 1; j <= size-i; j++)      //每行后的字符
                cout << " " ;
            cout << endl;
        } 
        return;
    }

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

    扩展1:在main.cpp中加入以下代码

        char a;
        int i;
        while (cin >> a >> i) {
            Graph graph(a, i);
            graph.draw();
        }

     扩展1(改)

    #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()在屏幕上绘制图形
    
        char a;
        int i;
        while (1) {
            cout << "是否需要重设图像,是(y),否(n):";
            char c;
            cin >> c;
            if (c == 'n')break;
            cout << "请输入图像的字符与尺寸:";
            cin >> a>> i;
            Graph graph(a, i);
            graph.draw();    
        }
        return 0; 
    } 

    2.fraction类

     ① fraction.h
    #pragma once
    
    class fraction {
    
    public:
    
        fraction();                                    //构造函数
        fraction(int t, int b);                        //函数重载
        fraction(int t);                               //函数重载
        void show();                                   //输出
        void jia(fraction &f1);                        //相加
        void jian(fraction &f1);                       //相减
        void cheng(fraction &f1);                      //相乘
        void chu(fraction &f1);                        //相除
        void compare(fraction f1, fraction f2);        //比较
    
    private:
        int top;                //分子
        int bottom;             //分母
    };

     ② fraction.cpp

    #include <iostream>
    #include "fraction.h"
    using namespace std;
    
    
    fraction::fraction() :top(0), bottom(1) {                          //不提供初始值的函数实现
    }
    
    fraction::fraction(int t, int b) : top(t), bottom(b) {            //提供两个初始值的函数实现
    }
    
    fraction::fraction(int t) : top(t), bottom(1) {                   //提供一个初始值的函数实现
    }
    
    void fraction::jia(fraction &f1) {                                //加法
        fraction f2;
        f2.top = top * f1.bottom + f1.top*bottom;
        f2.bottom = bottom * f1.bottom;
        f2.show();
    }
    
    void fraction::jian(fraction &f1) {                               //减法
        fraction f2;
        f2.top = top * f1.bottom - f1.top*bottom;
        f2.bottom = bottom * f1.bottom;
        f2.show();
    }
    
    void fraction::cheng(fraction &f1) {                              //乘法
        fraction f2;
        f2.top = top * f1.top;
        f2.bottom = bottom * f1.bottom;
        f2.show();
    }
    
    void fraction::chu(fraction &f1) {                                //除法
        fraction f2;
        f2.top = top * f1.bottom;
        f2.bottom = bottom * f1.top;
        f2.show();
    }
    
    void fraction::show() {                                           //输出
        cout << top << "/" << bottom << endl;
    }
    
    void fraction::compare(fraction f1, fraction f2) {               //比较
        float a = float(f1.top) / float(f1.bottom); float b = float(f2.top) / float(f2.bottom);
        if (a < b) { 
            cout << f1.top << "/" << f1.bottom << "<"; f2.show(); cout << endl; 
        }
        if (a==b) {
            cout << f1.top << "/" << f1.bottom << "="; f2.show(); cout << endl;
        }
        if (a > b) { 
            cout << f1.top << "/" << f1.bottom << ">"; f2.show(); cout << endl; 
        }
    }

     ③ main.cpp

    #include <iostream>
    #include "fraction.h"
    using namespace std;
    
    int main() {
        fraction f1;              //不提供初始值
        fraction f2(2,3);         //提供两个初始值
        fraction f3(3);           //提供一个初始值
        fraction f4(3,4);
        cout << "分数a:"; f1.show();
        cout << "分数b:"; f2.show();
        cout << "分数c:"; f3.show();
        cout << "分数d:"; f4.show();
        cout << "分数b+d:"; f2.jia(f4);
        cout << "分数b-d:"; f2.jian(f4);
        cout << "分数b*d:"; f2.cheng(f4);
        cout << "分数b/d:"; f2.chu(f4);
        cout << "分数b与d的比较:"; f1.compare(f2, f4);
        return 0;
    }

    实验总结与体会

    这次实验感觉如果不做扩展的话还是挺容易的...
    做了扩展后,我充分意识到了自己的不足...
    然后...我就决定放弃扩展(笑cry)
    虽然最后是真的没把扩展全部做完,但还是去思考了很多的
    像上面写了的扩展1,方法1是我想到的最快捷的方法
    但这是无限次重设,显然不符函数实际要求
    后来我重新修改了while语句,感觉效果挺好的
    扩展2可以用system"colour __" 实现
    然而我不怎么会改下划线里的值
    还可以在控制台窗口属性里调,调完重开就行了
    然后...就没有然后了
    也许过几天有心情了再来做完吧...溜了溜了




  • 相关阅读:
    JWT
    JS中try catch的用法
    React高级
    React基础
    获取当前时间前面的时间
    nodeJs
    数组里的字符串数字与数字互转
    寒假学习(二)spark学习
    寒假学习(一)Web开发人员学习路线图
    如何使用GitHub上传本地项目(idea功能强大可直接提交)
  • 原文地址:https://www.cnblogs.com/tensheep/p/8922426.html
Copyright © 2020-2023  润新知