Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- using namespace std;
- // ********** Declarations **********
- char init_(const char* pattern, char type);
- void init(const char* puncuation, const char* space, const char* noise, const char** sensitive_words);
- // ********** Global States **********
- char CharClassify[256] = { 0 };
- char** SensitiveWords = nullptr;
- int swCount;
- bool swMode = false;
- char swBuf[80] = { '\0' };
- int swBufPos = 0;
- int swIdx = 0;
- char* result;
- int rdIdx = 0;
- bool ReTry = true;
- // ********** State Machine **********
- void gotInfoChar(const char& ch)
- {
- result[rdIdx++] = ch;
- if (swMode) {
- // in sensitive word matching mode
- swBuf[swBufPos++] = CharClassify[ch];
- int i;
- for (i = 0; i < swCount; i++) {
- if (strncmp(swBuf, SensitiveWords[i], swBufPos) == 0) {
- break;
- }
- }
- if (i == swCount) {
- // matching failed
- swMode = false;
- swBufPos = 0;
- } else if (SensitiveWords[i][swBufPos] == 0) {
- // whether the matching finishes
- rdIdx = swIdx;
- swMode = false;
- swBufPos = 0;
- ReTry = true;
- return;
- }
- }
- // matching failed or not in matching mode
- // is it the header of any sensitive word?
- for (int i = 0; i < swCount; i++) {
- if (CharClassify[ch] == SensitiveWords[i][0]) {
- swBuf[swBufPos++] = CharClassify[ch];
- swMode = true;
- swIdx = rdIdx - 1;
- break;
- }
- }
- }
- void gotPunc(const char& ch)
- {
- // if we read a punctuation, the sensitive word is interruptted if exists
- result[rdIdx++] = ch;
- swMode = false;
- swBufPos = 0;
- }
- void gotSpaceOrNoise(const char& ch)
- {
- // do nothing when got the useless or space character
- result[rdIdx++] = ch;
- }
- void doit(const char* buffer)
- {
- rdIdx = 0;
- result = new char[strlen(buffer) + 1];
- for (int idx = 0; buffer[idx] != '\0'; idx++) {
- char type = CharClassify[buffer[idx]];
- if (type == 0) {
- cout << "锟斤拷锟诫不锟斤拷锟斤拷要锟斤拷" << endl;
- exit(EXIT_FAILURE);
- } else {
- switch (type & 0xf0) {
- case 0x10:
- case 0x20:
- case 0x30:
- gotInfoChar(buffer[idx]);
- break;
- case 0x40:
- gotPunc(buffer[idx]);
- break;
- default:
- gotSpaceOrNoise(buffer[idx]);
- }
- }
- }
- result[rdIdx] = '\0';
- }
- // ********** Main Program **********
- int main()
- {
- swCount = 3;
- SensitiveWords = new char*[swCount];
- const char* sw[swCount] = { "L4", "Fd", "D26" };
- init(".,\"", " \n", "@#$", sw);
- char buffer[64];
- cout << "Give no more than 20 characters";
- cin.getline(buffer, 20);
- doit(buffer);
- while (ReTry) {
- ReTry = false;
- strcpy(buffer, result);
- delete[] result;
- result = nullptr;
- doit(buffer);
- }
- cout << result << endl;
- delete[] result;
- for (int i = 0; i < swCount; i++) {
- delete[] SensitiveWords[i];
- }
- delete[] SensitiveWords;
- return 0;
- }
- char init_(const char* pattern, char type)
- {
- for (int idx = 0; pattern[idx] != '\0'; idx++) {
- CharClassify[pattern[idx]] = type++;
- }
- return type;
- }
- void init(const char* puncuation, const char* space, const char* noise, const char** sensitive_words)
- {
- init_("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0x10);
- init_("abcdefghijklmnopqrstuvwxyz", 0x10);
- init_("0123456789", 0x30);
- init_(puncuation, 0x40);
- init_(space, init_(noise, 0x80));
- for (int i = 0; i < swCount; i++) {
- SensitiveWords[i] = new char[strlen(sensitive_words[i]) + 1]{ 0 };
- for (int idx = 0; sensitive_words[i][idx] != '\0'; idx++) {
- SensitiveWords[i][idx] = CharClassify[sensitive_words[i][idx]];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement