2009年12月21日 星期一

01201程式

#include <stdio.h>

void print_zero(int width, int height, int line_num, int br_or_not);
void print_one(int width, int height, int line_num, int br_or_not);
void print_two(int width, int height, int line_num, int br_or_not);

int main()
{
    int i, width, height;
    width=9;
    height=9;
    for (i=1;i<=height;i++)
    {
        print_zero(width,height,i,0);
        printf(" ");
        print_one(width,height,i,0);
        printf(" ");
        print_two(width,height,i,0);
        printf(" ");
        print_zero(width,height,i,0);
        printf(" ");
        print_one(width,height,i,1);
    }
    return 0;
}

void print_zero(int width, int height, int line_num, int br_or_not)
{
    int i,j;
    j=0;
    // print first line
    if (line_num == 1 || line_num == height)
    {
        // print the whole line
        printf("o");
        for (i=0;i < width-2;i++)
        {
            printf("-");
        }
        printf("o");
        if (br_or_not == 1)
        {
            printf("\n");
        }
        else
        {
            // continue
        }
    }
    else
    {
        // repeat print position 1 and the last one

        for (j=0;j < width;j++)
        {
            if(j==0 || j==width-1)
            {
                printf("|");
            }
            else
            {
                printf(" ");
            }
        }
        if (br_or_not == 1)
        {
            printf("\n");
        }
        else
        {
            // continue
        }
    }
}

void print_one(int width, int height, int line_num, int br_or_not)
{
    int i, middle;
    middle = width/2;
    for (i=0;i<height;i++)
    {
        for (i=0;i<width;i++)
        {
            if (i==middle)
            {
                printf("|");
            }
            else
            {
                printf(" ");
            }
        }

        if (br_or_not == 1)
        {
            printf("\n");
        }
        else
        {
            // continue
        }
    }
}

void print_two(int width, int height, int line_num, int br_or_not)
{
    int i,j;
    j=0;
    // print first line
    if (line_num == 1 || line_num == height||line_num == (height/2)+1)
    {
        // print the whole line
        printf("o");
        for (i=0;i < width-2;i++)
        {
            printf("-");
        }
        printf("o");
        if (br_or_not == 1)
        {
            printf("\n");
        }
        else
        {
            // continue
        }
    }
    else
    {
        // repeat print position 1 and the last one

        for (j=0;j < width;j++)
        {
            if(j==width-1 && line_num <= (height/2))
            {
                printf("|");
            }
            else if(j==0 && line_num > (height/2))
            {
                printf("|");
            }
            else
            {
                printf(" ");
            }
        }
        if (br_or_not == 1)
        {
            printf("\n");
        }
        else
        {
            // continue
        }
    }
}

沒有留言:

張貼留言