長いコードですいません、
このコードの実用性に疑問を感じました。
retrieveItemなどのアルゴリズムは工夫してあると思うのですが、5のアイテムが欲しかったら、いったんinitializeしてから、retriveitemするのはなんだか面倒くさいなと感じてしまいました。
このコードでItemtype にそれぞれ別名をつけていくのはありだと思いますか?(Itemtype a,b,c,d,eといったように)
なんだかアルゴリズム自体はしっかりしているだけに
実際使ってみるとがっかりしました。
実際の現場でも同じようなコードが使われているのでしょうか?それとも学生用に単純にしているのでしょうか?
コード #include"Sortedlist.h" #include<iostream> using namespace std; int main() { Sortedlist sl; ItemType i; i.Initialize(3); sl.InsertItem(i); i.Initialize(5); sl.InsertItem(i); i.Initialize(7); sl.InsertItem(i); i.Initialize(11); sl.InsertItem(i); bool found = false; i.Initialize(7); sl.RetrieveItem(i, found); cout << found << endl; cout << sl.LengthIs() << endl; sl.DeleteItem(i); cout << sl.LengthIs() << endl; }
コード /* * ItemType.cpp * * Created on: 2015/11/18 * Author: 礼央 */ #include"ItemType.h" #include<iostream> using namespace std; ItemType::ItemType(){ this->value=0; } RelationType ItemType::ComparedTo(ItemType otheritem){ if(value<otheritem.value) return LESS; else if(value>otheritem.value) return GREATER; else return EQUAL; } void ItemType::Initialize(int value){ this->value=value; } void ItemType::print() const{ cout<<this->value<<endl; } ItemType::~ItemType(){ this->value=0; }
コード /* * Sortedlist.cpp * * Created on: 2015/11/19 * Author: 礼央 */ #include"Sortedlist.h" #include"ItemType.h" using namespace std; Sortedlist::Sortedlist() { length = 0; currentPos = 0; } ; void Sortedlist::makeEmpty() { length = 0; } ; bool Sortedlist::IsFull() { return (length = MAX_ITEMS); } ; int Sortedlist::LengthIs() { return length; } ; void Sortedlist::RetrieveItem(ItemType &item, bool &found) { int midPoint; int first = 0; int last = length - 1; bool moreToSearch = first <= last; found = false; while (moreToSearch && !found) { midPoint = (first + last) / 2; switch (item.ComparedTo(info[midPoint])) { case LESS: last = midPoint - 1; moreToSearch = first <= last; break; case GREATER: first = midPoint + 1; moreToSearch = first <= last; break; case EQUAL: found = true; item = info[midPoint]; break; } } } ; void Sortedlist::InsertItem(ItemType item) { bool moreToSearch; int location = 0; moreToSearch = (location < length); while (moreToSearch) { switch (item.ComparedTo(info[location])) { case LESS: moreToSearch = false; break; case GREATER: location++; moreToSearch = (location < length); break; case EQUAL: break; } } for (int index = length; index > location; index--) info[index] = info[index - 1]; info[location] = item; length++; } ; void Sortedlist::DeleteItem(ItemType item) { int location = 0; while (item.ComparedTo(info[location]) != EQUAL) location++; for (int index = location + 1; index < length; index++) info[index - 1] = info[index]; length--; } ; void Sortedlist::ResetList(){ currentPos=-1; }; void Sortedlist::getNextItem(ItemType &item){ currentPos++; item=info[currentPos]; } ; Sortedlist::~Sortedlist() { length = 0; currentPos = 0; };
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/29 01:36
2015/11/29 04:41