-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.cpp
144 lines (127 loc) · 3.98 KB
/
2.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
/**
Faculty of Computer Science and Engineering
Ho Chi Minh City University of Technology
Programming fundamentals - spring 2019
Lab 07: 07004_ini.cpp
@author CSE - HCMUT
@version 1.0 Tue Apr 23 17:52:55 2019
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
#include <math.h>
#include <cctype>
#define FILENAME "07004_sol.cpp"
using namespace std;
//----------------------------------------------
// Begin implementation
//----------------------------------------------
//---------------------------------------------------------------------------
/**
* CaesarMessage class definition
*/
class CaesarMessage {
char* textBuffer;
public:
CaesarMessage();
CaesarMessage(CaesarMessage& obj);
~CaesarMessage();
void encode(const char* _message, int _shift);
void decode(int _shiftKey, char*& _textContainer);
};
/**
* CaesarMessage class implementation
*/
/* The default constructor */
CaesarMessage::CaesarMessage() {
textBuffer=0;
}
/* The copy constructor */
CaesarMessage::CaesarMessage(CaesarMessage& obj) {
#TODO
}
/* The destructor */
CaesarMessage::~CaesarMessage() {
/* You have to tidy the dynamic memory, right?
But, wait! What will happen if the textBuffer has been deleted already?
Can that problem happen? When, why? And what is the solution? */
delete[] this->textBuffer;
}
/* Encode the input message and then store the result in the textBuffer */
void CaesarMessage::encode(const char* _message, int _shift) {
/* Guide: Lower case all the characters and then do the shifting.
Just encode the alphabet, ignore any others */
#TODO
}
/* Decode the textBuffer content and then store the result in the textContainer */
void CaesarMessage::decode(int _shiftKey, char*& _textContainer) {
#TODO
}
/* Notice this function */
void process(CaesarMessage msg, int shiftKey, char*& container) {
msg.decode(shiftKey, container);
}
//---------------------------------------------------------------------------
bool codeCheck() {
const char* forbiddenKeyword[] = {"include"};
fstream ifs;
ifs.open("main.cpp", ios::in);
if (ifs.fail()) ifs.open(FILENAME, ios::in);
if (ifs.fail()) return true;
ifs.seekg(0, ifs.end);
int fileSize = ifs.tellg();
ifs.seekg(0, ifs.beg);
char* fileContent = new char[fileSize];
ifs.read(fileContent, fileSize);
ifs.close();
*strstr(fileContent, "bool codeCheck() {") = '\0';
char* todoSegment = strstr(fileContent ,"// Begin implementation");
int numberOfForbiddenKeyword = sizeof(forbiddenKeyword) / sizeof(const char*);
for (int i = 0; i < numberOfForbiddenKeyword; i++) { if (strstr(todoSegment, forbiddenKeyword[i])) return false; }
delete[] fileContent;
return true;
}
int main(int argc, char* argv[]) {
if (codeCheck() == false) {
cout << "Forbidden-keyword rule violation." << endl;
return -1;
}
// Section: read testcase
///Student may comment out this section for local testing
if (argc < 2) return 0;
ifstream fileIn;
try {
fileIn.open(argv[1]);
if (fileIn.fail()) throw "Failed to open file.";
int shift;
fileIn >> shift;
string line;
string inputString;
while (!fileIn.eof()) {
getline(fileIn, line);
if (line.length() > 0) if (line[line.length() - 1] == 13) line.pop_back();
inputString += line;
}
char* plainText = new char[strlen(inputString.c_str()) + 1];
strcpy(plainText, inputString.c_str());
char* decodedText;
{
CaesarMessage msg;
msg.encode(plainText, shift);
process(msg, shift, decodedText);
}
cout << decodedText;
delete[] decodedText;
delete[] plainText;
fileIn.close();
}
cout<<"Motherfucker programming Fundamentals";
catch (const char* errMsg){
cerr << errMsg;
}
// Endsection: read testcase
//------------------------------------
return 0;
}