Скачиваний:
2
Добавлен:
01.05.2014
Размер:
4.09 Кб
Скачать
// mo003_advanced.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <math.h>

#define NON -6546.548752548
#define VAR 256
#define POW 282
#define NUM 300
#define LOG 301
#define EXP 302
#define ALL 500
#define EOL '\0'
#define VT_ADD 1
#define VT_GET 2
#define VT_SET 3
#define ZN 0.6180339887
#define DLTX 0.0000000001


	char usrstr[256],newm[256],vartype, vars[26];
	int lookup, count_str, numval, vc;
	char math[256],tmpbuf[128];
	double value,values[26];

	int lexer();
	void parse();
	double StringTable(int varname, double inval, int action);//varname - variable name; inval - value to set; action - type of action: VT_ADD - adds a variable, VT_SET - sets a valye for specified variable, VT_GET - gets a value of variable
	void expr();
	void term();
	void factor();
	void match_token(int t);
	char* StartTranslation(char *initstring);

char* StartTranslation(char *initstring)
{
        lookup=count_str=numval=vc=0;
        strset(usrstr,'\0');
        strset(newm,'\0');
        vartype='\0';
        strset(vars,'\0');
        strset(tmpbuf,'\0');
        strset(math,'\0');
        value=0;
        for(int i=0;i<0;i++)
          values[i]=0;
	strncpy(usrstr,initstring,strlen(initstring)+1);
	parse();
	return math;
}

double StringTable(int varname, double inval, int action)
{
	int match=0,i;

	if(action == VT_ADD)
	{
		for(i=0;i<26;i++)
			if(varname == vars[i]) {match=1; break;}
		if (match == 0)
		{
			vars[vc]=varname;
			vc++;
		}
		return 0;
	}
	if(action == VT_SET)
	{
		for(i=0;i<26;i++)
			if(varname == vars[i]) {match=1; break;}
		if (match == 1)
		{
			values[i]=inval;
		}
		return 0;
	}
	if (action==VT_GET)
	{
		for(i=0;i<26;i++)
			if(varname == vars[i]) {match=1; break;}
		if(match == 1)
			value=values[i];
		else
			value=NON;
		return value;
	}
        return -1;
}

int lexer()
{
	int ti,n,tn;
	char t;

        t=usrstr[count_str];
        //ShowMessage("count="+IntToStr(count_str));
        //ShowMessage(t);
	if(isdigit(t))
	{
		ti=0;
		numval=0;
		while(isdigit(t))
		{
			tmpbuf[ti]=t;
			count_str++;
			ti++;
			t=usrstr[count_str];
		}
		tmpbuf[ti++]=' ';
		tmpbuf[ti]='\0';

		n=ti-1;

		for(ti=ti-1;ti>=0;ti--)
		{
			numval=numval+pow(10,n-ti)*(tmpbuf[ti]-'0');
		}
		return NUM;
	}
	else if (isalpha(t))
	{
		if(t=='l')
		{
			tn=count_str;
			if( (usrstr[count_str+1]=='o')&& (usrstr[count_str+2]=='g') )
			{
				if(count_str==tn)
					count_str+=3;
				return LOG;
			}
		}

		else
		{
            vartype=t;
            StringTable(t,0,VT_ADD);
            count_str++;
            return VAR;
		}
	}
	else if (t==EOL)
		return ALL;
	else {count_str++; return t;}
        return -1;
}

void parse()
{
	lookup=lexer();

	while(lookup != ALL)
		expr();
}

void expr()
{
	char t;

	term();
	while(1)
		switch (lookup){
			case '+': case '-':
				t=lookup;
				match_token(lookup);
				term();
				math[strlen(math)]=t;
				continue;
			default:
				return;
		}
}

void term()
{
	char t;

	factor();
	while(1)
		switch(lookup) {
			case '*': case '/': case '^':
				t=lookup;
				match_token(t);
				factor();
				math[strlen(math)]=t;
				continue;
			default:
				return;
		}
}

void factor()
{
	switch(lookup) {
		case '(':
			match_token('(');
			expr();
			match_token(')');
			break;
		case NUM:
			strncat(math,tmpbuf,strlen(tmpbuf));
                        //ShowMessage(math);
			match_token(NUM);
			break;
		case VAR:
			math[strlen(math)]=vartype;
                        //math[strlen(math)]='\0';
                        //ShowMessage(math);
			match_token(VAR);
			break;
		case LOG:
			match_token(LOG);
            expr();
            math[strlen(math)]='~';
            //ShowMessage(math);
            //math[strlen(math)]='\0';
            break;
	}
}
void match_token(int t)
{
	if(lookup==t)
		lookup=lexer();
}
Соседние файлы в папке gera
  • #
    01.05.201417.18 Кб2Project1.obj
  • #
    01.05.2014876 б2Project1.res
  • #
    01.05.20142.95 Mб2Project1.tds
  • #
    01.05.20144.06 Кб2Project1.~bpr
  • #
    01.05.20141.18 Кб2Project1.~cpp
  • #
    01.05.20144.09 Кб2translator.h
  • #
    01.05.20144.95 Кб2Unit1.cpp
  • #
    01.05.201451 б2Unit1.ddp
  • #
    01.05.20143.19 Кб2Unit1.dfm
  • #
    01.05.20141.74 Кб2Unit1.h
  • #
    01.05.2014109.21 Кб2Unit1.obj