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
|
/*****************************
* Made by Me *
* Assignment 6 *
* ece270 *
*****************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Node
{
char name[20];
int ID;
int dist;
};
static const Node default_node = {"Mr. Nobody", 666, -1};
void READIN(Node[],string);
void READOUT(Node[],int);
void MAXDIST(Node[],int);
void BSORT(Node[],int);
int main()
{
string in_file = "a6.txt";
Node NA[10];
READIN(NA,in_file);
READOUT(NA,10);
MAXDIST(NA,10);
BSORT(NA,10);
return 0;
}
void READIN(Node NA[], string in_file)
{
ifstream in_data;
int i = 0;
in_data.open(in_file.c_str());
while (!in_data.eof())
{
in_data >> NA[i].name;
in_data >> NA[i].ID;
in_data >> NA[i].dist;
i++;
}
in_data.close();
}
void READOUT(Node NA[], int count)
{
for(int i=0; i<count; i++)
{
cout << NA[i].name << "\n";
cout << NA[i].ID << "\n";
cout << NA[i].dist << "\n" << "\n";
}
}
void MAXDIST(Node NA[], int count)
{
int maxNodeNum;
int maxDist = 0;
for(int i=0; i<count; i++)
{
if (NA[i].dist > maxDist)
{
maxNodeNum = i;
maxDist = NA[i].dist;
}
}
cout << NA[maxNodeNum].name << "\n";
cout << NA[maxNodeNum].ID << "\n";
cout << NA[maxNodeNum].dist << "\n" << "\n";
}
void BSORT(int NA[],int count)
{
for(int i=count; i>0; i--)
{
for(int j=0; j<i-1; j++)
{
if ( NA[j] > NA[j+1] )
{
Node temp = default_node;
temp = NA[j+1];
NA[j+1] = NA[j];
NA[j] = temp;
}
}
}
READOUT(NA,count);
}
| |