Здравствуйте, Zh0rzh, Вы писали:
Z>Вот такая вот незадача.
Z>Вводитм с клавиатуры формулу, вроде (b^2+3)/2+a^3
Z>Так же вводятся b, a. Нужно вычислить значение.
Z>Кто нибудь занимался такой проблемой. Может существуют уже готовые классы. Или у кого есть едия.
Z>Не обделите советом.
Если пользоваться придется только под Win32, то можно воспользоваться стандартной
технологией от M$. По крайней мере отлаженная технология, и сообщения об ошибках,
будут выдоваться в соотвествии с национальными настройками.
#pragma warning (disable : 4786)
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
#import "D:\winnt\system32\msscript.ocx"
void main(void)
{
::CoInitialize(NULL);
MSScriptControl::IScriptControlPtr ScriptEngine;
ScriptEngine.CreateInstance("MSScriptControl.ScriptControl");
ScriptEngine->Language="VBScript"; // or "JScript"
//ScriptEngine->AddCode("Option Explicit");
cout<<"Enter equation to evalueate or 'exit' to exit."<<endl
<<"Lines satrted form char '!' interpretated "<< endl
<<" as varaible definitions, or execute statament"<<endl
<<" all other stataments interpreted as equation to"<<endl
<<" be evaluated."<<endl<<endl;
for(string input; cout<<">"<<flush, cin>>input, input!="exit";)
{
try
{
if(input[0]=='!')
{
cout<< endl << "try execute: " << input << endl;
ScriptEngine->ExecuteStatement(input.c_str()+1);
cout<<"Ok"<<endl;
}
else
{
cout<< endl << "try evaluate: " << input << endl;
_variant_t result=ScriptEngine->Eval(input.c_str());
cout<< "result=" << (double)result << endl
<< flush;
}
}
catch(_com_error err)
{
cout<<"Error:"<<endl
<<"Err namber: "<<hex<<err.Error<<endl
<<"Err description: "<<(const char*)err.Description()<<endl
<<flush;
};
};
::CoUninitialize();
};
Пример использования:
Enter equation to evalueate or 'exit' to exit.
Lines satrted form char '!' interpretated
as varaible definitions, or execute statament
all other stataments interpreted as equation to
be evaluated.
>!A=2
try execute: !A=2
Ok
>!B=3
try execute: !B=3
Ok
>(b^2+3)/2+a^3
try evaluate: (b^2+3)/2+a^3
result=14