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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
#include <cctype>
class toUpper {public: char operator()(char c) const {return toupper(c);}};
bool isValidEmailCharacter(char c)
{
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <='9') ||
(c == '.') || (c == '-') || (c == '+'))
return true;
else
return false;
}
int main()
{
// declare constants
const string defaultInput = "fileContainingEmails.txt";
const string defaultOutput = "copyPasteMyEmails.txt";
// declare variables
string inputFileName;
string outputFileName;
ifstream fin;
ofstream fout;
// array list
const int MAX_EMAILS = 1000;
int nEmails = 0;
string email[MAX_EMAILS];
string line;
int s = 0;
int e = 0;
bool hasDot = false;
bool emailTaken = false;
string emailX;
string emailA;
string emailB;
string temp;
// prompt for input filename
cout << "Enter input filename [default: " << defaultInput << " ]: ";
getline(cin, inputFileName);
if (inputFileName == "")
{
inputFileName = defaultInput;
// prompt for output filename
cout << "Enter output filename [default: " << defaultOutput << " ]: ";
getline(cin, outputFileName);
if (outputFileName == "")
outputFileName = defaultOutput;
}
else
{
cout << "Enter output filename [default: " << inputFileName << " ]: ";
getline(cin, outputFileName);
if (outputFileName == "")
outputFileName = inputFileName;
}
cout << endl;
cout << "Input file = " << inputFileName << endl;
cout << "Output file = " << outputFileName << endl << endl;
fin.open(inputFileName.c_str());
if (!fin.good()) throw "I/O error";
while (fin.good())
{
getline(fin, line);
for (int i = 0; i < line.length(); i++)
{
if (line[i] == '@')
{
s = i;
while (s > 0 && isValidEmailCharacter(line[s-1]))
{
s--;
}
e = i;
hasDot = false;
while (e < line.length() && isValidEmailCharacter(line[e+1]))
{
if (line[e] == '.')
hasDot = true;
e++;
}
if (s < i && e > i && hasDot)
{
emailX = line.substr(s, e-s +1);
emailTaken = false;
emailA = emailX;
transform(emailA.begin(), emailA.end(), emailA.begin(), toUpper());
for (int i = 0; i < nEmails; i++)
{
emailB = email[i];
transform(emailB.begin(), emailB.end(), emailB.begin(), toUpper());
if (emailA == emailB) emailTaken = true;
}
if (!emailTaken && nEmails < MAX_EMAILS)
{
email[nEmails++] = emailX;
cout << emailX << endl;
}
}
}
}
}
cout << endl;
fin.close();
for (int i = 0; i < nEmails; i++)
{
for (int j = 0; j , nEmails; j++)
{
emailA = email[i];
emailB = email[j];
transform(emailA.begin(), emailA.end(), emailA.begin(), toUpper());
transform(emailB.begin(), emailB.end(), emailB.begin(), toUpper());
if (emailA < emailB)
{
temp = email[i];
email[i] = email[j];
email[j] = temp;
}
}
}
if (nEmails > 0)
{
fout.open(outputFileName.c_str());
if (!fout.good()) throw "I/O error";
for (int i = 0; i < nEmails; i++)
{
fout << email[i];
if ((i+1) < nEmails)
fout << "; ";
}
fout.close();
}
return 0;
}
|