-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwc.cpp
71 lines (63 loc) · 2.19 KB
/
wc.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
/*
* flag=0の間はカウントをしないので適宜追加、変更してください
* 特にbegin{figure}[]の位置指定は注意。
* -v を付けて実行することで何がカウントされているか出力されます。
*/
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <cstdio>
#include <cstring>
using namespace std;
int main(int argc, char *argv[]){
string str,tmp;
bool flag = false, v = false;
int cnt = 0;
map<string,int> dic;
for(int i=0;i<argc;i++){
if(strcmp(argv[i],"-v") == 0){
v = true;
}
}
while(getline(cin,str)){
if(str == "\\chapter{Introduction}") flag = true;
if(str == "\\chapter*{References}") flag = false;
if(str == "\\begin{eqnarray}") flag = false;
if(str == "\\end{eqnarray}") flag = true;
if(str == "\\begin{equation}") flag = false;
if(str == "\\end{equation}") flag = true;
if(str == "\\begin{table}[H]") flag = false;
if(str == "\\end{table}") flag = true;
if(str == "\\begin{enumerate}") flag = false;
if(str == "\\end{enumerate}") flag = true;
if(str == "\\begin{quote}") flag = false;
if(str == "\\end{quote}") flag = true;
if(str == "\\begin{figure}[H]") flag = false;
if(str == "\\end{figure}") flag = true;
if(str == "\\[") flag = false;
if(str == "\\]") flag = true;
if(str == "\\begin{thebibliography}[99]") flag = false;
if(str == "\\end{thebibliography}[99]") flag = true;
if(!flag || str[0] == '%' || str[0] == '\\') continue;
istringstream iss(str);
while(iss >> tmp){
if(tmp[0] == '$'){
flag = false;
}
if(flag && tmp[0] != '\\' && tmp[0] != '{' && tmp[0] != '('){
dic[tmp]++;
cnt++;
}
if(tmp[tmp.size()-1] == '$' || tmp[tmp.size()-2] == '$'){
flag = true;
}
}
}
cout << cnt << endl;
if(v){
for(map<string,int>::iterator it=dic.begin();it!=dic.end();++it){
cout << (*it).first << " :" << (*it).second << endl;
}
}
}