我是靠谱客的博主 失眠秋天,最近开发中收集的这篇文章主要介绍使用C++编写乌龟画图程序C++乌龟画图,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

C++乌龟画图

  • 输入命令并进行绘图

题目:Turtle Graphics

The Logo language, which is popular among elementary school children,made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the roomunder the control of a C++ program. The turtle holds a pen in one of two positions, up or down.While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem, you’ll simulate the operation of the turtle and create a computerized sketchpad as well.Use a 20-by-20 array floor that is initialized to false. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen up. The set of turtle commands your program must process are shown in Fig.

简单翻译过来,大意是一个拿着笔的乌龟,笔放下时,画出移动的路径;当笔向上时,乌龟自由地四处移动,不画任何东西。记录乌龟的操作命令和创建一个画板(使用一个20*20的数组),并随时记录海龟的当前位置,以及笔是否处于上升或下降状态。假设乌龟总是用它的笔从(0, 0)开始。

命令表

这里写图片描述

例如:

假设乌龟在靠近地板中心的某个地方。下面的命令将绘制和打印12*12的方形路径并且最后笔尖向上。

输入

2
5,12
3
5,12
3
5,12
3
5,12
1
6
9

输出

这里写图片描述


代码块

#include <iostream>
using namespace std;
const int COMMANDS = 100, SIZE = 40;  //这里SIZE为展示画板大小,由于原本行列间隔不一样,为美观我将行间隔
int turnRight(int);                //一个空位,COMMANDS为命令输入上限,为后面创建命令数组使用
int turnLeft(int);
void getCommands(int[][2]);
void movePen(int, int[][SIZE], int, int);
void printArray(const int[][SIZE]);

int main()
{
    int floor[SIZE][SIZE] = { 0 };
    int commands = 0;
    static int direction = 0;  //记录方向
    int commandArray[COMMANDS][2] = { 0 };  //储存命令
    int count = 0;
    int distance = 0;
    bool penDown = false;
    getCommands(commandArray);
    commands = commandArray[count][0];
    while (commands!=9)
    {
        //cout << "commands:" << commands << endl;//测试用
        switch (commands)
        {
        case 1:
            penDown = false;
            break;
        case 2:
            penDown = true;
            break;
        case 3:
            direction = turnRight(direction);
            break;
        case 4:
            direction = turnLeft(direction);
            break;
        case 5:
            distance = commandArray[count][1];
            movePen(penDown, floor, direction, distance);
            break;
        case 6:
            cout << "nThe drawing is:nn";
            printArray(floor);
            break;
        default:
            break;
        }
        commands = commandArray[++count][0];  //从数组中取出命令
    }
    return 0;
}

void getCommands(int commands[][2])
{
    int tempCommand, i;
    cout << "Enter command(9 to end input): ";
    cin >> tempCommand;
    for ( i = 0; tempCommand!=9&&i<COMMANDS; i++)
    {
        commands[i][0] = tempCommand;
        if (tempCommand==5)
        {
            cin.ignore();  //忽略5后面的字符','
            cin >> commands[i][1];  //将移动距离保存在commands[i][1]中。
        }
        cout << "Enter command(9 to end input): ";
        cin >> tempCommand;
    }
    commands[i][0] = 9;
}

int turnRight(int dir)
{
    return ++dir > 3 ? 0 : dir;  //原点为0,右转4次回到原点
}
int turnLeft(int dir)
{
    return --dir < 0 ? 3 : dir;
}
void movePen(int down, int a[][SIZE], int dir, int dis)
{
    static int xPosition = 0, yPosition = 0;  //设置原点
    int xTemp, yTemp, i;
    xTemp = xPosition;
    yTemp = yPosition;
    //cout << "Direction:" << dir << " Pen:" << down << endl;//测试用 
    switch (dir)
    {
    case 0:
        for ( i = 0; i < dis*2&&(yTemp+i)<SIZE; i+=2)
        {
            if (down)
            {
                a[xTemp][yTemp + i] = 1;
            }
            else if (down==0)
            {
                a[xTemp][yTemp + i] = 0;
            }
            xPosition = xTemp;
            yPosition = yTemp+i;
        }
        break;
    case 1:
        for (i = 0; i < dis && (xTemp + i)<SIZE; ++i)
        {
            if (down)
            {
                a[xTemp+i][yTemp] = 1;
            }
            else if (down == 0)
            {
                a[xTemp + i][yTemp] = 0;
            }
            xPosition = xTemp + i;
            yPosition = yTemp ;
        }
        break;
    case 2:
        for (i = 0;  i < dis * 2 && (yTemp - i)>=0; i += 2) //加2是为了让行和列的间隔一样,单纯美观
        {
            if (down)
            {
                a[xTemp][yTemp-i] = 1;
            }
            else if (down == 0)
            {
                a[xTemp][yTemp-i] = 0;
            }
            xPosition = xTemp;
            yPosition = yTemp - i;
        }
        break;
    case 3:
        for (i = 0; i < dis && (xTemp - i)>=0; ++i)
        {
            if (down)
            {
                a[xTemp - i][yTemp] = 1;
            }
            else if (down == 0)
            {
                a[xTemp - i][yTemp] = 0;
            }
            xPosition = xTemp - i;
            yPosition = yTemp;
        }
        break;
    }
}
void printArray(const int a[][SIZE])
{
    for (int i = 0; i < SIZE; ++i)
    {
        for (int j = 0; j < SIZE; ++j)
        {
            if (a[i][j] == 0) cout << " ";
            else cout << "*";
        }
        cout << endl;
    }
}

本人只是一个大一小白,如果你发现了错误或者有什么好的建议的话请在下方提出,谢谢!

٩꒰▽ ꒱۶⁼³₌₃ 学习去咯

最后

以上就是失眠秋天为你收集整理的使用C++编写乌龟画图程序C++乌龟画图的全部内容,希望文章能够帮你解决使用C++编写乌龟画图程序C++乌龟画图所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部