Notice
Recent Posts
Recent Comments
Link
할껀하고놀자
[자료구조] C++로 Unsorted ArrayList 만들기 본문
728x90
2018.02.21 개발일지
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #include "Application.h" Application::Application() { } Application::~Application() { } void Application::Run() { while (1) { m_Command = GetCommand(); switch (m_Command) { case 1: // read a record and add to list. AddItem(); break; case 2: // display all the records in list on screen. DisplayAllItem(); break; case 3: // make empty list. EmptyList(); break; case 4: // search item. SearchItem(); break; case 5: // delete item. DeleteItem(); break; case 6: // Replace item. ReplaceItem(); break; case 7: // load list data from a file. ReadDataFromFile(); break; case 8: // save list data into a file. WriteDataToFile(); break; case 0: return; default: cout << "\tIllegal selection...\n"; break; } } } int Application::GetCommand() { int command; cout << endl << endl; cout << "1. Add Item" << endl; cout << "2. Display All Items" << endl; cout << "3. Make Empty" << endl; cout << "4. Search Item" << endl; cout << "5. Delete Item" << endl; cout << "6. Replace Item" << endl; cout << "7. Read Data From File" << endl; cout << "8. Put To File" << endl; cout << "0. Quit" << endl; cout << endl << "choose a Command->"; cin >> command; cout << endl; return command; } //1번 내용 void Application::AddItem() { cout << "Let's Add Item!" << endl; m_List.AddItem(); } //2번 내용 void Application::DisplayAllItem() { cout << "Let's Display All Items!" << endl; m_List.DisplayAllItem(); } //3번 내용 void Application::EmptyList() { cout << "Let's Make Empty List!" << endl; m_List.MakeEmpty(); } //4번 내용 void Application::SearchItem() { cout << "Let's Search Item!" << endl; m_List.Search(); } //5번 내용 void Application::DeleteItem() { cout << "Let's Delete Item!" << endl; m_List.Delete(); } //6번 내용 void Application::ReplaceItem() { cout << "Let's Replace Item!" << endl; m_List.Replace(); } //7번 내용 void Application::ReadDataFromFile() { cout << "Let's Read Data From File!" << endl; m_List.ReadData(); } //8번 내용 void Application::WriteDataToFile() { cout << "Let's Write Data To File!" << endl; m_List.WriteData(); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | #include "ArrayList.h" ArrayList::ArrayList() { m_NumOfData = 0; } ArrayList::~ArrayList() { } bool ArrayList::IsFull() { if (m_NumOfData == MAXSIZE) { cout << "List is Full!" << endl; return TRUE; } else { return FALSE; } } int ArrayList::AddItem() { ItemType data; data.SetDataFormKB(); if(!IsFull()) { m_Array[m_NumOfData] = data; m_NumOfData++; cout << data.GetData()<<" Insert Successfully!" << endl; return TRUE; } else { cout << "List is FUll!!" << endl; return FALSE; } } void ArrayList::DisplayAllItem() { ItemType data; ResetCurPoint(); int curIndex = GetNextItem(data); if (!m_NumOfData) { cout << "\nThere's nothing to show List in this array!" << endl; } while (curIndex < m_NumOfData && curIndex != -1) { cout << "데이터 출력 : " << data.GetData() << endl; curIndex = GetNextItem(data); } } void ArrayList::ResetCurPoint() { m_CurPointer = -1; } int ArrayList::GetNextItem(ItemType& data) { m_CurPointer++; if (m_CurPointer == MAXSIZE) { return -1; } else{ data = m_Array[m_CurPointer]; return m_CurPointer; } } void ArrayList::MakeEmpty() { m_NumOfData = 0; } void ArrayList::Search() { ItemType temp; if (Get(temp)) cout << "->" << temp.GetData() << endl; } bool ArrayList::Get(ItemType& data) { data.SetDataFormKB(); for (int i = 0; i < m_NumOfData; i++) { if (data.GetData() == m_Array[i].GetData()) { data = m_Array[i]; cout << "Get Data Successfully!"; return TRUE; } } cout << "Fail to find Data!"; return FALSE; } void ArrayList::Delete() { ItemType data; if (Get(data)){ cout << " Delete Data->" << data.GetData() << endl; for (int i = 0; i < MAXSIZE; i++) { if (data.GetData() == m_Array[i].GetData()) { if (i == MAXSIZE-1) { m_NumOfData--; //배열 갯수 5개면 0~4배열이 형성된다. i가 4이면 //맨 끝이기 때문에 걍 숫자 하나만 줄여주면 된다. } //하지만 2,3위치에 있는 경우 뒷 배열을 다 끌어와야한다. 그래서 맥스사이즈 //까지 배열을 땡겨주는 작업을 거친다. else { for (i; i < MAXSIZE; i++) { m_Array[i] = m_Array[i + 1]; } m_NumOfData--; } } } } else { cout << " There's no data in this array!" << endl; } } void ArrayList::Replace() { ItemType data; if (Get(data)) { cout << "Replace \"" << data.GetData() << "\" data!" << endl; for (int i = 0; i < MAXSIZE; i++) { if (data.GetData() == m_Array[i].GetData()) { cout << endl; m_Array->SetDataFormKB(); } } cout << "Replace successfully!" << endl << endl; } } //수정 필요함. 파일 안의 내용 완전히 안가져와짐 //수정 완료! bool ArrayList::ReadData() { int index = 0; ItemType data; char filename[1000]; cout << "\n\nEnter input file name : "; cin >> filename; m_InFile.open(filename); if (m_InFile.is_open()) { while (!m_InFile.eof()) { data.ReadDataFromFile(m_InFile); if (!IsFull()) { m_Array[m_NumOfData] = data; m_NumOfData++; cout << data.GetData() << " Insert Successfully!" << endl; return TRUE; //return true; 를 삭제함. 이부분에서 아예 함수를 다 빠져나가버리는 현상 발생. } else { cout << "List is FUll!!" << endl; return FALSE; } } m_InFile.close(); return 1; } else{ return 0; } } bool ArrayList::WriteData() { ItemType data; char filename[1000]; cout << "\n\nEnter Output file name : "; cin >> filename; m_OutFile.open(filename); if (m_OutFile.is_open()) { ResetCurPoint(); int curIndex = GetNextItem(data); while (curIndex < m_NumOfData && curIndex != -1) { data.WriteDataToFile(m_OutFile); curIndex = GetNextItem(data); } m_OutFile.close(); // file close return 1; } else { return 0; } return 1; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include "ItemType.h" ItemType::ItemType() { } ItemType::~ItemType() { } void ItemType::SetDataFormKB() { cout << "Input Data->"; cin >> data; } int ItemType::GetData() { return data; } int ItemType::ReadDataFromFile(ifstream& fin) { fin >> data; return 1; } int ItemType::WriteDataToFile(ofstream& fout) { fout << endl; fout << data; return 1; } | cs |
4~8번까지 구현 완료.
'[IT] > Data Structure' 카테고리의 다른 글
[자료구조] C++로 Sorted ArrayList 만들기 (0) | 2018.02.22 |
---|---|
[자료구조] C++로 정렬되지 않은 배열리스트 만들기 (0) | 2018.02.20 |
Comments