2009年9月28日 星期一

印出01001

//
// sequential number print, file name:sq_num1.c
// 這個程式利用重覆迴圈與條件敘述句,練習循序數字列印的設計與應用
//
// 這是精簡化了老師顯示字幕的程式
// 給想挑戰的人看看
//

#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);

int main()
{
    int i, width, height;
    width=5;
    height=5;
    for (i=1;i<=height;i++)
    {
        print_zero(width,height,i,0);
        printf(" ");
        print_one(width,height,i,0);
        printf(" ");
        print_zero(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,k;
    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, j, 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
        }
    }
}

沒有留言:

張貼留言