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"
std::ostream& operator<<(std::ostream& os, const User& u)
{
return os << "(" << u.getCardNbr() << ") "<< u.getName();
std::ostream &operator<<(std::ostream &os, const User &u) {
return os << "(" << u.getCardNbr() << ") " << u.getName();
}

View file

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

View file

@ -1,24 +1,22 @@
#include "UserTable.h"
#include <fstream>
#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);
if(ufile.is_open()) {
while(ufile) {
if (ufile.is_open()) {
while (ufile) {
int cn;
if(ufile >> cn ) {
if (ufile >> cn) {
ufile.ignore(); // skip space
char n[80];
ufile.getline(n,80);
addUser(User(cn,n));
ufile.getline(n, 80);
addUser(User(cn, n));
}
}
} 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
ensureCapacity(n+1);
ensureCapacity(n + 1);
// 1. Hitta rätt plats
int pos{0};
while ( (pos < n) && (users[pos].getCardNbr() < u.getCardNbr())){
while ((pos < n) && (users[pos].getCardNbr() < u.getCardNbr())) {
++pos;
}
//2. skapa lucka i vektorn
for(int i=n; i > pos; --i){
users[i] = users[i-1];
// 2. skapa lucka i vektorn
for (int i = n; i > pos; --i) {
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;
}
User UserTable::find(int c) const
{
User UserTable::find(int c) const {
// binärsökning (baserad på Holm, 2007)
int low = 0;
int high = n - 1;
int mid = -1;
bool found = false;
while (low < high && ! found) {
while (low < high && !found) {
mid = (low + high) / 2;
//
const int midnbr = users[mid].getCardNbr();
@ -68,8 +64,7 @@ User UserTable::find(int c) const
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) {
if (users[i].getName() == name) {
return users[i];
@ -80,36 +75,32 @@ User UserTable::find(std::string name) const
return user_not_found;
}
void UserTable::ensureCapacity(int s)
{
if(s>capacity) {
while(s > capacity) {
capacity*=4;
void UserTable::ensureCapacity(int s) {
if (s > capacity) {
while (s > capacity) {
capacity *= 4;
}
auto tmp = new User[capacity];
std::copy(users, users+n, tmp);
std::copy(users, users + n, tmp);
delete[] users;
users=tmp;
users = tmp;
}
}
void UserTable::print(std::ostream& os) const
{
void UserTable::print(std::ostream &os) const {
os << "-------------" << std::endl;
for(int i = 0; i != getNbrUsers(); ++i) {
const auto& u = users[i];
os << "(" <<u.getCardNbr() << ") " << u.getName() << std::endl;
for (int i = 0; i != getNbrUsers(); ++i) {
const auto &u = users[i];
os << "(" << u.getCardNbr() << ") " << u.getName() << std::endl;
}
os << "=============" << std::endl;
}
/**
* Testmetod för binärsökningen:
* 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
* alla sökningar lyckades, returneras 0.
*/
int testFindNbr(const UserTable ut)
{
* Testmetod för binärsökningen:
* 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
* alla sökningar lyckades, returneras 0.
*/
int testFindNbr(const UserTable ut) {
for (int i = 0; i < ut.n; i++) {
int nbr = ut.users[i].getCardNbr();
User found = ut.find(nbr);
@ -119,4 +110,3 @@ int testFindNbr(const UserTable ut)
}
return 0;
}

View file

@ -5,28 +5,27 @@
#include "User.h"
class UserTable{
public:
class UserTable {
public:
UserTable();
UserTable(const std::string&);
~UserTable() {delete[] users;}
UserTable(const std::string &);
~UserTable() { delete[] users; }
void addUser(const User&);
void addUser(const User &);
User find(int) const;
User find(std::string) const;
int getNbrUsers() const {
return n;
}
int getNbrUsers() const { return n; }
void print(std::ostream&) const;
void print(std::ostream &) const;
static const User user_not_found;
private:
private:
int capacity{1000};
void ensureCapacity(int);
int n{0};
User* users;
User *users;
friend int testFindNbr(const UserTable ut);
};

View file

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