Back-end separation calculator based on C# Winform and MySQL Database

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617378696
Link to the finished project codegithub
The Aim of This AssignmentConstruct a back-end separation calculator
MU STU ID and FZU STU ID<21125210_832102106>

I. PSP form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning(total)500700
• Estimate500700
Development(total)360440
• Analysis5070
• Design Spec1520
• Design Review1520
• Coding Standard1520
• Design5060
• Coding125140
• Code Review5060
• Test4050
Reporting(total)120120
• Test Repor1010
• Size Measurement1010
• Postmortem & Process Improvement Plan100100
Sum9801260

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

frmCalculator
Database Connection
string memory
Event Handler
inputNumberOrOperator
btnClear_Click
btnBackspace_Click
btnEquals_Click
calculateBrackets
calculate
con.Open
con.Close
ans_Click

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值