RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-511244

nyekitka's questions

Martin Hope
nyekitka
Asked: 2022-07-21 00:34:49 +0000 UTC

测量 Qt 窗口应用程序中各种类型的运行时间

  • 1

我最近才开始使用 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. 程序在运行模式下仍然崩溃,但在调试模式下工作
  2. 我原本想要串行输出,即 对一个数组进行一次排序 - 带来结果,走得更远。但是,只有在所有排序都完成后,程序才会一次性显示所有结果。如何实现这个想法?
  3. 第 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是写的

c++ qt
  • 1 个回答
  • 72 Views
Martin Hope
nyekitka
Asked: 2022-07-19 20:28:20 +0000 UTC

将标记(##)插入字符串

  • 0

出现了以下问题:如何使用#define 插入任何名称、标题等。在线,即

#include <iostream>
#define GREETING(Name) std::cout << "Hi, Name##!";
int main() {
    GREETING(Nick);
    return 0;
}

我希望程序在编译后输出“Hi,Nick!”,但它会打印“Hi,Name##!”。如何用define正常显示一行?

c++ макросы
  • 3 个回答
  • 41 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5