From 6130bdee6684c3410bed302b4528ba8a5a7f834d Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 27 Nov 2024 17:25:14 +0100 Subject: [PATCH] Format --- lab3/User.cc | 5 ++- lab3/User.h | 30 +++++++++------- lab3/UserTable.cc | 80 +++++++++++++++++++------------------------ lab3/UserTable.h | 21 ++++++------ lab3/UserTableTest.cc | 1 - 5 files changed, 65 insertions(+), 72 deletions(-) diff --git a/lab3/User.cc b/lab3/User.cc index 0268bd2..d45437e 100644 --- a/lab3/User.cc +++ b/lab3/User.cc @@ -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(); } diff --git a/lab3/User.h b/lab3/User.h index cbedfe0..b4a8868 100644 --- a/lab3/User.h +++ b/lab3/User.h @@ -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 diff --git a/lab3/UserTable.cc b/lab3/UserTable.cc index ec31eff..b52f0c6 100644 --- a/lab3/UserTable.cc +++ b/lab3/UserTable.cc @@ -1,24 +1,22 @@ #include "UserTable.h" -#include #include +#include -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 << "(" <