putchar(".:-=+*#%@"[(int)(d * 5.0f)])
前两天在玩知乎时候见到有个用C语言画心的小代码感觉还是蛮好玩的,不过,里面有行代码看了好久才懂: putchar(".:-=+*#%@"[(int)(d * 5.0f)]); ,先来看看putchar这个函数,用man查看一下其原型: int putchar(int c)
然后我们写个例程看看:
#include <stdio.h>
int main(int argc, char const *argv[])
{
/*int putchar(int c)等价于int putc(int c, FILE *stream)*/
putchar(".:-=+*#%@"[]);
return ;
}
输出是下面这样的,就只有一个@这个字符

再多写几行把那串字符串里的'@'这个字符改一下再输出:
#include <stdio.h>
int main(int argc, char const *argv[])
{
/*int putchar(int c)等价于int putc(int c, FILE *stream)*/
putchar(".:-=+*#%@"[]);
putchar(".:-=+*#%3"[]);
putchar(".:-=+*#%s"[]);
putchar(".:-=+*#%h"[]);
return ;
}
输出:

仔细看看貌似输出的字符都是我们改过来的,我们再数数发现那个'@'字符正好是".:-=+*#%@"这个字符串的第9个元素……
我天,这其实就相当于str[8]嘛……真的是蠢到家!!!
最后把原始代码贴出来吧,知乎在这里:如何用 C 语言画「心形」?
#include <stdio.h>
#include <math.h> float f(float x, float y, float z)
{
float a = x * x + 9.0f / 4.0f * y * y + z * z - ;
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
} float h(float x, float z)
{
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
if(f(x, y, z) <= 0.0f)
return y;
return 0.0f;
} int main(int argc, char const *argv[])
{
for (float z = 1.5f; z > -1.5f; z -= 0.05f)
{
for (float x = -1.5f; x < 1.5f; x += 0.025f)
{
float v = f(x, 0.0f, z);
if(v <= 0.0f)
{
float y0 = h(x, z);
float ny = 0.01f;
float nx = h(x + ny, z) - y0;
float nz = h(x, ny + z) - y0;
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
putchar(".:-=+*#%@"[(int)(d * 5.0f)]);
}
else
{
putchar(' ');
}
}
putchar('\n');
}
return ;
}
putchar(".:-=+*#%@"[(int)(d * 5.0f)])的更多相关文章
- 用C++画心(转)
原地址https://www.zhihu.com/topic/19613730/top-answers 首先上一个动态的心 代码如下: #include <stdio.h> #includ ...
- 用C语言画一个“爱心”
/*每个main函数代表一种形状*/#include<iostream> using namespace std; #include<stdio.h> #include< ...
- 【Spec for GS5】不要嘲笑程序员不懂烂漫
// // main.cpp // 生日快乐 // // Created by wasdns on 16/11/21. // Copyright © 2016年 wasdns. All rights ...
- C++命令行画心形<转载>
#include <stdio.h> int main() { for (float y = 1.5f; y > -1.5f; y -= 0.1f) { for (float x = ...
- Gift for GS5
// // main.cpp // 生日快乐 // // Created by wasdns on 16/11/21. // Copyright © 2016年 wasdns. All rights ...
- 【C语言】用C语言输出“心形”图案
在你们的世界里,是不是觉得程序猿一点浪漫都不懂?其实不是的,程序猿的世界也是很浪漫滴! 傻瓜版 int main() { printf("❤"); ; } 高级版 //版本一:单个 ...
- 关于int类型的赋值语句正确的有
A.char a=65; 对 B.int a=12.0; C.int a=12.0f; D.int a=(int)12.0 对
- C putchar() 和 getchar()
C 库函数 int getchar(void) 从 终端输入获取一个字符 : 返回值:该函数以无符号 char 强制转换为 int 的形式返回读取的字符,如果到达文件末尾或发生读错误,则返回 EO ...
- 字符的输入和输出即:getchar和putchar
#include <stdio.h> int main(int argc, const char * argv[]) { putchar(getchar());//这种方式就是输入一个字符 ...
随机推荐
- UIViewController push或presentViewController 弹出方式
//导航控制器数量 add xjz 判断是push还是present出来的 NSArray *viewcontrollers = self.navigationController.viewContr ...
- 屏幕坐标点转UGUI坐标【包含屏幕适配】
using UnityEngine; public class ScreenToUI : MonoBehaviour { public const float UI_Width = 1366f; pu ...
- smbumount - 为普通用户卸载smb文件系统
总览 smbumount 装载点 描述 普通用户使用这个程序可以卸载smb文件系统.它在工作时会suid到root身份,并且向普通linux用户提供了对资源更多的控制能力.在suid方面,它拥有足够的 ...
- 03.LNMP架构-PHP源码包编译部署详细步骤
一.环境准备 操作系统:CentOS_Server_7.5_x64_1804.iso 部署组件:yasm+libmcrypt+libvpx+tiff+libpng+freetype+jpeg+libg ...
- [BZOJ1023][SHOI2008]cactus仙人掌图 DP
题目链接 套路就是先考虑一般的树上做法.求直径的dp的做法大家应该都会吧. 那么设\(dp[i]\)表示\(i\)的子树中的点到\(i\)的最大距离. 在dp的过程中 \[ ans=\max\{dp[ ...
- 用pycharm运行pytest
安装pytest 1. 在pycharm中建项目,建文件,文件名字要以test_开头 2.在文件中插入pytest模块 import pytest #引用pytest模块 3.定义test函数,以及断 ...
- kd树解平面最近点对
早上起来头有点疼,突然就想到能不能用kd树解平面最近点对问题,就找了道题试了一下,结果可以,虽然效率不高,但还是AC了~ 题目链接:http://acm.hdu.edu.cn/showproblem. ...
- centos 6.5 配置网络
编辑 vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改内容 DEVICE="eth0" BOOTPROTO="static& ...
- less和vim中使用正则表达式搜索
使用less查看 txt 文件之后,按\可以正则表达式来搜索: less phonelist.txt (232) 298-2265 (624) 381-1078 (540) 126-1980 (874 ...
- spring+springMVC+mybatis框架整合——配置文件说明
如下图 web.xml配置说明: spring配置文件说明-1: spring配置文件说明-2: spring配置助记: 扫注(base) 读配(loc) 数据源(和comb(使用c3p0数据源)) ...