来源:码农网
很多程序员都觉得自己的编程工作十分的鼓噪乏味,一整天面对的都是一些写不完的代码和改不完的Bug。今天我们要给大家分享一些有趣的C语言代码,或许可以为你无聊的工作带来一丝乐趣。这些代码可以完成几个不同的爱心图案,其中有一个例子甚至可以在黑色背景的控制台上打印出跳动的3D爱心动画,效果相当震撼。 我用的是Visual Studio 2015开发环境,如果你也使用这个开发环境,可以直接拷贝下面的代码进行效果演示。 普通的爱心图案C语言代码: #include 'stdafx.h'#include #include int main(){ float y, x, a; for (y = 1.5f;y > -1.5f;y -= 0.1f) { for (x = -1.5f;x < 1.5f;x="" +="0.05f)" {="" a="x*x" +="" y*y="" -="" 1;="" putchar(a*a*a="" -="" x*x*y*y*y=""><= 0.0f="" '*'="" :="" '="" ');="" }="" putchar('\n');="" }="" return="">=> 效果图:

带花纹的爱心图案C语言代码: #include 'stdafx.h'#include #include int main(){ float y, x, z,f; for (y = 1.5f;y > -1.5f;y -= 0.1f) { for (x = -1.5f;x < 1.5f;x="" +="0.05f)" {="" z="x*x" +="" y*y="" -="" 1;="" f="z*z*z" -="" x*x*y*y*y;="" putchar(f=""><= 0.0f="" '.:-="+*#%@'[(int)(f*-8.0f)]" :="" '="" ');="" }="" putchar('\n');="" }="" getchar();="" return="">=> 效果图: 
跳动的3D爱心图案C语言代码: #include 'stdafx.h'#include #include #include #include float f(float x, float y, float z) { float a = x * x + 9.0f / 4.0f * y * y + z * z - 1; 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()="" {="" handle="" o="GetStdHandle(STD_OUTPUT_HANDLE);" _tchar="" buffer[25][80]="{" _t('="" ')="" };="" _tchar="" ramp[]="_T('.:-=+*#%@');" for="" (float="" t="0.0f;;" t="" +="0.1f)" {="" int="" sy="0;" float="" s="sinf(t);" float="" a="s" *="" s="" *="" s="" *="" s="" *="" 0.2f;="" for="" (float="" z="1.3f;" z=""> -1.2f; z -= 0.1f) { _TCHAR* p = &buffer[sy++][0]; float tz = z * (1.2f - a); for (float x = -1.5f; x < 1.5f;="" x="" +="0.05f)" {="" float="" tx="x" *="" (1.2f="" +="" a);="" float="" v="f(tx," 0.0f,="" tz);="" if="" (v=""><= 0.0f)="" {="" float="" y0="h(tx," tz);="" float="" ny="0.01f;" float="" nx="h(tx" +="" ny,="" tz)="" -="" y0;="" float="" nz="h(tx," tz="" +="" ny)="" -="" y0;="" float="" nd="1.0f" sqrtf(nx="" *="" nx="" +="" ny="" *="" ny="" +="" nz="" *="" nz);="" float="" d="(nx" +="" ny="" -="" nz)="" *="" nd="" *="" 0.5f="" +="" 0.5f;="" *p++="ramp[(int)(d" *="" 5.0f)];="" }="" else="" *p++=' ' ;="" }="" }="" for="" (sy="0;" sy="">=>< 25;="" sy++)="" {="" coord="" coord="{" 0,="" sy="" };="" setconsolecursorposition(o,="" coord);="" writeconsole(o,="" buffer[sy],="" 79,="" null,="" 0);="" }="" sleep(33);="">=> 效果图 跳动的爱心动画需要你运行程序后才可以看到。 
你还有更有趣的方案吗?欢迎告诉我们!
本文链接:http://www./article/c-3d-heart.html
本文链接:http://www./article/c-3d-heart.html 本文作者:码农网 – 小峰
|