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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
//Constant Declarations
const int W_WIDTH = 80; ///< Width of the terminal window
//Forward Declarations - Function Prototypes
///
/// Utility Function to read a value from an input stream ( any type with a operator>> defined ) - non-interactive
///
template<class T>
bool readValue( istream &in_stream, T &value_in);
///
/// Utility Function to read a value from an input stream - interactive
///
template<class T>
bool readValue( istream &in_stream, T &value_in, string message, int width=34 );
///
/// Print a horizontal line
///
void printHorizontalLine( ostream& outs, char line_char, int width );
///
/// Print a centered heading
///
void printHeading( string title, int width );
///
/// Print the stylized program/project heading
///
void printProjectHeading( ostream& outs, int width );
///
/// Apply the caesar cipher
///
char caesarShift( char to_shift, int shift_value);
///
/// Print the encode and decode selection menu
///
void printMenu();
///
/// Read the shift value from the keyboard
///
int readShiftValue();
///
/// initiaize array to the contents of the line of input
///
void initializeArray(string str, char list[], int listSize);
///
/// Main Function
///
int main(){
fstream in_file; ///< Input File Stream
fstream out_file; ///< Output File Stream
string input_filename; ///< Input File Name
string output_filename; ///< Output File Name
string line; ///< String to store each line
int shift_value; ///< Shift Value for caesar Cipher
char choice = '\0'; ///< User menu selection
char current_char = '\0'; ///< Current character to be processed
//Print the program heading
printProjectHeading( cout, W_WIDTH);
// Read the input and output file names
readValue( cin, input_filename, "Read from which file? ");
readValue( cin, output_filename, "Write to which file? ");
//Open the filestreams
in_file.open( input_filename.c_str(), ios::in );
out_file.open( output_filename.c_str(), ios::out );
//Validate the filestreams
if( in_file.fail() ){
cerr << "Input file is invalid.\n";
return 1;
}
if( out_file.fail() ){
cerr << "Output file is invalid.\n";
return 2;
}
//Read the Shift "Key" Value
shift_value = readShiftValue( );
//Display the Encode Decode Selection Menu
printMenu();
//Read the User Selection and convert to uppercasei
readValue( cin, choice, "Select an Option: " );
choice = toupper( choice );
// validate the user choice, if invalid exit the program
if (choice != 'D' && choice != 'E' )
{
cerr << "\n\n**********************************" << endl
<< "ERROR! ERROR! ERROR! ERROR! " << endl
<< "**********************************" << endl;
cerr << "\nYou have made an invalid choice!" << endl
<< "The program will now terminate." << endl;
return 3;
}
if( choice == 'D' ){
shift_value = -1 * ( shift_value );
}
//read each line of the file
while( !in_file.eof() ){
//Read the line
getline( in_file, line );
//Iterate through each character in the line string
for( int i = 0; i < line.length(); i++ ){
//Retrieve the i-th character from the string
//MODIFY the following line
//Hint: array subscripts - line[ some # ]
char *lineToProcess; //declare pointer
int arraySize = line.length(); //declare dynamic array size variable equal to length of string
lineToProcess = new char[arraySize]; //declare dynamic array and assign to pointer
initializeArray(line, lineToProcess, arraySize); //initialize array with contents of string line
for (int a = 0; a < arraySize; a++) //assign each character of array to current char and perform encode/decode
{
current_char = lineToProcess[a]; //assigns array index to current char
//Convert the line to uppercase
current_char = toupper( current_char );
//If the character is in the range A to Z, apply the caesar shift
//otherwise output the character unmodified
if( current_char >= 'A' && current_char <= 'Z' ){
out_file << caesarShift( current_char, shift_value );
}
else{
out_file << current_char;
}
}// end for
delete [] lineToProcess; // delete dynamic array
}// end for
//If the current line is not the last, output a newline character
// MODIFY - Do not output an extra newline at the end of the file
out_file << "\n";
}//end while
//Close the input and output files
in_file.close();
out_file.close();
}
///
///
///
template<class T>
bool readValue( istream &in_stream, T &value_in){
in_stream >> value_in;
//Return False on read error
return in_stream.good();
}
///
///
///
template<class T>
bool readValue( istream &in_stream, T &value_in, string message, int width ){
cout << left << setw( width ) << message << ": ";
return readValue( in_stream, value_in );
}
///
/// Function to read and verify the shift value
///
int readShiftValue(){
int shift_value = 0;
while (shift_value < 1 || shift_value > 25) //validate the shift value until a correct value is entered
{
readValue( cin, shift_value, "Enter a shift value between 1 and 25: "); //Read the value
}
return shift_value;
}
///
/// Function to encode or decode the file
///
char caesarShift( char to_shift, int shift_value){
char result_char; ///< resulting char
//Replace the following line with the shift code
if (to_shift == 'Z' && shift_value >= 0)
{
result_char = 'A';
}
else if (to_shift = 'A' && shift_value <= 0)
{
result_char = 'Z';
}
else
{
result_char = to_shift + shift_value;
}
return result_char;
}
///
/// Function to initializr dynamic array with the contents of line
///
void initializeArray(string str, char list[], int listSize)
{
int index;
for (index = 0; index < listSize; index++)
{
list[index] = str.at(index);
}
}
|