Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

HW by Valery Myrzin #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions QtDemo.pro
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
ResizeFunc.cpp \
StrcatFunc.cpp \
User.cpp \
main.cpp \
mainwindow.cpp

HEADERS += \
ResizeFunc.h \
StrcatFunc.h \
User.h \
mainwindow.h

Expand Down
4 changes: 4 additions & 0 deletions Resize.h.autosave
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef RESIZE_H
#define RESIZE_H
char* resize(char* str, unsigned size, unsigned new_size)
#endif // RESIZE_H
14 changes: 14 additions & 0 deletions ResizeFunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "ResizeFunc.h"

char* resizenew(char* str, unsigned size, unsigned new_size)
{
if (new_size < size)
return nullptr;
auto* temp = new char[new_size];
for (unsigned i = 0; i < size; i++)
{
temp[i] = str[i];
}
delete[] str;
return temp;
}
4 changes: 4 additions & 0 deletions ResizeFunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef RESIZE_H
#define RESIZE_H
char* resizenew(char* str, unsigned size, unsigned new_size);
#endif // RESIZE_H
14 changes: 14 additions & 0 deletions StrcatFunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "StrcatFunc.h"

void strcatvoid(char* to, const char* from)
{
while (*to)
to++;
while (*from)
{
*to = *from;
to++;
from++;
}
*to = '\0';
}
4 changes: 4 additions & 0 deletions StrcatFunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef STRCATFUNC_H
#define STRCATFUNC_H
void strcatvoid(char* to, const char* from);
#endif // STRCATFUNC_H
38 changes: 38 additions & 0 deletions User.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "User.h"

User::User(int age, QString name)
{
this->age_ = age;
this->name_ = name;
}
QString User::getName() const
{
return name_;
}
int User::getAge() const
{
return age_;
}
size_t User::getCount()
{
return users_.size();
}
void User::addUser(User user)
{
users_.push_back(user);
}
void User::removeUserAt(int index)
{
users_.erase(users_.begin() + index);
}
void User::removeUserByName(QString name)
{
users_.erase(std::remove_if(users_.begin(), users_.end(), [&](User found) { return found.getName() == name; }),
users_.end());
}
std::vector<QString> User::getAllNames()
{
std::vector<QString> list;
std::for_each(users_.begin(), users_.end(), [&](User found) { list.push_back(found.getName()); });
return list;
}
22 changes: 22 additions & 0 deletions User.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef USER_H
#define USER_H
#include <QString>
#include <vector>

class User
{
int age_;
QString name_;
static std::vector<User> users_;

public:
User(int age, QString name);
QString getName() const;
int getAge() const;
static size_t getCount();
static void addUser(User user);
static void removeUserAt(int index);
static void removeUserByName(QString name);
static std::vector<QString> getAllNames();
};
#endif // USER_H
10 changes: 5 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#include <QApplication>

int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
114 changes: 46 additions & 68 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,85 +1,63 @@
#include "mainwindow.h"
#include "ResizeFunc.h"
#include "StrcatFunc.h"
#include "User.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
delete ui;
}


class User{
int age_;
QString name_;
public:
static std::vector<User> users_;
User(int age, QString name){
this->age_=age;
this->name_=name;
}
QString getName(){
return name_;
}
int getAge(){
return age_;
}
static size_t getCount(){
return users_.size();
}
static void addUser(User user){
users_.push_back(user);
}
static void removeUserAt(int index){
users_.erase(users_.begin() + index);
}
static void removeUserByName(QString name){
users_.erase(std::remove_if(users_.begin(), users_.end(),[&](User found){return found.getName() == name;}), users_.end());
}
static std::vector<QString> getAllNames(){
std::vector<QString> list;
std::for_each(users_.begin(),users_.end(),[&](User found){list.push_back(found.getName());});
return list;
}
};

std::vector<User> User::users_;

void MainWindow::on_submitPushButton_clicked()
{
qDebug() << "User clicked on submit button";


User::addUser(User(22,"Timofey"));
User::addUser(User(33,"Aufar"));
User::addUser(User(44,"Valery"));
User::addUser(User(55,"Kirill"));
User::addUser(User(66,"Artem"));
User::addUser(User(77,"Camila"));

qDebug() << "Current students' count: " << User::getCount();
qDebug() << "All students: ";
qDebug() << User::getAllNames();
qDebug() << "Removing user at 4 index";

User::removeUserAt(4);

qDebug() << "Current Students' Count: " << User::getCount();
qDebug() << "All students: ";
qDebug() << User::getAllNames();
qDebug() << "Removing user named Aufar ";

User::removeUserByName("Aufar");

qDebug() << "Current Students' Count: " << User::getCount();
qDebug() << "All students: ";
qDebug() << User::getAllNames();
{
qDebug() << "User clicked on submit button";

User::addUser(User(22, "Timofey"));
User::addUser(User(33, "Aufar"));
User::addUser(User(44, "Valery"));
User::addUser(User(55, "Kirill"));
User::addUser(User(66, "Artem"));
User::addUser(User(77, "Camila"));

qDebug() << "Current students' count: " << User::getCount();
qDebug() << "All students: ";
qDebug() << User::getAllNames();
qDebug() << "Removing user at 4 index";

User::removeUserAt(4);

qDebug() << "Current Students' Count: " << User::getCount();
qDebug() << "All students: ";
qDebug() << User::getAllNames();
qDebug() << "Removing user named Aufar ";

User::removeUserByName("Aufar");

qDebug() << "Current Students' Count: " << User::getCount();
qDebug() << "All students: ";
qDebug() << User::getAllNames();

auto a = User(666, "Devil");
qDebug() << a.getName() + "\'s Age:" << a.getAge();

char char1[12] = "First";
char char2[] = "Second";
qDebug() << char1;
qDebug() << char2;
strcatvoid(char1, char2);
qDebug() << "Super Deep Cat Func Check starting...";
qDebug() << char1;
if (strcmp(char1, "FirstSecond") == 0)
qDebug() << "Test passed";
}
17 changes: 10 additions & 7 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
MainWindow(QWidget* parent = nullptr);
~MainWindow();

private slots:
void on_submitPushButton_clicked();
void on_submitPushButton_clicked();

private:
Ui::MainWindow *ui;
Ui::MainWindow* ui;
};
#endif // MAINWINDOW_H
#endif // MAINWINDOW_H