Day 22
#include <iostream>
#include<fstream>
#include <cmath>
using namespace std;
#define FILENAME "data.txt"
int main(void)
{
//Declare and initialize variables.
double unknown[5] = { 5.4, 7.2, 7.9, 7.4, 5.1 },
known[5], result[6];
//= { 6.2, 7.0, 8.0, 7.4, 5.8 };
double distance(double hand_1[5], double hand_2[5], double result[]);
//cout << "Please enter five known data" << endl;
//cin >> known[0] >> known[1] >> known[2] >> known[3] >> known[4];
ifstream data;
data.open(FILENAME);
int k = 0;
if (data.fail())
cout << "Error opening input file." << endl;
else
{
while (!data.eof())
{
data >> known[k];
k++;
}
// Compute and print distance.
cout << "Distance: " << distance(unknown, known, result) << endl;
for (int i = 0; i < 5; i++)
{
//cout.setf(ios::fixed);
cout.precision(2);
cout << fixed<<"The distence of " << i + 1 << " is " << result[i] << endl;
}
cout << defaultfloat << "Known " << result[5] << " has best metch" << endl;
}
system("pause");
// Exit program.
return 0;
}
//-----------------------------------------------------------------
// This function computes the distance between two hand measurements.
double distance(double hand_1[5], double hand_2[5], double result[])
{
// Declare variables.
int k;
double sum = 0,min=hand_1[0];
// Compute sum of absolute value differences.
for (k = 0; k <= 4; k++)
{
sum = sum + fabs(hand_1[k] - hand_2[k]);
result[k] = fabs(hand_1[k] - hand_2[k]);
if (result[k] < min)
{
result[5] = k+1;
min = result[k];
}
}
// Return distance value.
return sum;
}
#include<fstream>
#include <cmath>
using namespace std;
#define FILENAME "data.txt"
int main(void)
{
//Declare and initialize variables.
double unknown[5] = { 5.4, 7.2, 7.9, 7.4, 5.1 },
known[5], result[6];
//= { 6.2, 7.0, 8.0, 7.4, 5.8 };
double distance(double hand_1[5], double hand_2[5], double result[]);
//cout << "Please enter five known data" << endl;
//cin >> known[0] >> known[1] >> known[2] >> known[3] >> known[4];
ifstream data;
data.open(FILENAME);
int k = 0;
if (data.fail())
cout << "Error opening input file." << endl;
else
{
while (!data.eof())
{
data >> known[k];
k++;
}
// Compute and print distance.
cout << "Distance: " << distance(unknown, known, result) << endl;
for (int i = 0; i < 5; i++)
{
//cout.setf(ios::fixed);
cout.precision(2);
cout << fixed<<"The distence of " << i + 1 << " is " << result[i] << endl;
}
cout << defaultfloat << "Known " << result[5] << " has best metch" << endl;
}
system("pause");
// Exit program.
return 0;
}
//-----------------------------------------------------------------
// This function computes the distance between two hand measurements.
double distance(double hand_1[5], double hand_2[5], double result[])
{
// Declare variables.
int k;
double sum = 0,min=hand_1[0];
// Compute sum of absolute value differences.
for (k = 0; k <= 4; k++)
{
sum = sum + fabs(hand_1[k] - hand_2[k]);
result[k] = fabs(hand_1[k] - hand_2[k]);
if (result[k] < min)
{
result[5] = k+1;
min = result[k];
}
}
// Return distance value.
return sum;
}

Comments
Post a Comment