Created
March 31, 2017 13:46
-
-
Save splitline/150809a2526b1ef5ac19fa1de93ef7d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ID/Name :B10530027 黃志仁 | |
// Date:March 30.2017 | |
// Last Update:March 30.2017 | |
// Problem statement : Pointer-Advanced | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <sstream> | |
using namespace std; | |
class Pointer { | |
public: | |
Pointer() { | |
_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; //all the value of one Pointer object, use pointer for easy to assign | |
string _assignTo; //the name this Pointer assign to | |
}; | |
//Create a Pointer object | |
void Pointer::Create(string name, string val) { | |
_name = name; | |
istringstream ss(val); | |
int oneVal; | |
//push all the values in vals into _vals | |
while (ss >> oneVal) _vals->push_back(oneVal); | |
//initialize _assignTo | |
_assignTo = _name; | |
} | |
//destroy a Pointer to let it become NULL | |
void Pointer::Destroy() { | |
if (_vals->size() != 0) {//is null? | |
cout << "Destroying Pointer"; | |
this->Print(); //print all values | |
_vals->clear(); //clear _vals vector | |
_assignTo = ""; //clear _assignTo | |
} | |
else { | |
cout << "Null Pointer" << endl; | |
} | |
} | |
//Test Pointer is null or not | |
void Pointer::Test() { | |
cout << ((_vals->size() == 0) ? 0 : 1) << endl; | |
} | |
//just print all values | |
void Pointer::Print() { | |
if (_vals->size() != 0) { | |
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; | |
} | |
//return name of one Pointer | |
string Pointer::GetName() { | |
return _name; | |
} | |
//assign p1 to this Pointer | |
void Pointer::Assign(Pointer p1) { | |
this->Destroy(); | |
this->_assignTo = p1._assignTo; //change assign target | |
this->_vals = p1._vals; //change _vals | |
} | |
//is p2 assign to the same target with this Pointer? | |
void Pointer::Same(Pointer p2) { | |
if (_assignTo == p2._assignTo && _assignTo != "")cout << 1 << endl; | |
else cout << 0 << endl; | |
} | |
//modify new value for _vals[index] in this Pointer | |
void Pointer::newVal(int index, int newV) { | |
if (index >= _vals->size()) cout << "Out Of Range" << endl; | |
else if (_vals->size() == 0) cout << "Null Pointer" << endl; | |
else _vals->at(index) = newV; | |
} | |
int main() { | |
vector<Pointer> ptrV; //a vector to store Pointers | |
string cmd, name, val, name1, name2; | |
while (cin >> cmd) { | |
//Create Name Value(s) | |
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()) { | |
Pointer tmp; | |
tmp.Create(name, val); | |
ptrV.push_back(tmp); | |
} | |
} | |
//Destroy Name | |
if (cmd == "Destroy") { | |
cin >> name ; | |
vector<Pointer>::iterator i; | |
for (i = ptrV.begin(); i != ptrV.end(); i++) { | |
if (name == i->GetName()) { | |
i->Destroy(); | |
break; | |
} | |
} | |
if (i == ptrV.end()) | |
cout << "Not Found" << endl; | |
} | |
//Test Name | |
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; | |
} | |
//Print Name | |
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; | |
} | |
//Assign Name1 Name2 | |
if (cmd == "Assign") { | |
cin >> name1 >> name2; | |
vector<Pointer>::iterator i1, i2; | |
int found = 0; | |
for (i1 = ptrV.begin(); i1 != ptrV.end(); i1++) { | |
if (name1 == i1->GetName()) { | |
found++; | |
break; | |
} | |
} | |
for (i2 = ptrV.begin(); i2 != ptrV.end(); i2++) { | |
if (name2 == i2->GetName()) { | |
found++; | |
break; | |
} | |
} | |
if (found == 2) { | |
i2->Assign(*i1); | |
} | |
else { | |
while (found--)cout << "Not Found" << endl; | |
} | |
} | |
//Same Name1 Name2 | |
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()) { | |
p1 = *i; | |
found++; | |
} | |
else if (name2 == i->GetName()) { | |
p2 = *i; | |
found++; | |
} | |
} | |
if (found == 2) { | |
p1.Same(p2); | |
} | |
else { | |
while (found--)cout << "Not Found" << endl; | |
} | |
} | |
//At Name Index NewValue | |
if (cmd == "At") { | |
int ind, newValue; | |
cin >> name >> ind >> newValue; | |
vector<Pointer>::iterator i; | |
for (i = ptrV.begin(); i != ptrV.end(); i++) { | |
if (name == i->GetName()) { | |
i->newVal(ind, newValue); | |
break; | |
} | |
} | |
if (i == ptrV.end()) cout << "Not Found" << endl; | |
} | |
if (cmd == "Exit") return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment