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
|
// ============================================================================
// algos.cpp
// ~~~~~~~~~~~~~~~~
// author: JOHN DOE
// - this is the ONLY file you can modify
// - feel free to include more headers if you need to
// ============================================================================
#include <iostream>
#include <sstream>
#include <stdexcept> // to throw exceptions if you need to
#include <fstream> // to open & read from input file
#include <cstdlib> // for atoi() if you want to use it
#include <set> // for sba algorithm
#include <vector> // for vba algorithm
#include <algorithm> // for vba algorithm
//#include "Lexer.h"
//#include "algos.h"
using namespace std; // BAD PRACTICE
void printVector(vector<pair<int, int> > & myVec)
{
vector<pair<int, int> >::iterator i;
for (i = myVec.begin(); i != myVec.end(); ++i) {
cout << "(" << i->first << ", " << i->second << ") ";
}
cout << endl;
}
int vba(string filename)
{
ifstream ifs;
ifs.open(filename.c_str());
string line;
vector<pair<int, int> > pairVector;
while (getline(ifs, line))
if (line.substr(0,4)=="exit") {
exit(1);
}
if (line=="#") {
cout << endl;
}
else
{
istringstream iss;
int a;
int b;
iss >> a;
iss >> b;
pair<int, int> p;
p = make_pair(a, b);
pairVector.push_back(p);
cout << "# of inserted pairs = " << pairVector.size() << endl;
printVector(pairVector);
sort(pairVector.begin(), pairVector.end());
printVector(pairVector);
vector<pair<int, int> >::iterator i = pairVector.begin();
while (i != pairVector.end())
{
vector<pair<int, int> >::iterator j = i+1;
if (j != pairVector.end() && *j == *i)
{
i = pairVector.erase(i);
}
}
printVector(pairVector);
return pairVector.size();
}
return 0;
}
int sba(string filename)
{
int x;
int y;
pair<int, int> p1;
istringstream is;
set<pair<int, int> > set;
ifstream myfile;
myfile.open(filename.c_str());
if (myfile.fail()) {
cerr << "ERROR: Failed to open file " << filename << endl;
myfile.clear();
} else {
if (myfile.is_open()) {
string line;
while (getline(myfile,line) )
{
if (line[0] != '#') {
istringstream is(line);
is >> x;
is >> y;
if ( y > x) {
p1 = make_pair(x, y);
} else {
p1 = make_pair(y,x);
}
// edgeSet.insert(p1);
}
}
// cout << edgeSet.size() << '\n';
myfile.close();
}
}
return 1;
}
int main()
{
}
|