N
Neo
hi,
This is my first post in C++ group, so please be nice to me. Thanks.
Also, I will post my first C++ program following. There is a compile
error, I cannot figure it out even I can fix it. And please give me
your comments on my program as much as you can. I think I need some
basic programming convention of C++.
BTW, I used C a lot before, so if you can explain me from low-level
view that would be great!
/* ------------------------------------------------------------
* state.hpp
* ----------------------------------------------------------*/
#ifndef _STATE_HPP
#define _STATE_HPP
#include <map>
#include <string>
#include <iostream>
using namespace std;
class state {
private:
map<string, state *> p_trans;
bool b_accept;
bool b_start;
public:
string p_state_ID;
state(string p_state_id, bool start, bool accept);
state::state();
~state();
void addTrans(string symbol, state * target);
state * delta(string symbol);
bool operator<(state s) const
{ return (p_state_ID<s.getStateID()); }
string getStateID();
void dumpState();
};
#endif /* _STATE_HPP */
/* ------------------------------------------------------
* state.cpp
* ---------------------------------------------------- */
#include <iostream>
#include <string.h>
#include "state.hpp"
state::state(string p_state_id, bool start, bool accept) {
p_state_ID = p_state_id;
b_start = start;
b_accept = accept;
// p_trans = new map<string, state>();
}
/*
state::state(const state& r_state) {
p_state_ID = r_state.p_state_ID;
b_start = r_state.b_start;
b_accept = r_state.b_accept;
}
*/
state::~state() {
}
void state::addTrans(string symbol, state *target) {
p_trans[symbol] = target;
}
state * state::delta(string symbol) {
return p_trans[symbol];
}
string state::getStateID() {
return p_state_ID;
}
void state::dumpState() {
cout<<"state name =\""<<p_state_ID<<"\".";
cout<<"accept = \""<<b_accept<<"\".";
cout<<"start = \""<<b_accept<<"\".\n";
map<string, state*>::iterator p;
for (p = p_trans.begin(); p!= p_trans.end(); ++p) {
cout<<p->first<<'\t'<<p->second->getStateID()<<'\n';
}
}
/* -----------------------------------------------
* automata.cpp
* ---------------------------------------------*/
#include "automata.hpp"
automata::automata(string v_name) {
name = v_name;
trap_st = addState("trap", false, false);
start_st = NULL;
trap_st = NULL;
}
automata::~automata() {
}
void automata::addTransition(state s, string a, state t) {
s.addTrans(a, &t);
}
state * automata::addState(string state_id, bool b_start, bool
b_accept) {
state * st = new state(state_id, b_start, b_accept);
states_set.insert(*st);
if (b_start == true) {
start_st = st;
}
if (b_accept == true) {
accept_states_set.insert(*st);
}
return st;
}
string automata::addSymbol(string id) {
symbols_set.insert(id);
return id;
}
state * automata::delta(state * p_str_st, string a) {
state *target = p_str_st->delta(a);
return (target == NULL) ? trap_st : target;
}
state* automata:elta(state * p_str_st, vector<string> vec_sigma) {
state * tmp = p_str_st;
for (unsigned int i = 0; i < vec_sigma.size(); i++) {
tmp = delta(tmp, vec_sigma.at(i));
}
return tmp;
}
void automata::dumpAutomata() {
set<state, less<state> >::const_iterator it_st;
for (it_st = states_set.begin(); it_st != states_set.end();
it_st++) {
// state st = (state)(*it_st);
// ((state)(*it_st)).dumpState(); <-- It works. but why?
it_st->dumpState(); // <-- It cannot work. Why?
}
}
/* ------------------------------------------------------------
* automata.hpp
* --------------------------------------------------------- */
#ifndef _AUTOMATA_HPP
#define _AUTOMATA_HPP
#include "state.hpp"
#include <set>
#include <vector>
#include <string>
#include <iostream>
class automata {
private:
/* The name of this automata */
string name;
set<string> symbols_set;
/* All states of this automata */
set<state, less<state> > states_set;
/* All accepted stats of this automata */
set<state, less<state> > accept_states_set;
state *start_st;
state *trap_st;
state * delta(state * p_str_st, string a);
public:
automata(string v_name);
~automata();
state* addState(string state_id, bool b_start, bool b_accept);
void addTransition(state p_dst_st, string str);
state* Delta(state * p_str_st, vector<string> vec_sigma);
set<state, less<state> > DeltaLifted(set<state, less<state> >
src_set, vector<string> vec_sigma);
void addTransition(state s, string a, state t);
string addSymbol(string id);
void dumpAutomata();
};
/* -------------------------------------------------------
* test_automata.cpp
* --------------------------------------------------------
#include "automata.hpp"
#include <iostream>
int main(int argc, char ** argv) {
cout<<"----->Begin Test Automata\n";
cout<<"<-----End Test Automata\n";
automata * ocpa = new automata("OpenClose");
state * p_closed = ocpa->addState("closed", true, true);
state * p_opened = ocpa->addState("opened", false, false);
ocpa->dumpAutomata();
string open = ocpa->addSymbol("open");
string close = ocpa->addSymbol("close");
ocpa->addTransition(*p_closed, open, *p_opened);
ocpa->addTransition(*p_opened, close, *p_closed);
return 0;
}
This is my first post in C++ group, so please be nice to me. Thanks.
Also, I will post my first C++ program following. There is a compile
error, I cannot figure it out even I can fix it. And please give me
your comments on my program as much as you can. I think I need some
basic programming convention of C++.
BTW, I used C a lot before, so if you can explain me from low-level
view that would be great!
/* ------------------------------------------------------------
* state.hpp
* ----------------------------------------------------------*/
#ifndef _STATE_HPP
#define _STATE_HPP
#include <map>
#include <string>
#include <iostream>
using namespace std;
class state {
private:
map<string, state *> p_trans;
bool b_accept;
bool b_start;
public:
string p_state_ID;
state(string p_state_id, bool start, bool accept);
state::state();
~state();
void addTrans(string symbol, state * target);
state * delta(string symbol);
bool operator<(state s) const
{ return (p_state_ID<s.getStateID()); }
string getStateID();
void dumpState();
};
#endif /* _STATE_HPP */
/* ------------------------------------------------------
* state.cpp
* ---------------------------------------------------- */
#include <iostream>
#include <string.h>
#include "state.hpp"
state::state(string p_state_id, bool start, bool accept) {
p_state_ID = p_state_id;
b_start = start;
b_accept = accept;
// p_trans = new map<string, state>();
}
/*
state::state(const state& r_state) {
p_state_ID = r_state.p_state_ID;
b_start = r_state.b_start;
b_accept = r_state.b_accept;
}
*/
state::~state() {
}
void state::addTrans(string symbol, state *target) {
p_trans[symbol] = target;
}
state * state::delta(string symbol) {
return p_trans[symbol];
}
string state::getStateID() {
return p_state_ID;
}
void state::dumpState() {
cout<<"state name =\""<<p_state_ID<<"\".";
cout<<"accept = \""<<b_accept<<"\".";
cout<<"start = \""<<b_accept<<"\".\n";
map<string, state*>::iterator p;
for (p = p_trans.begin(); p!= p_trans.end(); ++p) {
cout<<p->first<<'\t'<<p->second->getStateID()<<'\n';
}
}
/* -----------------------------------------------
* automata.cpp
* ---------------------------------------------*/
#include "automata.hpp"
automata::automata(string v_name) {
name = v_name;
trap_st = addState("trap", false, false);
start_st = NULL;
trap_st = NULL;
}
automata::~automata() {
}
void automata::addTransition(state s, string a, state t) {
s.addTrans(a, &t);
}
state * automata::addState(string state_id, bool b_start, bool
b_accept) {
state * st = new state(state_id, b_start, b_accept);
states_set.insert(*st);
if (b_start == true) {
start_st = st;
}
if (b_accept == true) {
accept_states_set.insert(*st);
}
return st;
}
string automata::addSymbol(string id) {
symbols_set.insert(id);
return id;
}
state * automata::delta(state * p_str_st, string a) {
state *target = p_str_st->delta(a);
return (target == NULL) ? trap_st : target;
}
state* automata:elta(state * p_str_st, vector<string> vec_sigma) {
state * tmp = p_str_st;
for (unsigned int i = 0; i < vec_sigma.size(); i++) {
tmp = delta(tmp, vec_sigma.at(i));
}
return tmp;
}
void automata::dumpAutomata() {
set<state, less<state> >::const_iterator it_st;
for (it_st = states_set.begin(); it_st != states_set.end();
it_st++) {
// state st = (state)(*it_st);
// ((state)(*it_st)).dumpState(); <-- It works. but why?
it_st->dumpState(); // <-- It cannot work. Why?
}
}
/* ------------------------------------------------------------
* automata.hpp
* --------------------------------------------------------- */
#ifndef _AUTOMATA_HPP
#define _AUTOMATA_HPP
#include "state.hpp"
#include <set>
#include <vector>
#include <string>
#include <iostream>
class automata {
private:
/* The name of this automata */
string name;
set<string> symbols_set;
/* All states of this automata */
set<state, less<state> > states_set;
/* All accepted stats of this automata */
set<state, less<state> > accept_states_set;
state *start_st;
state *trap_st;
state * delta(state * p_str_st, string a);
public:
automata(string v_name);
~automata();
state* addState(string state_id, bool b_start, bool b_accept);
void addTransition(state p_dst_st, string str);
state* Delta(state * p_str_st, vector<string> vec_sigma);
set<state, less<state> > DeltaLifted(set<state, less<state> >
src_set, vector<string> vec_sigma);
void addTransition(state s, string a, state t);
string addSymbol(string id);
void dumpAutomata();
};
/* -------------------------------------------------------
* test_automata.cpp
* --------------------------------------------------------
#include "automata.hpp"
#include <iostream>
int main(int argc, char ** argv) {
cout<<"----->Begin Test Automata\n";
cout<<"<-----End Test Automata\n";
automata * ocpa = new automata("OpenClose");
state * p_closed = ocpa->addState("closed", true, true);
state * p_opened = ocpa->addState("opened", false, false);
ocpa->dumpAutomata();
string open = ocpa->addSymbol("open");
string close = ocpa->addSymbol("close");
ocpa->addTransition(*p_closed, open, *p_opened);
ocpa->addTransition(*p_opened, close, *p_closed);
return 0;
}