采用逐段比较法直线插补曲线 y=1/4*x2,x∈[-5,5]麻烦把程序编辑出来行么~
来源:学生作业帮助网 编辑:作业帮 时间:2024/12/01 07:14:17
采用逐段比较法直线插补曲线 y=1/4*x2,x∈[-5,5]麻烦把程序编辑出来行么~
采用逐段比较法直线插补曲线 y=1/4*x2,x∈[-5,5]
麻烦把程序编辑出来行么~
采用逐段比较法直线插补曲线 y=1/4*x2,x∈[-5,5]麻烦把程序编辑出来行么~
TurboC 逐段比较直线插补曲线
#include
#include
#include
#include
#include
#define SIZE 3000
float fun(float x);
float xchange(float x);
float ychange(float y);
void drawline(int color);
void drawchaline(int n, int color);
int chazhi(int *n, float deta);
const int left = 50;
const int right = 550;
const int top = 50;
const int bottom = 400;
float x[SIZE];
float x1[SIZE];
float fun(float x)
{
return x * x / 4;
}
int main()
{
int GraphDriver;
int GraphMode;
int i;
int n = 2;
float deta = 0.0001;
char buffer[10] = {'\0'};
char szfilename[255]= {"c:\\text1.txt"};
int ok;
FILE *fp;
GraphDriver = DETECT;
initgraph(&GraphDriver, &GraphMode, "");
x[0] = -5;
x[1] = 5;
setcolor(12);
outtextxy(10, 20, "Times:");
for (i = 0; i < 20; i++)
{
setcolor(0);
outtextxy(60, 20, buffer);
sprintf(buffer, "%d ", i + 1);
setcolor(12);
outtextxy(60, 20, buffer);
drawchaline(n, 0);
ok = chazhi(&n, deta);
drawchaline(n, 14);
drawline(12);
getch();
if (ok == 1)
break;
}
if ((fp = fopen(szfilename, "wt")) != NULL)
{
for (i = 0; i < n; i++)
{
fprintf(fp, "%f, %f\n", x[i], fun(x[i]));
}
fclose(fp);
}
closegraph();
return 0;
}
float xchange(float x)
{
const float fdiv = (-left + right) / 10.0f;
return (x + 5) * fdiv + left;
}
float ychange(float y)
{
const float fdiv = (top - bottom) / 6.25f;
return top + (y - 6.25) * fdiv;
}
void drawchaline(int n, int color)
{
int i;
setcolor(color);
moveto(xchange(x[0]), ychange(fun(-5)));
for (i = 1; i < n; i++)
{
lineto(xchange(x[i]), ychange(fun(x[i])));
}
}
/*min: x:-1.6,y:-17.8*/
void drawline(int color)
{
float x;
setcolor(color);
moveto(xchange(-5), ychange(fun(-5)));
for (x = -5; x deta)
{
return 0;
}
}
return 1;
}