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 2 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
12 changes: 12 additions & 0 deletions ResizeFunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "ResizeFunc.h"

char *resizenew(char *str, unsigned size, unsigned new_size) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Забыли заинклудить хедер-файл. Здесь - не критично, потому что хедер сам не инклудит ничего, но лучше сразу привыкать к хорошему.

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
12 changes: 12 additions & 0 deletions StrcatFunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#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
23 changes: 23 additions & 0 deletions User.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#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;
}
21 changes: 21 additions & 0 deletions User.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#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
119 changes: 46 additions & 73 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,85 +1,58 @@
#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)
{
ui->setupUi(this);
}

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


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;
}
};
MainWindow::~MainWindow() { delete ui; }

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();
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();

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";
}