1

我必须创建显示在 ppm 文件中的国家标志。我有下面的程序工作,除了当我显示标志时,法国国旗的条纹是对角线而不是垂直的,另外两个是水平的。我知道这是函数i < width中的东西make_flag_row,但我真的不知道什么会使条纹水平或垂直。

int main(void) {
    int width;
    int country_code;

    fscanf(stdin, "%d %d", &country_code, &width);
    fprintf(stderr, "Making country %d width %d \n", country_code, width);

    make_ppm_image(country_code, width);

    return(0);
}

void make_pixel(int r,int g,int b) {
    fprintf(stdout, "%c%c%c", r, g, b);
}

void make_ppm_header(int width, int height) {
    fprintf(stdout, "P6\n");
    fprintf(stdout, "%d %d %d\n", width, height, 255);
}

void make_ppm_image(int country_code, int width) {
    if(country_code==0) {
        make_france_flag(width);
    }
    else if(country_code==1) {
        make_germany_flag(width);
    }
    else {
        make_lithuania_flag(width);
    }
}   

void make_france_flag(int width) {
    int i;
    int height;
    height = width * 2/3;
    make_ppm_header(width, height);
    for(i = 0; i < height; i++)
        make_france_flag_row(width);
}

void make_germany_flag(int width) {
    int i;
    int height;
    height = width * 3/5;
    make_ppm_header(width, height);   
    for(i = 0; i < height; i++)
        make_germany_flag_row(width);
}

void make_lithuania_flag(int width) {
    int i;
    int height;
    height = width * 3/5;
    make_ppm_header(width, height);
    for(i = 0; i < height; i++)
        make_lithuania_flag_row(width);
}

void make_france_flag_row(int width) {
    int i;
    for (i = 0; i < width / 3; i++)
        make_pixel(0, 85, 164);
    for (i = 0; i < width / 3; i++)
        make_pixel(255, 255, 255);
    for (i = 0; i < width / 3; i++)
        make_pixel(250, 60, 50);
}

void make_germany_flag_row(int width) {
    int i;
    for (i = 0; i <= width / 3; i++)
        make_pixel(0, 0, 0);
    for (i = 0; i <= width / 3; i++)
        make_pixel(255, 0, 0);
    for (i = 0; i <= width / 3; i++)
        make_pixel(255, 204, 0);
}

void make_lithuania_flag_row(int width) {
    int i;
    for (i = 0; i < width / 3; i++)
        make_pixel(253, 185, 19);
    for (i = 0; i < width / 3; i++)   
        make_pixel(0, 106 , 68 );
    for (i = 0; i < width / 3; i++) 
        make_pixel(193, 39, 45);
}
4

0 回答 0