English 中文(简体)
清除终端屏幕
原标题:Clearing the terminal screen
  • 时间:2011-11-03 17:52:13
  •  标签:
  • c++
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;

void circle(int x, int y, int radius);
void line(int a, int b, int c, int d);
bool buffer[26][81];
char drawSpace[26][81];

int main() {
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int x = 0;
    int y = 0;
    int radius = 0;
    char choice;

    cout << "Type  c  to draw a circle or type  l  to draw a line." << endl;
    cin >> choice;

    if (choice ==  c ){
        cout << "please enter an x coordinate for the center of the circle 
";
        cin >> x;
        cout << "please enter a y coordinate for the center of the circle 
";
        cin >> y;
        cout << "please enter a value for the radius of the circle 
";
        cin >> radius;
        int moves = (x - radius) / 10;
        for (int s = 0; s < moves; s++){
            circle(x, y, radius);
            system("clear");
            x = x -10;
        }
    }

    else if (choice ==  l ){
        cout << "Please enter the x coordinate for the first point on the line 
";
        cin >> a;
        cout << "Please enter the y coordinate for the first point on the line 
";
        cin >> b;
        cout << "Please enter the x coordinate for the end point on the line 
";
        cin >> c;
        cout << "Please enter the y coordinate for the end point on the line 
";
        cin >> d;
    }

    else
        cout << "you did not enter an appropriate letter, please restart the program and try again."<< endl;

    return 0;
}

void circle(int x, int y, int radius){
    if (x + radius >= 81|| x - radius <= 0 || y + radius >= 26 || y - radius <= 0){
        cout << "the coordinates provided for the circle will not fit on the screen" << endl;
        return;
    }

    for (int i = 0; i < 26; i++) {
        for(int j = 0; j < 81; j++) {
            int a = abs (x - j);
            int b = abs (y - i);
            int distance =  pow(a, 2) + pow(b, 2);
            int realDistance = pow(radius, 2);
            if (abs(realDistance - distance) <= 3){
                buffer[i][j] = true;
            }
        }
    }

    for (int m = 0; m < 26; m++){
        for(int n = 0; n < 81; n++){
            if (buffer[m][n]){
                drawSpace[m][n] = 42;
            }
            else
                drawSpace[m][n] = 32;
        }
    }

    for (int row = 25; row >= 0; row--) {
        for (int col = 0; col < 81; col++) {
            cout << drawSpace[row][col];
        }
        cout << "
";
    }
}

void line(int a, int b, int c, int d){
    if (a >= 81 || c >= 81 || a <= 0 || c <= 0 || b >= 26 || d >= 26 || b <= 0 || d <= 0){
        return;
    }
    int intercept = 0;
    double rise = d - b;
    double run = c - a;
    double slope = rise/run;
    intercept = b - (slope*a);
    for (int i = 0; i < 26; i++) {
        for(int j = 21; j < 81; j++) {
            if (slope > 0){
                if (j > a && j < c){
                    int newIntercept = i - (slope*j);
                    int test = abs (intercept - newIntercept);
                    if (test <= 0)
                        buffer[i][j] = true;
                else
                    buffer[i][j] = false;
                }
            }
            else if (slope < 0){
                if (j < a && j > c){
                    int newIntercept = i - (slope*j);
                    int test = abs (newIntercept - intercept);
                    if (test <= 0)
                        buffer[i][j] = true;

                }
                else
                    break;
            }
        }
    }

    for (int m = 0; m < 26; m++){
        for(int n = 0; n < 81; n++){
            if (buffer[m][n])
                drawSpace[m][n] = 42;
            else
                drawSpace[m][n] = 32;
        }
    }

    for (int row = 25; row >= 0; row--) {
        for (int col = 0; col < 81; col++) {
            cout << drawSpace[row][col];
        }
        cout << "
";
    }
}

我已经为一项方案拟订任务撰写了这一守则,其目标就是为一个圈子或线的坐标和尺寸提供投入,并按图表将其印制到终端。 第二步是使形状从屏幕右侧移至左边。 我开始为这个圈子撰写这一守则,但出于某种原因,该系统电话似乎并不清晰,它只是印刷外圈,而不去掉老圈。 如果有人能够提供帮助,我会真心赞赏。

最佳回答

最初的海报没有足够回响,因此我在这里为他写了:

I was actually a bit off base. The system("clear") I was using actually did work, the problem I was encountering was that I did not reset the bool array I was using to plot out the points that needed to be drawn. Thanks for the help, I learned a few things about how to clear the screen before I found my own problem.

问题回答

Try:

cout << "33[2J33[1;1H";

www.un.org/Depts/DGACM/index_chinese.htm

关于六氯环己烷(和其他特殊产品),也可使用>ncurses Library 向终端输出。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签