新年快乐
C++打印新年快乐和心形图案的代码,可以参考下。
#include <>
#include <>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <>
#include <string>
#include <>
#include <>
#include <>
using namespace std;
clock_t startTime, endTime;
void print();
void time(int i);
void one();
void two();
void three();
void image();
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() {
system("mode con cols=120 lines=50");
three();
two();
one();
system("color 4");
print();
time(2);
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 20;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
wcscpy_s(cfi.FaceName, L"Lucida Console");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
system("cls");
cout << "祝你在2021年心想事成!!" << endl;
time(2);
image();
system("cls");
return 0;
}
void print() {
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = 50;
cfi.dwFontSize.Y = 30;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
wcscpy_s(cfi.FaceName, L"Lucida Console");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
for (int i = 0; i < 5; i++)
cout << endl;
cout << setw(20) << "2021新年快乐" << endl;
cout << setw(20) << "2021新年快乐" << endl;
cout << setw(20) << "2021新年快乐" << endl;
for (int i = 0; i < 5; i++)
cout << endl;
}
void time(int i) {
bool flag = false; double sum;
startTime = clock();
do {
endTime = clock();
sum = (double)(endTime - startTime) / CLOCKS_PER_SEC;
} while (sum <= i);
}//运行一秒,产生切屏效果
void one()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//RED位置控制字体颜色
system("color 4");//这个好用一点
int x = 0;//
for (int i = 0; i < 30; i++)
{
if (i < 5)printf("\n");
if (i > 4 && i < 10) {
cout << setw(48LL - static_cast<long long>(x));//后面打印的第一个字符受setw控制,所以j<=66
for (int j = 48 - x + 1; j <= 66; j++)
{
cout << "1";
}
cout << endl;
x = x + 2;
}
if (i > 9)
{
cout << setw(65) << "11111111111111111" << endl;
}
}
time(1);
system("cls");
}
void two()
{
system("color 4");
int x = 0, y = 0;
for (int i = 0; i < 30; i++)
{
if (i < 5) { printf("\n"); continue; }
else {
if (i >= 5 && i < 10)cout << setw(42) << " ";//打印2的一行
if (i >= 10 && i < 15) { x = 35; cout << setw(77) << " "; }//打印一列
if (i >= 15 && i < 20) { cout << setw(42) << " "; x = 0; y = 0; }
if (i >= 20 && i < 25) {
cout << setw(42) << " "; y = 35; x = 0;
}
if (i >= 25) { cout << setw(42) << " "; x = 0; y = 0; }
for (int j = 30 + x; j < 70 - y; j++)
{
cout << "2";
}
}
cout << endl;
}
time(1);
system("cls");
}
void three()
{
system("color 4");
int x = 0, y = 0;
for (int i = 0; i < 30; i++)
{
if (i < 5) { printf("\n"); continue; }
else {
if (i >= 5 && i < 10)cout << setw(42) << " ";//打印3的一行
if (i >= 10 && i < 15) { x = 35; cout << setw(77) << " "; }//打印一列
if (i >= 15 && i < 20) { cout << setw(42) << " "; x = 0; y = 0; }
if (i >= 20 && i < 25) {
cout << setw(77) << " "; y = 35; x = 0;
}
if (i >= 25) { cout << setw(42) << " "; x = 0; y = 0; }
for (int j = 30 + x; j < 70 - y; j++)
{
cout << "3";
}
}
cout << endl;
}
time(1);
system("cls");
}
void image() {
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);
}
祝大家新年快乐。
参考文章