mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-24 10:33:10 +08:00
replace using std::cin, etc
This commit is contained in:
@@ -7,14 +7,8 @@
|
|||||||
* @note The implementation can be optimized by using OOP style.
|
* @note The implementation can be optimized by using OOP style.
|
||||||
*/
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using std::cin;
|
|
||||||
using std::cout;
|
|
||||||
using std::endl;
|
|
||||||
using std::string;
|
|
||||||
|
|
||||||
namespace { // keep the code local to this file by assigning them to an unnamed
|
namespace { // keep the code local to this file by assigning them to an unnamed
|
||||||
// namespace
|
// namespace
|
||||||
|
|
||||||
@@ -65,25 +59,26 @@ int linearProbe(int key, bool searching) {
|
|||||||
return notPresent;
|
return notPresent;
|
||||||
}
|
}
|
||||||
if (searchingProber(entry, key)) {
|
if (searchingProber(entry, key)) {
|
||||||
cout << "Found key!" << endl;
|
std::cout << "Found key!" << std::endl;
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
cout << "Found tombstone or equal hash, checking next" << endl;
|
std::cout << "Found tombstone or equal hash, checking next"
|
||||||
|
<< std::endl;
|
||||||
i++;
|
i++;
|
||||||
} else {
|
} else {
|
||||||
if (putProber(entry, key)) {
|
if (putProber(entry, key)) {
|
||||||
if (!rehashing) {
|
if (!rehashing) {
|
||||||
cout << "Spot found!" << endl;
|
std::cout << "Spot found!" << std::endl;
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
if (!rehashing) {
|
if (!rehashing) {
|
||||||
cout << "Spot taken, looking at next" << endl;
|
std::cout << "Spot taken, looking at next" << std::endl;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (i == totalSize) {
|
if (i == totalSize) {
|
||||||
cout << "Linear probe failed" << endl;
|
std::cout << "Linear probe failed" << std::endl;
|
||||||
return notPresent;
|
return notPresent;
|
||||||
}
|
}
|
||||||
} while (entry.key != notPresent);
|
} while (entry.key != notPresent);
|
||||||
@@ -120,16 +115,16 @@ bool searchingProber(const Entry& entry, int key) {
|
|||||||
void display() {
|
void display() {
|
||||||
for (int i = 0; i < totalSize; i++) {
|
for (int i = 0; i < totalSize; i++) {
|
||||||
if (table[i].key == notPresent) {
|
if (table[i].key == notPresent) {
|
||||||
cout << " Empty ";
|
std::cout << " Empty ";
|
||||||
} else if (table[i].key == tomb) {
|
} else if (table[i].key == tomb) {
|
||||||
cout << " Tomb ";
|
std::cout << " Tomb ";
|
||||||
} else {
|
} else {
|
||||||
cout << " ";
|
std::cout << " ";
|
||||||
cout << table[i].key;
|
std::cout << table[i].key;
|
||||||
cout << " ";
|
std::cout << " ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Rehashes the table into a bigger table
|
/** Rehashes the table into a bigger table
|
||||||
@@ -152,7 +147,7 @@ void rehash() {
|
|||||||
}
|
}
|
||||||
// delete[] oldTable;
|
// delete[] oldTable;
|
||||||
rehashing = false;
|
rehashing = false;
|
||||||
cout << "Table was rehashed, new size is: " << totalSize << endl;
|
std::cout << "Table was rehashed, new size is: " << totalSize << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Adds entry using linear probing. Checks for load factor here
|
/** Adds entry using linear probing. Checks for load factor here
|
||||||
@@ -173,9 +168,9 @@ void add(int key) {
|
|||||||
void remove(int key) {
|
void remove(int key) {
|
||||||
int index = linearProbe(key, true);
|
int index = linearProbe(key, true);
|
||||||
if (index == notPresent) {
|
if (index == notPresent) {
|
||||||
cout << "key not found" << endl;
|
std::cout << "key not found" << std::endl;
|
||||||
}
|
}
|
||||||
cout << "Removal Successful, leaving tomb" << endl;
|
std::cout << "Removal Successful, leaving tomb" << std::endl;
|
||||||
table[index].key = tomb;
|
table[index].key = tomb;
|
||||||
size--;
|
size--;
|
||||||
}
|
}
|
||||||
@@ -184,14 +179,14 @@ void remove(int key) {
|
|||||||
* @param key key value to hash and add
|
* @param key key value to hash and add
|
||||||
*/
|
*/
|
||||||
void addInfo(int key) {
|
void addInfo(int key) {
|
||||||
cout << "Initial table: ";
|
std::cout << "Initial table: ";
|
||||||
display();
|
display();
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
|
std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
|
||||||
<< " == " << hashFxn(key) % totalSize;
|
<< totalSize << " == " << hashFxn(key) % totalSize;
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
add(key);
|
add(key);
|
||||||
cout << "New table: ";
|
std::cout << "New table: ";
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,14 +194,14 @@ void addInfo(int key) {
|
|||||||
* @param key key value to hash and remove
|
* @param key key value to hash and remove
|
||||||
*/
|
*/
|
||||||
void removalInfo(int key) {
|
void removalInfo(int key) {
|
||||||
cout << "Initial table: ";
|
std::cout << "Initial table: ";
|
||||||
display();
|
display();
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
|
std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
|
||||||
<< " == " << hashFxn(key) % totalSize;
|
<< totalSize << " == " << hashFxn(key) % totalSize;
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
remove(key);
|
remove(key);
|
||||||
cout << "New table: ";
|
std::cout << "New table: ";
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -216,45 +211,45 @@ void removalInfo(int key) {
|
|||||||
*/
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
int cmd = 0, hash = 0, key = 0;
|
int cmd = 0, hash = 0, key = 0;
|
||||||
cout << "Enter the initial size of Hash Table. = ";
|
std::cout << "Enter the initial size of Hash Table. = ";
|
||||||
cin >> totalSize;
|
std::cin >> totalSize;
|
||||||
table = std::vector<Entry>(totalSize);
|
table = std::vector<Entry>(totalSize);
|
||||||
bool loop = true;
|
bool loop = true;
|
||||||
while (loop) {
|
while (loop) {
|
||||||
system("pause");
|
system("pause");
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
cout << "PLEASE CHOOSE -" << endl;
|
std::cout << "PLEASE CHOOSE -" << std::endl;
|
||||||
cout << "1. Add key. (Numeric only)" << endl;
|
std::cout << "1. Add key. (Numeric only)" << std::endl;
|
||||||
cout << "2. Remove key." << endl;
|
std::cout << "2. Remove key." << std::endl;
|
||||||
cout << "3. Find key." << endl;
|
std::cout << "3. Find key." << std::endl;
|
||||||
cout << "4. Generate Hash. (Numeric only)" << endl;
|
std::cout << "4. Generate Hash. (Numeric only)" << std::endl;
|
||||||
cout << "5. Display Hash table." << endl;
|
std::cout << "5. Display Hash table." << std::endl;
|
||||||
cout << "6. Exit." << endl;
|
std::cout << "6. Exit." << std::endl;
|
||||||
cin >> cmd;
|
std::cin >> cmd;
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case 1:
|
case 1:
|
||||||
cout << "Enter key to add = ";
|
std::cout << "Enter key to add = ";
|
||||||
cin >> key;
|
std::cin >> key;
|
||||||
addInfo(key);
|
addInfo(key);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
cout << "Enter key to remove = ";
|
std::cout << "Enter key to remove = ";
|
||||||
cin >> key;
|
std::cin >> key;
|
||||||
removalInfo(key);
|
removalInfo(key);
|
||||||
break;
|
break;
|
||||||
case 3: {
|
case 3: {
|
||||||
cout << "Enter key to search = ";
|
std::cout << "Enter key to search = ";
|
||||||
cin >> key;
|
std::cin >> key;
|
||||||
Entry entry = table[linearProbe(key, true)];
|
Entry entry = table[linearProbe(key, true)];
|
||||||
if (entry.key == notPresent) {
|
if (entry.key == notPresent) {
|
||||||
cout << "Key not present";
|
std::cout << "Key not present";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 4:
|
case 4:
|
||||||
cout << "Enter element to generate hash = ";
|
std::cout << "Enter element to generate hash = ";
|
||||||
cin >> key;
|
std::cin >> key;
|
||||||
cout << "Hash of " << key << " is = " << hashFxn(key);
|
std::cout << "Hash of " << key << " is = " << hashFxn(key);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
display();
|
display();
|
||||||
@@ -264,7 +259,7 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
// delete[] table;
|
// delete[] table;
|
||||||
}
|
}
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,8 @@
|
|||||||
*/
|
*/
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using std::cin;
|
|
||||||
using std::cout;
|
|
||||||
using std::endl;
|
|
||||||
using std::string;
|
|
||||||
|
|
||||||
namespace { // keep the code local to this file by assigning them to an unnamed
|
namespace { // keep the code local to this file by assigning them to an unnamed
|
||||||
|
|
||||||
// fwd declarations
|
// fwd declarations
|
||||||
@@ -59,29 +53,30 @@ int quadraticProbe(int key, bool searching) {
|
|||||||
return notPresent;
|
return notPresent;
|
||||||
}
|
}
|
||||||
if (searchingProber(entry, key)) {
|
if (searchingProber(entry, key)) {
|
||||||
cout << "Found key!" << endl;
|
std::cout << "Found key!" << std::endl;
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
cout << "Found tombstone or equal hash, checking next" << endl;
|
std::cout << "Found tombstone or equal hash, checking next"
|
||||||
|
<< std::endl;
|
||||||
i++;
|
i++;
|
||||||
} else {
|
} else {
|
||||||
if (putProber(entry, key)) {
|
if (putProber(entry, key)) {
|
||||||
if (!rehashing) {
|
if (!rehashing) {
|
||||||
cout << "Spot found!" << endl;
|
std::cout << "Spot found!" << std::endl;
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
if (!rehashing) {
|
if (!rehashing) {
|
||||||
cout << "Spot taken, looking at next (next index = "
|
std::cout << "Spot taken, looking at next (next index = "
|
||||||
<< (hash +
|
<< (hash + static_cast<size_t>(
|
||||||
static_cast<size_t>(std::round(std::pow(i + 1, 2)))) %
|
std::round(std::pow(i + 1, 2)))) %
|
||||||
totalSize
|
totalSize
|
||||||
<< endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (i == totalSize * 100) {
|
if (i == totalSize * 100) {
|
||||||
cout << "Quadratic probe failed (infinite loop)" << endl;
|
std::cout << "Quadratic probe failed (infinite loop)" << std::endl;
|
||||||
return notPresent;
|
return notPresent;
|
||||||
}
|
}
|
||||||
} while (entry.key != notPresent);
|
} while (entry.key != notPresent);
|
||||||
@@ -117,16 +112,16 @@ Entry find(int key) {
|
|||||||
void display() {
|
void display() {
|
||||||
for (int i = 0; i < totalSize; i++) {
|
for (int i = 0; i < totalSize; i++) {
|
||||||
if (table[i].key == notPresent) {
|
if (table[i].key == notPresent) {
|
||||||
cout << " Empty ";
|
std::cout << " Empty ";
|
||||||
} else if (table[i].key == tomb) {
|
} else if (table[i].key == tomb) {
|
||||||
cout << " Tomb ";
|
std::cout << " Tomb ";
|
||||||
} else {
|
} else {
|
||||||
cout << " ";
|
std::cout << " ";
|
||||||
cout << table[i].key;
|
std::cout << table[i].key;
|
||||||
cout << " ";
|
std::cout << " ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rehashes the table into a bigger table
|
// Rehashes the table into a bigger table
|
||||||
@@ -146,7 +141,7 @@ void rehash() {
|
|||||||
}
|
}
|
||||||
// delete[] oldTable;
|
// delete[] oldTable;
|
||||||
rehashing = false;
|
rehashing = false;
|
||||||
cout << "Table was rehashed, new size is: " << totalSize << endl;
|
std::cout << "Table was rehashed, new size is: " << totalSize << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks for load factor here
|
// Checks for load factor here
|
||||||
@@ -163,36 +158,36 @@ void add(int key) {
|
|||||||
void remove(int key) {
|
void remove(int key) {
|
||||||
int index = quadraticProbe(key, true);
|
int index = quadraticProbe(key, true);
|
||||||
if (index == notPresent) {
|
if (index == notPresent) {
|
||||||
cout << "key not found" << endl;
|
std::cout << "key not found" << std::endl;
|
||||||
}
|
}
|
||||||
table[index].key = tomb;
|
table[index].key = tomb;
|
||||||
cout << "Removal successful, leaving tombstone" << endl;
|
std::cout << "Removal successful, leaving tombstone" << std::endl;
|
||||||
size--;
|
size--;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Information about the adding process
|
// Information about the adding process
|
||||||
void addInfo(int key) {
|
void addInfo(int key) {
|
||||||
cout << "Initial table: ";
|
std::cout << "Initial table: ";
|
||||||
display();
|
display();
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
|
std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
|
||||||
<< " == " << hashFxn(key) % totalSize;
|
<< totalSize << " == " << hashFxn(key) % totalSize;
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
add(key);
|
add(key);
|
||||||
cout << "New table: ";
|
std::cout << "New table: ";
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Information about removal process
|
// Information about removal process
|
||||||
void removalInfo(int key) {
|
void removalInfo(int key) {
|
||||||
cout << "Initial table: ";
|
std::cout << "Initial table: ";
|
||||||
display();
|
display();
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
|
std::cout << "hash of " << key << " is " << hashFxn(key) << " % "
|
||||||
<< " == " << hashFxn(key) % totalSize;
|
<< totalSize << " == " << hashFxn(key) % totalSize;
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
remove(key);
|
remove(key);
|
||||||
cout << "New table: ";
|
std::cout << "New table: ";
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -200,45 +195,45 @@ void removalInfo(int key) {
|
|||||||
// I/O
|
// I/O
|
||||||
int main() {
|
int main() {
|
||||||
int cmd = 0, hash = 0, key = 0;
|
int cmd = 0, hash = 0, key = 0;
|
||||||
cout << "Enter the initial size of Hash Table. = ";
|
std::cout << "Enter the initial size of Hash Table. = ";
|
||||||
cin >> totalSize;
|
std::cin >> totalSize;
|
||||||
table = std::vector<Entry>(totalSize);
|
table = std::vector<Entry>(totalSize);
|
||||||
bool loop = true;
|
bool loop = true;
|
||||||
while (loop) {
|
while (loop) {
|
||||||
system("pause");
|
system("pause");
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
cout << "PLEASE CHOOSE -" << endl;
|
std::cout << "PLEASE CHOOSE -" << std::endl;
|
||||||
cout << "1. Add key. (Numeric only)" << endl;
|
std::cout << "1. Add key. (Numeric only)" << std::endl;
|
||||||
cout << "2. Remove key." << endl;
|
std::cout << "2. Remove key." << std::endl;
|
||||||
cout << "3. Find key." << endl;
|
std::cout << "3. Find key." << std::endl;
|
||||||
cout << "4. Generate Hash. (Numeric only)" << endl;
|
std::cout << "4. Generate Hash. (Numeric only)" << std::endl;
|
||||||
cout << "5. Display Hash table." << endl;
|
std::cout << "5. Display Hash table." << std::endl;
|
||||||
cout << "6. Exit." << endl;
|
std::cout << "6. Exit." << std::endl;
|
||||||
cin >> cmd;
|
std::cin >> cmd;
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case 1:
|
case 1:
|
||||||
cout << "Enter key to add = ";
|
std::cout << "Enter key to add = ";
|
||||||
cin >> key;
|
std::cin >> key;
|
||||||
addInfo(key);
|
addInfo(key);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
cout << "Enter key to remove = ";
|
std::cout << "Enter key to remove = ";
|
||||||
cin >> key;
|
std::cin >> key;
|
||||||
removalInfo(key);
|
removalInfo(key);
|
||||||
break;
|
break;
|
||||||
case 3: {
|
case 3: {
|
||||||
cout << "Enter key to search = ";
|
std::cout << "Enter key to search = ";
|
||||||
cin >> key;
|
std::cin >> key;
|
||||||
Entry entry = table[quadraticProbe(key, true)];
|
Entry entry = table[quadraticProbe(key, true)];
|
||||||
if (entry.key == notPresent) {
|
if (entry.key == notPresent) {
|
||||||
cout << "Key not present";
|
std::cout << "Key not present";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 4:
|
case 4:
|
||||||
cout << "Enter element to generate hash = ";
|
std::cout << "Enter element to generate hash = ";
|
||||||
cin >> key;
|
std::cin >> key;
|
||||||
cout << "Hash of " << key << " is = " << hashFxn(key);
|
std::cout << "Hash of " << key << " is = " << hashFxn(key);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
display();
|
display();
|
||||||
@@ -248,7 +243,7 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
// delete[] table;
|
// delete[] table;
|
||||||
}
|
}
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user