.
1
: .
: , , . , , . . , , , .
(char*).
.
:
1) .
2) .
3) .
4) .
5) ++.
6) .
7) . .
; - . , (0,1,10,011).
(, , , ).
, .
. :
S → 0|1
→ 0|1
→
:
0 | 1 | 2...9 | _ | |
S | A | A | E | F |
A | A | A | E | F |
E | E | E | E | F |
F | - | - | - | - |
.
#include "stdafx.h"
#include <vector>
#include <fstream>
using namespace std;
//
int matrix[3][4] =
{
1, 1, 2, 3, //A A E F
1, 1, 2, 3, //A A E F
2, 2, 2, 3 //E E E F
};
//
enum AState {S, A, E, F};
//
struct Lex {
bool valid; // ( )
char* str; //
};
//
|
|
int GetMatrixCol(char currentChar)
{
if (currentChar == '0') return 0;
if (currentChar == '1') return 1;
if (currentChar == ' ') return 3;
return 2;
}
//
vector<Lex>* LexAnalysis(char* str)
{
vector<Lex>* result = new vector<Lex>();
int position = 0; //
AState state = AState::S; //
Lex lexema; //
int firstPos; //
while (str[position]!= '\0')
{
char currentChar = str[position];
//
if (state == AState::S && currentChar!= ' ')
{
firstPos = position;
lexema.valid = true;
}
//
state = (AState) matrix[state][GetMatrixCol(currentChar)];
//
if (state == AState::E)
lexema.valid = false;
//
if (state == AState::F)
{
int length = position - firstPos;
lexema.str = new char[length + 1];
strncpy_s(&lexema.str[0], length + 1, &str[0] + firstPos, length); //
result->push_back(lexema); //
state = AState::S;
}
position++;
}
return result;
}
int main()
{
ifstream ifs("input.txt");
// ( 99 )
char str[100];
ifs.getline(str, 100);
// ( )
ifs.seekg(0, ios::end);
int length = ifs.tellg();
char* str = new char[length];
ifs.seekg(0, ios::beg);
ifs.read(&str[0], length);
str[length] = '\0';
vector<Lex>* result = LexAnalysis(str);
//
delete[] str;
ifs.close();
< , , >
return 0;
}
1) | 6 ; . , , . |
2) | ; . , . |
3) | 6 ; . , . |
4) | ; . , . |
5) | ; . , . . |
6) | ; . , . |
7) | ; . , . |
8) | ; . , . |
9) | 6 ; . , . |
10) | ; . , . |
11) | ; . , ( ). |
12) | ; . , . |
13) | ; . . |
14) | ; . , ( ). |
15) | ; . , . |
16) | ; . , . |
17) | ; . , . . |
18) | ; . , . . |
19) | ; . , . . (1, 234, 45, 6789). |
20) | ; . , . . (1, 98, 7654, 876). |
21) | ; . , . |
22) | ; . , ( ) . |
23) | ; . , ( ) . |
24) | ; . , ( ). |
25) | 6 ; . , (). |
:
|
|
|
|
, | ||
/ |