This commit is contained in:
Imbus 2024-11-27 17:25:14 +01:00
parent 8abebb692a
commit 6130bdee66
5 changed files with 65 additions and 72 deletions

View file

@ -1,6 +1,5 @@
#include "User.h" #include "User.h"
std::ostream& operator<<(std::ostream& os, const User& u) std::ostream &operator<<(std::ostream &os, const User &u) {
{
return os << "(" << u.getCardNbr() << ") " << u.getName(); return os << "(" << u.getCardNbr() << ") " << u.getName();
} }

View file

@ -11,13 +11,19 @@ class User {
public: public:
User() : cardNbr{0}, name{"default"} {} User() : cardNbr{0}, name{"default"} {}
User(int c, std::string n) : cardNbr{c}, name{n} {} User(int c, std::string n) : cardNbr{c}, name{n} {}
~User() {cardNbr=-2; name="--------------------";} // overwrite values for security reasons ~User() {
cardNbr = -2;
name = "--------------------";
} // overwrite values for security reasons
User(const User &u) = default; User(const User &u) = default;
User &operator=(const User &) = default; User &operator=(const User &) = default;
int getCardNbr() const { return cardNbr; } int getCardNbr() const { return cardNbr; }
std::string getName() const { return name; } std::string getName() const { return name; }
bool operator==(const User& u) const {return cardNbr == u.cardNbr && name == u.name;} bool operator==(const User &u) const {
return cardNbr == u.cardNbr && name == u.name;
}
bool operator!=(const User &u) const { return !(u == *this); } bool operator!=(const User &u) const { return !(u == *this); }
private: private:
int cardNbr; int cardNbr;
std::string name; std::string name;

View file

@ -1,13 +1,12 @@
#include "UserTable.h" #include "UserTable.h"
#include <fstream>
#include <algorithm> #include <algorithm>
#include <fstream>
const User UserTable::user_not_found = User{-1, "Not Found"}; const User UserTable::user_not_found = User{-1, "Not Found"};
UserTable::UserTable() : users{new User[capacity]} {} UserTable::UserTable() : users{new User[capacity]} {}
UserTable::UserTable(const std::string& fname) :UserTable{} UserTable::UserTable(const std::string &fname) : UserTable{} {
{
std::ifstream ufile(fname); std::ifstream ufile(fname);
if (ufile.is_open()) { if (ufile.is_open()) {
@ -18,7 +17,6 @@ UserTable::UserTable(const std::string& fname) :UserTable{}
char n[80]; char n[80];
ufile.getline(n, 80); ufile.getline(n, 80);
addUser(User(cn, n)); addUser(User(cn, n));
} }
} }
} else { } else {
@ -26,8 +24,7 @@ UserTable::UserTable(const std::string& fname) :UserTable{}
} }
} }
void UserTable::addUser(const User& u) void UserTable::addUser(const User &u) {
{
// gör tabellen större vid behov // gör tabellen större vid behov
ensureCapacity(n + 1); ensureCapacity(n + 1);
// 1. Hitta rätt plats // 1. Hitta rätt plats
@ -45,8 +42,7 @@ void UserTable::addUser(const User& u)
users[pos] = u; users[pos] = u;
} }
User UserTable::find(int c) const User UserTable::find(int c) const {
{
// binärsökning (baserad på Holm, 2007) // binärsökning (baserad på Holm, 2007)
int low = 0; int low = 0;
@ -68,8 +64,7 @@ User UserTable::find(int c) const
return found ? users[mid] : user_not_found; return found ? users[mid] : user_not_found;
} }
User UserTable::find(std::string name) const User UserTable::find(std::string name) const {
{
for (int i = 0; i != n; ++i) { for (int i = 0; i != n; ++i) {
if (users[i].getName() == name) { if (users[i].getName() == name) {
return users[i]; return users[i];
@ -80,8 +75,7 @@ User UserTable::find(std::string name) const
return user_not_found; return user_not_found;
} }
void UserTable::ensureCapacity(int s) void UserTable::ensureCapacity(int s) {
{
if (s > capacity) { if (s > capacity) {
while (s > capacity) { while (s > capacity) {
capacity *= 4; capacity *= 4;
@ -91,10 +85,8 @@ void UserTable::ensureCapacity(int s)
delete[] users; delete[] users;
users = tmp; users = tmp;
} }
} }
void UserTable::print(std::ostream& os) const void UserTable::print(std::ostream &os) const {
{
os << "-------------" << std::endl; os << "-------------" << std::endl;
for (int i = 0; i != getNbrUsers(); ++i) { for (int i = 0; i != getNbrUsers(); ++i) {
const auto &u = users[i]; const auto &u = users[i];
@ -108,8 +100,7 @@ void UserTable::print(std::ostream& os) const
* Om något kortnummer inte kunde sökas upp returneras detta. Annars, om * Om något kortnummer inte kunde sökas upp returneras detta. Annars, om
* alla sökningar lyckades, returneras 0. * alla sökningar lyckades, returneras 0.
*/ */
int testFindNbr(const UserTable ut) int testFindNbr(const UserTable ut) {
{
for (int i = 0; i < ut.n; i++) { for (int i = 0; i < ut.n; i++) {
int nbr = ut.users[i].getCardNbr(); int nbr = ut.users[i].getCardNbr();
User found = ut.find(nbr); User found = ut.find(nbr);
@ -119,4 +110,3 @@ int testFindNbr(const UserTable ut)
} }
return 0; return 0;
} }

View file

@ -15,13 +15,12 @@ public:
User find(int) const; User find(int) const;
User find(std::string) const; User find(std::string) const;
int getNbrUsers() const { int getNbrUsers() const { return n; }
return n;
}
void print(std::ostream &) const; void print(std::ostream &) const;
static const User user_not_found; static const User user_not_found;
private: private:
int capacity{1000}; int capacity{1000};
void ensureCapacity(int); void ensureCapacity(int);

View file

@ -19,4 +19,3 @@ int main() {
UserTable ut1 = UserTable("users.txt"); UserTable ut1 = UserTable("users.txt");
} }