C语言编程-输出心形图案

目录

方法3:使用心形公式


方法1:for循环输出图案

#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;
}

方法1补充:符号的输出可以用printf()实现也可以用putchar()实现,构成图案的符号可以试着用字符型变量代替,看官可以自行修改。

方法2:使用心形公式

 实现代码

#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;
}

使用pow()函数修改程序

#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;
}

效果

 

 

方法3:使用心形公式

 

 实现代码

#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;
}

效果图

 扩展

用 system(“color 0A”)更改颜色; 其中color后面的0是背景色代号,A是前景色代号。各颜色代码分别是:0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=亮蓝色 A=亮绿色 B=亮湖蓝色 C=亮红色 D=亮紫色 E=亮黄色 F=亮白色

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年12月26日
下一篇 2023年12月26日

相关推荐