ASCII字符显示例子

代码块


#include<stdio.h>
#include<stdlib.h>
//算法是最费心思,也是最有趣的事情了,char字符也很有趣,使用字符画矩形。
//简单的程序很多问题,不过乐乐而已
int main(void)
{
    FILE *fp;//保存的文件
    int i, total;//字符总数
    int row, col;//行列数
    char ch; //初始字符
    char count[80][80];//字符矩阵

    puts("Enter the total of rings: ");
    scanf("%d", &total);

    ch = 32;
    for (row = 1; row <= 2 * total; row++)
    for (col = 1; col <= 2 * total; col++)
        count[row][col] = 0;
    //置空数组
    //row:1…n
    //  col:1…n
    //    v[r,c]=0

    /*利用循环语句分别输出上下左右四条边,注意每个圈往里缩一个位置*/
    for (i = 1; i <= total; i++)
    {
        row = i;
        for (col = i + 1; col <= 2 * total - i; col++)//绘制倒三角,上边框
        {
            count[row][col] = ch;
        }
        row = 2 * total - i + 1;
        for (col = i + 1; col <= 2 * total - i; col++)//绘制正三角,下边框
        {
            count[row][col] = ch;
        }
        col = i;
        for (row = i; row <= 2 * total - i + 1; row++)//绘制左三角,左边框
        {
            count[row][col] = ch;
        }
        col = 2 * total - i + 1;
        for (row = i; row <= 2 * total - i + 1; row++)//绘制右三角,右边框
        {
            count[row][col] = ch;
        }
        ch++;
    }
    /*利用判断语句输出上下左右四条边,注意每个圈往里缩一个位置*/
    for (i = 1; i <= total; i++)
    {
        for (row = i; row <= 2 * total - i; row++)
        for (col = i; col <= 2 * total - i; col++)
        if (row == i || row == 2 * total - i || col == i || col == 2 * total - i)
            count[row][col] = ch;
        ch++;
    }

    if ((fp = fopen("sdas.txt", "w")) == NULL)
    {
        fprintf(stderr, "\nError opening file \n");
        exit(1);
    }
    /*输出数组(即图案)*/
    for (row = 1; row <= 2 * total; row++)
    {
        for (col = 1; col <= 2 * total; col++)
        {
            fprintf(fp, "%c ", count[row][col]);//输出到文件,
            fprintf(stdout, "%c ", count[row][col]);//输出到屏幕
        }
        fprintf(fp, "\n");
        fprintf(stdout, "\n");
    }

    system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微澜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值