來源:碼農(nóng)網(wǎng)
很多程序員都覺得自己的編程工作十分的鼓噪乏味,一整天面對的都是一些寫不完的代碼和改不完的Bug。今天我們要給大家分享一些有趣的C語言代碼,或許可以為你無聊的工作帶來一絲樂趣。這些代碼可以完成幾個不同的愛心圖案,其中有一個例子甚至可以在黑色背景的控制臺上打印出跳動的3D愛心動畫,效果相當震撼。 我用的是Visual Studio 2015開發(fā)環(huán)境,如果你也使用這個開發(fā)環(huán)境,可以直接拷貝下面的代碼進行效果演示。 普通的愛心圖案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 本文作者:碼農(nóng)網(wǎng) – 小峰
|