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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
zeta4321
Date
Program Name
Objectives:
1. To determine if codes from an input file are valid or invalid.
Steps:
1. The codes are first read in and sorted from an array of strings using a swap function
2. The codes are then checked for validity given the following conditions:
a. digit/digit/letter/letter/letter/digit/digit (length=7)
b. digit/digit/digit/letter/letter/letter/digit/digit/digit (length=6)
c. uppercaseletter/lowercaseletter/lowercaseletter/digit/digit/digit (length=9)
3. If the codes are valid, they are output to a file called "valid.txt"
4. If the codes are invalid, they are output to a file called "errors.txt"
5. If duplicates exist, they are simply skipped over and not output to any file.
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
void swap(string A[], int i, int j);
void sort_the_codes(string A[], int size);
bool validate (string A[], int i, ofstream& outdata, ofstream& outdata2);
bool validate_length_nine (string A[], int i, ofstream& outdata, ofstream& outdata2);
bool validate_length_seven (string A[], int i, ofstream& outdata, ofstream& outdata2);
bool validate_length_six (string A[], int i, ofstream& outdata, ofstream& outdata2);
int main()
{
string code[1000];
ifstream indata;
indata.open("codes.txt");
ofstream outdata;
outdata.open("valid.txt");
ofstream outdata2;
outdata2.open("errors.txt");
int i=0;
indata >> code[i];
while(!indata.eof())
{
//if validate_length_nine (string code[], int size, ofstream& outdata, ofstream& outdata2);
//outdata << code[i] << endl;
//else outdata2 << code[i] <<endl;
i++;
indata >> code[i];
}
sort_the_codes(code,i);
validate (code, i, outdata, outdata2);
validate_length_nine (code, i, outdata, outdata2);
validate_length_seven (code, i, outdata, outdata2);
validate_length_six (code, i, outdata, outdata2);
/*
for(int j=0; j < i; j++)
{
cout << code[j] << endl;
}
*/
//Used for validation purposes
indata.close();
outdata.close();
outdata2.close();
return 0;
}
void swap(string A[], int i, int j)
{
string temp;
temp=A[i];
A[i]=A[j];
A[j]=temp;
return;
// code that defines the terms of the swap
}
void sort_the_codes(string A[], int size)
{
for (int x=1; x<size; x++) //controlling passes
//prime loop at 0
{
for (int j=0; j<size-x; j++) //controlling comparisons within each pass
{
if (A[j]>A[j+1]) swap (A, j, j+1);
}
}
// sorts the codes within the array
return;
}
bool validate_length_nine (string code[], int size, ofstream& outdata, ofstream& outdata2);
{
for (int x=0; x<size; x++)
{
if (code[x].length()==9) //When the code length equals nine, the function checks the conditions necessary for a nine character code to be valid
{
if ((isdigit(code[x].at(0)) > 0) && (isdigit(code[x].at(1)) > 0)
&& (isdigit(code[x].at(2)) > 0) && (isalpha(code[x].at(3)) > 0)
&& (isalpha(code[x].at(4)) > 0) && (isalpha(code[x].at(5)) > 0)
&& (isdigit(code[x].at(6)) > 0) && (isdigit(code[x].at(7)) > 0)
&& (isdigit(code[x].at(8)) > 0));
{
outdata << code[x]<<endl;//If the code meets the stated conditions, it is output to "valid.txt"
}
else outdata2<<code[x]<<endl; //If the code is length nine but it does not meet the stated conditions, the code will then be sent to the "errors.txt" file
}
}
}
bool validate_length_seven (string code[], int size, ofstream& outdata, ofstream& outdata2);
{
for (int x=0; x<size; x++)
{
if (code[x].length()==7)
{
if ((isdigit(code[x].at(0)) > 0) && (isdigit(code[x].at(1)) > 0)
&& (isalpha(code[x].at(2)) > 0) && (isalpha(code[x].at(3)) > 0)
&& (isalpha(code[x].at(4)) > 0) && (isdigit(code[x].at(5)) > 0)
&& (isdigit(code[x].at(6)) > 0))
{
outdata << code[x] << endl;
}
else outdata2<<code[x] << endl;
}
}
}
bool validate_length_six (string code[], int size, ofstream& outdata, ofstream& outdata2);
{
for (int x=0; x<size; x++)
{
if (code[x].length()==6)
{
if ((isupper(code[x].at(0)) > 0) && (islower(code[x].at(1)) > 0)
&& (islower(code[x].at(2)) > 0) && (isdigit(code[x].at(3)) > 0)
&& (isdigit(code[x].at(4)) > 0) && (isdigit(code[x].at(5)) > 0))
{
outdata << code[x] << endl;
}
else outdata2 << code[x] << endl;
}
}
}
bool validate_overall_length (string code[], int size, ofstream& outdata, ofstream& outdata2)
{
//declare any variables that are local to function
for (int x=0; x<size; x++)
{
if (code[x+1]!=code[x]) //Determines if the codes are duplicates
{
{
if (code[x].length()==9)
validate_length_nine (code, size, outdata, outdata2);
}
{
if (code[x].length()==7)
{
validate_length_seven (code, size, outdata, outdata2);
}
}
{
if (code[x].length()==6)
validate_length_six (code, size, outdata, outdata2);
}
if (code[x].length()!=9 || code[x].length()!=7 || code[x].length()!=6)
outdata2<<code[x]<<endl;
}
else if (code[x+1]==code[x])
{
outdata<<endl;
}
}
}
|