Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program written by Jimmy Johnson
- //GSI Instructor: Robert Perricone
- //This program decodes a message from
- //a text file written in a ceaser cipher
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- //Requires: nothing
- //Modifies: nothing
- //Effects: reads in the name of a file
- // input by the user
- string getFile();
- //Requires: fileName must have characters in it
- //Modifies: fileName
- //Effects: Checks to make sure that the file name
- // given by the user points to a valid address.
- // If it doesn't, the program asks the user to input
- // a correct address
- string checkFile(string);
- //Requires: fileName must point to a valid address
- //Modifies: nothing
- //Effects: Reads each character from the outfile. It also
- // determines which direction, and by how much, the letters
- // of the ceaser cipher shift
- void readFile(string);
- //Requires: shiftToA and shiftToZ assigned to either true or false,
- // Totalshift to be assigned some value, and a ch to be assigned
- // to an ASCII value.
- //Modifies: nothing
- //Effects: Calls the function "toA" if shiftToA and ShiftIsPositive
- // are both true, or if shiftToZ is true and ShiftIsPositive is false.
- // Calls the function "toZ" if shiftToZ and ShiftIsPositive are both
- // true, or if shiftToA is true, and ShiftIsPositive is false.
- void translateAndPrint(bool, bool, int, char);
- //Requires: ch must be assigned the ASCII value of a capital letter,
- // and totalShift must be assigned a value
- //Modifies: nothing
- //Effects: shifts ch toward Z by the value of totalShift
- void toZ(char, int);
- //Requires: ch must be assigned the ASCII value of a capital letter,
- // and totalShift must be assigned a value
- //Modifies: nothing
- //Effects: shifts ch toward A by the value of totalShift
- void toA(char, int);
- int main()
- {
- string fileName;
- fileName = getFile();
- fileName = checkFile(fileName);
- readFile(fileName);
- return 0;
- }
- string getFile()
- {
- string fileName;
- cout << "Please enter the name of the file: ";
- cin >> fileName;
- cout << endl;
- return fileName;
- }
- string checkFile(string fileName)
- {
- ifstream ins;
- string file;
- ins.open(fileName);
- while (ins.fail() == true)
- {
- cout << "\n*** Error Code 1 -- Error in opening file ***\n"
- << "You entered an invalid file name. Please try again.\n\n";
- ins.open(file = getFile());
- fileName = file;
- }
- ins.close();
- return fileName;
- }
- void readFile(string fileName)
- {
- ifstream ins;
- char ch = ' ';
- bool shiftToZ = false, shiftToA = false;
- int shift = 0, shiftPlus = 0, shiftPlusTotal = 0, totalShift = 0;
- ins.open(fileName);
- ins >> noskipws;
- ins >> ch;
- while (!ins.eof())
- {
- while (ch != '#')
- {
- if (ins.eof())
- {
- return;
- }
- while (ch == '@' || ch == '%' || ch == '^')
- {
- if (ch == '@')
- {
- shift = 0;
- shiftPlus = 0;
- shiftPlusTotal = 0;
- shiftToZ = false;
- shiftToA = true;
- ins >> shift;
- if (shift == 0)
- {
- cout << "*** Error Code 2 -- No shift value as expected ***\n";
- return;
- }
- ins >> ch;
- }
- else if (ch == '%')
- {
- shift = 0;
- shiftPlus = 0;
- shiftPlusTotal = 0;
- shiftToA = false;
- shiftToZ = true;
- ins >> shift;
- if (shift == 0)
- {
- cout << "*** Error Code 2 -- No shift value as expected ***\n";
- return;
- }
- ins >> ch;
- }
- else if (ch == '^')
- {
- shiftPlus = 0;
- ins >> shiftPlus;
- shiftPlusTotal += shiftPlus;
- if (shiftPlus == 0)
- {
- cout << "*** Error Code 3 -- No shift alteration value as expected ***\n";
- return;
- }
- ins >> ch;
- }
- }
- totalShift = 0;
- totalShift = shift + shiftPlusTotal;
- translateAndPrint(shiftToA, shiftToZ, totalShift, ch);
- ins >> ch;
- }
- cout << endl << endl;
- ins >> ch;
- }
- return;
- }
- void translateAndPrint(bool shiftToA, bool shiftToZ, int totalShift, char ch)
- {
- char translatedChar;
- int x = 0;
- bool shiftIsPositive;
- if (ch > 'Z' || ch < 'A')
- {
- cout << ch;
- return;
- }
- if (totalShift >= 0)
- {
- shiftIsPositive = true;
- }
- else
- {
- shiftIsPositive = false;
- totalShift *= -1;
- }
- if (shiftToA == true && shiftIsPositive == true)
- {
- toA(ch, totalShift);
- }
- else if (shiftToA == true && shiftIsPositive == false)
- {
- toZ(ch, totalShift);
- }
- if (shiftToZ == true && shiftIsPositive == true)
- {
- toZ(ch, totalShift);
- }
- else if (shiftToZ == true && shiftIsPositive == false)
- {
- toA(ch, totalShift);
- }
- }
- void toA(char ch, int totalShift)
- {
- char translatedChar;
- int x;
- translatedChar = ch;
- for (x = 1; x <= totalShift; x++)
- {
- translatedChar -= 1;
- if (translatedChar < 'A')
- translatedChar = 'Z';
- }
- cout << translatedChar;
- }
- void toZ(char ch, int totalShift)
- {
- char translatedChar;
- int x;
- translatedChar = ch;
- for (x = 1; x <= totalShift; x++)
- {
- translatedChar += 1;
- if (translatedChar > 'Z')
- translatedChar = 'A';
- }
- cout << translatedChar;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement