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

@ -8,20 +8,26 @@ using std::cout;
using std::endl; using std::endl;
class User { 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() {
User(const User& u) =default; cardNbr = -2;
User& operator=(const User&) =default; name = "--------------------";
int getCardNbr() const {return cardNbr;} } // overwrite values for security reasons
std::string getName() const {return name;} User(const User &u) = default;
bool operator==(const User& u) const {return cardNbr == u.cardNbr && name == u.name;} User &operator=(const User &) = default;
bool operator!=(const User& u) const {return ! (u == *this);} int getCardNbr() const { return cardNbr; }
private: 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 !(u == *this); }
private:
int cardNbr; int cardNbr;
std::string name; std::string name;
}; };
std::ostream& operator<<(std::ostream& os, const User& u); std::ostream &operator<<(std::ostream &os, const User &u);
#endif #endif

View file

@ -1,24 +1,22 @@
#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()) {
while(ufile) { while (ufile) {
int cn; int cn;
if(ufile >> cn ) { if (ufile >> cn) {
ufile.ignore(); // skip space ufile.ignore(); // skip space
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,34 +24,32 @@ 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
int pos{0}; int pos{0};
while ( (pos < n) && (users[pos].getCardNbr() < u.getCardNbr())){ while ((pos < n) && (users[pos].getCardNbr() < u.getCardNbr())) {
++pos; ++pos;
} }
//2. skapa lucka i vektorn // 2. skapa lucka i vektorn
for(int i=n; i > pos; --i){ for (int i = n; i > pos; --i) {
users[i] = users[i-1]; users[i] = users[i - 1];
} }
//3. stoppa in den nya användaren i luckan // 3. stoppa in den nya användaren i luckan
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;
int high = n - 1; int high = n - 1;
int mid = -1; int mid = -1;
bool found = false; bool found = false;
while (low < high && ! found) { while (low < high && !found) {
mid = (low + high) / 2; mid = (low + high) / 2;
// //
const int midnbr = users[mid].getCardNbr(); const int midnbr = users[mid].getCardNbr();
@ -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,36 +75,32 @@ 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;
} }
auto tmp = new User[capacity]; auto tmp = new User[capacity];
std::copy(users, users+n, tmp); std::copy(users, users + n, tmp);
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];
os << "(" <<u.getCardNbr() << ") " << u.getName() << std::endl; os << "(" << u.getCardNbr() << ") " << u.getName() << std::endl;
} }
os << "=============" << std::endl; os << "=============" << std::endl;
} }
/** /**
* Testmetod för binärsökningen: * Testmetod för binärsökningen:
* går igenom alla användare och kollar att deras kortnummer kan sökas upp. * går igenom alla användare och kollar att deras kortnummer kan sökas upp.
* 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

@ -5,28 +5,27 @@
#include "User.h" #include "User.h"
class UserTable{ class UserTable {
public: public:
UserTable(); UserTable();
UserTable(const std::string&); UserTable(const std::string &);
~UserTable() {delete[] users;} ~UserTable() { delete[] users; }
void addUser(const User&); void addUser(const User &);
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);
int n{0}; int n{0};
User* users; User *users;
friend int testFindNbr(const UserTable ut); friend int testFindNbr(const UserTable ut);
}; };

View file

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