#include<iostream>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include"stack.h"
#include<windows.h>
#define MAXSIZE 64
using namespace std;
int main()
{
stack ma1,ma2;
char buffer[MAXSIZE];
char end[] = "end";
cout << "请输入自变量x的值" << endl;
while (cin >> buffer)
{
if (strcmp(end, buffer) == 0)
{
cout << "x输入结束" << endl;
break;
}
double num1 = atof(buffer);
ma1.push(num1);
memset(buffer, 0, MAXSIZE);
}
memset(buffer, 0, MAXSIZE);
cout << "请输入因变量y的值" << endl;
while (cin >> buffer)
{
if (strcmp(end, buffer) == 0)
{
cout << "y输入结束" << endl;
break;
}
double num1 = atof(buffer);
ma2.push(num1);
memset(buffer, 0, MAXSIZE);
}
if (ma1.size != ma2.size)
{
if (ma1.size > ma2.size)
{
cout << "x多输了或者y少输了" << endl;
}
if (ma1.size < ma2.size)
{
cout << "y多输了或者x少输了" << endl;
}
}
double avgx = ma1.sum()/ma1.size, avgy = ma2.sum()/ma2.size;
double xiyi = 0,xixi = 0;
for (int i = 1; i < ma1.size; i++)
{
xiyi += ma1.num[i] * ma2.num[i];
xixi += ma1.num[i] * ma1.num[i];
}
double k = (xiyi - ma1.size * avgx * avgy)/(xixi - ma1.size * avgx * avgx);
double b = avgy - k * avgx;
cout << "k = " << k << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}