Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
#include <vector>
#include <string>
#include <map>
bool asserted=0;//ijһ�������Ƿ��Ѿ�����error�������ظ����
Graph::Graph()
{
cnt = 0;
}
bool asserted=0;//ijһ��eval�Ƿ��Ѿ�����error�������ظ����

Node* Graph::operator[](const std::string str)
{
Expand All @@ -19,34 +15,26 @@ Node* Graph::operator[](const std::string str)

void Graph::push(std::string str,Node* newnode)
{
/*if(nodes[str]!=nullptr)
{
std::cout<<"Same name!"<<std::endl;
return ;
}*/
nodes[str] = newnode;
}

void Graph::reset(string nodename)
{
// for(auto it=nodes.begin(); it != nodes.end(); it++)
// it->second->reset();
asserted=0;
nodes[nodename]->reset();

}

float Graph::eval(string nodename, const map<string,float> &phMap)
{
++cnt;
reset(nodename);
for(auto iter=phMap.begin(); iter != phMap.end(); iter++)
reset(nodename);
for(auto iter=phMap.begin(); iter != phMap.end(); iter++)//��placeholder��ֵ
nodes[iter->first]->setvalue(iter->second);
outvalue[cnt] = nodes[nodename]->geteval();
return nodes[nodename]->geteval();
return outvalue[cnt];
}

void Graph::setvariable(string vname,float value)
void Graph::setvariable(string vname,float value)
{
++cnt;
nodes[vname]->setvalue(value);
Expand Down
23 changes: 10 additions & 13 deletions graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
extern bool asserted;
class Graph
{
std::map<std::string,Node*> nodes;
std::map<std::string,Node*> nodesamename[50];
std::map<int,float> outvalue;
int size,cnt;
public:
Graph();

Node* operator[] (const string str);//获取节点
void push(std::string str,Node* newnode);
void reset(string nodename);
float eval(string nodename, const map<string,float> &phMap);
void setvariable(string vname,float value);
float lookupanswer(int t);
std::map<std::string,Node*> nodes;//�ڵ�ָ���б�,������������������,���Բ�һ���������нڵ�
std::map<int,float> outvalue;//��������ʷ��¼
int cnt=0;//eval�Ĵ���
public:
Node* operator[] (const string str);//��֪�ڵ���,��ȡ�ڵ�ָ��
void push(std::string str,Node* newnode);//�����½ڵ�
void reset(string nodename);//��ij��eval�Ľ������Ա������һ�μ���
float eval(string nodename, const map<string,float> &phMap/*placeholder�б�*/);//����ij�ڵ���ֵ,���û��placeholder��phMapΪ��
void setvariable(string vname,float value);//����ij��variable��ֵ
float lookupanswer(int t);//��ȡ��t��eval�Ľ��
};
54 changes: 26 additions & 28 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,68 @@
#include <cassert>
#include <iomanip>
#include <sstream>
// #include <vector>
#include <map>
#include "node.h"
#include "graph.h"
#include <iostream>

using namespace std;

map <string,float> placeHolderMap;
map <string,float> placeHolderMap;//placeholder����������ֵһһ��Ӧ����Ϊeval�IJ���
int main()
{
Graph gra;
int n,m,q;
string name,type,tmp;
float value;
cin>>n;
Graph gra;//����ͼ
int n,m,q;//��������������ֲ����ĸ���
string name,type,tmp;//�õ�����ʱ�ַ���
float value;//�õ�����ʱfloat
cin>>n;//C V P���ֽڵ�
for(int i=0; i<n; i++)
{
cin>>name;
cin>>type;
if(type=="C")
{
cin>>value;
gra.push(name,new Constant(value));
gra.push(name,new Constant(value));//�����µ�Constant
}
else if(type=="V")
{
cin>>value;
gra.push(name,new Variable(value));
gra.push(name,new Variable(value));//�����µ�Variable
}
else if(type=="P")
{
gra.push(name,new Placeholder);
gra.push(name,new Placeholder);//�����µ�PlaceHolder
}
else assert(0);
else assert(0);//������������
}

cin>>m;
cin>>m;//����ڵ�
for(int i=0; i<m; i++)
{
cin>>name;
cin>>tmp;
assert(tmp=="=");
string p1,p2,p3,p4;
string p1,p2,p3,p4;//������������
cin>>p1;
if(p1=="COND")
{
cin>>p2;
cin>>p3;
cin>>p4;
gra.push(name,new Cond(gra[p2],gra[p3],gra[p4]));
gra.push(name,new Cond(gra[p2],gra[p3],gra[p4]));//�����µ�Cond
continue;
}
else if(p1=="PRINT")
{
cin>>p2;
gra.push(name,new Print(p2,gra[p2]));
gra.push(name,new Print(p2,gra[p2]));//�����µ�Print
continue;
}
else if(p1=="SIN")
{
cin>>p2;
gra.push(name,new Opn<sin>(gra[p2]));
gra.push(name,new Sin(gra[p2]));//�����Ǵ������ֵ���������
continue;
}
else if(p1=="EXP")
Expand Down Expand Up @@ -98,7 +97,7 @@ int main()
cin>>p2;
if(type=="+")
{
gra.push(name,new sum(gra[p1],gra[p2]));
gra.push(name,new sum(gra[p1],gra[p2]));//�����Ǵ�������˫��������
}
else if(type=="-")
{
Expand Down Expand Up @@ -132,17 +131,16 @@ int main()
{
gra.push(name,new EQU(gra[p1],gra[p2]));
}
else assert(0);
else assert(0);//������������
}
}

cin>>q;
// int t=0;
cin>>q;//Eval SetConstant SetAnswer
for(int i=0; i<q; i++)
{
string command;
getline(cin,command);
if(command.empty())
if(command.empty())//��������
{
i--;
continue;
Expand All @@ -151,18 +149,18 @@ int main()
ss>>type;
if(type=="EVAL")
{
placeHolderMap.clear();
placeHolderMap.clear();//���placeholder�б�
ss>>name;
string phcountstr;
ss>>phcountstr;
int phcount=0;
if(!phcountstr.empty())
if(!phcountstr.empty())//�ж��Ƿ���placeholder
{
phcount=stoi(phcountstr);
}

// cout<<phcountstr<<endl;
for(int j=0; j<phcount; j++)
for(int j=0; j<phcount; j++)//��ȡplaceholder�б�
{
string x;
float y;
Expand All @@ -171,22 +169,22 @@ int main()
}

float ov = gra.eval(name,placeHolderMap);
if(!isnan(ov))
if(!isnan(ov))//���������Ϊnan�������������������󣬲���Ҫ������
cout << fixed << setprecision(4) <<ov<<endl;
}
else if(type=="SETCONSTANT")
{
ss>>name;
ss>>value;
gra.setvariable(name,value);
gra.setvariable(name,value);//��name�ڵ��ֵ��Ϊvalue
}
else if(type=="SETANSWER")
{
ss>>name;
int t;
ss>>t;
gra.setvariable(name,gra.lookupanswer(t));
gra.setvariable(name,gra.lookupanswer(t));//��name�ڵ��ֵ��Ϊgra.lookupanswer(t)(��t�μ���Ľ��)
}
else assert(0);
else assert(0);//������������
}
}
39 changes: 4 additions & 35 deletions node.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "node.h"
// float Node::geteval()
// {
// }

void Node::setvalue(float a)
void Node::setvalue(float a)//����variable�������������������
{
}

Expand All @@ -14,7 +11,7 @@ void Node::reset()

void OneParentNode::reset()
{
p1->reset();
p1->reset();//���ڵ�ҲҪ����
calculated=0;
}
void TwoParentsNode::reset()
Expand All @@ -33,17 +30,12 @@ void ThreeParentsNode::reset()

float ZeroParentNode::geteval()
{
return 1;
}

int ZeroParentNode::getParentsNum()
{
return 0;
return NAN;
}

float OneParentNode::geteval()
{
if(calculated)return tempeval;
if(calculated)return tempeval;//����Ѽ����ֱ�ӷ��ؼ�����ֵ
else
{
calculated=1;
Expand All @@ -52,22 +44,11 @@ float OneParentNode::geteval()
}
}

int OneParentNode::getParentsNum()
{
return 1;
}

// float OneParentNode::func(float x){}

OneParentNode::OneParentNode(Node* parent1)
{
p1=parent1;
}

// float TwoParentsNode::func(float x,float y)
// {
// }

float TwoParentsNode::geteval()
{
if(calculated)return tempeval;
Expand All @@ -79,19 +60,12 @@ float TwoParentsNode::geteval()
}
}

int TwoParentsNode::getParentsNum()
{
return 2;
}

TwoParentsNode::TwoParentsNode(Node* parent1,Node* parent2)
{
p1=parent1;
p2=parent2;
}

// float ThreeParentsNode::func(float x,float y,float z){}

float ThreeParentsNode::geteval()
{
if(calculated)return tempeval;
Expand All @@ -103,11 +77,6 @@ float ThreeParentsNode::geteval()
}
}

int ThreeParentsNode::getParentsNum()
{
return 3;
}

ThreeParentsNode::ThreeParentsNode(Node* parent1,Node* parent2,Node* parent3)
{
p1=parent1;
Expand Down
Loading