环状图形的输出方法

作者在 2008-01-24 17:52:22 发布以下内容

图形:
A A A A A A A A A A A A A
A B B B B B B B B B B B A
A B C C C C C C C C C B A
A B C D D D D D D D C B A
A B C D E E E E E D C B A
A B C D E F F F E D C B A
A B C D E F G F E D C B A
A B C D E F F F E D C B A
A B C D E E E E E D C B A
A B C D D D D D D D C B A
A B C C C C C C C C C B A
A B B B B B B B B B B B A
A A A A A A A A A A A A A  

代码:

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
   FILE *fp;
 int i, sum, total, row, line;/*sum表示总的圈数*/
 char ch;
   char count[80][80];

   puts("Enter the total of row: ");
   scanf("%d",&total);
   for(row=1; row<=total; row++)
    for(line=1; line<=total; line++)
       count[row][line]=0 ;

   if(total%2 == 0)/*根据输入数据的奇偶性,确定圈数*/
    sum=total/2;
   else
    sum=total/2+1;

  for(ch=65,i=1; i<=sum; i++,ch++)/*sum表示总的圈数*/
     for(row=1; row<=total; row++)
         for(line=1; line<=total; line++)
           if (0 == count[row][line])/*该位置是否已被占*/
              if( row == i || row ==(total-i+1) || line == i || line == (total-i+1) )/*是否为边界位置*/
            count[row][line]=ch;

   if ( (fp=fopen("sdas.txt","w")) == NULL)
    {
        fprintf(stderr,"\nError opening file \n");
    exit(1);
    }

   for(row=1; row<=total; row++)/*输出数组(即图案)*/
   {
   for(line=1; line<=total; line++)
       {
           fprintf(fp, "%c ", count[row][line]);
           fprintf(stdout, "%c ", count[row][line]);
         }
   fprintf(fp, "\n");
  fprintf(stdout, "\n");
   }

   system("pause");
   return 0;
}

C | 阅读 1641 次
文章评论,共0条
游客请输入验证码
浏览50201次
最新评论