词法分析
代码实现
#include<bits/stdc++.h>
#define int long long
#define PII pair<int , int>
using namespace std;
const int N = 1e6 + 10;
int n , k = 1 , x1 = 0 , x0 = 0;
int mid = 1e9 + 7;
map<string , int>mp;
void matt(){
string s;
while(getline(cin , s)){
stringstream t(s);
string ss;
while (t >> ss)
{
n = ss.size();
int i = 0;
string ssr = "";
// cout << ss << '\n';
while (i < n)
{
if(i != n - 1 && ss[i] == '*' && ss[i + 1] == '/') {
x0 = 0 ;
i += 2;
continue;
}
if(x0) {i ++ ; continue ;}
if(isalpha(ss[i]) || ss[i] == '_' || isdigit(ss[i])){
ssr += ss[i];
}
else{
if(i != n - 1 && ss[i] == '.' && isdigit(ss[i - 1]) && isdigit(ss[i + 1])) {
ssr += '.';
i++;
continue;
}
if(ssr != ""){
if(!mp[ssr]){
mp[ssr] = k++;
}
cout << ssr << '\n';
ssr = "";
}
if(i != n - 1 && ss[i] == '/' && ss[i + 1] == '/') {
x1 = 1 ;
break;
}
else if(i != n - 1 && ss[i] == '/' && ss[i + 1] == '*') {
x0 = 1 ;
i++;
continue;
}
else if(i != n - 1 && ss[i] == '+' && ss[i + 1] == '+'){
cout << "++\n";
if(!mp["++"]){
mp["++"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '-' && ss[i + 1] == '-'){
cout << "--\n";
if(!mp["--"]){
mp["--"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '=' && ss[i + 1] == '='){
cout << "==\n";
if(!mp["=="]){
mp["=="] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '!' && ss[i + 1] == '='){
cout << "!=\n";
if(!mp["!="]){
mp["!="] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '+' && ss[i + 1] == '='){
cout << "+=\n";
if(!mp["+="]){
mp["+="] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '-' && ss[i + 1] == '='){
cout << "-=\n";
if(!mp["-="]){
mp["-="] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '*' && ss[i + 1] == '='){
cout << "*=\n";
if(!mp["*="]){
mp["*="] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '/' && ss[i + 1] == '='){
cout << "/=\n";
if(!mp["/="]){
mp["/="] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '&' && ss[i + 1] == '&'){
cout << "&&\n";
if(!mp["&&"]){
mp["&&"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '|' && ss[i + 1] == '|'){
cout << "||\n";
if(!mp["||"]){
mp["||"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '<' && ss[i + 1] == '<'){
cout << "<<\n";
if(!mp["<<"]){
mp["<<"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '>' && ss[i + 1] == '>'){
cout << ">>\n";
if(!mp[">>"]){
mp[">>"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '\\' && ss[i + 1] == 'n'){
cout << "\\n" << '\n';
if(!mp["\\n"]){
mp["\\n"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '%' && ss[i + 1] == 'd'){
cout << "%d\n";
if(!mp["%d"]){
mp["%d"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '%' && ss[i + 1] == 'f'){
cout << "%f\n";
if(!mp["%f"]){
mp["%f"] = k++;
}
i++;
}
else if(i != n - 1 && ss[i] == '%' && ss[i + 1] == 'c'){
cout << "%c\n";
if(!mp["%c"]){
mp["%c"] = k++;
}
i++;
}
else {
cout << ss[i] << '\n';
ssr += ss[i];
if(!mp[ssr]){
mp[ssr] = k++;
}
ssr = "";
}
}
i++;
}
if(ssr != "") {
cout << ssr << '\n';
if(!mp[ssr]) mp[ssr] = k++;
}
if(x1) {
x1 = 0;
break;
}
}
}
cout << '\n' << '\n';
for(auto x : mp){
cout << x.first << ' ' << x.second << '\n';
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin >> T;
while (T--)
{
matt();
}
return 0;
}
输入内容
int main(){
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int T = 1;
cin >> T;
while (T--)
{
int n , m;
cin >> n;
scanf("%d" , &m);
int ans = 0;
double x = 1.2;
while (n)
{
if(n <= 2) n = 0 , ans++;
else if( n % 2 == 0) n /= 2 , ans++;
else n-- , ans++;
}
cout << ans << '\n';
}
return 0;
}
输出内容
int
main
(
)
{
int
T
=
1
;
cin
>>
T
;
while
(
T
--
)
{
int
n
,
m
;
cin
>>
n
;
scanf
(
"
%d
"
,
&
m
)
;
int
ans
=
0
;
double
x
=
1.2
;
while
(
n
)
{
if
(
n
<
=
2
)
n
=
0
,
ans
++
;
else
if
(
n
%
2
==
0
)
n
/=
2
,
ans
++
;
else
n
--
,
ans
++
;
}
cout
<<
ans
<<
'
\n
'
;
}
return
0
;
}
" 18
% 31
%d 19
& 20
' 37
( 3
) 4
++ 29
, 15
-- 13
/= 33
0 22
1 8
1.2 25
2 28
; 9
< 27
<< 36
= 7
== 32
>> 11
T 6
\n 38
ans 21
cin 10
cout 35
double 23
else 30
if 26
int 1
m 16
main 2
n 14
return 39
scanf 17
while 12
x 24
{ 5
} 34
标签:分析,cout,int,cin,词法,ss,while,ans
From: https://www.cnblogs.com/zhljy/p/18538089