-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
136 lines (136 loc) · 2.9 KB
/
main.cpp
File metadata and controls
136 lines (136 loc) · 2.9 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
#include <iostream>
#include <fstream>
using namespace std;
string path;
string getString() {
char ch = getchar();
while(ch == '\r' || ch == '\n') ch = getchar();
string res;
while(ch != '\r' && ch != '\n') res+= ch, ch = getchar();
return res;
}
#define PART1
string readPause(const string & str, int &p) {
int np = p;
while(str.size() > p && (str[np] == ' ' || str[np] == '\n')) ++p;
while(str.size() > np && str[np] != ' ' && str[np] != '\n') ++np;
string res = str.substr(p, np - p);
p = np;
return res;
}
#define PART2
string Compression(string instr) {
char nxtch;
string ststr, tmpstr, tmpstr2;
string res;
int p = 0, infen = 0;
string fh = "+-*/=;%<>?!~^&(),{}|";
while(instr.size() > p) {
nxtch = instr[p];
int flag = (infen == 1 && nxtch == '\"') || (infen == 2 && nxtch == '\'');
if(infen && (!flag || (instr[p - 1] == '\\') && instr[p - 2] != '\\')) {
ststr+= nxtch;
++p;
continue;
}
switch(nxtch) {
case '#': {
if(p && instr[p - 1] != '\n') {
ststr+= nxtch;
break;
}
if(ststr.size()) {
res+= ststr;
if(ststr[ststr.size() - 1] != ';') res+= ";";
res+= "\n", ststr = "";
}
++p;
tmpstr = readPause(instr, p);
++p;
res+= "#" + tmpstr;
if(instr[p] == '\n') break;
res+= " ";
while(instr.size() > p) {
nxtch = instr[p];
if (nxtch == ' ' && ststr.size() && ststr[ststr.size() - 1] == ' ') continue;
if (nxtch == '\n') break;
ststr+= nxtch, ++p;
}
res+= ststr + "\n", ststr = "";
break;
}
case '\n': case ' ' : case ' ' : {
if(ststr.empty() || fh.find(ststr[ststr.size() - 1]) != -1) {
break;
}
if(instr.size() > p + 1 && fh.find(instr[p + 1]) != -1) {
break;
}
if(!ststr.empty() && ststr[ststr.size() - 1] != ' '){
ststr+= ' ';
}
break;
}
case '/' : {
if(instr.size() > p + 1 && (instr[p + 1] == '/' || instr[p + 1] == '*')) {
if(instr[p + 1] == '/') {
while(instr.size() > p) {
nxtch = instr[p];
if (nxtch == '\n') break;
++p;
}
}
else {
string llstr;
while(instr.size() > p) {
nxtch = instr[p];
llstr += nxtch;
if (llstr.find("*/") != -1) break;
++p;
}
++p;
}
}
break;
}
case'\"' : {
if(infen) infen = 0;
else infen = 1;
ststr+= nxtch;
break;
}
case '\'' : {
if(infen) infen = 0;
else infen = 2;
ststr+= nxtch;
break;
}
default : {
ststr+= nxtch;
break;
}
}
++p;
}
if(ststr.size()) res+= ststr;
return res;
// return Compression2(res, m);
}
#define PART3
int main() {
cout << "In File : ";
path = getString();
ifstream ifile(path.c_str());
string str, tmpstr;
while(!ifile.eof()){
getline(ifile, tmpstr);
str += tmpstr + "\n";
}
ifile.close();
str = Compression(str);
path+= ".min.cpp";
ofstream ofile(path.c_str());
ofile << str;
ofile.close();
return 0;
}