• 实验


    目前我不太知道如何在博客园中上传本地视频,所以实验一和实验二的视频并未上传

    (1)实验一小球

    #ifndef BALL_H
    #define BALL_H
    
    class Ball {
        public:
            Ball(int x0=0, int y0=0);   // 在坐标(x,y)处构造一个小球(小球用字符O表示) 
            void left(int step=1);        // 左移step 
            void right(int step=1);        // 右移step 
            void up(int step=1);        // 上移step 
            void down(int step=1);         // 下移step 
            void show(int,int);         //根据坐标,显示小球 
            void whole(int,int,int,int);//弹球 
        private:
            int x;    // x坐标
            int y;  // y坐标 
    };
    
    #endif
    ballball.h
    #include "ballball.h"
    #include <iostream>
    #include <cstdlib>  // 因为使用了system("cls"); 所以需要包含这个头文件 
    using std::cout;
    using std::endl;
    
    const int SIZE_X=50;   // 小球x轴移动范围0~SIZE_X 
    const int SIZE_Y=50;   // 小球y轴移动范围0~SIZE_Y
    
    
    Ball::Ball(int x0, int y0):x(x0), y(y0) {
        
        show(x,y);
        
    }
    
    void Ball::whole(int x,int y,int z,int k){
        right(x);
        left(y);
        up(z);
        down(k);
    } 
    void Ball::left(int step) {
        x = x-step;
        if(x <= 0)
            x=0;
        
        show(x,y);
    }
    
    void Ball::right(int step) {
        x = x+step;
        if(x >= SIZE_X)
            x=SIZE_X;
        
        show(x,y);
        
    } 
    
    void Ball::up(int step) {
        y = y-step;
        if(y <= 0)
            y=0;
    
        show(x,y);
        
    }
    
    void Ball::down(int step) {
        y = y+step;
        if(y >= SIZE_Y)
            y = SIZE_Y;
        
        show(x,y);
    }
    
    void Ball::show(int x0,int y0){
        // 清屏 
        system("cls");
        
        // 打印y0-1行空行
        for(int line=1; line <= y0-1; line++)
            cout << endl;
        
        // 打印x0-1个空格
        for(int col=1; col <= x0-1; col++) 
            cout << " ";
        
        // 打印小球
        cout << "O" << endl; 
    }
    ballball.cpp
    #ifndef CANVAS_H
    #define CANVAS
    
    #include <string>
    using std::string;
    
    class Canvas {
        public:
            Canvas(string bg0="0", string fg0="A");
            void changeCanvasBg(string bg0);
            void changeCanvasFg(string fg0);
            void changeCanvasColor(string bg0, string fg0); 
        private:
            string bg; // background color
            string fg; // foreground color 
    };
    
    #endif
    canvascanvas.h
    #include "canvascanvas.h"
    #include <cstdlib>
    
    Canvas::Canvas(string bg0, string fg0):bg(bg0), fg(fg0) {
        string color = "color ";
        color += bg0;
        color += fg0;
        system(color.c_str());
    } 
    void Canvas::changeCanvasBg(string bg0) {
        bg = bg0; // 更新画布背景色 
        
        string color = "color ";
        color += bg;
        color += fg;
        system(color.c_str());
        
    }
    void Canvas::changeCanvasFg(string fg0) {
        fg = fg0; // 更新画布前景色 
        
        string color = "color ";
        color += bg;
        color += fg;
        system(color.c_str());
        
    }
    void Canvas::changeCanvasColor(string bg0, string fg0){
        bg = bg0;  // 更新画布背景色
        fg = fg0;   // 更新画布前景色 
        
        string color = "color ";
        color += bg;
        color += fg;
        system(color.c_str());    
    }
    canvascanvas.cpp
    #include <iostream>
    #include "canvascanvas.h"
    #include "ballball.h"
    using std::cout;
    using std::cin;
    using std::endl;
    #include<conio.h>                 //getch()的头文件 
    #include<ctime>                   //使得程序可以取随机数 
    #include<stdlib.h>
    
    int main() {
        Canvas canvas;
        char s;
        srand(time(0));
        
        Ball ball1(10,10);
        system("pause");
        
        ball1.left(5);
        system("pause");
        
        ball1.up(20);
        system("pause");
    
        canvas.changeCanvasFg("E");  // 更新画布前景色 
        system("pause");
        
        canvas.changeCanvasBg("A");  // 更新画布背景色 
        system("pause");
        
        cout<<"you can control the ball yourself :)"<<endl;
        cout<<"through'a'the left,'d'the right,'w'the up and 's' the down"<<endl;
        cout<<"and,if you donot want to control the ball yourself,just enter 't',and the ball will move ifself :)"<<endl;
    
        while(s=getch()){
            if(s=='a')
            ball1.left(1);
            else if(s=='d')
            ball1.right(1);
            else if(s=='w')
            ball1.up(1);
            else if(s=='s')
            ball1.down(1);
            else if(s=='t')
            break;
        }
        
        while(int x=rand()%60){
            ball1.whole(x,rand()%60,rand()%60,rand()%60);
        }
        
        return 0;
    }
    ballmain.cpp

    (2)实验二graph

    #ifndef GRAPH_H
    #define GRAPH_H
    
    // 类Graph的声明 
    
    class Graph {
        
        public:
            Graph(char ch, int n);   // 带有参数的构造函数 
            void draw();              // 绘制图形 
            void changegragh(char ch,int n); //改变符号,尺寸 
            void up();          //上移
            void down();          //下移 
            void left();          //左移 
            void right();          //右移 
            
        private:
            char symbol;
            int size;
            int xx,yy;
            
    };
    
    
    #endif
    graph.h
    // 类graph的实现
     
    #include "graph.h" 
    #include <iostream>
    using namespace std;
    #include<cstdlib>
    
    // 带参数的构造函数的实现 
    Graph::Graph(char ch, int n): symbol(ch), size(n),xx(0),yy(0) {
    }
    
    
    // 成员函数draw()的实现
    // 功能:绘制size行,显示字符为symbol的指定图形样式 
    void Graph::draw() {
        for(int i=1;i<=size;i++)
        {
            for(int x=1;x<=size-i;x++)
            cout<<" ";
            for(int y=1;y<=2*i-1;y++)
            cout<<symbol;
            cout<<endl;
        }
    }
    
    void Graph::up(){
        yy=yy-1;
        if(yy>=0){
            system("cls");
            for(int line=1;line<=yy-1;line++)
            cout<<endl;
            for(int i=1;i<=size;i++)
        {
            if(xx>=0)
            for(int line=1;line<=xx-1;line++)
            cout<<" ";
            for(int x=1;x<=size-i;x++)
            cout<<" ";
            for(int y=1;y<=2*i-1;y++)
            cout<<symbol;
            cout<<endl;
        }
    
        }
    }
    
    
    void Graph::down(){
        yy=yy+1;
        if(yy>=0){
            system("cls");
            for(int line=1;line<=yy-1;line++)
            cout<<endl;
            for(int i=1;i<=size;i++)
        {
            if(xx>=0)
            for(int line=1;line<=xx-1;line++)
            cout<<" ";
            for(int x=1;x<=size-i;x++)
            cout<<" ";
            for(int y=1;y<=2*i-1;y++)
            cout<<symbol;
            cout<<endl;
        }
    
    }
    }
    
    
    void Graph::left(){
        xx=xx-1;
        if(xx>=0){
            system("cls");
            
                if(yy>=0)
            for(int line=1;line<=yy-1;line++)
            cout<<endl;
            for(int i=1;i<=size;i++){
            for(int line=1;line<=xx-1;line++)
            cout<<" ";
            for(int x=1;x<=size-i;x++)
            cout<<" ";
            for(int y=1;y<=2*i-1;y++)
            cout<<symbol;
            cout<<endl;
        }
    
    
    }
    }
    
    void Graph::right(){
        xx=xx+1;
        if(xx>=0){
            system("cls");
                        
            if(yy>=0)
            for(int line=1;line<=yy-1;line++)
            cout<<endl;
            for(int i=1;i<=size;i++){
    
            for(int line=1;line<=xx-1;line++)
            cout<<" ";
            for(int x=1;x<=size-i;x++)
            cout<<" ";
            for(int y=1;y<=2*i-1;y++)
            cout<<symbol;
            cout<<endl;
        }
    
    }
    }
    
    
    void Graph::changegragh(char ch,int n){
        system("pause"); 
    //清屏 
        system("cls");             
        symbol=ch;
        size=n;
    }
    graph.cpp
    #ifndef CANVAS_H
    #define CANVAS
    
    #include <string>
    using std::string;
    
    class Canvas {
        public:
            Canvas(string bg0="0", string fg0="A");
            void changeCanvasBg(string bg0);
            void changeCanvasFg(string fg0);
            void changeCanvasColor(string bg0, string fg0); 
        private:
            string bg; // background color
            string fg; // foreground color 
    };
    
    #endif
    canvascanvas.h
    #include "canvascanvas.h"
    #include <cstdlib>
    
    Canvas::Canvas(string bg0, string fg0):bg(bg0), fg(fg0) {
        string color = "color ";
        color += bg0;
        color += fg0;
        system(color.c_str());
    } 
    void Canvas::changeCanvasBg(string bg0) {
        bg = bg0; // 更新画布背景色 
        
        string color = "color ";
        color += bg;
        color += fg;
        system(color.c_str());
        
    }
    void Canvas::changeCanvasFg(string fg0) {
        fg = fg0; // 更新画布前景色 
        
        string color = "color ";
        color += bg;
        color += fg;
        system(color.c_str());
        
    }
    void Canvas::changeCanvasColor(string bg0, string fg0){
        bg = bg0;  // 更新画布背景色
        fg = fg0;   // 更新画布前景色 
        
        string color = "color ";
        color += bg;
        color += fg;
        system(color.c_str());    
    }
    canvascanvas.cpp
    #include <iostream>
    #include "graph.h"
    #include"canvascanvas.h"
    using namespace std;
    #include<conio.h>
    
    int main() {
        Canvas canvas1;
        canvas1.changeCanvasColor("0","A");      //图形的前景色,背景色的设置和调整 
        
        Graph graph1('*',5);
        graph1.draw();
        
        graph1.changegragh('#',6);
        graph1.draw();
        
        system("pause");
        system("cls");
        
        Graph graph2('$',7);
        graph2.draw();
        
        cout<<"you can control it yourself :)"<<endl;
        cout<<"through'a'the left,'d'the right,'w'the up and 's' the down"<<endl;
        cout<<"and,if you donot want to control the ball yourself,just enter 't',and the ball will move ifself :)"<<endl;
    
        while(char s=getch()){
            if(s=='a')
            graph2.left();
            else if(s=='d')
            graph2.right();
            else if(s=='w')
            graph2.up();
            else if(s=='s')
            graph2.down();
            else if(s=='t')
            break;
        }
        
        return 0; 
    } 
    main.cpp

     

    (3)实验三Fraction

    #ifndef FRACTION_H
    #define FRACTION_H
    
    #include<iostream>
    using std::cout;
    using std::endl;
    
    class Fraction{
        public:
            Fraction(int top=0,int bottom=1):top(top),bottom(bottom){
                if(bottom==0)
                cout<<"the fenmu should not be 0"<<endl;
            }
            void guifan();                       //规范化处理 
            int maxfactor(int,int);              //求最大公约数 
            
            void add(Fraction,Fraction);
            void reduce(Fraction,Fraction);
            void cheng(Fraction,Fraction);
            void chu(Fraction,Fraction);
            void compare(Fraction,Fraction);
            double output();                     //输出小数形式的值 
            
            int gettop(){                        //取出分子 
                return top;
            }
            int getbottom(){                     //取出分母 
                return bottom;
            }
        private:
            int top;                             //分子 
            int bottom;                          //分母 
    };
    
    #endif
    Fraction.h
    #include<iostream>
    #include"Fraction.h"
    using std::cout;
    using std::cin;
    using std::endl;
    #include<cmath>
    
    
    void Fraction::add(Fraction a,Fraction b){   //计算相加 
        int fenmu,fenzi;
        fenmu=a.bottom*b.bottom;
        fenzi=a.top*b.bottom+b.top*a.bottom;
        
        int t=maxfactor(abs(fenmu),abs(fenzi));  //最大公约数 
        fenmu=fenmu/t;
        fenzi=fenzi/t;
        
        top=fenzi;                           
        bottom=fenmu;
        
    }
    
    void Fraction::reduce(Fraction a,Fraction b){   //计算相减 
        int fenmu,fenzi;
        fenmu=a.bottom*b.bottom;
        fenzi=a.top*b.bottom-b.top*a.bottom;
        
        int t=maxfactor(abs(fenmu),abs(fenzi));  //最大公约数 
        fenmu=fenmu/t;
        fenzi=fenzi/t;
        
        top=fenzi;                           
        bottom=fenmu;
        
    }
    
    void Fraction::cheng(Fraction a,Fraction b){ //相乘 
        int fenzi,fenmu;
        fenmu=a.bottom*b.bottom;
        fenzi=a.top*b.top;
        
        int t=maxfactor(abs(fenmu),abs(fenzi));  //最大公约数 
        fenmu=fenmu/t;
        fenzi=fenzi/t;
        
        top=fenzi;
        bottom=fenmu;
    }
     
     
    void  Fraction::chu(Fraction a,Fraction b){  //相除 
        int fenzi,fenmu;
        fenmu=a.bottom*b.top;
        fenzi=a.top*b.bottom;
        
        int t=maxfactor(abs(fenmu),abs(fenzi));  //最大公约数 
        fenmu=fenmu/t;
        fenzi=fenzi/t;
        
        top=fenzi;
        bottom=fenmu;
    } 
    
    
    void Fraction::compare(Fraction a,Fraction b){//比较大小 
        Fraction c;
        c.reduce(a,b);
        
        if(c.top*c.bottom<0)
          cout<<a.top<<"/"<<a.bottom<<" < "<<b.top<<"/"<<b.bottom<<endl; 
        else if(c.top*c.bottom==0)
          cout<<a.top<<"/"<<a.bottom<<" = "<<b.top<<"/"<<b.bottom<<endl; 
        else 
          cout<<a.top<<"/"<<a.bottom<<" > "<<b.top<<"/"<<b.bottom<<endl; 
    }
    
    void Fraction::guifan(){                      //规范 
        if(bottom<0)
        {
            top=-top;
            bottom=-bottom;
        }
    } 
    
                                 
    int Fraction::maxfactor(int x,int y){         //最大公约数 
        while(x%y!=0){
            int c=x%y;
            x=y;
            y=c;
        }    
        return y;
    }   
    
    
    double Fraction::output(){                    //输出小数形式 
        return ((double)top/(double)bottom);
    }
    Fraction.cpp
    #include"Fraction.h" 
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main(){
        
        Fraction a(2,-8);
        Fraction b(2,3);
        Fraction c;
        
        //分别以小数形式输出a,b,c的值 
        cout<<"the result of a is:"<<a.output()<<endl; 
        cout<<"the result of b is:"<<b.output()<<endl;
        cout<<"the result of c is:"<<c.output()<<endl<<endl;
        
        //a与b相加并且求值 
        c.add(a,b);
        c.guifan();
        cout<<"c.top="<<c.gettop()<<",c.bottom="<<c.getbottom()<<endl;
        cout<<"the result is:"<<c.output()<<endl<<endl;
    
        //a与b相减并且求值  
        c.reduce(a,b);
        c.guifan();
        cout<<"c.top="<<c.gettop()<<",c.bottom="<<c.getbottom()<<endl;
        cout<<"the result is:"<<c.output()<<endl<<endl;
        
        //a与b相乘并且求值 
        c.cheng(a,b);
        c.guifan();
        cout<<"c.top="<<c.gettop()<<",c.bottom="<<c.getbottom()<<endl;
        cout<<"the result is:"<<c.output()<<endl<<endl;
        
        c.chu(a,b);
        c.guifan();
        cout<<"c.top="<<c.gettop()<<",c.bottom="<<c.getbottom()<<endl;
        cout<<"the result is:"<<c.output()<<endl<<endl;
        
        c.compare(a,b);
        return 0;
    }
    fenshu.cpp

    我觉得这次实验的收获很大这让我对于类的使用以及项目的使用更加熟练,然后我觉得在这些一次次的有浅至深的实验下我才能够更好的提升自己。

    https://www.cnblogs.com/pink-fairy/

    https://www.cnblogs.com/elise00/

    https://www.cnblogs.com/jyf13/

  • 相关阅读:
    js事件冒泡替我背了很多黑锅 嘿嘿
    opencvmin函数
    关于Block Formatting Context--BFC和IE的hasLayout
    javascript面向对象包装类Class的类库解析
    nodejs中exports与module.exports的区别
    ie6固定定位层fixed
    CSS的类名交集复合选择器
    遮罩层覆盖整个页面
    nodejs的require模块及路径
    struts.properties配置详解
  • 原文地址:https://www.cnblogs.com/sqcmxg/p/10742740.html
Copyright © 2020-2023  润新知