Скачиваний:
18
Добавлен:
01.05.2014
Размер:
2.23 Кб
Скачать
// Here we are returning to Borland C++ v.3.0!!
// The concept is to calculate the Holstead metrics for this bullshit..

// The following code really contains some bullshit... Yet, I am not
// expected to optimize it anyhow in the first lab...

#include <Math.h>
#include <IOStream.h>
#include <Conio.h>

const pi = 3.14159;
const max = 20;

float seed;
int first, done;

float random(float dummy) {
  float x;  int i;
  x = seed + pi;
  x = exp(5.0 * log(x));
  seed = x - int(x);
  return seed;
}

void get_data(float *x, float *y, int &n) {
   const a = 5.0;
   const b = 2.0;
   int i,j;
   float fudge;

  cout << "\nFudge? (<0 to terminate): ";  cin >> fudge;
  // Fudge is a synonym to bullshit. Negative fudge breaks cycle!
  if (fudge < 0.0) {
     done = 1;
  } else {
     // Reading correct number of points
     do {
	cout << "How many points? "; cin >> n;
     } while (!(n>2) || !(n<=max));
     // Mystical line for me now...
     if (first) first = 0; else clrscr();
     // This, I expect, forms linear sequence with random deviation
     for (i=1; i<=n; i++) {
	  j = n + 1 - i;
	  x[i] = j;
	  y[i] = (a*j + b) * (1.0 + (2.0 * random(0) - 1.0) * fudge);
     }
  }
}


void write_data(float *x, float *y, int n) {
  int i;
  for (i = 1; i <= n; i++)
     cout << "X[" << i << "]=" << x[i] << "\t\tY[" << i << "]=" << y[i] << "\n";
}


void main() {
   float x[max];
   float y[max];
   int n;
   float sumx,sumy;
   float sumxy, sumx2;
   int k;
   float resa,resb;

  clrscr();
  seed = 4.0;
  first = 1;
  done  = 0;
  do {
    get_data(x,y,n);
    if (!done) {
	write_data(x,y,n);
	//----------------------------------------- Calculative part starts
	// Accumulating statistics
	sumx = 0; sumy = 0; sumxy = 0; sumx2 = 0;
	for (k=1; k<=n; k++) {
	  sumx  += x[k];
	  sumy  += y[k];
	  sumxy += x[k] * y[k];
	  sumx2 += x[k] * x[k];
	}
	// Recieving verdict
	resa = (sumxy  -  sumx*sumy/n) / (sumx2  -  sumx*sumx/n);
	resb = (sumy  -  resa * sumx) / n;
	cout << "Approximated with y=ax+b where a = " << resa << ", b=" << resb;
	//------------------------------------------- Calculative part ends
    }
  } while (!done);
}
Соседние файлы в папке Sido Lab1