数组数据丢失
#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,x2,x3;
double y1,y2,y3;
/*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);
getchar();
scanf("%lf",&p1);
}
for(i=0;i<num;i++)
{
printf("(%.1lf,%.1lf) ",p,p1);
}*/
double x={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={
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,y,x,x);
printf("------------------------------------------------------------\n");
}
if(i==0)
{
k1 = (y-y)/(x-x);
x1 = x;
y1 = y;
count1++;
}
else
{
// printf("i=%d\n",i);
// printf("%.1lf = (%.1lf-%.1lf)/(%.1lf-%.1lf)\n",k,y,y,x,x);
// printf("------------------------------------------------------------\n");
k = (y-y)/(x-x);
if(k2 == 0)
{
if(k >= 2*k1)
{
k2 = k;
x2 = x;
y2 = y;
count2++;
x1 = x;
y1 = y;
count1++;
}
else
{
x1 = x;
y1 = y;
count1++;
}
}
else
{
if(k3 == 0)
{
if(k<0)
{
k3 = k;
x3 = x;
y3 = y;
count3++;
x2 = x;
y2 = y;
count2++;
}
else if((k <= 0.5*k2)&&(k>0))
{
k3 = k;
x3 = x;
y3 = y;
count3++;
}
else
{
x2 = x;
y2 = y;
count2++;
}
}
else
{
x3 = x;
y3 = y;
count3++;
}
}
}
}
// free(p);
// free(p1);
/*double x={0.0,1.0,2.0,3.0,4.0,5.0};
double y={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,y1);
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,y2);
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,y3);
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={0.0,1.0,2.0,3.0,4.0,5.0};
// float y={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*y+mxy;
xx=1.0*x+xx;
yy=1.0*y+yy;
x2=1.0*x*x*6.0+x2;
x22=1.0*x+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 这个数对为什么丢失了吗,试了一下午,头都炸了{:10_266:} #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, x2, x3;
//double y1, y2, y3;
//double x = { 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 = (double)(z + 1);
for (int z = 1; z < 11; z++)
y = (double)z * 0.5;
for (int z = 10; z < 19; z++)
y = y + 3;
for (int z = 19; z < 31; z++)
y = y - 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, y, x, x);
printf("------------------------------------------------------------\n");
}
if (i == 0)
{
k1 = (y - y) / (x - x);
x1 = x;
y1 = y;
count1++;
}
else
{
// printf("i=%d\n",i);
// printf("%.1lf = (%.1lf-%.1lf)/(%.1lf-%.1lf)\n",k,y,y,x,x);
// printf("------------------------------------------------------------\n");
k = (y - y) / (x - x);
if (k2 == 0)
{
if (k >= 2 * k1)
{
k2 = k;
x2 = x;
y2 = y;
count2++;
x1 = x;
y1 = y;
count1++;
}
else
{
x1 = x;
y1 = y;
count1++;
}
}
else
{
if (k3 == 0)
{
if (k < 0)
{
k3 = k;
x3 = x;
y3 = y;
count3++;
x2 = x;
y2 = y;
count2++;
}
else if ((k <= 0.5 * k2) && (k > 0))
{
k3 = k;
x3 = x;
y3 = y;
count3++;
}
else
{
x2 = x;
y2 = y;
count2++;
}
}
else
{
x3 = x;
y3 = y;
count3++;
}
}
}
}
// free(p);
// free(p1);
/*double x={0.0,1.0,2.0,3.0,4.0,5.0};
double y={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, y1);
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, y2);
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, y3);
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={0.0,1.0,2.0,3.0,4.0,5.0};
// float y={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 * y + mxy;
xx = 1.0 * x + xx;
yy = 1.0 * y + yy;
x2 = 1.0 * x * x * 6.0 + x2;
x22 = 1.0 * x + 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;
}
页:
[1]