系统功能简述:
初等函数曲线图形的简易绘制:设屏幕显示文本是25行,
80列,可以用“+”和“——〉”号画坐标系,用“*”号画曲线上的点。用户给出初等函数,如cos(x),e^x,x^3-3x+1等,及x的取值范围,程序绘制出对应初等函数的曲线图。
功能要求:
1.从键盘上输入x的显示范围;
2.根据x的取值范围在屏幕上画出函数图形;
3.函数的选择可以由键盘输入得到。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NANCHAR ' '
#define NAN -99999
#define e0 2.718281828459
char grid[25][80];
double data[80];
double dmax = 0;
double dmin = 0;
double fun1(double x)
{
double y = cos(x);
return y;
}
double fun(double x)
{
double y = x*x*x-3*x+1;
return y;
}
double fun2(double x)
{
double y = pow(e0,x);
return y;
}
void getminmax(int s,int e,int sel)
{
float step = (e-s)/80.0;
int k = 0;
for(double i=s;i<=e;i+=step)
{
double y = 0;
switch(sel)
{
case 1:
y = fun(i);
break;
case 2:
y = fun1(i);
break;
case 3:
y = fun2(i);
break;
default:
return;
}
if(i==s)
dmax = dmin = y;
else if(dmax < y)
dmax = y;
else if(dmin > y)
dmin = y;
data[k++] = y;
}
}
void getV(int range)
{
float yscale = (dmax-dmin)/25; //图形适当进行压缩或拉升
float xscale = range/80.0;
int k = 0;
for(double i=0;i<range;i+=xscale)
{
if(data[k] != NAN)
{
int y = int((dmax-data[k])/yscale);
int x = int(i/xscale);
grid[y][x] = '*';
}
k++;
}
}
void getGrid(int range)
{
float yscale = (dmax-dmin)/25; //图形适当进行压缩或拉升
float xscale = range/80.0;
int cx = 12;
int cy = 40;
for(int i=0;i<25;i++)
{
for(int j=0;j<80;j++)
{
if(i==cx && j==cy)
grid[i][j] = '+';
else if(i==cx)
grid[i][j] = '-';
else if(j==cy)
grid[i][j] = '|';
else
grid[i][j] = ' ';
}
}
grid[11][79] = '\\';
grid[13][79] = '/';
grid[0][39] = '/';
grid[0][41] = '\\';
}
void display()
{
int i,j;
for(i=0;i<25;i++)
{
for(j=0;j<80;j++)
{
printf("%c",grid[i][j]);
}
printf("\n");
}
printf("\n");
}
void main()
{
while(1)
{
int i,j;
for(i=0;i<25;i++)
for(j=0;j<80;j++)
grid[i][j] = NANCHAR;
for(j=0;j<80;j++)
data[j] = NAN;
system("cls");
//showmenu
printf(" 函数图形绘制系统\n\n");
printf(" 1、函数:y=x^3-3x+1\n");
printf(" 2、函数:y=cos(x)\n");
printf(" 3、函数:y=e^x\n");
printf(" 0、退出系统\n\n");
printf(" 请输入您的选择:");
int sel;
scanf("%d",&sel);
if(sel == 0)
exit(0);
printf("请设定函数x的取值范围:");
int s,e;
scanf("%d%d",&s,&e);
system("cls");
getminmax(s,e,sel);
getGrid(e-s);
getV(e-s);
display();
system("pause");
}
}