#include<stdio.h>
main()
{FILE *infile;
int i, n;
double a, b, x, y, x_sum, y_sum, xy_sum, xx_sum;
if ((infile=fopen("input.dat","r"))==NULL) {
printf("can't open file \n");
exit;
}
n=0;
xy_sum=0;
x_sum=0;
y_sum=0;
xx_sum=0;
while( fscanf(infile, "%lf%lf\n", &x, &y) != EOF ) {
xy_sum=xy_sum+x*y; /* x*y の和 */
x_sum=x_sum+x; /* x の和 */
y_sum=y_sum+y; /* y の和 */
xx_sum=xx_sum+x*x; /* x*x の和 */
n=n+1; /* データの数 n */
}
a=(x_sum*y_sum-n*xy_sum) / (x_sum*x_sum-n*xx_sum);
b=(y_sum-a*x_sum)/n;
printf("y = ax + b\n");
printf("a = %lf\nb = %lf\n", a, b);
fclose(infile);
}