简体   繁体   English

带有 c 程序的图形编辑器(编程挑战 110105)

[英]graphical editor with c program (programming challenges 110105)

Graphical editors such as Photoshop allow us to alter bit-mapped images in the same way that text editors allow us to modify documents. Photoshop 等图形编辑器允许我们以与文本编辑器允许我们修改文档相同的方式修改位图图像。 Images are represented as an M x N array of pixels, where each pixel has a given color.图像表示为 M x N 像素阵列,其中每个像素具有给定的颜色。

Your task is to write a program which simulates a simple interactive graphical editor.您的任务是编写一个程序来模拟一个简单的交互式图形编辑器。

Input输入

The input consists of a sequence of editor commands, one per line.输入由一系列编辑器命令组成,每行一个。 Each command is represented by one capital letter placed as the first character of the line.每个命令由一个大写字母表示,作为该行的第一个字符。 If the command needs parameters, they will be given on the same line separated by spaces.如果命令需要参数,它们将在以空格分隔的同一行中给出。

Pixel coordinates are represented by two integers, a column number between 1...M and a row number between 1...N, where 1$ \\le$M, N$ \\le$250.像素坐标由两个整数表示,列号介于 1...M 之间,行号介于 1...N 之间,其中 1$\\le$M,N$\\le$250。 The origin sits in the upper-left corner of the table.原点位于表格的左上角。 Colors are specified by capital letters.颜色由大写字母指定。

The editor accepts the following commands:编辑器接受以下命令:

IMN Create a new M x N image with all pixels initially colored white (O). IMN 创建一个新的 M x N 图像,所有像素最初都为白色 (O)。 C Clear the table by setting all pixels white (O). C 通过将所有像素设置为白色 (O) 来清除表格。 The size remains unchanged.大小保持不变。 LXYC Colors the pixel (X, Y) in color (C). LXYC 以颜色 (C) 为像素 (X, Y) 着色。 VX Y1 Y2 C Draw a vertical segment of color (C) in column X, between the rows Y1 and Y2 inclusive. VX Y1 Y2 C 在 X 列中,在 Y1 和 Y2 行之间(包括 Y1 和 Y2)绘制一条垂直的颜色 (C) 线段。 H X1 X2 YC Draw a horizontal segment of color (C) in the row Y, between the columns X1 and X2 inclusive. H X1 X2 YC 在 Y 行中,在 X1 和 X2 列之间(包括 X1 和 X2)绘制一条颜色 (C) 的水平线段。 K X1 Y1 X2 Y2 C Draw a filled rectangle of color C, where (X1, Y1) is the upper-left and (X2, Y2) the lower right corner. K X1 Y1 X2 Y2 C 画一个颜色为C 的实心矩形,其中(X1, Y1) 是左上角,(X2, Y2) 是右下角。 FXYC Fill the region R with the color C, where R is defined as follows. FXYC 用颜色 C 填充区域 R,其中 R 的定义如下。 Pixel (X, Y) belongs to R. Any other pixel which is the same color as pixel (X, Y) and shares a common side with any pixel in R also belongs to this region.像素(X,Y)属于R。与像素(X,Y)颜色相同并与R中的任何像素共享公共边的任何其他像素也属于该区域。 S Name Write the file name in MSDOS 8.3 format followed by the contents of the current image. S 名称 以 MSDOS 8.3 格式写入文件名,后跟当前图像的内容。 X Terminate the session. X 终止会话。 Output输出

On every command S NAME, print the filename NAME and contents of the current image.在每个命令 S NAME 上,打印文件名 NAME 和当前图像的内容。 Each row is represented by the color contents of each pixel.每行由每个像素的颜色内容表示。 See the sample output.请参阅示例输出。

Ignore the entire line of any command defined by a character other than I, C, L, V, H, K, F, S, or X, and pass on to the next command.忽略由 I、C、L、V、H、K、F、S 或 X 以外的字符定义的任何命令的整行,并传递到下一个命令。 In case of other errors, the program behavior is unpredictable.万一出现其他错误,程序行为是不可预测的。

Sample Input样本输入

I 5 6我 5 6

L 2 3 A L 2 3 A

S one.bmp一个.bmp

G 2 3 J G 2 3 J

F 3 3 J F 3 3 J

V 2 3 4 W电压 2 3 4 瓦

H 3 4 2 Z高 3 4 2 Z

S two.bmp S二.bmp

X X

Sample Output样本输出

one.bmp一个.bmp

OOOOO呜呜呜

OOOOO呜呜呜

OAOOO噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢

OOOOO呜呜呜

OOOOO呜呜呜

OOOOO呜呜呜

two.bmp二.bmp

JJJJJ JJJJJ

JJZZJ锦江锦江

JWJJJ JWJJJ

JWJJJ JWJJJ

JJJJJ JJJJJ

JJJJJ JJJJJ

Here is my code.这是我的代码。 It compiles with gcc compiler but will not create the char array as I was expecting.它使用 gcc 编译器进行编译,但不会像我预期的那样创建 char 数组。 Is there anything wrong with the pointer or malloc?指针或malloc有什么问题吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **Pixel;
int *m, *n;

void Create(int m, int n) {
    int i, j;
    Pixel = malloc(sizeof (char*)*n * n);
    for (i = 0; i < m * n; i++)
        Pixel[i] = malloc(sizeof (char*)*m);
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++) {
            Pixel[i][j] = '0';
        }
}

void ColorOneSpot(int x, int y, char color) {
    Pixel[x - 1][y - 1] = color;
}

void ColorColumn(int x, int y1, int y2, char color) {
    int i;
    for (i = y1; i <= y2; i++)
        Pixel[x - 1][i] = color;
}

void ColorRow(int x1, int x2, int y, char color) {
    int i;
    for (i = x1; i <= x2; i++)
        Pixel[i][y - 1] = color;
}

void ColorSquare(int *x1, int x2, int y1, int y2, char color) {
    int i, j;
    for (i = x1 - 1; i < x2; i++)
        for (j = y1 - 1; j < y2; j++)
            Pixel[i][j] = color;
}

void ColorNeighbor(int x, int y, char color) {
    int i, j;
    Pixel[y][x] = color;
    for (i = x; i <= x + 2; i++)
        if (i >= 0 && i <= m) {
            for (j = y; j <= y + 2; j++) {
                if (Pixel[i][j] == 0 && j >= 0 && j <= n)
                    Pixel[i][j] = color;
                ColorNeighbor(x, y - 1, color);
                ColorNeighbor(x, y + 1, color);
                ColorNeighbor(x - 1, y, color);
                ColorNeighbor(x + 1, y, color);
            }
        }

}

int main(void) {
    int i, j;
    char Input;
    while (Input != 'X') {
        int x, y, x1, y1, x2, y2;
        char color, name[20];
        scanf("%1s", &Input);
        switch (Input) {
            case 'I':
                scanf("%d%d", &m, &n);
                Create(*m, *n);
                break;
            case 'L':
                scanf("%d%d%s", &x, &y, &color);
                ColorOneSpot(x, y, color);
                break;
            case 'V':
                scanf("%d%d%d%s", &x, &y1, &y2, &color);
                ColorColumn(x, y1, y2, color);
                break;
            case 'H':
                scanf("%d%d%d%s", &x1, &x2, &y, &color);
                ColorRow(x1, x2, y, color);
                break;
            case 'K':
                scanf("%d%d%d%d%s", &x1, &x2, &y1, &y2, &color);
                ColorSquare(x1, x2, y1, y2, color);
                break;
            case 'F':
                scanf("%d%d%s", &x, &y, &color);
                ColorNeighbor(x, y, color);
                break;
            case 'S':
                scanf("%s", &name);
                printf("%s\n", name);
                for (i = 0; i < m; i++) {
                    for (j = 0; j < n; j++)
                        printf("%s ", Pixel[i][j]);
                    printf("\n");
                }

                break;
            case 'X':break;
        }
    }
    free(Pixel);
    return 0;
}

Your color value is a char but you're reading it with "%s" in the scanf calls.您的color值是一个char但您在scanf调用中使用"%s"读取它。 You're reading the Input char the same way.您正在以相同的方式读取Input字符。

Use the %c conversion specifier to read a single char.使用%c转换说明符读取单个字符。

enter link description here 在此处输入链接描述

Graphical editors allow users to edit images in the same way text editors let us modify documents.图形编辑器允许用户以与文本编辑器让我们修改文档相同的方式编辑图像。 Images are represented as an M x N array of pixels with each pixel given colour.图像表示为 M x N 像素阵列,每个像素给定颜色。 Produce a program that simulates a simple interactive graphical editor.制作一个模拟简单交互式图形编辑器的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM