Загрузить файлы в «/»
This commit is contained in:
commit
63786a9f65
208
Readme.md
Normal file
208
Readme.md
Normal file
@ -0,0 +1,208 @@
|
|||||||
|
### Калькулятор с графическим интерфейсом
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QGridLayout>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
class Calculator : public QWidget {
|
||||||
|
public:
|
||||||
|
Calculator() {
|
||||||
|
auto *layout = new QGridLayout(this);
|
||||||
|
|
||||||
|
display = new QLineEdit();
|
||||||
|
display->setReadOnly(true);
|
||||||
|
display->setAlignment(Qt::AlignRight);
|
||||||
|
layout->addWidget(display, 0, 0, 1, 4);
|
||||||
|
|
||||||
|
QString buttons[4][4] = {
|
||||||
|
{"7","8","9","/"},
|
||||||
|
{"4","5","6","*"},
|
||||||
|
{"1","2","3","-"},
|
||||||
|
{"C","0","=","+"}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
for (int j = 0; j < 4; j++) {
|
||||||
|
QString text = buttons[i][j];
|
||||||
|
auto *btn = new QPushButton(text);
|
||||||
|
layout->addWidget(btn, i + 1, j);
|
||||||
|
|
||||||
|
connect(btn, &QPushButton::clicked, this, [=]() {
|
||||||
|
handleInput(text);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QLineEdit *display;
|
||||||
|
QString current;
|
||||||
|
|
||||||
|
void handleInput(const QString &text) {
|
||||||
|
if (text == "C") {
|
||||||
|
current.clear();
|
||||||
|
}
|
||||||
|
else if (text == "=") {
|
||||||
|
current = QString::number(evaluate(current));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
current += text;
|
||||||
|
}
|
||||||
|
|
||||||
|
display->setText(current);
|
||||||
|
}
|
||||||
|
|
||||||
|
double evaluate(const QString &expr) {
|
||||||
|
// простой парсер: число операция число
|
||||||
|
QString op;
|
||||||
|
int pos = -1;
|
||||||
|
|
||||||
|
for (int i = 0; i < expr.size(); i++) {
|
||||||
|
if (QString("+-*/").contains(expr[i])) {
|
||||||
|
op = expr[i];
|
||||||
|
pos = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pos == -1) return expr.toDouble();
|
||||||
|
|
||||||
|
double a = expr.left(pos).toDouble();
|
||||||
|
double b = expr.mid(pos + 1).toDouble();
|
||||||
|
|
||||||
|
if (op == "+") return a + b;
|
||||||
|
if (op == "-") return a - b;
|
||||||
|
if (op == "*") return a * b;
|
||||||
|
if (op == "/") return b != 0 ? a / b : 0;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
|
Calculator w;
|
||||||
|
w.setWindowTitle("Calculator");
|
||||||
|
w.resize(250, 300);
|
||||||
|
w.show();
|
||||||
|
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Базовый ToDO с графическим интерфейсом
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
#include <QHBoxLayout>
|
||||||
|
#include <QListWidget>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QTextStream>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
class NotesApp : public QWidget {
|
||||||
|
public:
|
||||||
|
NotesApp() {
|
||||||
|
auto *layout = new QVBoxLayout(this);
|
||||||
|
|
||||||
|
list = new QListWidget();
|
||||||
|
input = new QLineEdit();
|
||||||
|
|
||||||
|
auto *btnAdd = new QPushButton("Add");
|
||||||
|
auto *btnDel = new QPushButton("Delete");
|
||||||
|
auto *btnSave = new QPushButton("Save");
|
||||||
|
|
||||||
|
auto *btnLayout = new QHBoxLayout();
|
||||||
|
btnLayout->addWidget(btnAdd);
|
||||||
|
btnLayout->addWidget(btnDel);
|
||||||
|
btnLayout->addWidget(btnSave);
|
||||||
|
|
||||||
|
layout->addWidget(list);
|
||||||
|
layout->addWidget(input);
|
||||||
|
layout->addLayout(btnLayout);
|
||||||
|
|
||||||
|
connect(btnAdd, &QPushButton::clicked, this, &NotesApp::addNote);
|
||||||
|
connect(btnDel, &QPushButton::clicked, this, &NotesApp::deleteNote);
|
||||||
|
connect(btnSave, &QPushButton::clicked, this, &NotesApp::saveNotes);
|
||||||
|
|
||||||
|
loadNotes();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QListWidget *list;
|
||||||
|
QLineEdit *input;
|
||||||
|
|
||||||
|
|
||||||
|
QString filePath = "/home/lux/Документы/notes.txt";
|
||||||
|
|
||||||
|
void addNote() {
|
||||||
|
QString text = input->text().trimmed();
|
||||||
|
if (!text.isEmpty()) {
|
||||||
|
list->addItem(text);
|
||||||
|
input->clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deleteNote() {
|
||||||
|
auto *item = list->currentItem();
|
||||||
|
if (item) delete item;
|
||||||
|
}
|
||||||
|
|
||||||
|
void saveNotes() {
|
||||||
|
QFile file(filePath);
|
||||||
|
|
||||||
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||||
|
qDebug() << "Ошибка сохранения:" << filePath;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextStream out(&file);
|
||||||
|
|
||||||
|
for (int i = 0; i < list->count(); i++) {
|
||||||
|
out << list->item(i)->text() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
qDebug() << "Сохранено в:" << filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadNotes() {
|
||||||
|
QFile file(filePath);
|
||||||
|
|
||||||
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
qDebug() << "Файл не найден, создаётся новый";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextStream in(&file);
|
||||||
|
|
||||||
|
while (!in.atEnd()) {
|
||||||
|
QString line = in.readLine().trimmed();
|
||||||
|
if (!line.isEmpty())
|
||||||
|
list->addItem(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
qDebug() << "Загружено из:" << filePath;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
|
NotesApp w;
|
||||||
|
w.setWindowTitle("Notes App");
|
||||||
|
w.resize(400, 300);
|
||||||
|
w.show();
|
||||||
|
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
|
```
|
||||||
Loading…
x
Reference in New Issue
Block a user