A - 小C语言—词法分析程序

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
#include<bits/stdc++.h>
using namespace std;
string kind[5] = { "keyword","identifier","integer","boundary","operator" };
string key[6] = { "main","if","else","for","while","int" };
void Check(string token){
if(token[0]>='0'&&token[0]<='9'){
cout<<"("<<kind[2]<<","<<token<<")"<<endl;
return;
}
else{
for(int i=0;i<6;i++){
if(token==key[i]){
cout<<"("<<kind[0]<<","<<token<<")"<<endl;
return;
}
}
cout<<"("<<kind[1]<<","<<token<<")"<<endl;
}
}
int main(){
string str;
while(cin>>str){
string token="";
int len = str.length();
for(int i=0;i<len;i++){
//遇到界符,往往表示单词结束,判断
if(str[i]=='('||str[i] == ')'||str[i]=='{'||str[i]=='}'||str[i]==','||str[i]==';'){
if(token.length()){
Check(token);
}
cout<<"("<<kind[3]<<","<<str[i]<<")"<<endl;
token="";
}//遇到运算符,往往表示单词结束,判断是否为双目运算符,是则输出,否则单目运算符,输出单目运算符
else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='='||str[i]=='>'||str[i]=='<'||str[i]=='!'){
if(token.length()){
Check(token);
}
token="";
if(i+1<len&&str[i+1]=='='){
cout<<"("<<kind[4]<<","<<str[i]<<"="<<")"<<endl;
i++;
}
else{
cout<<"("<<kind[4]<<","<<str[i]<<")"<<endl;
}
}//该字符不是界符也不是运算符,加入token中
else{
token+=str[i];
}

}//每次循环结束,判断token是否为空,不为空则检查其类型
if(token.length()){
Check(token);
}
}
}

B - 识别浮点常量问题

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
#include<bits/stdc++.h>
using namespace std;
bool isNum(char c){
if(c>='0'&&c<='9')
return true;
return false;
}
int findP(int l, int r, string s){
for(int i=0;i<=r;i++){
if(s[i]=='.'){
return i;
}
}
return -1;
}
int countP(int l, int r, string s){
int cnt=0;
for(int i=l;i<=r;i++){
if(s[i]=='.'){
cnt++;
}
}
return cnt;
}
bool isSign(char c){
if(c=='+'||c=='-')
return true;
return false;
}
bool isSmallNum(int l, int r, string s){
int p_l=findP(l,r,s);
int flag=0;
//判断小数点个数
if(!(countP(l,r,s)==1)){
return false;
}
//判小数点左侧是否有数字
for(int i=0;i<p_l;i++){
if(isNum(s[i])){
flag=1;
break;
}
}
if(!flag) return false;
flag=0;
//判小数点右侧是否有数字
for(int i=p_l+1;i<=r;i++){
if(isNum(s[i])){
flag=1;
break;
}
}
if(!flag) return false;
for(int i=l;i<=r;i++){
if(i==l&&isSign(s[l])){
if(r-l+1==1) return false;
continue;
}
else if(s[i]=='.'){
continue;
}
else if(isNum(s[i])){
continue;
}
else{
return false;
}
}
return true;

}

int FindE(string s){
for(int i=0;i<s.size();i++){
if(s[i]=='e'||s[i]=='E'){
return i;
}
}
return -1;
}
bool isInt(int l, int r, string s){
for(int i=l;i<=r;i++){
if(i==l&&isSign(s[l])){
if(r-l+1==1) return false;
continue;
}
else if(isNum(s[i])){
continue;
}
else{
return false;;
}
}
return true;
}
int main(){
string s;
int flag=0;
getline(cin, s);
int l=0, r=s.size()-1;
while(s[l]==' ') l++;
while(s[r]==' ') r--;
int len=s.size();
//3e-.3
int res=FindE(s);
if(res==-1){
if(isSmallNum(l, r, s)){
cout<<"YES";
return 0;
}
else{

cout<<"NO";
return 0;
}

}
else{
if(res==l||res==r){
flag=1;
}
if(!isSmallNum(l, res-1, s)&&!isInt(l,res-1,s)){
flag=1;
}
if(isInt(res+1,r,s)){
;
}
else{
flag=1;
}
if(flag==0){
cout<<"YES";
return 0;
}
else{
cout<<"NO";
return 0;
}
return 0;
}


}