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

View file

@ -11,13 +11,19 @@ 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() {
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 cardNbr == u.cardNbr && name == u.name;
}
bool operator!=(const User &u) const { return !(u == *this); }
private:
int cardNbr;
std::string name;

View file

@ -1,13 +1,12 @@
#include "UserTable.h"
#include <fstream>
#include <algorithm>
#include <fstream>
const User UserTable::user_not_found = User{-1, "Not Found"};
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()) {
@ -18,7 +17,6 @@ UserTable::UserTable(const std::string& fname) :UserTable{}
char n[80];
ufile.getline(n, 80);
addUser(User(cn, n));
}
}
} 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
ensureCapacity(n + 1);
// 1. Hitta rätt plats
@ -45,8 +42,7 @@ void UserTable::addUser(const User& u)
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;
@ -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,8 +75,7 @@ User UserTable::find(std::string name) const
return user_not_found;
}
void UserTable::ensureCapacity(int s)
{
void UserTable::ensureCapacity(int s) {
if (s > capacity) {
while (s > capacity) {
capacity *= 4;
@ -91,10 +85,8 @@ void UserTable::ensureCapacity(int s)
delete[] users;
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];
@ -108,8 +100,7 @@ void UserTable::print(std::ostream& os) const
* Om något kortnummer inte kunde sökas upp returneras detta. Annars, om
* alla sökningar lyckades, returneras 0.
*/
int testFindNbr(const UserTable ut)
{
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

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

View file

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