简易电子表格的设计
问题描述
设计一个支持基本计算统计功能和其它一些表格管理/处理功能的软件,使用户可在该软件的支持下,用交互方式进行表格建立、数据输入、数据编辑及其它一些表格操作。即类似于简易Execel表格处理软件。
基本要求
(1)建立表格:建立空白表格,同时在屏幕上显示,使其处于可输入数据的状态。用户可指定新建表格的行列数。
(2)输入数据与编辑数据:通过键盘将数据输入到显示在屏幕上的电子表上。
(3)基本统计计算:可选择按行或列合计、求平均、求最大最小。
(4)排序:使任意指定的行或列中的数据按大小(升或降)排列,对字符型据,还可选择大小写敏感。
(5)表格保存:使电子表格以磁盘文件的方式存储在磁盘上,并可随时读入,供继续处理。
(6)公式支持:单元格内可输入公式(表达式),使对应单元格的最终内容为公式的计算结果。公式最基本的形式是算术计算公式,可按名引用其它单元格。
算法思路
void MyExcel::on_ConfirmButton_pressed()
{int calculate_row=-1;int calculate_column=-1;if(ui->RCBox->currentText()=="行"){calculate_row=ui->lineEdit->text().toInt();if(calculate_row<1||calculate_row>ui->tableWidget->columnCount()){QMessageBox::about(this,"Warning","输入错误的行数");}}else if(ui->RCBox->currentText()=="列"){calculate_column=ui->lineEdit->text().toInt();if(calculate_column<1||calculate_column>ui->tableWidget->columnCount()){QMessageBox::about(this,"Warning","输入错误的列数");}}if(ui->FunctionBox->currentText()=="合计"){if(calculate_row!=-1){if(SumColumnPos==-1){SumColumnPos=ui->tableWidget->columnCount();ui->tableWidget->insertColumn(ui->tableWidget->columnCount());//计算某一行的合计时,就是插入新的一列,专门用于计算合计的}double sum=0;for(int i=0;i<Column;i++){if(ui->tableWidget->item(calculate_row-1,i)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{sum=sum+ui->tableWidget->item(calculate_row-1,i)->text().toDouble();}qDebug()<<sum;}QTableWidgetItem *SUM=new QTableWidgetItem(QString::number(sum));ui->tableWidget->setItem(calculate_row-1,SumColumnPos,SUM);}else if(calculate_column!=-1){if(SumRowPos==-1){SumRowPos=ui->tableWidget->rowCount();ui->tableWidget->insertRow(ui->tableWidget->rowCount());//就是插入新的一行,专门用于计算合计的}double sum=0;for(int i=0;i<Row;i++){if(ui->tableWidget->item(i,calculate_column-1)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{sum=sum+ui->tableWidget->item(i,calculate_column-1)->text().toDouble();}qDebug()<<sum;}QTableWidgetItem *SUM=new QTableWidgetItem(QString::number(sum));ui->tableWidget->setItem(SumRowPos,calculate_column-1,SUM);}}else if(ui->FunctionBox->currentText()=="求平均"){if(calculate_row!=-1){if(AverageColumnPos==-1){AverageColumnPos=ui->tableWidget->columnCount();ui->tableWidget->insertColumn(ui->tableWidget->columnCount());//计算某一行的平均时,就是插入新的一列,专门用于计算合计的}double sum=0;for(int i=0;i<Column;i++){if(ui->tableWidget->item(calculate_row-1,i)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{sum=sum+ui->tableWidget->item(calculate_row-1,i)->text().toDouble();}qDebug()<<sum;}QTableWidgetItem *Average=new QTableWidgetItem(QString::number(sum/Column));ui->tableWidget->setItem(calculate_row-1,AverageColumnPos,Average);}else if(calculate_column!=-1){if(AverageRowPos==-1){AverageRowPos=ui->tableWidget->rowCount();ui->tableWidget->insertRow(ui->tableWidget->rowCount());//计算某一行的平均时,就是插入新的一列,专门用于计算合计的}double sum=0;for(int i=0;i<Row;i++){if(ui->tableWidget->item(i,calculate_column-1)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{sum=sum+ui->tableWidget->item(i,calculate_column-1)->text().toDouble();}qDebug()<<sum;}QTableWidgetItem *Average=new QTableWidgetItem(QString::number(sum/Row));ui->tableWidget->setItem(AverageRowPos,calculate_column-1,Average);}}else if(ui->FunctionBox->currentText()=="最大值"){if(calculate_row!=-1){if(MaxColumnPos==-1){MaxColumnPos=ui->tableWidget->columnCount();ui->tableWidget->insertColumn(ui->tableWidget->columnCount());//计算某一行的平均时,就是插入新的一列,专门用于计算合计的}double maxNumber=0;for(int i=0;i<Column;i++){if(maxNumber<ui->tableWidget->item(calculate_row-1,i)->text().toDouble()){if(ui->tableWidget->item(calculate_row-1,i)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{maxNumber=ui->tableWidget->item(calculate_row-1,i)->text().toDouble();}qDebug()<<maxNumber;}}QTableWidgetItem *MaxNumber=new QTableWidgetItem(QString::number(maxNumber));ui->tableWidget->setItem(calculate_row-1,MaxColumnPos,MaxNumber);}else if(calculate_column!=-1){if(MaxRowPos==-1){MaxRowPos=ui->tableWidget->rowCount();ui->tableWidget->insertRow(ui->tableWidget->rowCount());//计算某一行的平均时,就是插入新的一列,专门用于计算合计的}double maxNumber=0;for(int i=0;i<Row;i++){if(maxNumber<ui->tableWidget->item(i,calculate_column-1)->text().toDouble()){if(ui->tableWidget->item(i,calculate_column-1)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{maxNumber=ui->tableWidget->item(i,calculate_column-1)->text().toDouble();}qDebug()<<maxNumber;}}QTableWidgetItem *MaxNumber=new QTableWidgetItem(QString::number(maxNumber));ui->tableWidget->setItem(MaxRowPos,calculate_column-1,MaxNumber);}}else if(ui->FunctionBox->currentText()=="最小值"){if(calculate_row!=-1){if(MinColumnPos==-1){MinColumnPos=ui->tableWidget->columnCount();ui->tableWidget->insertColumn(ui->tableWidget->columnCount());//计算某一行的平均时,就是插入新的一列,专门用于计算合计的}double minNumber=ui->tableWidget->item(calculate_row-1,0)->text().toDouble();for(int i=0;i<Column;i++){if(minNumber>ui->tableWidget->item(calculate_row-1,i)->text().toDouble()){if(ui->tableWidget->item(calculate_row-1,i)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{minNumber=ui->tableWidget->item(calculate_row-1,i)->text().toDouble();}qDebug()<<minNumber;}}QTableWidgetItem *MinNumber=new QTableWidgetItem(QString::number(minNumber));ui->tableWidget->setItem(calculate_row-1,MinColumnPos,MinNumber);}else if(calculate_column!=-1){if(MinRowPos==-1){MinRowPos=ui->tableWidget->rowCount();ui->tableWidget->insertRow(ui->tableWidget->rowCount());//计算某一行的平均时,就是插入新的一列,专门用于计算合计的}double minNumber=ui->tableWidget->item(0,calculate_column-1)->text().toDouble();for(int i=0;i<Row;i++){if(minNumber>ui->tableWidget->item(i,calculate_column-1)->text().toDouble()){if(ui->tableWidget->item(i,calculate_column-1)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{minNumber=ui->tableWidget->item(i,calculate_column-1)->text().toDouble();}qDebug()<<minNumber;}}QTableWidgetItem *MinNumber=new QTableWidgetItem(QString::number(minNumber));ui->tableWidget->setItem(MinRowPos,calculate_column-1,MinNumber);}}else if(ui->FunctionBox->currentText()=="升序排序"){if(calculate_row!=-1){double array[100];for(int i=0;i<Column;i++){if(ui->tableWidget->item(calculate_row-1,i)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{array[i]=ui->tableWidget->item(calculate_row-1,i)->text().toDouble();}}std::sort(array,array+Column);for(int i=0;i<Column;i++){QTableWidgetItem *arrayItem=new QTableWidgetItem(QString::number(array[i]));ui->tableWidget->setItem(calculate_row-1,i,arrayItem);}}else if(calculate_column!=-1){double array[100];for(int i=0;i<Row;i++){if(ui->tableWidget->item(i,calculate_column-1)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{array[i]=ui->tableWidget->item(i,calculate_column-1)->text().toDouble();}}std::sort(array,array+Row);for(int i=0;i<Row;i++){QTableWidgetItem *arrayItem=new QTableWidgetItem(QString::number(array[i]));ui->tableWidget->setItem(i,calculate_column-1,arrayItem);}}}else if(ui->FunctionBox->currentText()=="降序排序"){if(calculate_row!=-1){double array[100];for(int i=0;i<Column;i++){if(ui->tableWidget->item(calculate_row-1,i)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{array[i]=ui->tableWidget->item(calculate_row-1,i)->text().toDouble();}}std::sort(array,array+Column);for(int i=Column-1;i>=0;i--){QTableWidgetItem *arrayItem=new QTableWidgetItem(QString::number(array[i]));ui->tableWidget->setItem(calculate_row-1,Column-1-i,arrayItem);}}else if(calculate_column!=-1){double array[100];for(int i=0;i<Row;i++){if(ui->tableWidget->item(i,calculate_column-1)==0){QMessageBox::about(this,"Warning","有格没有被定义");break;}else{array[i]=ui->tableWidget->item(i,calculate_column-1)->text().toDouble();}}std::sort(array,array+Row);for(int i=Row-1;i>=0;i--){QTableWidgetItem *arrayItem=new QTableWidgetItem(QString::number(array[i]));ui->tableWidget->setItem(Row-1-i,calculate_column-1,arrayItem);}}}//调整行列宽度,避免和原来的表格数据混淆for(int i=Column;i<ui->tableWidget->columnCount();i++){ui->tableWidget->setColumnWidth(i,5);}for(int i=Column;i<ui->tableWidget->rowCount();i++){ui->tableWidget->setRowHeight(i,3);}
}