-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcaesar.cpp
200 lines (178 loc) · 4.24 KB
/
caesar.cpp
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
# include <cstdlib>
# include <cstring>
# include <ctime>
# include <iomanip>
# include <iostream>
using namespace std;
# include "caesar.hpp"
//****************************************************************************80
int i4_modp ( int i, int j )
//****************************************************************************80
//
// Purpose:
//
// I4_MODP returns the nonnegative remainder of I4 division.
//
// Discussion:
//
// If
// NREM = I4_MODP ( I, J )
// NMULT = ( I - NREM ) / J
// then
// I = J * NMULT + NREM
// where NREM is always nonnegative.
//
// The MOD function computes a result with the same sign as the
// quantity being divided. Thus, suppose you had an angle A,
// and you wanted to ensure that it was between 0 and 360.
// Then mod(A,360) would do, if A was positive, but if A
// was negative, your result would be between -360 and 0.
//
// On the other hand, I4_MODP(A,360) is between 0 and 360, always.
//
// I J MOD I4_MODP I4_MODP Factorization
//
// 107 50 7 7 107 = 2 * 50 + 7
// 107 -50 7 7 107 = -2 * -50 + 7
// -107 50 -7 43 -107 = -3 * 50 + 43
// -107 -50 -7 43 -107 = 3 * -50 + 43
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 26 May 1999
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I, the number to be divided.
//
// Input, int J, the number that divides I.
//
// Output, int I4_MODP, the nonnegative remainder when I is
// divided by J.
//
{
int value;
if ( j == 0 )
{
cerr << "\n";
cerr << "I4_MODP - Fatal error!\n";
cerr << " I4_MODP ( I, J ) called with J = " << j << "\n";
exit ( 1 );
}
value = i % j;
if ( value < 0 )
{
value = value + abs ( j );
}
return value;
}
//****************************************************************************80
string s_to_caesar ( string s1, int k )
//****************************************************************************80
//
// Purpose:
//
// S_TO_CAESAR applies a Caesar shift cipher to a string.
//
// Discussion:
//
// The Caesar shift cipher incremented each letter by 1, with Z going to A.
//
// This function can apply a Caesar shift cipher to a string of characters,
// using an arbitrary shift K, which can be positive, negative or zero.
//
// Letters A through Z will be shifted by K, mod 26.
// Letters a through z will be shifted by K, mod 26.
// Non-alphabetic characters are unchanged.
//
// s2 = s_to_caesar ( s1, 1 ) will apply the traditional Caesar shift cipher.
// s3 = s_to_caesar ( s2, -1 ) will decipher the result.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 31 January 2016
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string S1, a string of characters.
//
// Input, int K, the increment.
//
// Output, string S2, the string of enciphered characters.
//
{
int i;
int s1_length;
string s2;
s1_length = s1.length ( );
s2 = s1;
for ( i = 0; i < s1_length; i++ )
{
if ( 'A' <= s1[i] && s1[i] <= 'A' + 25 )
{
s2[i] = i4_modp ( s1[i] + k - 'A', 26 ) + 'A';
}
else if ( 'a' <= s1[i] && s1[i] <= 'a' + 25 )
{
s2[i] = i4_modp ( s1[i] + k - 'a', 26 ) + 'a';
}
}
return s2;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}