简体   繁体   中英

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. Images are represented as an M x N array of pixels, where each pixel has a given color.

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. 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). C Clear the table by setting all pixels white (O). The size remains unchanged. LXYC Colors the pixel (X, Y) in color (C). VX Y1 Y2 C Draw a vertical segment of color (C) in column X, between the rows Y1 and Y2 inclusive. H X1 X2 YC Draw a horizontal segment of color (C) in the row Y, between the columns X1 and X2 inclusive. 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. FXYC Fill the region R with the color C, where R is defined as follows. 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. S Name Write the file name in MSDOS 8.3 format followed by the contents of the current image. X Terminate the session. Output

On every command S NAME, print the filename NAME and contents of the current image. 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. In case of other errors, the program behavior is unpredictable.

Sample Input

I 5 6

L 2 3 A

S one.bmp

G 2 3 J

F 3 3 J

V 2 3 4 W

H 3 4 2 Z

S two.bmp

X

Sample Output

one.bmp

OOOOO

OOOOO

OAOOO

OOOOO

OOOOO

OOOOO

two.bmp

JJJJJ

JJZZJ

JWJJJ

JWJJJ

JJJJJ

JJJJJ

Here is my code. It compiles with gcc compiler but will not create the char array as I was expecting. Is there anything wrong with the pointer or 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. You're reading the Input char the same way.

Use the %c conversion specifier to read a single char.

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. Produce a program that simulates a simple interactive graphical editor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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