-
UID:136954
-
- 注册时间2012-10-05
- 最后登录2017-09-11
- 在线时间52小时
-
- 发帖41
- 搜Ta的帖子
- 精华0
- 金钱473
- 威望61
- 贡献值10
- 好评度53
-
访问TA的空间加好友用道具
|
linearAlgebra.cpp (28 K) 下载次数:241 线性代数代码: #include"linearAlgebra.h"#include<QMessageBox> #include<QCloseEvent> #include<QFile> #include<QList> #include<QUrl> #include<QFileDialog> #include<QTextStream> #include<QPlainTextEdit> #include<QVBoxLayout> #include<QHBoxLayout> #include<QSettings> #include<qmath.h> LinearAlgebra::LinearAlgebra(QWidget *parent):QWidget(parent) { setupUi(this); setAutoFillBackground(true); QPalette bgpal; bgpal.setColor(QPalette::Background, QColor(187,222,182)); setPalette(bgpal); move(QPoint(350,250)); inputTableWidget->setColumnWidth(0,colWidthSpinBox->value()); outputTableWidget->setColumnWidth(0,colWidthSpinBox->value()); inputTableWidget->installEventFilter(this);//调用installEventFilter()注册需要管理的对象 inputTableWidget->setAcceptDrops(false); //拖放一个 文本文件 this->setAcceptDrops(true); isAssiged[0]=isAssiged[1]=isAssiged[2]=isAssiged[3]=false; isLadder[0]=isLadder[1]=isLadder[2]=isLadder[3]=false; } void LinearAlgebra::closeEvent(QCloseEvent *event) { QMessageBox::StandardButton ret; ret = QMessageBox::question(this, tr("LinearAlgebra Tool"), tr("Are you sure you want to close this Window?"), QMessageBox::Ok| QMessageBox::Cancel,QMessageBox::Cancel); if (ret == QMessageBox::Ok) { event->accept(); } else { event->ignore(); } } void LinearAlgebra::changeRow(int row) { inputTableWidget->setRowCount(row); } void LinearAlgebra::changeCol(int col) { int colWidth=colWidthSpinBox->value(); inputTableWidget->setColumnCount(col); for(int i=0;i<col;++i) inputTableWidget->setColumnWidth(i,colWidth); } void LinearAlgebra::changeColWidth(int colWidth) { int columnCount1=inputTableWidget->columnCount(); int columnCount2=outputTableWidget->columnCount(); for(int i=0;i<columnCount1;++i) inputTableWidget->setColumnWidth(i,colWidth); for(int i=0;i<columnCount2;++i) outputTableWidget->setColumnWidth(i,colWidth); } void LinearAlgebra::on_inputPushButton_clicked() { int row,col; int Index=inputComboBox->currentIndex(); QRegExp regExp("^(-?\\d+)( \\.\\d+)?(/\\d+(\\.\\d+)?)?$"); if(fileInCheckBox->isChecked()) { // QString fileName = QFileDialog::getOpenFileName(this, // tr("打开文件..."),";",tr("Text files (*.txt)")); QFileDialog *fileDialog =new QFileDialog(this, tr("Open File..."),";",tr("Text files (*.txt)")); fileDialog->show(); QString fileName; if(fileDialog->exec()==QDialog::Accepted) { fileName=fileDialog->selectedFiles()[0]; readFile(fileName); } return; } else { QList <QTableWidgetItem *> listItem=inputTableWidget->selectedItems(); for(int i=0;i<listItem.length();i++) //将选中的表格项取消选中 {listItem ->setSelected(false);} row=inputTableWidget->rowCount(); col=inputTableWidget->columnCount(); matrix[Index]=new double*[row]; //分配空间 for(int i=0;i<row;i++) matrix[Index]=new double[col]; QTableWidgetItem *item; QString str; double filling; bool autoFilling=autoFillingCheckBox->isChecked(); if(autoFilling) filling=autoFillingDoubleSpinBox->value(); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { if(item=inputTableWidget->item(i,j)) { str=item->text(); if(str.isEmpty()) goto A; if(regExp.indexIn(str)!=-1) { int ret=str.indexOf('/'); if(ret==-1) matrix[Index][j]=str.toDouble(); else { double dl1,dl2; dl1=str.left(ret).toDouble(); dl2=str.right(str.length()-ret-1).toDouble(); matrix[Index][j]=dl1/dl2; } } else { item->setSelected(true); QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("The item(%1 , %2)is illegal!\nPlease check whether there are abnormal characters or Spaces") .arg(i+1).arg(j+1),QMessageBox::Ok); return ; } } else A: { if(autoFilling==false) { inputTableWidget->setItem(i,j,new QTableWidgetItem("")); inputTableWidget->item(i,j)->setSelected(true); QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("The item(%1 , %2)is empty!\n if you want to fill automatically,Please check the 'Fill' box," "and choose the number that needs to be filled.").arg(i+1).arg(j+1),QMessageBox::Ok); return ; } else { inputTableWidget->setItem(i,j,new QTableWidgetItem(QString().setNum(filling))); matrix[Index][j]=filling; } } } } isAssiged[Index]=true; matrixRow[Index]=row;matrixCol[Index]=col; isLadder[Index]=false; } } void LinearAlgebra::on_outputPushButton_clicked() { int Index=outputComboBox->currentIndex(); if(!isAssiged[Index]) { QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("The matrix that will be outputed hasn't assigned!,please check!"),QMessageBox::Ok); return ; } int row=matrixRow[Index],col=matrixCol[Index]; int precision=precisionSpinBox->value(); const double eps=1e-5; QString str; if(fileOutCheckBox->isChecked()) { QString fileName = QFileDialog::getSaveFileName(this, tr("Output to..."),".",tr("Text files (*.txt)")); if (!fileName.isEmpty()){ QFile file(fileName); file.open(QFile::WriteOnly | QFile::Text); QTextStream out(&file); out<<row<<' '<<col<<endl; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { if(qAbs(matrix[Index][j])<=eps) str.setNum(0); else str.setNum(matrix[Index][j],'g',precision); out<<str<<' '; } out<<endl; } } } else { rowLineEdit->setText(tr("%1").arg(row)); colLineEdit->setText(tr("%1").arg(col)); outputTableWidget->setRowCount(row); outputTableWidget->setColumnCount(col); int colWidth=colWidthSpinBox->value(); for(int i=0;i<col;++i) outputTableWidget->setColumnWidth(i,colWidth); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { if(qAbs(matrix[Index][j])<=eps) str.setNum(0); else str.setNum(matrix[Index][j],'g',precision); outputTableWidget->setItem(i,j,new QTableWidgetItem(str)); } } } } void LinearAlgebra::on_inputComboBox_activated(int Index) { if(!isAssiged[Index]) { inputTableWidget->clear(); inputRowSpinBox->setValue(1); inputColSpinBox->setValue(1); inputTableWidget->setRowCount(1); inputTableWidget->setColumnCount(1); } else { int row=matrixRow[Index],col=matrixCol[Index]; int precision=precisionSpinBox->value(); QString str; inputTableWidget->clear(); inputRowSpinBox->setValue(row); inputColSpinBox->setValue(col); inputTableWidget->setRowCount(row); inputTableWidget->setColumnCount(col); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { str.setNum(matrix[Index][j],'g',precision); inputTableWidget->setItem(i,j,new QTableWidgetItem(str)); } } } } void LinearAlgebra::on_outputComboBox_activated(int Index) { if(!isAssiged[Index]) { outputTableWidget->clear(); outputTableWidget->setRowCount(1); outputTableWidget->setColumnCount(1); return ; } int row=matrixRow[Index],col=matrixCol[Index]; int precision=precisionSpinBox->value(); QString str; rowLineEdit->setText(tr("%1").arg(row)); colLineEdit->setText(tr("%1").arg(col)); outputTableWidget->setRowCount(row); outputTableWidget->setColumnCount(col); int colWidth=colWidthSpinBox->value(); for(int i=0;i<col;++i) outputTableWidget->setColumnWidth(i,colWidth); const double eps=1e-5; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { if(qAbs(matrix[Index][j])<=eps) str.setNum(0); else str.setNum(matrix[Index][j],'g',precision); outputTableWidget->setItem(i,j,new QTableWidgetItem(str)); } } } void LinearAlgebra::on_Calc_pushButton_clicked() { bool isLegal=false; if(assign_radioButton->isChecked()) { int Index1=assignFirst_ComboBox->currentIndex(); int Index2=assignSecond_ComboBox->currentIndex(); assign(Index1,Index2); fileOutCheckBox->setChecked(false); outputComboBox->setCurrentIndex(Index1); on_outputPushButton_clicked(); return ; } if(basicCalc_radioButton->isChecked()) //基本运算 { int Index=basicFirst_ComboBox->currentIndex(); int Index1=basicSecond_ComboBox->currentIndex(); int Index2=basicFourth_ComboBox->currentIndex(); if(!isAssiged[Index1]||!isAssiged[Index2]) { QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("There is a matrix hasn't assigned,please check!"),QMessageBox::Ok); return; } int ret=basicThird_ComboBox->currentIndex(); if(ret==0) {isLegal=add(Index,Index1,Index2);} else if(ret==1) {isLegal=subtract(Index,Index1,Index2);} else {isLegal=multiply(Index,Index1,Index2);} if(isLegal&&autoShowResultCheckBox->isChecked()) { fileOutCheckBox->setChecked(false); outputComboBox->setCurrentIndex(Index); on_outputPushButton_clicked(); return ; } } int Index=choiceComboBox->currentIndex(); if(!isAssiged[Index]) { QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("The matrix that will be Calculated hasn't assigned,please check!"),QMessageBox::Ok); return; } if(det_radioButton->isChecked()) {det(Index,true);return;} else if(rank_radioButton->isChecked()) {rank(Index);return;} else if(transpose_radioButton->isChecked()) {isLegal=transpose(Index);} else if(adjoint_radioButton->isChecked()) {isLegal=adjoint(Index);} else if(ladder_radioButton->isChecked()) {isLegal=ladder(Index);} else if(inver_radioButton->isChecked()) {isLegal=inver(Index);} else if(generInver_radioButton->isChecked()) {isLegal=generInver(Index);} else if(matrixQR_radioButton->isChecked()) {isLegal=matrixQR(Index);} else if(eigenvalue_radioButton->isChecked()) {eigenValue(Index,true);return;} else if(eigenvector_radioButton->isChecked()) {eigenVector(Index);return ;} else if(isOrthogonal_radioButton->isChecked()) {isOrthogonal(Index);return;} else if(maxEigenValue_radioButton->isChecked()) {maxEigenValue(Index);return;} else if(eigenpolyomial_radioButton->isChecked()) {eigenPolyomial(Index);return ;} else if(homogen_radioButton->isChecked()) {homogen(Index);return;} else if(inhomogen_radioButton->isChecked()) {isLegal=inhomogen(Index);} if(isLegal&&autoShowResultCheckBox->isChecked()) { fileOutCheckBox->setChecked(false); outputComboBox->setCurrentIndex(3); on_outputPushButton_clicked(); } } bool LinearAlgebra::eventFilter(QObject *target, QEvent *event) { if(target ==inputTableWidget)//此判断可以省略,此程序中必进入此判断,但是有此句判断让程序更加规范 { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); int code=keyEvent->key(); if(code==Qt::Key_Return||code==Qt::Key_Enter) { int row=inputTableWidget->currentRow(); int col=inputTableWidget->currentColumn(); int rowNum=inputRowSpinBox->value(),colNum=inputColSpinBox->value(); if(row+1==rowNum&&col+1==colNum) { fileInCheckBox->setChecked(false); on_inputPushButton_clicked(); return true; } else { if(col+1==colNum) {inputTableWidget->setCurrentCell(row+1,0);} else {inputTableWidget->setCurrentCell(row,col+1);} return true; } } else { return false; } } else return false; } return QObject::eventFilter(target, event); } void LinearAlgebra::dragEnterEvent(QDragEnterEvent *event) { if(event->mimeData()->hasFormat("text/uri-list")); { event->acceptProposedAction(); } //中调用 acceptProposeAction() 函数,我们就可以向用户暗示,你可以将拖动的对 //象放在这个组件上。默认情况下,组件是不会接受拖放的。如果我们调用了这样的函数,那么 //Qt会自动地以光标 来提示用户是否可以将对象放在组件上。在这里,我们希望告诉用户, //窗口可以接受拖放 } void LinearAlgebra::dropEvent(QDropEvent *event) { QList<QUrl> urls=event->mimeData()->urls(); if(urls.isEmpty()){return ;} QString fileName=urls.first().toLocalFile(); if(fileName.isEmpty()){return ;} readFile(fileName); return ; } bool LinearAlgebra::readFile(const QString &fileName) { bool r=false; QFile file(fileName); QTextStream in(&file); if(file.open(QIODevice::ReadOnly))//可以打开 { int row,col; int Index=inputComboBox->currentIndex(); in>>row>>col; if(row>100||col>100||row<1||col<1) { QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("The rows or cols are beyond range!"),QMessageBox::Ok); return 0; } matrix[Index]=new double*[row]; //分配空间 for(int i=0;i<row;i++) matrix[Index]=new double[col]; for(int i=0;i<row;i++) for(int j=0;j<col;j++) in>>matrix[Index][j]; file.close(); inputRowSpinBox->setValue(row); inputColSpinBox->setValue(col); inputTableWidget->setRowCount(row); inputTableWidget->setColumnCount(col); QString str; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { str.setNum(matrix[Index][j],'g'); inputTableWidget->setItem(i,j,new QTableWidgetItem(str)); } } isAssiged[Index]=true; matrixRow[Index]=row;matrixCol[Index]=col; isLadder[Index]=false; r=true; } return r; } void LinearAlgebra::assign(int Index1, int Index2) { if(Index1==Index2) return ; int row=matrixRow[Index1]=matrixRow[Index2]; int col=matrixCol[Index1]=matrixCol[Index2]; isAssiged[Index1]=true; isLadder[Index1]=false; matrix[Index1]=new double*[row]; //分配空间 for(int i=0;i<row;i++) { matrix[Index1]=new double[col]; for(int j=0;j<col;j++) matrix[Index1][j]=matrix[Index2][j]; } } void LinearAlgebra::assign(int Index, double **a, int row, int col) { matrixRow[Index]=row; matrixCol[Index]=col; isAssiged[Index]=true; isLadder[Index]=false; matrix[Index]=new double*[row]; //分配空间 for(int i=0;i<row;i++) { matrix[Index]=new double[col]; for(int j=0;j<col;j++) matrix[Index][j]=a[j]; } } bool LinearAlgebra::add(int Index,int Index1,int Index2) { if(matrixRow[Index1]!=matrixRow[Index2]||matrixCol[Index1]!=matrixCol[Index2]) { QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("It does not comply with the addition principle!"),QMessageBox::Ok); return 0; } int row=matrixRow[Index]=matrixRow[Index1]; int col=matrixCol[Index]=matrixCol[Index1]; isAssiged[Index]=true; isLadder[Index]=false; if(Index==Index1) { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) matrix[Index][j]+=matrix[Index2][j]; } return 1; } if(Index==Index2) { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) matrix[Index][j]+=matrix[Index1][j]; } return 1; } matrix[Index]=new double*[row]; //分配空间 for(int i=0;i<row;i++) { matrix[Index]=new double[col]; for(int j=0;j<col;j++) matrix[Index][j]=matrix[Index1][j]+matrix[Index2][j]; } return 1; } bool LinearAlgebra::subtract(int Index, int Index1, int Index2) { if(matrixRow[Index1]!=matrixRow[Index2]||matrixCol[Index1]!=matrixCol[Index2]) { QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("It does not comply with the subtraction principle!"),QMessageBox::Ok); return 0; } int row=matrixRow[Index]=matrixRow[Index1]; int col=matrixCol[Index]=matrixCol[Index1]; isAssiged[Index]=true; isLadder[Index]=false; if(Index==Index1) { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) matrix[Index][j]-=matrix[Index2][j]; } return 1; } if(Index==Index2) { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) matrix[Index][j]=matrix[Index1][j]-matrix[Index2][j]; } return 1; } matrix[Index]=new double*[row]; //分配空间 for(int i=0;i<row;i++) { matrix[Index]=new double[col]; for(int j=0;j<col;j++) matrix[Index][j]=matrix[Index1][j]-matrix[Index2][j]; } return 1; } bool LinearAlgebra::multiply(int Index, int Index1, int Index2) { int i,j,k; if(matrixCol[Index1]!=matrixRow[Index2]) { QMessageBox::warning(this, tr("LinearAlgebra Tool"), tr("It does not meet the multiplication principle!"),QMessageBox::Ok); return 0; } double **dl; int row=matrixRow[Index1]; int col=matrixCol[Index2]; int col_row=matrixCol[Index1]; dl=new double *[row]; for(i=0;i<row;i++) { dl=new double[col]; } for(i=0;i<row;i++) for(j=0;j<col;j++) { for(k=0,dl[j]=0;k<col_row;k++) dl[j]+=matrix[Index1][k]*matrix[Index2][k][j]; } isAssiged[Index]=true; isLadder[Index]=false; matrix[Index]=new double*[row]; matrixRow[Index]=row,matrixCol[Index]=col; for(i=0;i<row;i++) { matrix[Index]=new double[col]; for(j=0;j<col;j++) {matrix[Index][j]=dl[j];} delete []dl; } delete []dl; return 1; } void LinearAlgebra::multiply(int Index1, double n, int Index2) { int i,j; int row=matrixRow[Index2],col=matrixCol[Index1]; isAssiged[Index1]=true; isLadder[Index1]=false; if(Index1==Index2) { isLadder[Index1]=false; for(i=0;i<row;++i) for(j=0;j<col;++j) {matrix[Index1][j]*=n;} } else { matrix[Index1]=new double*[row]; matrixRow[Index1]=row,matrixCol[Index1]=col; for(i=0;i<row;i++) { matrix[Index1]=new double[col]; for(j=0;j<col;j++) {matrix[Index1][j]=n*matrix[Index2][j];} } } } bool LinearAlgebra::transpose(int Index) { int col=matrixCol[3]=matrixRow[Index]; int row=matrixRow[3]=matrixCol[Index]; isAssiged[3]=true; isLadder[3]=false; matrix[3]=new double*[row]; for(int i=0;i<row;i++) { matrix[3]=new double[col]; for(int j=0;j<col;j++) {matrix[3][j]=matrix[Index][j];} } return 1; }
|