使用qt creator结合下面代码实现可视化:#include <iostream> #include <cstring> using namespace std; int m; int* nums = new int[m]; int* cnums = new int[m * 8]; class Solution { public: int singleNumber() { int result = 0; for (int i = 0; i < m; i++) { result = result ^ nums[i]; } return result; } }; int main() { int n = 0; while (cin>>m) { if (m == 0) { break; } for (int i = 0; i < m; i++) { cin >> nums[i]; } Solution a; cnums[n] = a.singleNumber(); n++; } cout << endl; for (int i = 0; i < n ; i++) { cout << cnums[i] << endl; } delete[] nums; delete[] cnums; return 0; }
时间: 2023-07-15 18:11:48 浏览: 139
以下是使用Qt Creator可视化的示例代码:
main.cpp:
```cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
```
mainwindow.h:
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_calculateButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <cstring>
using namespace std;
int m;
int* nums = new int[m];
int* cnums = new int[m * 8];
class Solution {
public:
int singleNumber() {
int result = 0;
for (int i = 0; i < m; i++) {
result = result ^ nums[i];
}
return result;
}
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
delete[] nums;
delete[] cnums;
}
void MainWindow::on_calculateButton_clicked()
{
QString inputString = ui->inputEdit->toPlainText();
QStringList inputList = inputString.split("\n");
int n = inputList.size();
for (int i = 0; i < n; i++) {
m = inputList[i].toInt();
if (m == 0) {
break;
}
for (int j = 0; j < m; j++) {
i++;
nums[j] = inputList[i].toInt();
}
Solution a;
cnums[i] = a.singleNumber();
}
QString outputString;
for (int i = 0; i < n ; i++) {
outputString += QString::number(cnums[i]) + "\n";
}
ui->outputEdit->setPlainText(outputString);
}
```
mainwindow.ui:
在Qt Creator中使用设计器设计界面,添加两个TextEdit和一个PushButton,并分别命名为inputEdit、outputEdit和calculateButton。
注意:使用完nums和cnums数组后需要delete[]释放内存,否则会存在内存泄露问题。
阅读全文