A Calculator For Advanced Requirements

A Calculator For Advanced Requirements

Overall introduction

In terms of this blog,

1.Create a calculator with a visual interface.

2.Record necessary work content and process.

Link to the finished project code:https://github.com/0128130Raven/EE308_calc/tree/main/calc_advanced

Program Requirements

  1. Basic requirement: Implement addition, subtraction, multiplication, division, and clear functions.
  2. Advanced requirement: Implement functionality for exponentiation, trigonometric functions, and more.
The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This Assignmentan advanced calculator
MU STU ID and FZU STU IDMU:21126747 | FZU:832101220

I. PSP form for this work

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning
• Estimate
30
15
Development
• Analysis
20
17
• Design Spec
3
3
• Design Review
10
10
• Coding Standard
2
2
• Design
10
10
• Coding
30
30
• Code Review
10
12
• Test
10
10
Reporting
• Test Report
30
30
• Size Measurement
5
5
• Postmortem & Process Improvement Plan
20
10
Sum
180
154

II. Problem-solving ideas for designation

Take program requirements into consideration and develop as fast as possible, here select web language.
(framworks to be neglected temporarily like PHP, Vue, React)

  1. necessary for input values including numbers and operations as well as functions to be advanced (HTML)
  2. handle with click events or keyboard pressed (JS)
  3. display as a simple and clean interface (CSS)

III. Practical implementation and process


here, a flow chart as shown to be convenience:
flow chart


IV. Code description

HTML to achieve key interface visualization
<div id="c_text">
   <input type="text" id="result" value="0" readonly="readonly" />
</div>
<div>
   <button class="button" onclick="appendToResult('(')">(</button>
   <button class="button" onclick="appendToResult(')')">)</button>
   <button class="button" onclick="appendToResult('^')">^</button>
   <button class="button" onclick="appendToResult('log(')">log</button>
   <button class="button" onclick="appendToResult('exp(')">exp</button>
   <button class="button" onclick="appendToResult('pow(')">pow</button>
   <button class="button" onclick="appendToResult('ceil(')">ceil</button>
   <button class="button" onclick="appendToResult('floor(')">floor</button>
   <button class="button" onclick="appendToResult('sin(')">sin</button>
   <button class="button" onclick="appendToResult('cos(')">cos</button>
   <button class="button" onclick="appendToResult('tan(')">tan</button>
   <button class="button" onclick="appendToResult('cot(')">cot</button>
 </div>
   <!-- Add other compound function buttons, such as sin, cos, etc -->

CSS handles interface styling and rendering
body {
    font-family: Arial, sans-serif;
    text-align: center;
  }

  #calculator {
    width: 300px;
    margin: 50px auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  }
  #c_text {
    margin: auto;
  }
  #result {
    display: block;
    padding: 0;
    font-size: 24px;
    margin-bottom: 10px;
    text-align: right;
    
  }
  #result:focus{
    outline: none;
  }
  .button {
    width: 50px;
    height: 50px;
    margin: 5px;
    text-align: center;
    font-size: 18px;
    cursor: pointer;
    border: 1px solid #ccc;
    border-radius: 5px;
    background-color: #f5f5f5;
  }

  .button:active,.button:hover {
    background-color: #e0e0e0;
  }

JS handles user operations and calculations
Main program:
 1. Analytic expression
 2. Handle user keyboard input and button click
 3. Processing computation expression
 4. Automatically cleared
  1. Analytic expression
function evaluateExpression(expression) {
  // Use eval to evaluate expressions, as well as more complex expression parsing libraries  
  // Add custom functions, such as log, exp, pow, etc
  expression = expression.replace(/log/g, "Math.log");
  expression = expression.replace(/exp/g, "Math.exp");
  expression = expression.replace(/pow/g, "Math.pow");
  // Add other functions, such as round, floor, ceil, sqrt, and so on
  expression = expression.replace(/round/g, "Math.round");
  expression = expression.replace(/floor/g, "Math.floor");
  expression = expression.replace(/ceil/g, "Math.ceil");
  expression = expression.replace(/sqrt/g, "Math.sqrt");
  expression = expression.replace(/sqrt/g, "Math.sqrt");
  expression = expression.replace(/sin/g, "Math.sin");
  expression = expression.replace(/cos/g, "Math.cos");
  expression = expression.replace(/tan/g, "Math.tan");
  expression = expression.replace(/cot/g, "Math.cot");
  return eval(expression);
}
  • Handle user keyboard input and button click
// keyboard input
document.addEventListener("keydown", function (event) {
  if (event.key === "Enter") {
    calculateResult();
  } else if (event.key === "Escape") {
    clearResult();
  } else if (event.key === "Backspace") {
    result.value = result.value.slice(0, -1);
  } else if (/^[0-9+\-*/.^()%]$/.test(event.key)) {
    appendToResult(event.key);
  }
});
// button click
const buttons = document.querySelectorAll(".button");
buttons.forEach((button) => {
  button.addEventListener("click", (event) => {
    event.preventDefault();
  });
});
  • Processing computation expression
function calculateResult() {
  try {
    const expression = result.value;
    result.value = evaluateExpression(expression);
    shouldClearResult = true;
  } catch (error) {
    result.value = "Error";
    shouldClearResult = true;
    setTimeout(() => {
      result.value = "0";
    }, 1500);
  }
}
  • Automatically cleared
function clearResult() {
  result.value = "0";
  shouldClearResult = false;
}

V. Display

display

VI. Summary

  • Completing the assignment was a valuable experience that provided insights into various aspects. Through the design process, I gained a profound appreciation for the art and challenge of programming. Designing this calculator not only enhanced my programming skills but also taught me how to translate abstract concepts into practical applications. Throughout this journey, I constantly tackled problems, refined my code, and improved my problem-solving abilities and coding proficiency.
  • This assignment also deepened my understanding of the significance of web languages as the bridge between people and the digital world. I am eagerly looking forward to continuing my exploration and learning of web languages in the future, engaging in more intriguing projects, and contributing to the creation of innovative web applications. I aspire to continually elevate my skills and become a proficient web developer, dedicated to providing users with superior online experiences.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值