Skip to content

Instantly share code, notes, and snippets.

@splitline
Created March 30, 2017 08:31
Show Gist options
  • Save splitline/7499a1a9febbc7a731eee760881c7515 to your computer and use it in GitHub Desktop.
Save splitline/7499a1a9febbc7a731eee760881c7515 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Pointer {
public:
Pointer() {
_isNull = 0;
_vals=new vector<int>();
}
void Create(string, string);
void Destroy();
void Test();
void Print();
string GetName();
void Same(Pointer p2);
void Assign(Pointer p1);
void newVal(int index,int newV);
private:
string _name;
vector<int> *_vals;
string _assignTo;
bool _isNull;
};
void Pointer::Create(string name, string val) {
_name = name;
istringstream ss(val);
int oneVal;
while (ss >> oneVal) {
_vals->push_back(oneVal);
}
_assignTo = _name;
_isNull = 0;
}
void Pointer::Destroy() {
if (!_isNull) {
cout << "Destroying Pointer(value=";
for (std::vector<int>::iterator i = _vals->begin(); i != _vals->end(); ++i) {
cout << *i << (i == _vals->end() - 1 ? ")\n" : ",");
}
_vals->clear();
_assignTo="";
_isNull = 1;
}
else {
cout << "Null Pointer" << endl;
}
}
void Pointer::Test() {
cout << (_isNull ? 0 : 1) << endl;
}
void Pointer::Print() {
if (!_isNull) {
cout << "(value=";
for (std::vector<int>::iterator i = _vals->begin(); i != _vals->end(); ++i) {
cout << *i << (i == _vals->end() - 1 ? ")\n" : ",");
}
}
else
cout << "Null Pointer" << endl;
}
string Pointer::GetName() {
return _name;
}
void Pointer::Assign(Pointer p1){
this->Destroy();
_assignTo=p1._name;
_vals=p1._vals;
}
void Pointer::Same(Pointer p2){
if(_assignTo==p2._assignTo&&_assignTo!="")cout << 1 << endl;
else cout << 0 << endl;
}
void Pointer::newVal(int index,int newV){
if(index >= _vals->size()) cout << "Out Of Range" <<endl;
else if(_isNull) cout << "Null Pointer" << endl;
else _vals->at(index)=newV;
}
vector<Pointer>::iterator find(vector<Pointer> vec,string name){
vector<Pointer>::iterator i;
for (i = vec.begin(); i != vec.end(); i++) {
if (name == i->GetName()) {
return i;
}
}
return i;
}
int main() {
cout <<"-";
vector<Pointer> ptrV;
//vector<Pointer>::iterator i;
Pointer tmp;
string cmd, name, val,name1,name2;
while (cin >> cmd) {
if (cmd == "Create") {
cin >> name;
getline(cin,val);
vector<Pointer>::iterator i;
for (i = ptrV.begin(); i != ptrV.end(); i++) {
if (name == i->GetName()) {
i->Create(name, val);
break;
}
}
if (i == ptrV.end()) {
tmp.Create(name, val);
ptrV.push_back(tmp);
}
}
if (cmd == "Destroy") {
cin >> name ;
vector<Pointer>::iterator i=find(ptrV,name);
for (i = ptrV.begin(); i != ptrV.end(); i++) {
if (name == i->GetName()) {
i->Destroy();
break;
}
}
if (i == ptrV.end())
cout << "Not Found" << endl;
}
if (cmd == "Test") {
vector<Pointer>::iterator i;
cin >> name ;
for (i = ptrV.begin(); i != ptrV.end(); i++) {
if (name == i->GetName()) {
i->Test();
break;
}
}
if (i == ptrV.end())cout << "Not Found" << endl;
}
if (cmd == "Print") {
vector<Pointer>::iterator i;
cin >> name;
for (i = ptrV.begin(); i != ptrV.end(); i++) {
if (name == i->GetName()) {
i->Print();
break;
}
}
if (i == ptrV.end()) cout << "Not Found" << endl;
}
if(cmd=="Assign"){
cin >> name1 >> name2;
vector<Pointer>::iterator i;
int found=0;
Pointer p1,p2;
for (i = ptrV.begin(); i != ptrV.end(); i++) {
if (name1 == i->GetName()){
found++;
p1=*i;
}
if (name2 == i->GetName()){
p1=*i;
found++;
}
}
if(found==2){
p2.Assign(p1);
}
else{
while(found--)cout << "Not Found" << endl;
}
}
if(cmd=="Same"){
cin >> name1 >> name2;
vector<Pointer>::iterator i;
int found=0;
Pointer p1,p2;
for (i = ptrV.begin(); i != ptrV.end(); i++) {
if (name1 == i->GetName()){
found++;
p1=*i;
}
if (name2 == i->GetName()){
p1=*i;
found++;
}
}
if(found==2){
p1.Same(p2);
}
else{
while(found--)cout << "Not Found" << endl;
}
}
if(cmd=="At"){
}
if (cmd == "Exit") return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment