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
|
#include "Lab9.h"
#include "basic.h"
void readData(string inputfile, string outputfile)
{
ifstream in(inputfile); //set in to inputfile
ofstream out(outputfile); //set out to outputfile
string datafile;
string _datafile2;
string filenametext;
string line;
string command;
stringstream datafile2;
ifstream anotherin(_datafile2);
struct basicStruct basic[SIZE], search;
int count = 0;
getline(anotherin, line);
while (!in.fail() && count < SIZE)
{
search = readFeature(line);
if (count < SIZE)
{
basic[count] = search;
count++;
}
getline(anotherin, line);
}
anotherin.close();
getline(in, filenametext, ' ');
getline(in, datafile, '\n');
getline(in, command, ' ');
datafile2 << "\"" << datafile << "\"";
_datafile2 = datafile2.str();
while (!in.fail())
{
if (command == "find-name")
{
findname(basic, count, in, out);
}
else if (command == "find-number")
{
findID(basic, count, in, out);
}
in >> command;
}
in.close();
}
basicStruct readFeature(string line)
{
basicStruct search;
istringstream in(line);
getline(in, search.name, ' ');
in >> search.number;
return search;
}
void findname(basicStruct myFeatures[], int count, ifstream &in, ofstream &out)
{
bool found = false;
string nameToFind;
getline(in, nameToFind, '\n');
for (int i = 0; i < count; i++)
{
if (myFeatures[i].name.find(nameToFind) != string::npos)
{
found = true;
out << "Found: " << myFeatures[i].name << " " << myFeatures[i].number << endl;
}
}
if (!found)
{
out << "Not found: " << nameToFind << endl;
}
}
void findID(basicStruct myFeatures[], int count, ifstream &in, ofstream &out)
{
bool found = false;
double idToFind;
in >> idToFind;
for (int i = 0; i < count; i++)
{
if (myFeatures[i].number == idToFind)
{
found = true;
out << "Found: " << myFeatures[i].name << " " << myFeatures[i].number << endl;
}
}
if (!found)
{
out << "Not found: " << idToFind << endl;
}
}
|