NJUST 2021 机械工程学院大一C语言大作业 简易绘图板

这是一个使用C语言实现的图形画布程序,允许用户进行线条绘制、矩形填充、颜色选择、区域复制粘贴等操作。程序初始化为白色背景,并提供了丰富的帮助信息,用户可以输入数字指令来执行不同的绘图命令。程序中还包含了一个九宫格刷子的概念,用于增强画布的编辑功能。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int over = 1;
int change = 1;
// 画布由20行,36列的二维数组成,通过符号常数定义
#define N_ROWS 20
#define N_COLS 36

// 初始给出的两种阴影的定义
#define BLACK 0 // 表示黑色
#define DARK 1
#define GREY 2
#define LIGHT 3
#define WHITE 4 // 表示白色
int color = BLACK;
int brush[3][3];
// 显示画布内容
void printCanvas(int canvas[N_ROWS][N_COLS]);

// 将整个画布设置为白色背景,即画布的初始化操作
void setBlankCanvas(int canvas[N_ROWS][N_COLS]);

// 显示帮助信息
void Show_help_info();

void item(int canvas[N_ROWS][N_COLS]);
void item1(int canvas[N_ROWS][N_COLS]);
void itembush(int canvas[N_ROWS][N_COLS]);
void item2(int canvas[N_ROWS][N_COLS]);
void item3();
void item4(int canvas[N_ROWS][N_COLS]);
void item5();
void item6(int canvas[N_ROWS][N_COLS]);
void item7(int canvas[N_ROWS][N_COLS]);
void item8();
void item9(int canvas[N_ROWS][N_COLS]);
void Stick(int canvas[N_ROWS][N_COLS], int row, int col, int tm[row][col], int target_row, int target_col)
{
    int i, j;
    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
            canvas[target_row + i][target_col + j] = tm[i][j];
}
void Copy(int canvas[N_ROWS][N_COLS], int row, int col, int tm[row][col], int start_row, int start_col)
{
    int i, j;
    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
            tm[i][j] = canvas[start_row + i][start_col + j];
}
void Fix(int canvas[N_ROWS][N_COLS])
{
    for (int i = 0; i < N_ROWS; i++)
        for (int j = 0; j < N_COLS; j++)
        {
            if (canvas[i][j] > 4)
                canvas[i][j] = 4;
            else if (canvas[i][j] < 0)
                canvas[i][j] = 0;
        }
}
int main(void)
{
    int canvas[N_ROWS][N_COLS]; // 定义画布对应的二维整型数组
    setBlankCanvas(canvas);     // 画布的初始化操作
    printCanvas(canvas);        // 显示当前画布内容
    Show_help_info();           // 显示程序的使用说明帮助
    item(canvas);
    getchar();
}

// ---------------------------------------------------
// 在终端上面显示画布内容
// ---------------------------------------------------
void printCanvas(int canvas[N_ROWS][N_COLS])
{
    int row = 0;

    int i = 0;

    printf("   ");

    while (i < N_COLS)
    {
        printf("%2d", i);
        i++;
    }

    printf("\n   -------------------------------------------------------------------------\n");

    while (row < N_ROWS)
    {
        int col = 0;

        printf("%2d|", row);

        while (col < N_COLS)
        {
            printf("%2d", canvas[row][col]);
            col++;
        }
        row++;
        printf(" |\n");
    }

    printf("   -------------------------------------------------------------------------\n");
}

// 将整个画布设置为白色背景,即画布的初始化操作
void setBlankCanvas(int canvas[N_ROWS][N_COLS])
{
    int row = 0;
    while (row < N_ROWS)
    {
        int col = 0;
        while (col < N_COLS)
        {
            canvas[row][col] = WHITE;
            col++;
        }
        row++;
    }
}

// --------------------------------------------------------------
// 显示帮助信息
// --------------------------------------------------------------
void Show_help_info()
{
    printf("0. Show this help screen!\n");
    printf("1. Draw a straight line (horizontal, vertical, or 45cm 135 degree diagonal)\n");
    printf("2. Rectangular area filling\n");
    printf("3. Change the gray scale of the brush\n");
    printf("4. Copy and paste of rectangular area\n");
    printf("5. Define additive brushes\n");
    printf("6. Reset the canvas\n");
    printf("7. Show only the contents of the canvas\n");
    printf("8. Displays the contents of the current 9-pixel brush\n");
    printf("9. Display the contents of the current canvas\n");
    printf("Enter other non-numeric characters to exit the program!\n");
}
void item(int canvas[N_ROWS][N_COLS])
{
    char x;
    int i;
    while (over)
    {
        scanf("%c", &x);
        getchar();
        i = x - '0';
        switch (i)
        {
        case 0:
            Show_help_info();
            break;
        case 1:
            if (change)
                item1(canvas);
            else
                itembush(canvas);
            break;
        case 2:
            item2(canvas);
            break;
        case 3:
            item3();
            break;
        case 4:
            item4(canvas);
            break;
        case 5:
            item5();
            break;
        case 6:
            item6(canvas);
            break;
        case 7:
            item7(canvas);
            break;
        case 8:
            item8();
            break;
        case 9:
            item9(canvas);
            break;
        default:
            printf("Press any key to continue.\n");
            over = 0;
            break;
        }
    }
}
void item1(int canvas[N_ROWS][N_COLS])
{
    int start_row, start_col, end_row, end_col;
    int a = 1;
    scanf("%d%d%d%d", &start_row, &start_col, &end_row, &end_col);
    if (start_row < 0 || start_row >= N_ROWS || start_col < 0 || start_col >= N_COLS ||
        end_row < 0 || end_row >= N_ROWS || end_col < 0 || end_col >= N_COLS)
        printf("Drawing area beyond canvas\n");
    else if (start_row != end_row && start_col != end_col && abs(end_row - start_row) != abs(end_col - start_col))
        printf("Can't forn a diagonal of 45 degrees.\nYou can't draw a normal slash.\n");
    else if (start_row == end_row && start_col != end_col)
    {
        if (start_col < end_col)
            for (int i = 0; i <= end_col - start_col; i++)
                canvas[start_row][start_col + i] = color;
        else
            for (int i = 0; i < start_col - end_col; i++)
                canvas[start_row][start_col - i] = color;
    }
    else if (start_col == end_col && start_row != end_row)
    {
        if (start_row < end_row)
            for (int i = 0; i <= end_row - start_row; i++)
                canvas[start_row + i][start_col] = color;
        else
            for (int i = 0; i <= start_row - end_row; i++)
                canvas[start_row - i][start_col] = color;
    }
    else if (abs(end_row - start_row) == abs(end_col - start_col))
    {
        if (start_row <= end_row && start_col <= end_col)
            for (int i = 0; i <= abs(end_row - start_row); i++)
                canvas[start_row + i][start_col + i] = color;
        else if (start_row <= end_row && start_col > end_col)
            for (int i = 0; i <= abs(end_row - start_row); i++)
                canvas[start_row + i][start_col - i] = color;
        else if (start_row > end_row && start_col <= end_col)
            for (int i = 0; i <= abs(end_row - start_row); i++)
                canvas[start_row - i][start_col + i] = color;
        else if (start_row > end_row && start_col > end_col)
            for (int i = 0; i <= abs(end_row - start_row); i++)
                canvas[start_row - i][start_col - i] = color;
    }
    getchar();
}
void itembush(int canvas[N_ROWS][N_COLS])
{
    int start_row, start_col, end_row, end_col;
    int a = 1;
    scanf("%d%d%d%d", &start_row, &start_col, &end_row, &end_col);
    if (start_row < 0 || start_row >= N_ROWS || start_col < 0 || start_col >= N_COLS ||
        end_row < 0 || end_row >= N_ROWS || end_col < 0 || end_col >= N_COLS)
    {
        printf("Drawing area beyond canvas\n");
        getchar();
        return;
    }
    else if (start_row < 1 || start_row >= N_ROWS - 1 || start_col < 1 || start_col >= N_COLS - 1 ||
             end_row < 1 || end_row >= N_ROWS - 1 || end_col < 1 || end_col >= N_COLS - 1)
    {
        if (start_row < 1 && end_row < 1 && start_col == end_col || start_row >= N_ROWS - 1 && end_row >= N_ROWS - 1 && start_col == end_col ||
            start_col < 1 && end_col < 1 && start_row == end_row || start_col >= N_ROWS - 1 && end_col >= N_ROWS - 1 && start_row == end_row)
            {
            printf("Drawing area beyond canvas\n");
            getchar();
            return;
            }
        else
        {
            if (start_row == 0)
                start_row++;
            if (start_row == N_ROWS - 1)
                start_row--;
            if (end_row == 0)
                end_row++;
            if (end_row == N_ROWS - 1)
                end_row--;
            if (start_col == 0)
                start_col++;
            if (start_col == N_ROWS - 1)
                start_col--;
            if (end_col == 0)
                end_col++;
            if (end_col == N_ROWS - 1)
                end_col--;
        }
    }
    if (start_row != end_row && start_col != end_col && abs(end_row - start_row) != abs(end_col - start_col))
        printf("Can't forn a diagonal of 45 degrees.\nYou can't draw a normal slash.\n");
    else if (start_row == end_row && start_col != end_col)
    {
        if (start_col < end_col)
            for (int i = 0; i <= end_col - start_col; i++)
            {
                canvas[start_row - 1][start_col + i - 1] += brush[0][0];
                canvas[start_row - 1][start_col + i] += brush[0][1];
                canvas[start_row - 1][start_col + i + 1] += brush[0][2];
                canvas[start_row][start_col + i - 1] += brush[1][0];
                canvas[start_row][start_col + i] += brush[1][1];
                canvas[start_row][start_col + i + 1] += brush[1][2];
                canvas[start_row + 1][start_col + i - 1] += brush[2][0];
                canvas[start_row + 1][start_col + i] += brush[2][1];
                canvas[start_row + 1][start_col + i + 1] += brush[2][2];
            }
        else
            for (int i = 0; i < start_col - end_col; i++)
            {
                //canvas[start_row][start_col - i] = color;
                canvas[start_row - 1][start_col - i - 1] += brush[0][0];
                canvas[start_row - 1][start_col - i] += brush[0][1];
                canvas[start_row - 1][start_col - i + 1] += brush[0][2];
                canvas[start_row][start_col - i - 1] += brush[1][0];
                canvas[start_row][start_col - i] += brush[1][1];
                canvas[start_row][start_col - i + 1] += brush[1][2];
                canvas[start_row + 1][start_col - i - 1] += brush[2][0];
                canvas[start_row + 1][start_col - i] += brush[2][1];
                canvas[start_row + 1][start_col - i + 1] += brush[2][2];
            }
    }
    else if (start_col == end_col && start_row != end_row)
    {
        if (start_row < end_row)
            for (int i = 0; i <= end_row - start_row; i++)
            {
                //canvas[start_row + i][start_col] = color;
                canvas[start_row + i - 1][start_col - 1] += brush[0][0];
                canvas[start_row + i - 1][start_col] += brush[0][1];
                canvas[start_row + i - 1][start_col + 1] += brush[0][2];
                canvas[start_row + i][start_col - 1] += brush[1][0];
                canvas[start_row + i][start_col] += brush[1][1];
                canvas[start_row + i][start_col + 1] += brush[1][2];
                canvas[start_row + i + 1][start_col - 1] += brush[2][0];
                canvas[start_row + i + 1][start_col] += brush[2][1];
                canvas[start_row + i + 1][start_col + 1] += brush[2][2];
            }
        else
            for (int i = 0; i <= start_row - end_row; i++)
            {
                //canvas[start_row - i][start_col] = color;
                canvas[start_row - i - 1][start_col - 1] += brush[0][0];
                canvas[start_row - i - 1][start_col] += brush[0][1];
                canvas[start_row - i - 1][start_col + 1] += brush[0][2];
                canvas[start_row - i][start_col - 1] += brush[1][0];
                canvas[start_row - i][start_col] += brush[1][1];
                canvas[start_row - i][start_col + 1] += brush[1][2];
                canvas[start_row - i + 1][start_col - 1] += brush[2][0];
                canvas[start_row - i + 1][start_col] += brush[2][1];
                canvas[start_row - i + 1][start_col + 1] += brush[2][2];
            }
    }
    else if (abs(end_row - start_row) == abs(end_col - start_col))
    {
        if (start_row <= end_row && start_col <= end_col)
            for (int i = 0; i <= abs(end_row - start_row); i++)
            {
                //canvas[start_row + i][start_col + i] = color;
                canvas[start_row + i - 1][start_col + i - 1] += brush[0][0];
                canvas[start_row + i - 1][start_col + i] += brush[0][1];
                canvas[start_row + i - 1][start_col + i + 1] += brush[0][2];
                canvas[start_row + i][start_col + i - 1] += brush[1][0];
                canvas[start_row + i][start_col + i] += brush[1][1];
                canvas[start_row + i][start_col + i + 1] += brush[1][2];
                canvas[start_row + i + 1][start_col + i - 1] += brush[2][0];
                canvas[start_row + i + 1][start_col + i] += brush[2][1];
                canvas[start_row + i + 1][start_col + i + 1] += brush[2][2];
            }
        else if (start_row <= end_row && start_col > end_col)
            for (int i = 0; i <= abs(end_row - start_row); i++)
            {
                //canvas[start_row + i][start_col - i] = color;
                canvas[start_row + i - 1][start_col - i - 1] += brush[0][0];
                canvas[start_row + i - 1][start_col - i] += brush[0][1];
                canvas[start_row + i - 1][start_col - i + 1] += brush[0][2];
                canvas[start_row + i][start_col - i - 1] += brush[1][0];
                canvas[start_row + i][start_col - i] += brush[1][1];
                canvas[start_row + i][start_col - i + 1] += brush[1][2];
                canvas[start_row + i + 1][start_col - i - 1] += brush[2][0];
                canvas[start_row + i + 1][start_col - i] += brush[2][1];
                canvas[start_row + i + 1][start_col - i + 1] += brush[2][2];
            }
        else if (start_row > end_row && start_col <= end_col)
            for (int i = 0; i <= abs(end_row - start_row); i++)
            {
                //canvas[start_row - i][start_col + i] = color;
                canvas[start_row - i - 1][start_col + i - 1] += brush[0][0];
                canvas[start_row - i - 1][start_col + i] += brush[0][1];
                canvas[start_row - i - 1][start_col + i + 1] += brush[0][2];
                canvas[start_row - i][start_col + i - 1] += brush[1][0];
                canvas[start_row - i][start_col + i] += brush[1][1];
                canvas[start_row - i][start_col + i + 1] += brush[1][2];
                canvas[start_row - i + 1][start_col + i - 1] += brush[2][0];
                canvas[start_row - i + 1][start_col + i] += brush[2][1];
                canvas[start_row - i + 1][start_col + i + 1] += brush[2][2];
            }
        else if (start_row > end_row && start_col > end_col)
            for (int i = 0; i <= abs(end_row - start_row); i++)
            {
                //canvas[start_row - i][start_col - i] = color;
                canvas[start_row - i - 1][start_col - i - 1] += brush[0][0];
                canvas[start_row - i - 1][start_col - i] += brush[0][1];
                canvas[start_row - i - 1][start_col - i + 1] += brush[0][2];
                canvas[start_row - i][start_col - i - 1] += brush[1][0];
                canvas[start_row - i][start_col - i] += brush[1][1];
                canvas[start_row - i][start_col - i + 1] += brush[1][2];
                canvas[start_row - i + 1][start_col - i - 1] += brush[2][0];
                canvas[start_row - i + 1][start_col - i] += brush[2][1];
                canvas[start_row - i + 1][start_col - i + 1] += brush[2][2];
            }
    }
    Fix(canvas);
    getchar();
}
void item2(int canvas[N_ROWS][N_COLS])
{
    int start_row, start_col, end_row, end_col;
    scanf("%d%d%d%d", &start_row, &start_col, &end_row, &end_col);
    if (start_row < 0 || start_row >= N_ROWS || start_col < 0 || start_col >= N_COLS ||
        end_row < 0 || end_row >= N_ROWS || end_col < 0 || end_col >= N_COLS)
        printf("Drawing area beyond canvas\n");
    else if (end_row >= start_row && end_col >= start_col)
        for (int i = start_row; i <= end_row; i++)
            for (int j = start_col; j <= end_col; j++)
                canvas[i][j] = color;
    else if (end_row < start_row && end_col >= start_col)
        for (int i = end_row; i <= start_row; i++)
            for (int j = start_col; j <= end_col; j++)
                canvas[i][j] = color;
    else if (end_row < start_row && end_col < start_col)
        for (int i = end_row; i <= start_row; i++)
            for (int j = end_col; j <= start_col; j++)
                canvas[i][j] = color;
    else if (end_row >= start_row && end_col < start_col)
        for (int i = start_row; i <= end_row; i++)
            for (int j = end_col; j <= start_col; j++)
                canvas[i][j] = color;
    getchar();
}
void item3()
{
    int i;
    scanf("%d", &i);
    getchar();
    switch (i)
    {
    case 0:
        color = BLACK;
        break;
    case 1:
        color = DARK;
        break;
    case 2:
        color = GREY;
        break;
    case 3:
        color = LIGHT;
        break;
    case 4:
        color = WHITE;
        break;
    default:
        printf("Invalid shade command!\n");
        break;
    }
    change = 1;
}
void item4(int canvas[N_ROWS][N_COLS])
{
    int start_row, start_col, end_row, end_col, target_row, target_col, tmp;
    scanf("%d%d%d%d%d%d", &start_row, &start_col, &end_row, &end_col, &target_row, &target_col);
    int tm[abs(end_row - start_row) + 1][abs(end_col - start_col) + 1];
    if (start_row < 0 || start_row >= N_ROWS || start_col < 0 || start_col >= N_COLS ||
        end_row < 0 || end_row >= N_ROWS || end_col < 0 || end_col >= N_COLS || target_row < 0 ||
        target_col < 0 || target_col >= N_COLS || target_row >= N_ROWS ||
        target_col + abs(end_col - start_col) >= N_COLS ||
        target_row + abs(end_row - start_row) >= N_ROWS)
        printf("Drawing area beyond canvas\n");
    else if (start_row > end_row && start_col <= end_col)
    {
        tmp = end_row;
        end_row = start_row;
        start_row = tmp;
        Copy(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, start_row, start_col);
        Stick(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, target_row, target_col);
    }
    else if (start_row <= end_row && start_col > end_col)
    {
        tmp = end_col;
        end_col = start_col;
        start_col = tmp;
        Copy(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, start_row, start_col);
        Stick(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, target_row, target_col);
    }
    else if (start_row > end_row && start_col > end_col)
    {
        tmp = end_row;
        end_row = start_row;
        start_row = tmp;
        tmp = end_col;
        end_col = start_col;
        start_col = tmp;
        Copy(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, start_row, start_col);
        Stick(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, target_row, target_col);
    }
    else
    {
        Copy(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, start_row, start_col);
        Stick(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, target_row, target_col);
    }
    getchar();
}
void item5()
{
    change = 0;

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d", &brush[i][j]);
    getchar();
}
void item6(int canvas[N_ROWS][N_COLS])
{
    setBlankCanvas(canvas);
    printCanvas(canvas);
    item8();
    change = 1;
}
void item7(int canvas[N_ROWS][N_COLS])
{
    int row = 0;
    int i = 0;
    printf("   ");
    while (i < N_COLS)
    {
        printf("%2d", i);
        i++;
    }
    printf("\n   -------------------------------------------------------------------------\n");
    while (row < N_ROWS)
    {
        int col = 0;
        printf("%2d|", row);
        while (col < N_COLS)
        {
            if (canvas[row][col] != 4)
                printf("%2d", canvas[row][col]);
            else
                printf("  ");
            col++;
        }
        row++;
        printf(" |\n");
    }
    printf("   -------------------------------------------------------------------------\n");
}
void item8()
{
    if (change)
        printf("Signal Pixel Brush!\n");
    else
        printf("Nine Pixel Brush!\n");
    printf("Shade = %d\n", color);
}
void item9(int canvas[N_ROWS][N_COLS])
{
    printCanvas(canvas);
}
//Written on March 28,2021.
//BUPT 2020  LinZhi.
### 回答1: 数学建模与系统辨识是南京理工大学自动化学院的一门课程,也是该专业的一门核心课程之一。这门课程旨在培养学生运用数学建模和系统辨识方法解决实际问题的能力。 njust大作业是这门课程的重要组成部分。作业的内容是通过研究某个实际问题,运用数学建模和系统辨识的理论和方法,对该问题进行分析和求解,并给出相应的模型和结论。该作业要求学生在进行建模和辨识过程中,充分运用所学的数学知识和相关的软件工具,同时要求学生合理选择方法和策略,严谨地论证和分析问题,以及准确地求解问题。 对于njust大作业的完成,首先需要理清问题的背景和要求,明确问题的目标和限制条件。然后,根据所学的数学建模和系统辨识的理论和方法,选择适当的模型和算法,对问题进行建模和求解。在建模过程中,需要进行数据分析和处理,选择合适的数学模型,确定模型的参数和结构,并进行模型的验证和优化。在系统辨识过程中,需要进行系统的特征分析和参数估计,掌握辨识方法的原理和步骤,进行实际数据的处理和模型的辨识。 最后,对于njust大作业的撰写和呈现,需要采用科学的结构和清晰的语言,将问题的分析和求解过程进行系统化的记录和阐述。同时,要注重结果的准确性和合理性,对模型的优缺点进行客观评价,给出合理的结论和建议。 通过完成njust大作业,学生能够进一步巩固和应用所学的数学建模和系统辨识的知识和技能,提高问题解决能力和实践能力。同时,该作业也为学生今后从事科研工作和工程实践打下了坚实的基础。 ### 回答2: 数学建模与系统辨识是南京理工大学(NJUST)数学与统计学院的一门重要大作业课程。本课程旨在培养学生的科研能力和综合应用数学知识解决实际问题的能力。 该课程的大作业要求学生团队选择一个实际问题进行建模和研究。这个问题可以涉及各个领域,例如经济、环境、能源、医学等。学生团队需要明确问题的背景与目标,并通过调研、收集数据、建立数学模型来分析和解决问题。 在建模过程中,学生需要选择适当的数学方法和工具,例如微分方程、概率统计、优化方法等。根据实际问题的特点,学生可以采用不同的建模技巧,例如参数估计、系统辨识、优化算法等。通过模型的建立和求解,学生可以对问题进行定量分析和预测,为问题的决策提供科学依据。 对于系统辨识的部分,学生需要从已有的数据中提取出系统的动态特性和结构,并用数学模型进行描述和表示。通过对系统的辨识,可以进一步分析系统的行为,并据此进行控制和优化。 完成大作业还需要学生团队具备良好的团队协作和沟通能力。一个成功的数学建模与系统辨识大作业,需要团队成员之间相互协作,分工合作,在有限的时间内完成任务,并对研究成果进行汇报和展示。 通过完成这个大作业,学生将不仅提高数学建模和系统辨识的实际应用能力,还培养了解决实际问题的全面思考和分析能力。这些能力对于学生未来的科研和实际工作都具有重要意义。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值