1.画余弦函数
#include <stdio.h>
#include <math.h> //本程序中反三角函数的计算用到标准库中的 math.h文件
//编译时出错,在后面加上 -lm即可
int main()
{
double y; //此处y为浮点型,提高计算精度
int x;
int m;
for(y = 1; y >= -1; y -= 0.1) //y值从1到-1,步长为0.1
{
m = acos(y)*10; //计算出y值对应的横坐标m,放大10倍
for(x = 1; x < m; x++)
{
printf(" "); //左半边的空格
}
printf("*"); //左半边的*号
for( ; x < 62 - m; x++)
{
printf(" "); //两个星号之间的空格
}
printf("*\n"); 最右侧星号后回车
}
return 0;
}
2.正弦曲线
#include <stdio.h> //详细分析过程见文章最后的附图
#include <math.h>
#define PAI 3.141592
int main()
{
double y;
double x,m;
double n;
for(y = 1; y >= 0; y -= 0.1)//x轴上半部分
{
m = asin(y)*10;//同理cos,放大十倍
for(x = 1; x < m; x++)
{
printf(" ");
}
printf("*");
for( ; x < 31-m; x++)
{
printf(" ");
}
printf("*\n");
}
for( ; y >= -1; y -= 0.1)//x轴下半部分
{
n = asin(-y);
m = (PAI + n)*10;
for(x = 1;x < m; x++)
{
printf(" ");
}
printf("*");
for( ; x < 94-m; x++)
{
printf(" ");
}
printf("*\n");
}
return 0;
}
3.同时绘制正余弦曲线
#include <stdio.h>
#include <math.h>
#define P 3.141592
int main()
{ //定义变量
int a,b,x;
double y,n;
//打印x轴上半部分
for(y = 1; y >= 0; y -= 0.1)
{
a = acos(y)*10;
b = asin(y)*10;
for(x = 0; x <= 62; x++)
{
if(x <= 31)
if((x == a && x == b) || (x == a && x == (31-b))) printf("+");
else if(x == a) printf("*");
else if(x == b || x == (31-b)) printf("+");
else printf(" ");
else if((x == (62-a) && x == b) || (x == (62-a) && x == (94-b))) printf("+");
else if(x == (62-a)) printf("*");
else if(x == b || x == (94-b)) printf("+");
else printf(" ");
}
printf("\n");
}
//打印x轴下半部分
for( ; y >= -1; y -= 0.1)
{
a = acos(y)*10;
n = asin(-y);
b = (P + n)*10;
for(x = 0; x <= 62; x++)
{
if(x <= 31)
if((x == a && x == b) || (x == a && x == (31-b))) printf("+");
else if(x == a) printf("*");
else if(x == b || x == (31-b)) printf("+");
else printf(" ");
else if((x == (62-a) && x == b) || (x == (62-a) && x == (94-b))) printf("+");
else if(x == (62-a)) printf("*");
else if(x == b || x == (94-b)) printf("+");
else printf(" ");
}
printf("\n");
}
return 0;
}