#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <iostream>
FILE *pfile;
using namespace std;
string temp_string;
string reserved[25] = {"AND", "CALL", "DECLARE", "DO", "ELSE", "ENDDECLARE", "ENDFUNCTION", "ENDIF", "ENDPROCEDURE", "ENDPROGRAM", "EXIT", "FALSE", "FOR", "FUNCTION", "IF", "IN", "INOUT", "NOT","OR", "PROCEDURE", "PROGRAM", "RETURN", "THEN", "TRUE", "WHILE"};
int main(void)
{
pfile = fopen("hello.cel", "r");
char cha, temp_token[30], temp;
int count = 0, check = 1, i;
cha = fgetc(pfile);
while (cha != EOF)
{
if(isalpha(cha) || cha == '_')
{
temp_token[0] = cha;
count = 1;
cha = fgetc(pfile);
while(isdigit(cha) || isalpha(cha) || cha == '_')
{
if(count < 30)
{
temp_token[count] = cha;
count++;
}
cha = fgetc(pfile);
}
count--;
for(i = 0; i <= count; i++)
{
temp_string += temp_token[i];
}
cout << temp_string;
for(i = 0; i < 25; i++)
{
if(temp_string == reserved[i])
{
printf(": RESERVED\n");
}
else
{
printf(": ALPHA\n");
}
}
cha = ungetc(cha, pfile);
count = 0;
}
fclose(pfile);
}
예약 된 [i] 문자열과 temp_string 문자열 사이의 비교 문에 문제가 있습니다. "RESERVED"인쇄를 계속할 수 없으며 항상 "ALPHA"를 인쇄합니다. 알다시피, 이것은 파일 (hello.cel)에서 각 문자를 가져 와서 각 토큰의 유형을 인쇄하는 프로그램입니다.C++의 문자열을 비교할 수 없습니다.
EDIT : temp_token은 임시 저장 단어입니다. 이 단어는이 줄에 문자를 추가하여 작성되었습니다. temp_string += temp_token[i];
당신의 문제를 구체화하십시오. 대신에 if (temp_string.compare (reserved [i]) == 0) –
어디에서'temp_string'을 선언 했습니까? 나는 그것이 선언이라고 생각하지 않는다. –
'temp_string'의 정의를 보여주지 않으려합니다. 아마도'string'일까요? 문제가 정확히 무엇입니까? –