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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include <iostream>
#include <fstream>
using namespace std;
class Point
{
public:
double x,y;
};
class Line
{
public:
double m, b;
};
class Linear_regress
{
private:
//const Point* fit_pts;
Point* fit_pts;
Line fit_line;
int num_fit_pts;
public:
//void perform_regression(const Point*, int, int);
void perform_regression(Point*, int, int);
};
class Traffic
{
private:
int max_pts;
cout<<"Enter the number of accident points: "<<endl;
cin>>max_pts;
enum MAX_NUM_ACCID_PTS(max_pts);
Point accid_pts[MAX_NUM_ACCID_PTS];
int index_at_max_accid, num_accid_pts;
//const Linear_regress& regression_reference;
Linear_regress& regression_reference;
public:
//Traffic(const Linear_regress&);
Traffic(Linear_regress&);
void read_data();
void find_index_at_max_accid();
void calc_lines();
};
//void Linear_regress::perform_regression(const Point* array_beginning,
void Linear_regress::perform_regression(Point* array_beginning,
int low_subscript, int high_subscript)
{
double c, d, e, f, n;
int i;
c=d=e=f=0.0;
num_fit_pts=high_subscript-low_subscript+1;
fit_pts=array_beginning+low_subscript;
for (i=0; i<num_fit_pts; i++)
{
c += fit_pts[i].x;
d += fit_pts[i].y;
e += fit_pts[i].x*fit_pts[i].x;
f += fit_pts[i].x*fit_pts[i].y;
}
n = num_fit_pts;
fit_line.m = (n*f - c*d)/(n*e - c*c);
fit_line.b = (d*e - c*f)/(n*e - c*c);
cout<<"The best fit line:"<<endl<<" m="<<fit_line.m<<endl<<
" b="<<fit_line.b<<endl<<endl;
}
//Traffic::Traffic(const Linear_regress& regress_ref_var)
Traffic::Traffic(Linear_regress& regress_ref_var)
: regression_reference (regress_ref_var),
index_at_max_accid(0), num_accid_pts(0)
{
int i;
for (i=0; i<MAX_NUM_ACCID_PTS; i++)
{
accid_pts[i].x=0;
accid_pts[i].y=0;
}
}
void Traffic::read_data()
{
int i;
ifstream infile("D:\\School\\CIS326\\Week 8\\BESTLINE.DAT");
i=0;
do
{
infile>>accid_pts[i].x;
infile>>accid_pts[i].y;
i++;
} while (!infile.eof());
num_accid_pts = i-1;
}
void Traffic::find_index_at_max_accid()
{
int i;
double max;
max = accid_pts[0].y;
for (i=1; i<num_accid_pts; i++)
{
if (accid_pts[i].y >= max)
{
max = accid_pts[i].y;
index_at_max_accid = i;
}
}
}
void Traffic::calc_lines()
{
cout<<"Line before maximum accidents."<<endl;
regression_reference.perform_regression(accid_pts, 0, index_at_max_accid);
cout<<"Line after maximum accidents."<<endl;
regression_reference.perform_regression(accid_pts, index_at_max_accid, num_accid_pts-1);
}
int main ( )
{
Linear_regress regression_object;
Traffic monthly_traffic(regression_object);
monthly_traffic.read_data();
monthly_traffic.find_index_at_max_accid();
monthly_traffic.calc_lines();
system("pause");
}
|