Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
23
Добавлен:
17.04.2013
Размер:
3.66 Кб
Скачать
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
//-------Проверка-------------------------------------------
int func0(string str)
{
	int i,j,countA=0,countB=0,k=0;
//Проверка на использование разрешенных символов	
	i=str.find_first_not_of("0123456789+-*/.e()f,",0);
	if(i!=-1)
	{
		cerr<<"!illegal character: "<<str[i]<<" on position: "<<i+1<<endl;
		return -1;
	}
//Символы '-,+,*,/' не должены стоять на последнем месте
	if(str[str.length()-1]=='-'||str[str.length()-1]=='+'||str[str.length()-1]=='*'||str[str.length()-1]=='/')
	{
		cerr<<"!error using '-,+,*,/' on position: "<<str.length()<<endl;
		return -1;
	}
//Символ '+' не должен стоять на первом месте
	if(str[0]=='+')
	{
		cerr<<"!error using '+' on position: "<<1<<endl;
		return -1;
	}
//Проверка на ошибки типа: --,-+,-*,-/,-),+-,*-,/-
	i=str.find("-",0);
	if((i!=-1&&(str[i+1]=='-'||str[i+1]=='+'||str[i+1]=='*'||str[i+1]=='/'||str[i+1]==')')) || (i>0&&(str[i-1]=='+'||str[i-1]=='*'||str[i-1]=='/')))
	{
		cerr<<"!error type is: '--,-+,-*,-/,-),+-,*-,/-' on position: "<<i+2<<endl;
		return -1;
	}
//Проверка на ошибки типа: ++,+*,+/,*+,/+
	i=str.find("+",0);
	if((i!=-1&&(str[i+1]=='+'||str[i+1]=='*'||str[i+1]=='/'||str[i+1]==')')) || (i>0&&(str[i-1]=='*'||str[i-1]=='/'||str[i-1]=='(')))
	{
		cerr<<"!error type is: '++,+*,+/,+),*+,/+,(+' on position: "<<i+2<<endl;
		return -1;
	}
//Кол-во символов '(' должно совпадать с кол-вом символов ')'
	for(i=0;i<str.length();i++)
	{
		if(str[i]=='(') countA++;
		if(str[i]==')') countB++;
	}
	if(countA!=countB)
	{
		cerr<<"!error using '(',')'"<<endl;
		return -1;
	}
//Проверка на правильность использования символа 'e'
	i=str.find('e',0);
	if(i==0||(i>0&&(str[i-1]=='+'||str[i-1]=='-'||str[i-1]=='*'||str[i-1]=='/'||str[i-1]==')'||str[i-1]=='('||str[i-1]=='.')))
	{
		cerr<<"!error using 'e' on position: "<<i+1<<endl;
		return -1;
	}
	if(str[i]=='e'&&str[i+1]!='-'&&str[i+1]!='+')
	{
		cerr<<"!error using 'e' on position: "<<i+1<<endl;
		return -1;
	}
	//Проверка на ошибку типа: 2e-3e-4
	int count=0,count_e=0;
	for(i=0;i<str.length();i++)
	{
		if((str[i]=='+'||(str[i]=='-'&&i!=0)||(str[i]=='*')||(str[i]=='/'))&&str[i-1]!='e')
			count++;
		if(str[i]=='e')
			count_e++;
	}
	if(count==0&&count_e>1)
	{
		cerr<<"!error using 'e'"<<endl;
		return -1;
	}
//Проверка на правильность использования символа '.'
	//Проверка на ошибку типа: '..'
	for(i=0;i<str.length();i++)
		if(str[i]=='.'&&str[i+1]=='.')
		{
			cerr<<"!error type is: '..' on position: "<<i+2<<endl;
			return -1;
		}
	//Проверка на ошибку типа: 2.56.7
	if(i!=-1)
	{
		int j=str.find_first_of("+-*/)",i+1);
		if(j==-1) j=str.length();
		int k=str.find(".",i+1);
		if(k!=-1 && k<j)
		{
			cerr<<"!syntax error on position: "<<k+1<<endl;
			return -1;
		}
	}
//Проверка на правильность использования символа '('
	for(i=1;i<str.length();i++)
	{
		if(str[i]=='('&&str[i-1]!='('&&str[i-1]!='f'&&str[i-1]!=','&&str[i-1]!='+'&&str[i-1]!='-'&&str[i-1]!='*'&&str[i-1]!='/')
		{
			cerr<<"!error using '(' on position: "<<i+1<<endl;
			return -1;
		}
	}
//Проверка на правильность использования символа ')'
	for(i=0;i<str.length()-1;i++)
	{
		if(str[i]==')'&&str[i+1]!='+'&&str[i+1]!=','&&str[i+1]!='-'&&str[i+1]!='*'&&str[i+1]!='/'&&str[i+1]!=')')
		{
			cerr<<"!error using ')' on position: "<<i+1<<endl;
			return -1;
		}
	}
//Символ '(' должен идти раньше символа ')'
	i=str.find("(",0);
	j=str.find(")",0);
	if(i!=-1&&j!=-1&&i>j)
	{
		cerr<<"!error using ')' on position: "<<i+1<<endl;
		return -1;
	}
//-----//
	return 0;
}
Соседние файлы в папке Calc