The Link Your Class | https://bbs.csdn.net/forums/ssynkqtd-04 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617378696 |
Link to the finished project code | github |
The Aim of This Assignment | Construct a back-end separation calculator |
MU STU ID and FZU STU ID | <21125210_832102106> |
Catalog
I. PSP form
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning(total) | 500 | 700 |
• Estimate | 500 | 700 |
Development(total) | 360 | 440 |
• Analysis | 50 | 70 |
• Design Spec | 15 | 20 |
• Design Review | 15 | 20 |
• Coding Standard | 15 | 20 |
• Design | 50 | 60 |
• Coding | 125 | 140 |
• Code Review | 50 | 60 |
• Test | 40 | 50 |
Reporting(total) | 120 | 120 |
• Test Repor | 10 | 10 |
• Size Measurement | 10 | 10 |
• Postmortem & Process Improvement Plan | 100 | 100 |
Sum | 980 | 1260 |
II. Introduction
When I set out to develop a scientific calculator, my goal was to create a powerful application with data storage and retrieval capabilities. I chose to use C# and WinForms for building this application and integrated a MySQL database to store and retrieve calculation history. In this blog post, I’ll share my development experience and the key features of this scientific calculator.
Link to the finished project code:Link
III. Development Environment and Technology Choices
I chose to use C# and WinForms because they provide robust tools for user interface design and seamless interaction with MySQL databases. Here are the primary technologies and tools I used:
1. C#
Serving as the primary programming language for the application, C# offers a wide range of features, including object-oriented programming, event handling, and graphical user interface design.
2. WinForms
WinForms is Microsoft’s library for creating Windows desktop applications. It boasts a rich set of UI controls and an easy-to-use designer.
3. MySQL Database
I employed a MySQL database system for storing calculation history records. MySQL was my database of choice due to its open-source nature and powerful relational database capabilities.
IV. Key Features and User Interface Design
1. User-Friendly Interface
I designed an intuitive and user-friendly interface with buttons and a text box. Users can easily perform calculations and access their history records. The calculation history is automatically updated with each calculation performed.
The code of Form.Designer.cs:
2. Calculation Functions
My scientific calculator includes standard mathematical and scientific functions, such as addition, subtraction, multiplication, division, square root, exponentiation, trigonometric functions, and more. Users can perform these calculations by clicking buttons on the interface. Results are displayed in a text box.
The code of Form.cs:
3. History Records
To implement the history records feature, I set up a MySQL database to store users’ calculation history. Each time a user performs a new calculation, the expressio and result are recorded in the database. This enables users to retrieve previous calculation records at any time.
V. Interaction with the MySQL Database
To interact with the MySQL database, I used the MySQL Connector library. This allowed me to establish a connection to the database, execute SQL queries, and update the history records table. Here are the main steps I followed for interacting with the database:
1. Create MySQL Database
I create a table named “CalculationHistory” with columns for the calculation string and its result.
CREATE TABLE CalculationHistory (
ID INT AUTO_INCREMENT PRIMARY KEY,
CalculationString VARCHAR(255),
Result VARCHAR(255)
);
2. Connecting to the Database
- To import the mysql database reference, in Explorer, right-click on the project and select Nuget Package
- Search for mysql and install
- Importing references to mysql and defining global variables
using MySql.Data.MySqlClient;
MySqlConnection con;
I established a connection to the MySQL database when the application was launched. I used a connection string to specify the database’s location, username, and password.
string connStr = "server=127.0.0.1;port=3306;user=root;password=Aa123456;database=calculator;";
con = new MySqlConnection(connStr);
3. Inserting History Records
Whenever a user performed a calculation, I created an INSERT query to insert the calculation expression and result into the history records table.
try
{
//First calculates any equations between brackets
List<string> newEquations = calculateBrackets(equations);
//Once the bracket equations have been evaluated, uses theh calculate method to calculate the final answer
txtDisplay.Text = calculate(newEquations);
con.Open();
string result = txtDisplay.Text;
// Save the calculation and result to the database
string query = "INSERT INTO CalculationHistory (CalculationString, Result) VALUES (@CalculationString, @Result)";
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@CalculationString", calculationString);
cmd.Parameters.AddWithValue("@Result", result);
cmd.ExecuteNonQuery();
}
}
catch (Exception exc)
{
txtDisplay.Text = "ERROR - Invalid Input";
}
4. Retrieving History Records
When a user wanted to view their history records, I executed a SELECT query to retrieve the ten most recent entries and displayed them in the application’s history
panel.
private void ans_Click(object sender, EventArgs e)
{
string connStr = "server=127.0.0.1;port=3306;user=root;password=Aa123456;database=calculator;";
con = new MySqlConnection(connStr);
try
{
con.Open();
string history = "";
string query = "SELECT CalculationString, Result FROM CalculationHistory ORDER BY ID DESC LIMIT 10";
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read() )
{
string calculationString = reader.GetString("CalculationString");
string result = reader.GetString("Result");
history = history+calculationString + " = " + result + "\r\n";
}
}
}
MessageBox.Show(history,"History");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
VI. Flow Chart
VII. Additional Features
Bracket Calculation Function: The calculator can handle mathematical expressions enclosed in brackets, perform calculations within the brackets, and return the results. This can be used for complex expressions and equations.
Trigonometric and Other Mathematical Functions: The calculator supports trigonometric functions (sin, cos, tan), exponential functions (exp), logarithmic functions (log), and more. You can input these functions in the input field, and the calculator will execute the corresponding mathematical operations.
Memory Function: The calculator has a memory function, including storage(MS), retrieval(MR), clearing of memory(MC), and addition(M+) and subtraction(M-) operations in memory. This allows you to store intermediate results during calculations and retrieve or modify them when needed.
VIII. Summary
By adopting a front-end and back-end separation approach, I successfully developed a powerful scientific calculator with a database. This approach not only enhances development but also lays a foundation for scaling the application and making future improvements.This project provided me with valuable insights into WinForms interface design and MySQL database operations, enhancing my development skills.