#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include "func.cpp"
#include <memory>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
p = true;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addchar(QChar ch)
{
if (p)
{
QString str = ui->lineEdit->text();
str = str + ch;
ui->lineEdit->setText(str);
}
else
{
QString str = ui->lineEdit_2->text();
str = str + ch;
ui->lineEdit_2->setText(str);
}
}
void MainWindow::on_pushButton_1_clicked()
{
addchar('1');
}
void MainWindow::on_pushButton_13_clicked()
{
addchar('.');
}
void MainWindow::on_pushButton_2_clicked()
{
addchar('2');
}
void MainWindow::on_pushButton_3_clicked()
{
addchar('3');
}
void MainWindow::on_pushButton_4_clicked()
{
addchar('4');
}
void MainWindow::on_pushButton_5_clicked()
{
addchar('5');
}
void MainWindow::on_pushButton_6_clicked()
{
addchar('6');
}
void MainWindow::on_pushButton_7_clicked()
{
addchar('7');
}
void MainWindow::on_pushButton_8_clicked()
{
addchar('8');
}
void MainWindow::on_pushButton_9_clicked()
{
addchar('9');
}
void MainWindow::on_pushButton_0_clicked()
{
addchar('0');
}
void MainWindow::on_pushButton_10_clicked()
{
addchar('+');
}
void MainWindow::on_pushButton_12_clicked()
{
addchar('-');
}
void MainWindow::on_pushButton_11_clicked()
{
addchar('x');
addchar('^');
}
void MainWindow::on_pushButton_clicked()
{
p = p^true;
if (p)
ui->pushButton->setText("单击以输入P2");
else
ui->pushButton->setText("单击以输入P1");
}
void MainWindow::on_pushButton_14_clicked()
{
close();
}
void MainWindow::on_pushButton_16_clicked()
{
if(p)
{
QString str = ui->lineEdit->text();
str = str.left(str.length() - 1);
ui->lineEdit->setText(str);
}
else
{
QString str = ui->lineEdit_2->text();
str = str.left(str.length() - 1);
ui->lineEdit_2->setText(str);
}
}
void MainWindow::on_pushButton_15_clicked()
{
QString str1 = ui->lineEdit->text();
int l = 0, r = 0, temp2 = 0;
double temp1 = 0;
Poly P1, P2, Pa, Pm;
while(r < str1.length())
{
bool g = true;
while(str1[r].isDigit()|| str1[r] == '.' ||g&&(str1[r] == '+'||str1[r] == '-')&&r<str1.length())
{
g = false;
r++;
}
temp1 = str1.mid(l,r-l).toDouble();
if (str1[r] == 'x')
{
r += 2;
l = r;
}
else
{
l = r;
P1.insert(temp1, 0);
g = true;
continue;
}
g = true;
while(str1[r].isDigit()||g&&(str1[r] == '+'||str1[r] == '-')&&r<str1.length())
{
if (!g)
if (str1[r] == '+'||str1[r] == '-')
break;
g = false;
r++;
}
temp2 = str1.mid(l,r-l).toInt();
P1.insert (temp1, temp2);
l = r;
}
QString str2 = ui->lineEdit_2->text();
l = 0; r = 0; temp1 = 0; temp2 = 0;
while(r < str2.length())
{
bool g = true;
while(str2[r].isDigit()|| str2[r] == '.' ||g&&(str2[r] == '+'||str2[r] == '-')&&r<str2.length())
{
g = false;
r++;
}
temp1 = str2.mid(l,r-l).toDouble();
if (str2[r] == 'x')
{
r += 2;
l = r;
}
else
{
l = r;
P2.insert(temp1,0);
g = true;
continue;
}
g = true;
while(str2[r].isDigit()||g&&(str2[r] == '+'||str2[r] == '-')&&r<str2.length())
{
if (!g)
if (str2[r] == '+'||str2[r] == '-')
break;
g = false;
r++;
}
temp2 = str2.mid(l,r-l).toInt();
P2.insert(temp1,temp2);
l = r;
}
QString ans1,ans2;
Pa = P1 + P2;
Pm = P1 - P2;
term * pa = Pa.head->pNext, * pm = Pm.head->pNext;
bool g = false;
if(pa==NULL) ans1=QChar('0');
for (;pa!=NULL;pa=pa->pNext)
if (pa->coef != 0)
{
g = true;
if(pa->coef > 0)
ans1 = ans1 + QChar('+');
if(pa->coef!=1 && pa->coef!=-1)
ans1 = ans1 + QString::number(pa->coef);
if(pa->coef == -1)
ans1 = ans1 + "-";
if (pa->exp != 0 && pa->exp != 1)
ans1 = ans1 + QString("x^") + QString::number(pa->exp);
else if (pa->exp == 1)
ans1 = ans1 + QString ("x");
else if (pa->exp == 0 && (pa->coef==1 || pa->coef==-1))
ans1 = ans1 + '1';
}
if (ans1[0] == '+')
ans1 = ans1.right(ans1.length()-1);
g = false;
if(pm==NULL) ans2=QChar('0');
for (;pm!=NULL;pm=pm->pNext)
if (pm->coef != 0)
{
g = true;
if(pm->coef > 0)
ans2 = ans2 + QChar('+');
if(pm->coef!=1 && pm->coef!=-1)
ans2 = ans2 + QString::number(pm->coef);
if(pm->coef == -1)
ans2 = ans2 + "-";
if (pm->exp != 0 && pm->exp != 1)
ans2 = ans2 + QString("x^") + QString::number(pm->exp);
else if (pm->exp == 1)
ans2 = ans2 + QString ("x");
else if (pm->exp == 0 && (pm->coef==1 || pm->coef==-1))
ans2 = ans2 + '1';
}
if (ans2[0] == '+')
ans2 = ans2.right(ans2.length()-1);
ui->lineEdit_3->setText(ans1);
ui->lineEdit_4->setText(ans2);
}