📄 jieshi4.cpp
字号:
//bianyi8.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <list>
#include <map>
#include <vector>
using namespace std;
class Object
{
protected:
// list < map<string,Object* > >list1;
map<string,Object*>map1;
map<string,Object*>::iterator it;
public:
void regist(string str,Object * object)
{
map1[str]=object;
// list1.push_back(map1);
}
virtual Object* findbyname(string str1)
{
it=map1.find(str1);
return it->second;
}
virtual int getparament( string str)
{
return 0;
}
};
class Moto :public Object
{
public:
virtual int getparament(string str)
{
if(str=="stop")
{
return 0;
}
else
return 1;
}
};
class Image :public Object
{
public:
virtual int getparament(string str)
{
if(str=="stop")
{
return 0;
}
else
return 1;
}
};
class Explain
{
public:
Explain()
{
fatherposition=0;
sonposition=0;
// strinfo="(moto_x_stop&&moto_y_stop)||moto_y_run";
// len=strinfo.length();
}
protected:
int len;
//string strinfo;
string::size_type fatherposition,sonposition;
string strinforson;
string strinforgrandson[5];
char curr_tok;
double number_value;
//double left;
public:
double expression(string strinfo);
double expr();
double term();
double prim();
void get_token(string strinfo);
};
Object * object=new Object;
Explain *explain=new Explain;
string strinfo="(!moto_x_run&&moto_y_stop)||image_y_stop";
double Explain::expression(string strinfo)
{
explain->get_token(strinfo);
double x=explain->expr();
return x;
}
void Explain::get_token(string strinfo)
{
if(fatherposition==string::npos)
{
curr_tok='=';
return;
}
switch(strinfo[fatherposition])
{
case'!':
fatherposition++;
curr_tok='!';
break;
case'(':
fatherposition++;
curr_tok='(';
break;
case')':
fatherposition++;
curr_tok=')';
break;
case'|':
fatherposition++;
fatherposition++;
curr_tok='|';
break;
case'&':
fatherposition++;
fatherposition++;
curr_tok='&';
break;
default:
string stralpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string strsymbol="|&!";
int firstsymbol = strinfo.find_first_of(strsymbol,fatherposition);
if(firstsymbol == string::npos)
{
strinforson=strinfo.substr();
}
strinforson=strinfo.substr(fatherposition, firstsymbol - fatherposition);
fatherposition=firstsymbol;
for(int i=0;i<strinforson.length();i++)
{
int firstunderline=strinforson.find('_',sonposition);
if(firstunderline == string::npos)
{
strinforgrandson[i]=strinforson.substr();
break;
}
strinforgrandson[i]=strinforson.substr(0, firstunderline);
strinforson=strinforson.erase ( 0 , firstunderline+1 );
}
Object * newobject;
newobject=object->findbyname(strinforgrandson[0]);
number_value=newobject->getparament(strinforgrandson[2]);
curr_tok='0';
}
}
double Explain::expr()
{
double left=prim();
for(;;)
switch(curr_tok){
case '|':
get_token(strinfo);
left=left||expr();
break;
case '&':
get_token(strinfo);
left=left&&expr();
break;
default:
return left;
}
}
double Explain::prim()
{
double p;
for(;;)
switch(curr_tok){
case '!':
get_token(strinfo);
p=!prim();
break;
case '0':
{
double v=number_value;
get_token(strinfo);
return v;
}
case '(':
{
get_token(strinfo);
double e=expr();
get_token(strinfo);//可以在这里直接加‘)’控制
if(curr_tok!=')'){};
return e;
}
default:
return p;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string str1="moto";
Moto moto;
object->regist("moto",&moto);
Image image;
object->regist("image",&image);
// explain->get_token(strinfo);
// cout<<explain->expr()<<"\n";
cout<<explain->expression(strinfo);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -