|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
void linearRegressionEquation(double x[],double y[],int n);
float my_fabs02(float fnumber);
int main()
{
int num,i;
int count1=0 ,count2=0 ,count3=0 ;
double k=0,k1=0,k2=0,k3=0;
double x1[50],x2[50],x3[50];
double y1[50],y2[50],y3[50];
/*double *p=0, *p1=0;
double currentTemperature;
printf("输入当前室温:");
scanf("%d",¤tTemperature);
printf("输入数组元素个数:");
scanf("%d",&num);
p = (double *)malloc(sizeof(double)*num);
p1 = (double *)malloc(sizeof(double)*num);
for(i=0;i<num;i++)
{
printf("第%d个数据的横纵坐标:",i+1);
scanf("%lf",&p[i]);
getchar();
scanf("%lf",&p1[i]);
}
for(i=0;i<num;i++)
{
printf("(%.1lf,%.1lf) ",p[i],p1[i]);
}*/
double x[50]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
double y[50]={
0.5 ,1 ,1.5 ,2 ,2.5 ,3 ,3.5 ,4 ,4.5 ,5,
8 ,11 ,14 ,17 ,20 ,23 ,26 ,29 ,32 ,31 ,
30 ,29 ,28 ,27 ,26 ,25 ,24 ,23 ,22 ,21};
num = 30;
for (i=0;i<num;i++)
{
if(i==19)
{
printf("i=%d\n",i);
printf("%.1lf = (%.1lf-%.1lf)/(%.1lf-%.1lf)\n",k,y[i+1],y[i],x[i+1],x[i]);
printf("------------------------------------------------------------\n");
}
if(i==0)
{
k1 = (y[1]-y[0])/(x[1]-x[0]);
x1[0] = x[0];
y1[0] = y[0];
count1++;
}
else
{
// printf("i=%d\n",i);
// printf("%.1lf = (%.1lf-%.1lf)/(%.1lf-%.1lf)\n",k,y[i+1],y[i],x[i+1],x[i]);
// printf("------------------------------------------------------------\n");
k = (y[i+1]-y[i])/(x[i+1]-x[i]);
if(k2 == 0)
{
if(k >= 2*k1)
{
k2 = k;
x2[i-count1] = x[i];
y2[i-count1] = y[i];
count2++;
x1[i] = x[i];
y1[i] = y[i];
count1++;
}
else
{
x1[i] = x[i];
y1[i] = y[i];
count1++;
}
}
else
{
if(k3 == 0)
{
if(k<0)
{
k3 = k;
x3[i-count1-count2] = x[i];
y3[i-count1-count2] = y[i];
count3++;
x2[i-count1] = x[i];
y2[i-count1] = y[i];
count2++;
}
else if((k <= 0.5*k2)&&(k>0))
{
k3 = k;
x3[i-count1-count2] = x[i];
y3[i-count1-count2] = y[i];
count3++;
}
else
{
x2[i-count1] = x[i];
y2[i-count1] = y[i];
count2++;
}
}
else
{
x3[i-count1-count2] = x[i];
y3[i-count1-count2] = y[i];
count3++;
}
}
}
}
// free(p);
// free(p1);
/*double x[6]={0.0,1.0,2.0,3.0,4.0,5.0};
double y[6]={1.0,1.0,1.0,1.0,1.0,1.0};
linearRegressionEquation(x,y,6);*/
for(i=0;i<count1;i++)
{
printf("(%.1lf,%.1lf) ",x1[i],y1[i]);
if(i==4)
{
printf("\n");
}
else if(i==9)
{
printf("\n");
}
else if(i==count1-1)
{
printf("\n");
}
}
printf("--------------------------------------------------------------------------\n");
for(i=0;i<count2;i++)
{
printf("(%.1lf,%.1lf) ",x2[i],y2[i]);
if(i==4)
{
printf("\n");
}
else if(i==9)
{
printf("\n");
}
else if(i==count2-1)
{
printf("\n");
}
}
printf("------------------------------------------------------------------------------\n");
for(i=0;i<count3;i++)
{
printf("(%.1lf,%.1lf) ",x3[i],y3[i]);
if(i==4)
{
printf("\n");
}
else if(i==9)
{
printf("\n");
}
else if(i==count3-1)
{
printf("\n");
}
}
return 0;
}
void linearRegressionEquation(double x[],double y[],int n)
{
// float x[6]={0.0,1.0,2.0,3.0,4.0,5.0};
// float y[6]={1.0,1.0,1.0,1.0,1.0,1.0};
double a,b,mxy,xx,yy,x2,x22;
int i;
a=b=mxy=xx=yy=x2=x22=0.0;
for(i=0;i<n;i++)
{
mxy=6.0*x[i]*y[i]+mxy;
xx=1.0*x[i]+xx;
yy=1.0*y[i]+yy;
x2=1.0*x[i]*x[i]*6.0+x2;
x22=1.0*x[i]+x22;
}
b=1.0*(mxy-xx*yy)/(x2-x22*x22);
a=1.0*yy/6.0-b*xx/6.0;
printf("Y=%0.2fx+%0.2f\n",b,a);
system("pause");
}
float my_fabs02(float fnumber)
{
*( (int *) &fnumber) &=0x7FFFFFFF;
return fnumber;
}
有大佬能告诉我20,31 这个数对为什么丢失了吗,试了一下午,头都炸了
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void linearRegressionEquation(double x[], double y[], int n);
float my_fabs02(float fnumber);
int main()
{
int num, i;
int count1 = 0, count2 = 0, count3 = 0;
double k = 0.0, k1 = 0.0, k2 = 0.0, k3 = 0.0;
//double x1[50], x2[50], x3[50];
//double y1[50], y2[50], y3[50];
//double x[50] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30 };
/*y = {
0.5 ,1 ,1.5 ,2 ,2.5 ,3 ,3.5 ,4 ,4.5 ,5,
8 ,11 ,14 ,17 ,20 ,23 ,26 ,29 ,32 ,31 ,
30 ,29 ,28 ,27 ,26 ,25 ,24 ,23 ,22 ,21 };*/
//下面是我改的 -- 请善用堆内存申请
/*************************************************/
double* x1 = (double*)malloc(sizeof(double) * 50);
double* x2 = (double*)malloc(sizeof(double) * 50);
double* x3 = (double*)malloc(sizeof(double) * 50);
double* y1 = (double*)malloc(sizeof(double) * 50);
double* y2 = (double*)malloc(sizeof(double) * 50);
double* y3 = (double*)malloc(sizeof(double) * 50);
double* x = (double*)malloc(sizeof(double) * 50);
double* y = (double*)malloc(sizeof(double) * 50);
if (!x || !x1 || !x2 || !x3 || !y || !y1 || !y2 || !y3)
{
printf_s("申请空间失败!\n");
return -1;
}
memset(x, 0, 50 * sizeof(double));
memset(x1, 0, 50 * sizeof(double));
memset(x2, 0, 50 * sizeof(double));
memset(x3, 0, 50 * sizeof(double));
memset(y, 0, 50 * sizeof(double));
memset(y1, 0, 50 * sizeof(double));
memset(y2, 0, 50 * sizeof(double));
memset(y3, 0, 50 * sizeof(double));
for (int z = 0; z < 30; z++)
x[z] = (double)(z + 1);
for (int z = 1; z < 11; z++)
y[z - 1] = (double)z * 0.5;
for (int z = 10; z < 19; z++)
y[z] = y[z - 1] + 3;
for (int z = 19; z < 31; z++)
y[z] = y[z - 1] - 1;
/*************************************************/
num = 30;
for (i = 0; i < num; i++)
{
if (i == 19)
{
printf("i=%d\n", i);
printf("%.1lf = (%.1lf-%.1lf)/(%.1lf-%.1lf)\n", k, y[i + 1], y[i], x[i + 1], x[i]);
printf("------------------------------------------------------------\n");
}
if (i == 0)
{
k1 = (y[1] - y[0]) / (x[1] - x[0]);
x1[0] = x[0];
y1[0] = y[0];
count1++;
}
else
{
// printf("i=%d\n",i);
// printf("%.1lf = (%.1lf-%.1lf)/(%.1lf-%.1lf)\n",k,y[i+1],y[i],x[i+1],x[i]);
// printf("------------------------------------------------------------\n");
k = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
if (k2 == 0)
{
if (k >= 2 * k1)
{
k2 = k;
x2[i - count1] = x[i];
y2[i - count1] = y[i];
count2++;
x1[i] = x[i];
y1[i] = y[i];
count1++;
}
else
{
x1[i] = x[i];
y1[i] = y[i];
count1++;
}
}
else
{
if (k3 == 0)
{
if (k < 0)
{
k3 = k;
x3[i - count1 - count2] = x[i];
y3[i - count1 - count2] = y[i];
count3++;
x2[i - count1] = x[i];
y2[i - count1] = y[i];
count2++;
}
else if ((k <= 0.5 * k2) && (k > 0))
{
k3 = k;
x3[i - count1 - count2] = x[i];
y3[i - count1 - count2] = y[i];
count3++;
}
else
{
x2[i - count1] = x[i];
y2[i - count1] = y[i];
count2++;
}
}
else
{
x3[i - count1 - count2] = x[i];
y3[i - count1 - count2] = y[i];
count3++;
}
}
}
}
// free(p);
// free(p1);
/*double x[6]={0.0,1.0,2.0,3.0,4.0,5.0};
double y[6]={1.0,1.0,1.0,1.0,1.0,1.0};
linearRegressionEquation(x,y,6);*/
for (i = 0; i < count1; i++)
{
printf("(%.1lf,%.1lf) ", x1[i], y1[i]);
if (i == 4)
{
printf("\n");
}
else if (i == 9)
{
printf("\n");
}
else if (i == count1 - 1)
{
printf("\n");
}
}
printf("--------------------------------------------------------------------------\n");
for (i = 0; i < count2; i++)
{
printf("(%.1lf,%.1lf) ", x2[i], y2[i]);
if (i == 4)
{
printf("\n");
}
else if (i == 9)
{
printf("\n");
}
else if (i == count2 - 1)
{
printf("\n");
}
}
printf("------------------------------------------------------------------------------\n");
for (i = 0; i < count3; i++)
{
printf("(%.1lf,%.1lf) ", x3[i], y3[i]);
if (i == 4)
{
printf("\n");
}
else if (i == 9)
{
printf("\n");
}
else if (i == count3 - 1)
{
printf("\n");
}
}
return 0;
}
void linearRegressionEquation(double x[], double y[], int n)
{
// float x[6]={0.0,1.0,2.0,3.0,4.0,5.0};
// float y[6]={1.0,1.0,1.0,1.0,1.0,1.0};
double a, b, mxy, xx, yy, x2, x22;
int i;
a = b = mxy = xx = yy = x2 = x22 = 0.0;
for (i = 0; i < n; i++)
{
mxy = 6.0 * x[i] * y[i] + mxy;
xx = 1.0 * x[i] + xx;
yy = 1.0 * y[i] + yy;
x2 = 1.0 * x[i] * x[i] * 6.0 + x2;
x22 = 1.0 * x[i] + x22;
}
b = 1.0 * (mxy - xx * yy) / (x2 - x22 * x22);
a = 1.0 * yy / 6.0 - b * xx / 6.0;
printf("Y=%0.2fx+%0.2f\n", b, a);
system("pause");
}
float my_fabs02(float fnumber)
{
*((int*)&fnumber) &= 0x7FFFFFFF;
return fnumber;
}
|
|