176
社区成员
发帖
与我相关
我的任务
分享| Course for This Assignment | https://bbs.csdn.net/topics/617378696 |
| Assignment Requirements | Calculator Features and Page Updates |
| Objectives of This Assignment | Add new arithmetic function, replace interface |
| Other References | Qt教程,Qt5编程入门教程(非常详细) (biancheng.net) |
Github:https://github.com/SenYuWolf/AssignmentCalc.git
Based on the last assignment, I have added trigonometry, logarithmic and exponential (and root sign) functions to the calculator, changed the final output to scientific notation with two decimal places, and the ability to use "x10^n" directly in the calculator.
Also, I improved the data storage function as an alternative to the parenthesis operation, which allows you to store, read, add, subtract, and clear the current data.Users can also use "Ans" to read the last output directly.
Finally, I made some page beautification according to the requirements, which is easy for users to recognize and use.
——————————UPGRADE——————————
I used SQLite to implement the ability to store the last ten calculations using a local database.
| Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
| Planning | ||
| • Estimate | 5 | 5 |
| Development | ||
| • Analysis | 15 | 10 |
| • Design Spec | 3 | 5 |
| • Design Review | 3 | 5 |
| • Coding Standard | 3 | 5 |
| • Design | 15 | 20 |
| • Coding | 10 | 5 |
| • Code Review | 5 | 3 |
| • Test | 5 | 3 |
| Reporting | ||
| • Test Report | 5 | 5 |
| • Size Measurement | 5 | 5 |
| • Postmortem & Process Improvement Plan | 10 | 10 |
| Sum | 84 | 81 |
After seeing the requirements, the first step was to analyze how to add the requirements within the existing logic, the easy additions were trigonometric, logarithmic, and root (exponential) operations, as well as reading the last output.
I chose to directly output the result of trigonometry and "Ans" with just one number.
else if(name == "Ans")
{
ui->lineEdit->setText(vec[3].toString());
}
else if(name == "sin")
{
auto num = qSin(ui->lineEdit->text().toFloat());
QString str = QString::number(num, 'f', 2);
ui->lineEdit->setText(str);
}
else if(name == "cos")
{
auto num = qCos(ui->lineEdit->text().toFloat());
QString str = QString::number(num, 'f', 2);
ui->lineEdit->setText(str);
}
Logarithmic and exponential operations are placed in the "=" determination because they involve two numbers.
else if(vec[1] == "log"){
vec[3] = qLn(vec[0].toFloat()) / qLn(vec[2].toFloat());
}
else if(vec[1] == "a^n"){
vec[3] = qPow(vec[0].toFloat(),vec[2].toFloat());
}
After this I thought about what the requirements for scientific notation were, and finally decided to convert the result directly to scientific notation and add the "x10^n" function.
QString result = QString::number(vec[3].toFloat(), 'e', 2);
vec[3] = result;
ui->lineEdit->setText(result);
else if(name == "x10^n")
{
vec[0] = val;
vec[1] = "x10^n";
ui->lineEdit->clear();
}
Finally, since I was using the old step-by-step calculator method, I was unable to realize the operation of entering parentheses directly, and after thinking about it, I chose the alternative method of data storage.
else if(name == "MR")
{
ui->lineEdit->setText(vec[4].toString());
}
else if(name == "MS")
{
vec[4] = ui->lineEdit->text();
}
else if(name == "MC")
{
vec[4].clear();
}
else if(name == "M+")
{
vec[4] = vec[4].toFloat() + ui->lineEdit->text().toFloat();
vec[4].toString();
}
else if(name == "M-")
{
vec[4] = vec[4].toFloat() - ui->lineEdit->text().toFloat();
vec[4].toString();
}
This completes all the requirements for the assignment.
——————————UPGRADE——————————
I originally used MySql to build the database, and then realized that the MySql system was too large to be used as a database for a small desktop application, so I switched to using SQLite to build the database. By connecting to the database, opening the database, and performing a series of operations on the table to realize the function of storing and reading the calculation history.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SqliteOperator sqlTest;
sqlTest.openDb();
QString str1 = QString("Calculate");
sqlTest.deleteTable(str1);
sqlTest.createTable();
QString str2 = QString("Calculate");
qDebug() << "isTabelExist:" <<sqlTest.isTableExist(str2);
Widget w;
w.show();
return a.exec();
}
SqliteOperator::SqliteOperator()
{
if (QSqlDatabase::contains("Calcu"))
{
database = QSqlDatabase::database("Calcu");
}
else
{
// 建立和SQlite数据库的连接
database = QSqlDatabase::addDatabase("QSQLITE");
// 设置数据库文件的名字
database.setDatabaseName("Calcu.db");
}
}
bool SqliteOperator::openDb()
{
if (!database.open())
{
qDebug() << "Error: Failed to connect database." << database.lastError();
}
else
{
}
return true;
}
void SqliteOperator::createTable()
{
QSqlQuery sqlQuery;
QString createtable = QString("CREATE TABLE Calculate (\
id INT PRIMARY KEY NOT NULL,\
num1 TEXT NOT NULL,\
Calc TEXT NOT NULL,\
num2 TEXT NOT NULL,\
resl TEXT NOT NULL)");
sqlQuery.prepare(createtable);
if(!sqlQuery.exec())
{
qDebug() << "Error: Fail to create table. " << sqlQuery.lastError();
}
else
{
qDebug() << "Table created!";
}
}
bool SqliteOperator::isTableExist(QString& tableName)
{
QSqlDatabase database = QSqlDatabase::database();
if(database.tables().contains(tableName))
{
return true;
}
return false;
}
void SqliteOperator::deleteTable(QString& tableName)
{
QSqlQuery sqlQuery;
sqlQuery.exec(QString("DROP TABLE %1").arg(tableName));
if(sqlQuery.exec())
{
qDebug() << sqlQuery.lastError();
}
else
{
qDebug() << "deleted table success";
}
}
QSqlQuery sqlQuery;
sqlQuery.prepare("REPLACE INTO Calculate VALUES(:id,:num1,:Calc,:num2,:resl)");
sqlQuery.bindValue(":id", realid);
sqlQuery.bindValue(":num1", res[0]);
sqlQuery.bindValue(":Calc", res[1]);
sqlQuery.bindValue(":num2", res[2]);
sqlQuery.bindValue(":resl", res[3]);
if(!sqlQuery.exec())
{
qDebug() << "Error: Fail to insert data. " << sqlQuery.lastError();
}
else
{
qDebug() << "data has been inserted";
}
}
else if(name == "HISTORY")
{
QSqlQuery sqlQuery;
ui->plainTextEdit->clear();
sqlQuery.exec("SELECT * FROM Calculate");
if(!sqlQuery.exec())
{
qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
}
else
{
while(sqlQuery.next())
{
QMessageBox result;
int id = sqlQuery.value(0).toInt();
QString num1 = sqlQuery.value(1).toString();
QString Calc = sqlQuery.value(2).toString();
QString num2 = sqlQuery.value(3).toString();
QString resl = sqlQuery.value(4).toString();
ui->plainTextEdit->appendPlainText(QString("id:%1 num1:%2 Calc:%3 num2:%4 resl:%5").arg(id).arg(num1).arg(Calc).arg(num2).arg(resl));
}
}
}
Basic arithmetic

Advanced arithmetic

Data manipulation

——————————UPGRADE——————————
