-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasgn12.cpp
More file actions
executable file
·154 lines (128 loc) · 2.78 KB
/
Copy pathasgn12.cpp
File metadata and controls
executable file
·154 lines (128 loc) · 2.78 KB
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
/*
CPSC 121-0X
Paul De Palma
depalma
Assignment 12
There are a number of approaches to the problem as stated. I've assumed that
the input is stream of characters, including,possibly, '\n'. I output
50 encrypted characters per line.
Notice, especially, the decomposition and the way tokenization is handled.
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream* gfopenIn(string);
ofstream* gfopenOut(string);
void getKey(ifstream*, string&, char&);
void readLoop(string,char, ifstream*, ofstream*);
char tokenize(char);
char encode(char,char,char);
char intToAlph(int);
int alphToInt(char);
const int SIZE = 50; //number of characters per line
int main(int argc, char* argv[])
{
char mode;
string key;
ifstream* fin;
ifstream* fkey;
ofstream* fout;
fkey = gfopenIn(argv[1]);
fin = gfopenIn(argv[2]);
fout = gfopenOut(argv[3]);
getKey(fkey,key,mode);
readLoop(key,mode,fin,fout);
fkey->close();
fin->close();
fout->close();
delete fkey;
delete fin;
delete fout;
return 0;
}
ifstream* gfopenIn(string fileIn)
{
ifstream* fin = new ifstream;
fin->open(fileIn);
if (!fin->fail())
return fin;
cout << "Error opening input file: " << fileIn << endl;
exit(0);
}
ofstream* gfopenOut(string fileOut)
{
ofstream* fout = new ofstream;
fout->open(fileOut);
if (!fout->fail())
return fout;
cout << "Error opening output file: " << fileOut << endl;
exit(0);
}
void getKey(ifstream* fkey, string& key, char& mode)
{
char ch;
fkey->get(ch); //first character of key file is 'e' or 'd'
mode = ch;
int i = 0;
fkey->get(ch);
while(*fkey)
{
key = key + ch;
fkey->get(ch);
i++;
}
}
void readLoop(string key, char mode, ifstream* fin, ofstream* fout)
{
char ch;
int keyPos = 0;
int linePos = 0;
fin->get(ch);
while(*fin)
{
ch = tokenize(ch);
if (isalpha(ch))
ch = encode(mode,key.at(keyPos),ch);
if (ch != '*') //indicates a character to be passed over
{
if (linePos % SIZE == 0) //EOL after fifty characters
fout->put('\n');
linePos++;
//advance key position for every character encrypted. mod by
//length - 1 b.c. we start at 0
keyPos = (keyPos + 1) % (key.length() - 1);
fout->put(ch);
}
fin->get(ch);
}
fout->put('\n');
}
char tokenize(char ch)
{
if (isupper(ch))
ch = tolower(ch);
if (isdigit(ch) || isalpha(ch))
return ch;
else
return '*'; //anything but an alphanumeric character
}
char encode(char mode, char key, char ch)
{
int posch = alphToInt(ch);
int poskey = alphToInt(key);
if (mode == 'e')
posch = (posch + poskey) % 26;
else
posch = (posch - poskey + 26) % 26; //add 26 to deal with neg. numbers
ch = intToAlph(posch);
return ch;
}
char intToAlph(int pos)
{
return pos + 'a';
}
int alphToInt(char ch)
{
return ch - 'a';
}