Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Курсовой проект / Копия Analiz

.cpp
Скачиваний:
8
Добавлен:
28.06.2014
Размер:
5.14 Кб
Скачать
#include "struct.cpp"
#include "list.h"



class Analiz {
public:
	UnicodeString input;
	UnicodeString output;
	UnicodeString text;
	list<ItemD>lex;
	void analiz(1);
private:
	int stat;
	friend int strstr(UnicodeString str, UnicodeString substr);
};

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;
};

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