情人节到了,作为一名程序员,我们拥有属于我们的浪漫。
这里我总结了几种常见的爱心代码,简单易上手。
一.这是一种最为常见的爱心代码
#include<stdio.h>
#include<Windows.h>int main()
{float x, y, a;for (y = 1.5; y > -1.5; y -= 0.1){for (x = -1.5; x < 1.5; x += 0.05){a = x * x + y * y - 1;putchar(a * a * a - x * x * y * y * y <= 0.0 ? '*' : ' ');}system("color 0c");putchar('\n');}return 0;
}
也许我们可以再加一丁点东西让它变的更加浪漫一些
#include<stdio.h>
#include<Windows.h>int main()
{float x, y, a;char color;while (1){for (y = 1.5; y > -1.5; y -= 0.1){for (x = -1.5; x < 1.5; x += 0.05){a = x * x + y * y - 1;putchar(a * a * a - x * x * y * y * y <= 0.0 ? '*' : ' ');}system("color 0c");putchar('\n');}}return 0;
}
二.一个会跳动的爱心又有谁不爱呢?
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <tchar.h>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);}
}
三.简单的love
#include <stdio.h>int main(){int i, j, k, n = 0, x = 0, y = 50;//爱心的头部没有规律,所以直接打印printf("\n\n\n\n\n");printf(" lovelove lovelove\n");printf(" lovelovelove lovelovelove\n");printf(" lovelovelovelove lovelovelovelove\n");printf(" lovelovelovelovelove lovelovelovelovelove\n");printf(" lovelovelovelovelovelo lovelovelovelovelovelo\n");printf(" lovelovelovelovelovelove lovelovelovelovelovelov\n");for (i = 0; i < 2; i++){printf("lovelovelovelovelovelovelovelovelovelovelovelovelove\n");}for(i=0;i<5;i++) //爱心的中间部分的上部分{y = 50;y = y - i*2;n++;for (k = 0; k < n; k++) //在每一行的起始位置先打印空格{printf(" ");}while(1) //空格后面打印love,但是要注意love即使没打印完,也要换行{if (x < y){printf("l");y--;}elsebreak;if (x < y){printf("o");y--;}elsebreak; if (x < y){printf("v");y--;}elsebreak; if (x < y){printf("e");y--;}elsebreak;}printf("\n");}//最下面的部分,具体内容同上,没和上一部分放一起是因为从这行开始多两个空格for (i = 0,n=3; i < 10; i++){y = 37;y = y - i * 4;n++;for (k = 0; k < n; k++){printf(" ");}while (1){if (x < y){printf("l");y--;}elsebreak;if (x < y){printf("o");y--;}elsebreak; if (x < y){printf("v");y--;}elsebreak; if (x < y){printf("e");y--;}elsebreak;}printf("\n");}printf("\n\n\n\n\n\n\n\n\n\n\n\n");return 0;
}
四.简单又明了
#include<stdio.h>
#define V 4//设置心形放大倍数
#define list (4*V+3)//列数
#define row (3*V+3)//行数
void FindMainPoint(char line[row][list]);
void Connect(int r1, int r2, int l1, int l2, char line[row][list]);//r行l列
void Print(char line[row][list]);
void Finger(char point[17][20]);
int main(void)
{char line[row][list];char point[17][20];FindMainPoint(line);Print(line);printf("\n\n");Finger(point);return 0;
}//找到关键点
void FindMainPoint(char line[row][list])
{int list_mid = (list + 1) / 2 - 1;int list_left = (list + 1) / 4 - 1;int list_left_right = list_left + 1;int list_right = list_mid + list_left + 1;int list_right_left = list_right - 1;int row_above = 1 - 1;int row_mid = row / 3 - 1;int row_mid_below = row_mid + 1;int row_below = row - 1;line[row_above][list_left] = '*';//(4,1)line[row_above][list_left_right] = '*';//(5,1)line[row_above][list_right] = '*';//(12,1)line[row_above][list_right_left] = '*';//(11,1)line[row_mid][0] = '*';//(1,4)line[row_mid][list_mid] = '*';//(8,4)line[row_mid][list - 1] = '*';//(15,4)line[row_mid_below][0] = '*';//(1,5)line[row_mid_below][list - 1] = '*';//(15,5)line[row_below][list_mid] = '*';//(8,12)Connect(row_mid, row_above, 0, list_left, line);//(1,4)&&(4,1)Connect(row_above, row_mid, list_left_right, list_mid, line);//(5,1)&&(8,4)Connect(row_mid, row_above, list_mid, list_right_left, line);//(8,4)&&(11,1)Connect(row_above, row_mid, list_right, list - 1, line);//(12,1)&&(15,4)Connect(row_mid_below, row_below, 0, list_mid, line);//(1,5)&&(8,12)Connect(row_below, row_mid_below, list_mid, list - 1, line);//(8,12)&&(15,5)return;
}//连接关键点
void Connect(int r1, int r2, int l1, int l2, char line[row][list])
{int i, j;if (r1 > r2){for (i = r1, j = l1;i >= r2;i--, j++){line[i][j] = '*';}}else{for (i = r1, j = l1;i <= r2;i++, j++){line[i][j] = '*';}}return;
}//打印爱心
void Print(char line[row][list])
{int i, j;for (i = 0;i < row;i++){printf("\t\t\t\t\t\t");//将图形尽量居中,改变大小后,自行修改for (j = 0;j < list;j++){if (line[i][j] != '*'){printf(" ");}elseprintf("*");}printf("\n");}
}//打印手指
void Finger(char point[17][20])
{int i, j;for (i = 0, j = 5;j <= 6;j++)point[i][j] = '*';//for (j = 4, i = 1;i <= 8;i++)point[i][j] = '*';//for (j = 7, i = 1;i <= 6;i++)point[i][j] = '*';//for (i = 4, j = 8;j <= 10;j++)point[i][j] = '*';//for (j = 11, i = 5;i <= 6;i++)point[i][j] = '*';//for (i = 5, j = 12;j <= 14;j++)point[i][j] = '*';//for (j = 15, i = 6;i <= 7;i++)point[i][j] = '*';//for (i = 6, j = 15;j <= 18;j++)point[i][j] = '*';for (j = 19, i = 7;i <= 11;i++)point[i][j] = '*';//for (j = 18, i = 12;i <= 13;i++)point[i][j] = '*';//for (j = 17, i = 14;i <= 15;i++)point[i][j] = '*';//for (i = 16, j = 6;j <= 16;j++)point[i][j] = '*';//for (i = 14, j = 7;j <= 15;j++)point[i][j] = '*';for (j = 7;j <= 15;j += 4)//for (i = 10;i <= 12;i++)point[i][j] = '*';for (j = 5, i = 14;i <= 15;i++)point[i][j] = '*';//for (j = 2, i = 10;i <= 11;i++)point[i][j] = '*';//for (j = 0, i = 7;i <= 8;i++)point[i][j] = '*';//for (i = 6, j = 1;j <= 2;j++)point[i][j] = '*';point[7][3] = '*';point[9][1] = '*';point[12][3] = '*';point[13][4] = '*';for (i = 0;i < 17;i++){printf("\t\t\t\t\t\t ");//将图形尽量居中,改变大小后,自行修改for (j = 0;j < 20;j++){if (point[i][j] != '*'){printf(" ");}elseprintf("*");}printf("\n");}
}
五.好看又直接
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#define I 20
#define R 350
int main()
{
system("mode con cols=80 lines=80");
system("color F4");
int i, j, e;
int a;
long time;
for (i = 1, a = I; i < I / 2; i++, a--)
{
for (j = (int)(I - sqrt(I * I - (a - i) * (a - i))); j > 0; j--)
printf(" ");
for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e++)
printf("\3");
for (j = (int)
(2 * (I - sqrt(I * I - (a - i) * (a - i)))); j > 0; j--)
printf(" ");
for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e++)
printf("\3");
printf("\n");
}
for (i = 1; i < 49;i++)
{
if (i == 20)
{
printf(" 我喜欢你 ");
i += 15;
}
printf("\3");
}
printf("\n");
for (i = 1; i <= R / 2; i++)
{
if (i % 2 || i % 3)
continue;
for (j = (int)(R - sqrt(R * R - i * i)); j > 0; j--)
printf(" ");
for (e = 1; e <= 2 * (sqrt(R * R - i * i) - (R - 2 * I)); e++)
printf("\3");
printf("\n");
}
system("pause");
}
码字不易,求关注~~
(文中代码与部分内容来源与网络,如有侵权请联系删除)