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

实验三

时间:2019-04-24 00:13:22      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:.cpp   公约数   step   样式   class   info   system   头文件   private   

一、画布类和小球类

代码:

技术图片
#include <iostream>
#include "canvas.h"
#include "Ball.h"
int main() {
    Canvas canvas;
    
    Ball ball1(10,10);
    system("pause");
    
    ball1.left(5);
    system("pause");
    
    ball1.up(20);
    system("pause");

    canvas.changeCanvasFg("E");  // 更新画布前景色 
    system("pause");
    
    canvas.changeCanvasBg("D");  // 更新画布背景色 
    system("pause");

    return 0;
main.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
canvas.h
技术图片
#include "canvas.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());    
}
canvas.cpp
技术图片
#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 
        private:
        int x;     // x坐标
        int y;     // y坐标 
};

#endif

ball.h
ball.h
技术图片
#include "ball.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) {
    // 打印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; 
    
}

void Ball::left(int step) {
    x = x-step;
    if(x <= 0)
        x=0;
    
    // 清屏 
    system("cls");
    
    // 打印y-1行空行
    for(int line=1; line <= y-1; line++)
        cout << endl;
    
    // 打印x-1个空格
    for(int col=1; col <= x-1; col++) 
        cout << " ";
    
    // 打印小球
    cout << "O" << endl; 
    
}

void Ball::right(int step) {
    x = x+step;
    if(x >= SIZE_X)
        x=SIZE_X;
    
    // 清屏 
    system("cls");
    
    // 打印y-1行空行
    for(int line=1; line <= y-1; line++)
        cout << endl;
    
    // 打印x-1个空格
    for(int col=1; col <= x-1; col++) 
        cout << " ";
    
    // 打印小球
    cout << "O" << endl; 
    
} 

void Ball::up(int step) {
    y = y-step;
    if(y <= 0)
        y=0;
    
    // 清屏 
    system("cls");
    
    // 打印y-1行空行
    for(int line=1; line <= y-1; line++)
        cout << endl;
    
    // 打印x-1个空格
    for(int col=1; col <= x-1; col++) 
        cout << " ";
    
    // 打印小球
    cout << "O" << endl; 
    
}

void Ball::down(int step) {
    y = y+step;
    if(y >= SIZE_Y)
        y = SIZE_Y;
    
    // 清屏 
    system("cls");
    
    // 打印y-1行空行
    for(int line=1; line <= y-1; line++)
        cout << endl;
    
    // 打印x-1个空格
    for(int col=1; col <= x-1; col++) 
        cout << " ";
    
    // 打印小球
    cout << "O" << endl;     
}

ball.cpp
ball.cpp

运行结果:

技术图片

二、定义Graph类对象

代码:

技术图片
#include <iostream>
#include "graph.h"
using namespace std;

int main() {
    Graph graph1(*,5);
    graph1.draw();
    
    system("pause");
    system("cls");
    
    Graph graph2($,7);
    graph2.draw();

    system("pause");
    
    return 0; 
} 

main.cpp
main.cpp
技术图片
#define GRAPH_H
class Graph {
    public:
        Graph(char ch, int n);
        void draw();
    private:
        char symbol;
        int size;
};

#endif
graph.h
技术图片
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
void Graph::draw() {
    // 补足代码
    int i,j,k;
    for(i=0;i<size;i++)
    {for(j=0;j<size-1-i;j++)
        cout<<" ";
     for(k=0;k<2*i+1;k++)
         cout<<symbol;
     cout<<endl;
    }
}

graph.cpp
graph.cpp

运行结果:

技术图片

三、分数

代码:

技术图片
#include"Fraction.h"usingstd::cout;usingstd::cin;usingstd::endl;intmain(){Fractiona(2,-8);Fractionb(2,3);Fractionc;//a与b相加并且求值c.add(a,b);c.guifan();cout<<"c.top="<<c.gettop()<<",c.bottom="<<c.getbottom()<<endl;cout<<"theresultis:"<<c.output()<<endl<<endl;//a与b相减并且求值c.reduce(a,b);c.guifan();cout<<"c.top="<<c.gettop()<<",c.bottom="<<c.getbottom()<<endl;cout<<"theresultis:"<<c.output()<<endl<<endl;//a与b相乘并且求值c.cheng(a,b);c.guifan();cout<<"c.top="<<c.gettop()<<",c.bottom="<<c.getbottom()<<endl;cout<<"theresultis:"<<c.output()<<endl<<endl;//分别以小数形式输出a,b,c的值cout<<"theresultofais:"<<a.output()<<endl;cout<<"theresultofbis:"<<b.output()<<endl;cout<<"theresultofcis:"<<c.output()<<endl<<endl;return0;}
main.cpp
技术图片
#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<<"data error"<<endl;
        }
        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
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; 
}


double Fraction::output(){                    //输出小数形式 
    return ((double)top/(double)bottom);
}
fraction.cpp

程序中没加规范化分数代码。

实验三

标签:.cpp   公约数   step   样式   class   info   system   头文件   private   

原文地址:https://www.cnblogs.com/sq102217/p/10759771.html

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