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
|
// File: StormChaser.cpp
// Programmer:
// Class: COP 2931
// Date:
// Description: This program will read hurricane data from a file and produce
// a series of reports from it.
// Header Files...
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstring>
#include "Storm.h"
using namespace std;
// Constants...
#define MAX_STORMS 200
// Function Prototypes...
Storm* GetStorm(ifstream& input);
void Sort( Storm* List[], int CurStorm );
void DisplayStorms( char *title, Storm List[200], int CurStorm );
int main() {
// Declarations...
Storm **cPoint; // Pointer to an object.
cPoint = new Storm *[200];
Storm *List[200]; // array of pointers to Storm
static int CurStorm = 0; // Number of Storms in array.
int Total = 0; // Number of Storms in input file.
// Temporary Storage Declarations...
int year;
int month;
int day;
int hour;
int category;
std::string name;
double useless;
int wind;
int pressure;
// Opening Data File.
cout << "Opening data file." << endl;
ifstream inf("hurricane.data");
if(inf)
{
cout << "File opened successfully..." << endl;
cout << "Reading file..." << endl << endl;
}
else
{
cout << "File open failure." << endl;
return 0;
}
// Data file opened.
// Looping GetStorm until there's no more data.
//while(!inf.eof())
while(CurStorm < 200)
{
cPoint[CurStorm] = GetStorm(inf);
Total++;
category = cPoint[CurStorm]->getCategory();
// Saving the Storm if category is > 2.
if (category > 2)
{
List[CurStorm] = cPoint[CurStorm];
CurStorm++;
}
}
cout << "Number of storms: " << Total << endl;
cout << "Hurricanes with category 3 and above: " << CurStorm << endl;
DisplayStorms("First Ten Storms", List[200], 10);
/*Sort( List, NStorms );
DisplayStorms( "Top Ten Storms", List, 10 );
input.close(); */
system("pause");
return 0;
}
Storm* GetStorm(ifstream& inf) {
int year, month, day, hour, duration, wind, pressure;
std::string name;
double useless;
int category, date, StrLength;
char charname[15];
// 1. Reading the file.
inf >> year >> month >> day >> hour >> duration >> name >> useless >> useless >> wind >> pressure;
// Converting date to correct format.
date = (year * 100000) + (month * 100) + (day);
// Converting name to char array.
StrLength = name.size();
for (int i = 0; i < StrLength; i++)
{ charname[i] = name[i]; }
// Creating Storm Object.
Storm *cMember;
cMember = new Storm(date, duration, charname, wind, pressure);
return cMember;
}
void DisplayStorms(char* title, Storm List[], int NStorms )
{
// Display NStorms number of Storms
// Print the title and column headings; make a loop
// to print out each Storm.
cout << title << endl << endl;
cout << "Begin Date Duration Name Category Maximum Minimum" << endl;
cout << " (hours) Winds (mph) Press. (mb)" << endl;
cout << "----------------------------------------------------------------" << endl;
// Print out the first ten Storms in the list
for (int i = 0; i < 10; i++)
{
List[i].print();
}
cout << endl << endl;
return;
}
void Sort( Storm* StormList[], int N )
{
// Selection sort the list of Storm pointers
int swp_index = 0; //tracks smallest found value
Storm* temp;
for (int a = 0; a < (N - 1); a++)
{
swp_index = a;
for(int index = a + 1; index < N; index++)
{
if (StormList[index]->getCategory() < StormList[swp_index]->getCategory() )
{
swp_index = index;
}
}
temp = StormList[a];
StormList[a] = StormList[swp_index];
StormList[swp_index] = temp;
}
return;
}
|