Table of contents
Method 1: for loop output pattern
Method 2: Use the Heart Shape Formula
Use the pow() function to modify the program
Method 3: Use the Heart Shape Formula
Method 1: for loop output pattern
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j;
//开头空5行
for(i=1;i<=5;i++)
printf("\n");
//前三行
for(i=1;i<=3;i++)
{
for(j=1;j<=17-2*i;j++)
printf(" ");
for(j=1;j<=4*i+1 ;j++)
printf("*");
for(j=1;j<=13-4*i;j++)
printf(" ");
for(j=1;j<=4*i+1 ;j++)
printf("*");
printf("\n");
}
//中间3行输出29颗星
for(i=1;i<=3;i++)
{
for(j=1;j<=10;j++)
printf(" ");
for(j=1;j<=29;j++)
printf("*");
printf("\n");
}
//下7行 倒三角造型
for(i=1;i<=7;i++)
{
for(j=1;j<=2*i-1+10;j++)
printf(" ");
for(j=1;j<=31-4*i;j++)
printf("*");
printf("\n");
}
//图案最后一行一颗星
for(i=1;i<=14+10;i++)
printf(" ");
printf("*");
//下方空5行
for(i=1;i<=5;i++)
printf("\n");
system("color 0c"); //修改系统色,前景色为c红色,背景色为0黑色。
return 0;
}
Supplement to method 1: The output of symbols can be implemented using printf() or putchar(). The symbols that make up the pattern can be replaced by character variables, and the viewer can modify them by himself.
Method 2: Use the Heart Shape Formula
Implement code
#include<stdio.h>
#include<stdlib.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;
if(a*a*a-x*x*y*y*y<=0.0)
putchar('*');
else
putchar(' ');
}
system("color 0c");
putchar('\n');
}
return 0;
}
Use the pow() function to modify the program
#include<stdio.h>
#include<stdlib.h>
#include<math.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)
{
if(pow((x*x+y*y-1),3)-pow(x,2)*pow(y,3)<=0.0)
putchar('*');
else
putchar(' ');
}
system("color 0c");
putchar('\n');
}
return 0;
}
Effect
Method 3: Use the Heart Shape Formula
Implement code
#include<stdio.h>
#include<stdlib.h>
#include<math.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)
{
if((pow(x,2)+pow(5.0*y/4.0-sqrt(fabs(x)),2))<=1) putchar('*');
else
putchar(' ');
}
system("color 0c");
putchar('\n');
}
return 0;
}
renderings
Expand
Use system("color 0A") to change the color; the 0 after color is the background color code, and A is the foreground color code. The color codes are: 0 = black 1 = blue 2 = green 3 = lake blue 4 = red 5 = purple 6 = yellow 7 = white 8 = gray 9 = bright blue A = bright green B = bright lake blue Color C=bright red D=bright purple E=bright yellow F=bright white