WA on test 1
查看原帖
WA on test 1
448887
cancan123456楼主2023/10/15 21:44
#include <iostream>
#include <vector>
using namespace std;
vector < string > reserved_set, symbol_set;
bool gets(string & s) {
	s.clear();
	char c;
	while (true) {
		c = cin.get();
		if (c == EOF) {
			return false;
		}
		if (c == '\n') {
			break;
		}
		s += c;
	}
	return true;
}
bool is_hex(char c) {
	return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
}
int main() {
	freopen("in.txt", "r", stdin);
	reserved_set.push_back("and");
	reserved_set.push_back("break");
	reserved_set.push_back("do");
	reserved_set.push_back("else");
	reserved_set.push_back("elseif");
	reserved_set.push_back("end");
	reserved_set.push_back("false");
	reserved_set.push_back("for");
	reserved_set.push_back("function");
	reserved_set.push_back("if");
	reserved_set.push_back("in");
	reserved_set.push_back("local");
	reserved_set.push_back("nil");
	reserved_set.push_back("not");
	reserved_set.push_back("or");
	reserved_set.push_back("repeat");
	reserved_set.push_back("return");
	reserved_set.push_back("then");
	reserved_set.push_back("true");
	reserved_set.push_back("until");
	reserved_set.push_back("while");
	symbol_set.push_back("+");
	symbol_set.push_back("-");
	symbol_set.push_back("*");
	symbol_set.push_back("/");
	symbol_set.push_back("%");
	symbol_set.push_back("^");
	symbol_set.push_back("#");
	symbol_set.push_back("==");
	symbol_set.push_back(">=");
	symbol_set.push_back("<=");
	symbol_set.push_back(">");
	symbol_set.push_back("<");
	symbol_set.push_back("~=");
	symbol_set.push_back("(");
	symbol_set.push_back(")");
	symbol_set.push_back("{");
	symbol_set.push_back("}");
	symbol_set.push_back("[");
	symbol_set.push_back("]");
	symbol_set.push_back(";");
	symbol_set.push_back(":");
	symbol_set.push_back(",");
	symbol_set.push_back("...");
	symbol_set.push_back("..");
	symbol_set.push_back(".");
	symbol_set.push_back("=");
	string s;
	while (gets(s)) {
		int now = 0;
		while (now < (int)s.length()) {
			// SPACE
			if (isspace(s[now])) {
				now++;
			}
			// COMMENT
			if (s.substr(now, 2) == "--") {
				break;
			}
			// RESERVED
			for (string word : reserved_set) {
				if (s.substr(now, word.length()) == word) {
					cout << "[RESERVED] " << word << endl;
					now += word.length();
					goto end_loop;
				}
			}
			// NUMBER
			if (isdigit(s[now]) || s[now] == '.') {
				if (s.substr(now, 2) == "0x" || s.substr(now, 2) == "0X") {
					cout << "[NUMBER] " << s.substr(now, 2);
					now += 2;
					while (now < (int)s.length() && is_hex(s[now])) {
						cout << s[now];
						now++;
					}
					cout << endl;
					goto end_loop;
				}
				string number;
				while (now < (int)s.length() && isdigit(s[now])) {
					number += s[now];
					now++;
				}
				if (now < (int)s.length() && s[now] == '.') {
					number += s[now];
					now++;
					while (now < (int)s.length() && isdigit(s[now])) {
						number += s[now];
						now++;
					}
				}
				if (now < (int)s.length() && s[now] == 'e') {
					number += s[now];
					now++;
					if (s[now] == '+' || s[now] == '-') {
						number += s[now];
						now++;
					}
					while (now < (int)s.length() && isdigit(s[now])) {
						number += s[now];
						now++;
					}
				}
				if (number != ".") {
					cout << "[NUMBER] " << number << endl;
					goto end_loop;
				} else {
					now--;
				}
			}
			// SYMBOL
			for (string word : symbol_set) {
				if (s.substr(now, word.length()) == word) {
					cout << "[SYMBOL] " << word << endl;
					now += word.length();
					goto end_loop;
				}
			}
			// NAME
			if (isalpha(s[now])) {
				cout << "[NAME] " << s[now];
				now++;
				while (now < (int)s.length() && (isalpha(s[now]) || isdigit(s[now]) || s[now] == '_')) {
					cout << s[now];
					now++;
				}
				cout << endl;
				goto end_loop;
			}
			// STRING
			if (s[now] == '\'') {
				cout << "[STRING] " << s[now];
				now++;
				bool escape = false;
				while (now < (int)s.length()) {
					cout << s[now];
					now++;
					if (escape) {
						escape = false;
					} else if (s[now - 1] == '\\') {
						escape = true;
					}
					if (s[now - 1] == '\'' && !escape) {
						break;
					}
				}
				cout << endl;
				goto end_loop;
			}
			if (s[now] == '"') {
				cout << "[STRING] " << s[now];
				now++;
				bool escape = false;
				while (now < (int)s.length()) {
					cout << s[now];
					now++;
					if (s[now - 1] == '"' && !escape) {
						break;
					}
					if (escape) {
						escape = false;
					} else if (s[now - 1] == '\\') {
						escape = true;
					}
				}
				cout << endl;
				goto end_loop;
			}
end_loop:
			;
		}
		cout << "[EOL]" << endl;
	}
	return 0;
}
2023/10/15 21:44
加载中...