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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "stdafx.h"
#include <iostream>
using namespace std;
 
const int MAX_LEN = 1024;
const int MAX_TOKEN = 100;
const int TOKEN_NUMBER=1;
const int TOKEN_FUNCTION=2;
const int TOKEN_PARETHESIS=3;
const int TOKEN_OPERATOR=4;
 
void preprocess_input(char *input);
bool check_exit(char *input);
int get_token_positions(char *input, int position[][2]);
void print_tokens(int token_count, int position[][2], char *input);
void triming_input(char *input);
void make_lower(char *input);
void tokenizing(char *input, char *delim, int position[][2], int &count);
void sort_position(int count, int position[][2]);
void find_parenthesis(char *input, int position[][2], int &count);
int token_values(int count, char *input, int position[][2], int *type, double *value);
int check_tokens(int count, int *type, double *value);
int evaluate_sentence( int count, int *type, double *value, double &result);
void get_token_string(int start, int end, char *input, char *token);
bool is_numbers(char *token);
bool is_parenthesis(char *token);
bool is_operator(char *token);
void print_sentence(int count, int *type, double *value);
 
 
 
 
void main(int argc, char* argv[])
{
    char input[MAX_LEN];
    int token_count;
    int position[MAX_TOKEN][2];
    bool break_condition = false;
    int error_code;
    int type[MAX_TOKEN];
    double value[MAX_TOKEN], result;
 
 
 
 
    do{
        cout << ">> "; // 한 라인을 입력
        cin.getline(input, MAX_LEN);
 
        preprocess_input(input); // 입력 라인에 대한 전처리
 
        // cout << "|" << input << "|" << endl;
 
        if(check_exit(input)) break; // 종료 조건 검사
 
        token_count = get_token_positions(input, position); // 토큰 위치 계산
 
        print_tokens(token_count, position, input); // 토큰 출력
 
         
        //토큰을 값으로 변환한다.
        error_code=token_values(token_count, input, position, type, value);
        if(error_code){
            cout<<"*수식에 오류가 있습니다.\n";
            continue;
        }
        print_sentence(token_count,type,value);
 
        //토큰의 오류를 검사한다.
        error_code= check_tokens(token_count, type, value);
         
        //수식을 계산한다.
        error_code= evaluate_sentence(token_count, type, value, result);
        cout<<"="<<result<<endl;
 
 
    }while(!break_condition);
}
 
void preprocess_input(char *input)
{
    // 시작과 끝 부분의 공백 제거
    triming_input(input);
 
    // 소문자로 알파벳 문자를 통일
    make_lower(input);
 
}
 
void triming_input(char *input)
{
    int len = strlen(input), start = 0, end = len - 1;
 
    while(input[start] == ' ' || input[start] == '\t'){
        start++;
    }
    while(input[end] == ' ' || input[end] == '\t'){
        end--;
    }
    if(end >= start){
        for(int i = start; i <= end; i++)
            input[i - start] = input[i];
        input[end - start + 1] = '\0';
    }
    else
        input[0] = '\0';
}
 
void make_lower(char *input)
{
    int len = strlen(input);
 
    for(int i = 0; i < len; i++){
        if(input[i] >= 'A' && input[i] <= 'Z')
            input[i] = input[i] + 'a' - 'A';
    }
}
 
bool check_exit(char *input)
{
    // strcmp : return 0 when match
    if(!strcmp(input, "exit")) return true;
    if(!strcmp(input, "quit")) return true;
 
    return false;
}
 
void print_tokens(int token_count, int position[][2], char *input)
{
    for(int i = 0; i < token_count; i++){
        cout.width(4);
        cout << position[i][0];
        cout.width(4);
        cout << position[i][1] << " : ";
 
        for(int j = position[i][0]; j <= position[i][1]; j++)
            cout << input[j];
        cout << endl;
    }
 
}
 
void tokenizing(char *input, char *delim, int position[][2], int &count)
{
    char *pch;
 
    pch = strtok(input, delim);
    while(pch != NULL){
        int start, end;
 
        start = pch - input;
        end = start + strlen(pch) - 1;
 
        position[count][0] = start;
        position[count][1] = end;
        count++;
 
        pch = strtok(NULL, delim);
    }
}
 
void sort_position(int count, int position[][2])
{
    int temp;
 
    for(int i = 0; i < count - 1; i++){
        for(int j = i + 1; j < count; j++){
            if(position[i][0] > position[j][0]){
                temp = position[i][0];
                position[i][0] = position[j][0];
                position[j][0] = temp;
 
                temp = position[i][1];
                position[i][1] = position[j][1];
                position[j][1] = temp;
            }
        }
    }
}
 
void find_parenthesis(char *input, int position[][2], int &count)
{
    int len = strlen(input);
 
    for(int i = 0; i < len; i++){
        if(input[i] == '(' || input[i] == ')'){
            position[count][0] = i;
            position[count][1] = i;
            count++;
 
            input[i] = ' ';
        }
    }
}
 
int get_token_positions(char *_input, int position[][2])
{
    char input[MAX_LEN], input1[MAX_LEN];
    char delim1[] = " \t+-*/()";
    char delim2[] = " \t1234567890.";
 
    strcpy(input, _input);
    int count = 0; // number of tokens
 
    // 괄호를 찾고 지운다.
    find_parenthesis(input, position, count);
 
    // 연산자도 괄호를 찾는 것과 유사한 방식으로 찾고 지움으로써
    // 이후 처리를 간단히 할 수 있다.
     
    // tokenizing
    strcpy(input1, input);
    tokenizing(input1, delim1, position, count); // numbers
 
    strcpy(input1, input);
    tokenizing(input1, delim2, position, count); // operators
 
    // sort
    sort_position(count, position);
 
    return count;
}
 
int token_values(int count, char *input, int position[][2], int *type, double *value)
{
 
    char token[100];
    for(int i=0; i<count; i++)
    {
        get_token_string(position[i][0], position[i][1], input, token);
 
        if(is_numbers(token)){
            type[i]=TOKEN_NUMBER;
            value[i]=atof(token);
        }
        else if(is_parenthesis(token)){
            type[i]=TOKEN_PARETHESIS;
            if(token[0]=='(')value[i]=-1;
            else value[i]=1;
        }
        else if(is_operator(token)){
            type[i]=TOKEN_OPERATOR;
            value[i]=token[0];
        }
        else
        {
            return 1;
            //no error
        }
    }
    return 0;//no error
}
int check_tokens(int count, int *type, double *value)
{
    return 0;
    //no error
}
 
int evaluate_sentence( int count, int *type, double *value, double &result)
{
    result=1;
 
 
    return 0; //no error
 
}
void get_token_string(int start, int end, char *input, char *token)
{
    int pos=0;
 
    for(int j=start; j<=end; j++)
    {
        token[pos]=input[j];
        pos++;
    }
    token[pos]='\0';
}
 
bool is_numbers(char *token)
{
 
    int len=strlen(token);
 
    for(int i=0; i<len ; i++)
    {
        if(token[i]>='0' && token[i]<='9')
            continue;
        else if(token[i]=='.')continue;
        else return false;
    }
 
    return true;
 
}
 
bool is_parenthesis(char *token)
{
    if(token[0]=='(' || token[0]==')')return true;
 
    return false;
}
 
bool is_operator(char *token)
{
    if(token[0]=='+' || token[0]=='-'   || token[0]=='*'   || token[0]=='/')return true;
 
    return false;
}
 
void print_sentence(int count, int *type, double *value)
{
    for(int i=0; i<count ; i++)
    {
        switch(type[i]){
        case TOKEN_NUMBER:
                cout<<" "<<value[i];
                break;
                 
        case TOKEN_PARETHESIS:
            if(value[i]==-1)cout<<" (";
            else cout<<")";
            break;
        case TOKEN_OPERATOR:
            cout<<" "<<(char)value[i];
            break;
        }
    }
    cout<<endl;
}

'Study > C++' 카테고리의 다른 글

Hangover1003  (0) 2011.09.29
6174  (0) 2011.09.29
볼링게임  (0) 2011.09.29
스택과 큐  (0) 2011.09.29
기말 프로젝트 - 토크나이저  (0) 2011.09.29
Posted by 코딩하는 야구쟁이
,