ball.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
#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 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 <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

// 类graph的实现
 
#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
#ifndef GRAPH_H
#define GRAPH_H
class Graph {
    public:
        Graph(char ch, int n);
        void draw();
    private:
        char symbol;
        int size;
};


#endif
graph.h
#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

 

#include"fraction.h"
#include<iostream>
using namespace std;
void Fraction::show() {
    if (top == 0) cout << 0 << endl;
    else if (bottom == 1) cout << top << endl;
    else if (top / bottom < 0) cout << "-" << top << "/" << bottom << endl;
    else cout << top << "/" << bottom << endl;
}

void Fraction::plus(Fraction &a, Fraction &b) {
    int t1, b1, t2, b2, m, n, temp, x, y, i;
    t1 = a.top;
    t2 = b.top;
    b1 = a.bottom;
    b2 = b.bottom;
    y = b1 * b2;
    x = t1 * b2 + t2 * b1;
    m = x;
    n = y;
    if (m < n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for (i = n; i >= 1; i--)
    {
        if (x%i == 0 && y%i == 0) break;
    }
    x = x / i;
    y = y / i;
    cout << x << "/" << y << endl;
}

void Fraction::minus(Fraction &a, Fraction &b) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = a.top;
    t2 = b.top;
    b1 = a.bottom;
    b2 = b.bottom;
    y = b1 * b2;
    x = t1 * b2 - t2 * b1;
    m = x;
    n = y;
    if (m < n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for (i = n; i >= 1; i--)
    {
        if (x%i == 0 && y%i == 0) break;
    }
    x = x / i;
    y = y / i;
    cout << x << "/" << y << endl;
}

void Fraction::times(Fraction &a, Fraction &b) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = a.top;
    t2 = b.top;
    b1 = a.bottom;
    b2 = b.bottom;
    y = b1 * b2;
    x = t1 * t2;
    m = x;
    n = y;
    if (m < n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for (i = n; i >= 1; i--)
    {
        if (x%i == 0 && y%i == 0) break;
    }
    x = x / i;
    y = y / i;
    cout << x << "/" << y << endl;
}

void Fraction::divide(Fraction &a, Fraction &b) {
    int t1, t2, b1, b2, x, y, m, n, temp, i;
    t1 = a.top;
    t2 = b.top;
    b1 = a.bottom;
    b2 = b.bottom;
    y = b1 * t2;
    x = t1 * b2;
    m = x;
    n = y;
    if (m < n)
    {
        temp = m;
        m = n;
        n = temp;
    }
    for (i = n; i >= 1; i--)
    {
        if (x%i == 0 && y%i == 0) break;
    }
    x = x / i;
    y = y / i;
    cout << x << "/" << y << endl;
}

void Fraction::compare(Fraction &a, Fraction &b) {
    int t1, t2, b1, b2, x, y;
    t1 = a.top;
    t2 = b.top;
    b1 = a.bottom;
    b2 = b.bottom;
    y = b1 * b2;
    x = t1 * b2 - t2 * b1;
    if (x < 0) cout << a.top << "/" << a.bottom << "<" << b.top << "/" << b.bottom << endl;
    else if (x > 0) cout << a.top << "/" << a.bottom << ">" << b.top << "/" << b.bottom << endl;
    else if (x == 0) cout << a.top << "/" << a.bottom << "=" << b.top << "/" << b.bottom << endl;
}
fraction.cpp
#ifndef FRACTION_H
#define TRACTION_H
class Fraction
{
 public:
    Fraction(int x=0,int y=1):top(x),bottom(y){}
    Fraction(const Fraction &t):top(t.top),bottom(t.bottom){}
    void plus(Fraction &a, Fraction &b);
    void minus(Fraction &a, Fraction &b);
    void times(Fraction &a, Fraction &b);
    void divide(Fraction &a, Fraction &b);
    void compare(Fraction &a, Fraction &b);
    void show();
 private:
     int top;
     int bottom;
};
#endif
fraction.h
#include"fraction.h"
#include<iostream>
using namespace std;

int main() {
    Fraction a;
    a.show();
    Fraction b(3, 4);
    b.show();
    Fraction c(5);
    c.show();
    int x, y;
    cin >> x >> y;
    Fraction d(x, y);
    a.plus(b, d);
    a.minus(b, d);
    a.times(b, d);
    a.divide(b, d);
    a.compare(b, d);

    system("pause");
}
main

总结:不知道为什么第二题的main.cpp中system会显示错误,这次尝试了先理解题目意思,然后慢慢写出了程序。

 posted on 2019-04-21 21:53  TOKISOKI  阅读(120)  评论(0编辑  收藏  举报