Сама ошибка "C:\Qt\Projects\MainWindow\mymainwindow.cpp:43: ошибка: conversion from 'QCharRef' to non-scalar type 'QString' requested
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];"
mymainwindow.cpp
#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include <QMessageBox>
#include <QPushButton>
#include <QDebug>
#include <qmath.h>
MyMainWindow::MyMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyMainWindow)
{
ui->setupUi(this);
ui->comboBox->addItem("-");
ui->comboBox->addItem("sin");
ui->comboBox->addItem("cos");
ui->comboBox->addItem("ln");
ui->comboBox->addItem("x²");
ui->comboBox->addItem("√x");
}
MyMainWindow::~MyMainWindow()
{
delete ui;
}
void MyMainWindow::on_Exit_triggered()
{
close();
}
void MyMainWindow::on_Author_triggered()
{
QMessageBox msg;
msg.setWindowTitle("Автор");
msg.setText("M-134 Вайс Илья");
msg.exec();
}
void MyMainWindow::on_Button0_clicked()
{
if (ui->lineEdit->text() != "0")
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1]; // в строчках такого рода - ошибки
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "0");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*0");
}
}
void MyMainWindow::on_Button1_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "1");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*1");
}
void MyMainWindow::on_Button2_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "2");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*2");
}
void MyMainWindow::on_Button3_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "3");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*3");
}
void MyMainWindow::on_Button4_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "4");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*4");
}
void MyMainWindow::on_Button5_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "5");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*5");
}
void MyMainWindow::on_Button6_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "6");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*6");
}
void MyMainWindow::on_Button7_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "7");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*7");
}
void MyMainWindow::on_Button8_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "8");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*8");
}
void MyMainWindow::on_Button9_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ")")
ui->lineEdit->setText(ui->lineEdit->text() + "9");
else
ui->lineEdit->setText(ui->lineEdit->text() + "*9");
}
void MyMainWindow::on_Plus_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != "+" && str != "-" && str != "*" && str != "/" && ui->lineEdit->text() != "" && str != ".")
{
ui->lineEdit->setText(ui->lineEdit->text() + "+");
}
}
void MyMainWindow::on_Minus_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != "+" && str != "-" && str != "*" && str != "/" && str != ".")
{
ui->lineEdit->setText(ui->lineEdit->text() + "-");
}
}
void MyMainWindow::on_Multiply_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != "+" && str != "-" && str != "*" && str != "/" && ui->lineEdit->text() != "" && str != ".")
{
ui->lineEdit->setText(ui->lineEdit->text() + "*");
}
}
void MyMainWindow::on_Divide_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != "+" && str != "-" && str != "*" && str != "/" && ui->lineEdit->text() != "" && str != ".")
{
ui->lineEdit->setText(ui->lineEdit->text() + "/");
}
}
void MyMainWindow::on_Equally_clicked()
{
if (ExceptionHandling())
{
QString str = ProcessingStr();
if (str == "inf")
{
QMessageBox msg;
msg.setWindowTitle("Ошибка");
msg.setText("Нельзя делить на ноль");
msg.exec();
}
else
{
QString condition = ui->comboBox->currentText();
if (condition == "sin")
str = QString::number(qSin(str.toFloat()));
if (condition == "cos")
str = QString::number(qCos(str.toFloat()));
if (condition == "ln")
str = QString::number(qLn(str.toFloat()));
if (condition == "x²")
str = QString::number(qPow(str.toFloat(), 2));
if (condition == "√x")
str = QString::number(qSqrt(str.toFloat()));
if (str == "nan")
{
QMessageBox msg;
msg.setWindowTitle("Ошибка");
msg.setText("Нельзя извлечь вещественный корень или логарифм из отрицательного числа");
msg.exec();
}
else if (str == "-inf")
{
QMessageBox msg;
msg.setWindowTitle("Ошибка");
msg.setText("Нельзя извлечь логарифм из нуля");
msg.exec();
}
else
ui->lineEdit->setText(str);
}
}
}
void MyMainWindow::on_Point_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str != ".")
{
QString str1 = ui->lineEdit->text();
if (str1.isEmpty() || str == "+" || str == "-" || str == "*" || str == "/")
ui->lineEdit->setText(ui->lineEdit->text() + "0.");
else if (str == "(" || str == ")")
ui->lineEdit->setText(ui->lineEdit->text() + "*0.");
else
ui->lineEdit->setText(ui->lineEdit->text() + ".");
}
}
void MyMainWindow::on_Bracket_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (ui->lineEdit->text() == "" || str == "(" || str == "+" || str == "-" || str == "*" || str == "/" && str != ".")
{
opBr++;
ui->lineEdit->setText(ui->lineEdit->text() + "(");
}
else if ((str == ")" && opBr == clBr) || ((str.toInt() || str == "0") && opBr == clBr) && str != ".")
{
opBr++;
ui->lineEdit->setText(ui->lineEdit->text() + "*(");
}
else
{
if (str != ".")
{
clBr++;
ui->lineEdit->setText(ui->lineEdit->text() + ")");
}
}
}
void MyMainWindow::on_Clear_clicked()
{
opBr = 0;
clBr = 0;
ui->lineEdit->setText("");
}
void MyMainWindow::on_Back_clicked()
{
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str == "(")
opBr--;
if (str == ")")
clBr--;
ui->lineEdit->setText(ui->lineEdit->text().remove(ui->lineEdit->text().size()-1, 1));
}
// ошибки
bool MyMainWindow::ExceptionHandling ()
{
bool ret = true;
QString str = ui->lineEdit->text()[ui->lineEdit->text().size()-1];
if (str == "+" || str == "-" || str =="*" || str == "/")
{
ret = false;
QMessageBox msg;
msg.setWindowTitle("Ошибка");
msg.setText("Последний символ должен быть цифрой");
msg.exec();
}
/*QRegExp rx("/0"); // исправить
if (rx.indexIn(ui->lineEdit->text()) != -1)
{
ret = false;
QMessageBox msg;
msg.setWindowTitle("Ошибка");
msg.setText("Нельзя делить на ноль");
msg.exec();
}*/
if (opBr != clBr)
{
ret = false;
QMessageBox msg;
msg.setWindowTitle("Ошибка");
msg.setText("Не все скобки закрыты");
msg.exec();
}
return ret;
}
// скобки
QString MyMainWindow::ProcessingStr ()
{
QString str = ui->lineEdit->text();
QRegExp rxMain("[-|+|*|/|)|(]");
while (rxMain.indexIn(str) != -1)
{
//qDebug() << str; // подробное решение
QRegExp rx("[)]");
if (rx.indexIn(str) == -1)
return Analysis(str);
else
{
for (int i = rx.indexIn(str); i >= 0; i--)
{
QString symb = ui->lineEdit->text()[i];
if (symb == "(")
{
QString str1 = (str).mid(i+1, rx.indexIn(str)-i-1);
str.replace(i, rx.indexIn(str)-i+1, Analysis(str1));
}
}
}
}
return str;
}
// само выражение
QString MyMainWindow::Analysis(QString data)
{
//qDebug() << data;
QRegExp val1("[-|/|*|+|)|(]");
QStringList list1 = data.split(val1, QString::SkipEmptyParts); // цифры
QRegExp val2("[0|1|2|3|4|5|6|7|8|9|.|)|(]");
QStringList list2 = data.split(val2, QString::SkipEmptyParts); // знаки
QString str = ui->lineEdit->text()[0];
if (str == "-")
{
list1.insert(0, 0);
}
for(int i = 0; i < list2.count();)
{
if (list2[i] == "*" || list2[i] == "/")
{
float f;
if (list2[i] == "*")
{
f = list1[i].toFloat()*list1[i+1].toFloat();
}
if (list2[i] == "/")
{
f = list1[i].toFloat()/list1[i+1].toFloat();
}
list1.erase(list1.begin() + i);
list1.erase(list1.begin() + i);
list2.erase(list2.begin() + i);
list1.insert(i, QString::number(f));
}
else
i++;
}
for (int i = 0; i < list2.count();)
{
if (list2[i] == "+" || list2[i] == "-")
{
float f;
if (list2[i] == "+")
{
f = list1[i].toFloat()+list1[i+1].toFloat();
}
if (list2[i] == "-")
{
f = list1[i].toFloat()-list1[i+1].toFloat();
}
list1.erase(list1.begin() + i);
list1.erase(list1.begin() + i);
list2.erase(list2.begin() + i);
list1.insert(i, QString::number(f));
}
else
i++;
}
return list1[0];
}
mymainwindow.h
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MyMainWindow;
}
class MyMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MyMainWindow(QWidget *parent = 0);
~MyMainWindow();
QString operStr;
float operFloat = 0;
/*enum e_stat
{
plus,
minus,
multiply,
divide,
none
};
e_stat stat = none;*/
int opBr = 0;
int clBr = 0;
private slots:
void on_Exit_triggered();
void on_Author_triggered();
void on_Button0_clicked();
void on_Button1_clicked();
void on_Button2_clicked();
void on_Button3_clicked();
void on_Button4_clicked();
void on_Button5_clicked();
void on_Button6_clicked();
void on_Button7_clicked();
void on_Button8_clicked();
void on_Button9_clicked();
void on_Plus_clicked();
void on_Minus_clicked();
void on_Multiply_clicked();
void on_Divide_clicked();
void on_Point_clicked();
void on_Bracket_clicked();
void on_Equally_clicked();
void on_Clear_clicked();
void on_Back_clicked();
bool ExceptionHandling ();
QString ProcessingStr ();
QString Analysis (QString data);
private:
Ui::MyMainWindow *ui;
};
#endif // MYMAINWINDOW_H