Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
8
Добавлен:
28.06.2014
Размер:
5.17 Кб
Скачать
#include "struct.cpp"

#pragma package(smart_init)

int strstr(UnicodeString str, UnicodeString substr) {
	for (int i = 1; i <= str.Length(); i++)
		if (str[i] == substr[1]) {
			int j;
			for (j = 1; (j <= substr.Length() && i <= str.Length() && substr[j] == str[i]); j++, i++);
			if (j > substr.Length()) return 1;
		}
	return 0;
};

class Analiz {
public:
	UnicodeString output;
	UnicodeString text;
	list<ItemD> lex;
	bool analiz(UnicodeString input);
	int stat;
};

bool Analiz::analiz(	UnicodeString input) {
	lex.clear();
	stat = 4;
	int vloj = 0;
	const char num[] = "0123456789ABCDEFabcdef";
	const char lit[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	const char znak[] = "-*+/";
	const char func[] = "sign ";
	ItemD tmpItem;
	tmpItem.type = -1;
	tmpItem.value = "";
	output = "";
	text = "";
	UnicodeString temp="";
	for (int i = 1; (stat <= 7) && (i <= input.Length()+1); i++) {
		if (i > input.Length()){
			switch(stat) {
				case 1:	if (strstr(func, tmpItem.value+" ") != 0) stat = 14; break;
				case 2: stat = 13;break;
				case 4: stat = 8;break;
				case 6: stat = 14;break;
				case 7: stat = 13;break;
			}
			if (tmpItem.type != -1)	lex.push_back(tmpItem);
			if ((vloj != 0) && (stat <=7 )) stat = 9;
		}
		lf (input[i] == '0') {
			switch(stat) {
				case 2: stat = 13;break;
				case 4:	stat = 2;	break;
				case 5: stat = 11;break;
				case 6: stat = 14;break;
				case 7: stat = 3; break;
			}
			tmpItem.value = tmpItem.value +"0";
			if (tmpItem.type == -1)	tmpItem.type = 6;
		}
		lf(input[i] == 'x') {
			switch(stat) {
				case 2: stat = 7;	break;
				case 3: stat = 13;break;
				case 4:	stat = 1; break;
				case 5: stat = 11;break;
				case 6: stat = 14;break;
				case 7: stat = 13;break;
			}
			tmpItem.value = tmpItem.value +"x";
			if (tmpItem.type == -1) tmpItem.type = 7;
		}
		lf((strchr(lit, input[i]) != 0) && (tmpItem.type != 6)) {
			switch(stat) {
				case 2:
				case 3: stat = 13;break;
				case 4: stat = 1; break;
				case 5: stat = 11;break;
				case 6: stat = 14;break;
				case 7: stat = 13;break;
			}
			tmpItem.value = tmpItem.value +input[i];
			if (tmpItem.type == -1) tmpItem.type = 7;
		}
		lf(strchr(num, input[i]) != 0) {
			switch(stat) {
				case 2:
				case 4: stat = 13;break;
				case 5: stat = 11;break;
				case 6: stat = 14;break;
				case 7: stat = 3; break;
			}
			tmpItem.value = tmpItem.value +input[i];
		}
		lf(strchr(znak, input[i]) != 0) {
			switch(stat) {
				case 1:
					if (strstr(func, tmpItem.value+" ") != 0) stat = 14;
					el stat = 4;
					break;
				case 2: stat = 13;break;
				case 3: stat = 4; break;
				case 4: stat = 8;	break;
				case 5: stat = 4; break;
				case 6: stat = 14;break;
				case 7: stat = 13;break;
			}
			if (tmpItem.type != -1 ) lex.push_back(tmpItem);
			tmpItem.value = input[i];
			if ((input[i] == '+') || (input[i] == '-')) tmpItem.type = 3;
			lf ((input[i] == '*') || (input[i] == '/')) tmpItem.type = 4;
			lex.push_back(tmpItem);
			tmpItem.value = "";
			tmpItem.type = -1;
		}
		lf(input[i] == '(') {
			vloj++;
			switch(stat) {
				case 1:
					if (strstr(func, tmpItem.value+" ") != 0) {stat = 4; temp = tmpItem.value;}
					el stat = 10;
					break;
				case 2: stat = 13;break;
				case 3:
				case 5: stat = 11;break;
				case 6: stat = 4; break;
				case 7: stat = 13;break;
			}
			if (tmpItem.type != -1 ) {
				if(strstr(func, tmpItem.value+" ") != 0) tmpItem.type = 5;
				lex.push_back(tmpItem);
			}
				tmpItem.value = input[i];
				tmpItem.type = 1;
				lex.push_back(tmpItem);
				tmpItem.value = "";
				tmpItem.type = -1;
		}
		lf(')' == input[i]) {
			switch(stat) {
				case 1:
					if (strstr(func, tmpItem.value+" ") != 0) {stat = 14; temp = tmpItem.value;}
					el stat = 5;
					break;
				case 2: stat = 13;break;
				case 3: stat = 5;	break;
				case 7: stat = 13; break;
			}
			if ((--vloj  < 0) && (stat <= 7)) stat = 9;
			if (tmpItem.type != -1 )lex.push_back(tmpItem);
			tmpItem.value = input[i];
			tmpItem.type = 2;
			lex.push_back(tmpItem);
			tmpItem.value = "";
			tmpItem.type = -1;
		}
		lf(' ' == input[i]) {
			switch(stat) {
				case 1:
					if (strstr(func, tmpItem.value+" ") != 0) { stat = 6;   tmpItem.type = 5;  }
					el stat = 5;
					break;
				case 2: stat = 13;break;
				case 3: stat = 5; break;
				case 7: stat = 13;break;
			}
			lex.push_back(tmpItem);
			tmpItem.value = "";
			tmpItem.type = -1;
		}
		el {stat = 12; temp = input[i];}
		if (i<=input.Length()) {
			output = output + input[i];
		}
	}
	switch(stat) {
	case 1: case 3: case 5: text = "Верно";	break;
	case 8:	text = "Пропущен операнд";	break;
	case 9: text = "Не правильно расставлены скобки"; break;
	case 10:text = temp; text = "Не функция ";	 break;
	case 11:text = "Пропущен оператор "; break;
	case 12:text = "Неверный символ "; text = text + temp; break;
	case 13:text = "Не правильный формат числа"; break;
	case 14:text = "Не задан параметр функции "; text = text + temp; break;
	}
	return stat <= 6;
}

Соседние файлы в папке Курсовой проект