我最近才开始使用 Qt Creator,所以我是初学者。但从一开始,我就决定以实现一个可以测量各种运行时间的应用程序为目标。使用不同长度的数组进行 10 次测试,并将结果记录在表格中。只要我按下开始按钮,程序就会冻结,什么也没有发生。但是,如果您减小测量中使用的数组的大小,那么程序就会立即崩溃。请帮我弄清楚我做错了什么:
标题
#ifndef TIMETESTING_H
#define TIMETESTING_H
#include <QDialog>
#include <QtWidgets>
#include <QTableWidget>
#include "sorts.h"
class TimeTesting : public QDialog
{
Q_OBJECT
public:
explicit TimeTesting(QWidget *parent = nullptr);
private slots:
void start_test();
void stop_test();
private:
QTableWidget* table;
QPushButton* start;
QPushButton* stop;
bool isProcessing;
};
#endif // TIMETESTING_H
.cpp 文件
#include "timetesting.h"
TimeTesting::TimeTesting(QWidget *parent): QDialog{parent}
{
isProcessing = false;
start = new QPushButton("Старт");
stop = new QPushButton("Стоп");
table = new QTableWidget;
table->setColumnCount(10);
table->setRowCount(13);
table->setHorizontalHeaderLabels(QStringList() <<
"1000 элементов" <<
"2000 элементов" <<
"3000 элементов" <<
"4000 элементов" <<
"5000 элементов" <<
"6000 элементов" <<
"7000 элементов" <<
"8000 элементов" <<
"9000 элементов" <<
"10000 элементов"
);
table->setVerticalHeaderLabels(QStringList() <<
"Сортировка пузырьком" <<
"Шейкерная сортировка" <<
"Метод простых вставок" <<
"Сортировка выбором" <<
"Сортировка подсчётом" <<
"Сортировка двоичными вставками" <<
"Двухсторонняя сортировка выбором" <<
"Сортировка деревом" <<
"Сортировка слиянием" <<
"Пирамидальная сортировка" <<
"Быстрая сортировка" <<
"Битонная сортировка" <<
"Сортировка Шелла"
);
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
connect(start, SIGNAL(clicked()), this, SLOT(start_test()));
connect(stop, SIGNAL(clicked()), this, SLOT(stop_test()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(table);
layout->addWidget(start);
layout->addWidget(stop);
setLayout(layout);
setWindowTitle("Тестирование на время");
}
void TimeTesting::start_test() {
start->setDisabled(true);
start->setText("В процессе...");
isProcessing = true;
void (*list_of_sortings[])(Sequence<int>*, int (*)(int, int)) = {
bubble_sort<int>,
shaker_sort<int>,
insertion_sort<int>,
selection_sort<int>,
counting_sort<int>,
bin_insertion_sort<int>,
double_selection_sort<int>,
tree_sort<int>,
merge_sort<int>,
heap_sort<int>,
quick_sort<int>,
bitonic_sort<int>
};
ArraySequence<int> arrays[10];
for (size_t i = 0; i < 10; ++i) {
for (size_t j = 0; j < (i + 1)*1000; ++j) {
arrays[i].append(rand());
}
}
for (size_t i = 0; i < 12; ++i) {
for (size_t j = 0; j < 10; ++j) {
if (!isProcessing) {
start->setEnabled(true);
start->setText("Старт");
return;
}
clock_t start_time = clock();
list_of_sortings[i](&arrays[j], [](int a, int b) {return a - b;});
size_t time = clock() - start_time;
QTableWidgetItem* item = new QTableWidgetItem(tr("%1 мс").arg(time));
table->setItem(i, j, item);
if (!isProcessing) {
start->setEnabled(true);
stop->setEnabled(true);
start->setText("Старт");
return;
}
for (size_t k = 0; k < (j + 1)*1000; ++k) {
arrays[j][k] = rand();
}
}
}
start->setEnabled(true);
start->setText("Старт");
}
void TimeTesting::stop_test() {
isProcessing = false;
stop->setDisabled(true);
}
像 rand 或 ctime 这样的头文件已经包含在 sorts.h 中,所有类型都可以正常工作(通过单元测试检查),集合也可以。如果您发现任何其他与问题无关的门框,那么您可以戳它们,这样我以后就不允许它们了)
更新
错误是错误的:
for (size_t i = 0; i < 13; ++i) {
应该是 12 而不是 13。但是还有其他问题:
- 程序在运行模式下仍然崩溃,但在调试模式下工作
- 我原本想要串行输出,即 对一个数组进行一次排序 - 带来结果,走得更远。但是,只有在所有排序都完成后,程序才会一次性显示所有结果。如何实现这个想法?
- 第 1 点的错误是因为我排除了 ShellSort,因为 这是唯一具有附加默认参数的排序,排序序列(2 的幂序列、Sedgwick、Pratt、Fibonacci 等)。所有这些序列都在 sorts.h 中声明为全局变量(我知道这种风格很糟糕)。因为他们,有一个覆盖:
:-1: error: CMakeFiles/sorts.dir/main.cpp.obj:D:/Nick/Prog projects/Qt/sorts/sorts.h:17: multiple definition of `DefaultSequence'; CMakeFiles/sorts.dir/sorts_autogen/mocs_compilation.cpp.obj:D:/Nick/Prog projects/Qt/sorts/sorts.h:17: first defined here
虽然#ifndef SORTS_H
是写的