angular简易计算器

news/2025/2/25 10:28:58

说明:
用angular实现计算器效果,ui风格为暗黑
效果图:
在这里插入图片描述

step1: C:\Users\Administrator\WebstormProjects\untitled4\src\app\calnum\calnum.component.ts

javascript">import { Component } from '@angular/core';

@Component({
  selector: 'app-calnum',
  imports: [],
  templateUrl: './calnum.component.html',
  styleUrl: './calnum.component.css'
})
export class CalnumComponent {

  mainDisplay = '0';
  subDisplay = '';
  currentValue = '';
  operator = '';
  firstOperand: number | null = null;

  handleInput(value: string): void {
    if (value === '.' && this.currentValue.includes('.')) return;

    this.currentValue = this.currentValue === '0' ? value : this.currentValue + value;
    this.mainDisplay = this.currentValue;
  }

  setOperator(op: string): void {
    if (this.currentValue === '') return;

    this.operator = op;
    this.firstOperand = parseFloat(this.currentValue);
    this.subDisplay = `${this.currentValue} ${op}`;
    this.currentValue = '';
  }

  calculate(): void {
    if (!this.firstOperand || !this.operator) return;

    const secondOperand = parseFloat(this.currentValue);
    let result: number;

    switch (this.operator) {
      case '+':
        result = this.firstOperand + secondOperand;
        break;
      case '-':
        result = this.firstOperand - secondOperand;
        break;
      case '×':
        result = this.firstOperand * secondOperand;
        break;
      case '÷':
        result = this.firstOperand / secondOperand;
        break;
      default:
        return;
    }

    this.mainDisplay = result.toString();
    this.subDisplay = `${this.firstOperand} ${this.operator} ${secondOperand} =`;
    this.currentValue = result.toString();
    this.firstOperand = null;
    this.operator = '';
  }

  clear(): void {
    this.mainDisplay = '0';
    this.subDisplay = '';
    this.currentValue = '';
    this.firstOperand = null;
    this.operator = '';
  }

}

step2: C:\Users\Administrator\WebstormProjects\untitled4\src\app\calnum\calnum.component.html

<div class="calculator">
  <div class="display">
    <div class="sub-display">{{ subDisplay }}</div>
    <div class="main-display">{{ mainDisplay }}</div>
  </div>

  <div class="buttons">
    <button (click)="clear()">AC</button>
    <button (click)="handleInput('7')">7</button>
    <button (click)="handleInput('8')">8</button>
    <button (click)="handleInput('9')">9</button>
    <button class="operator" (click)="setOperator('÷')">÷</button>

    <button (click)="handleInput('4')">4</button>
    <button (click)="handleInput('5')">5</button>
    <button (click)="handleInput('6')">6</button>
    <button class="operator" (click)="setOperator('×')">×</button>

    <button (click)="handleInput('1')">1</button>
    <button (click)="handleInput('2')">2</button>
    <button (click)="handleInput('3')">3</button>
    <button class="operator" (click)="setOperator('-')">-</button>

    <button (click)="handleInput('0')">0</button>
    <button (click)="handleInput('.')">.</button>
    <button (click)="calculate()">=</button>
    <button class="operator" (click)="setOperator('+')">+</button>
  </div>
</div>

step3: C:\Users\Administrator\WebstormProjects\untitled4\src\app\calnum\calnum.component.css

/* calculator.component.css */
.calculator {
  background: #1a1a1a;
  border-radius: 16px;
  padding: 24px;
  width: 320px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  border: 1px solid #333;
  margin: 20px auto;
}

.display {
  background: #000;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  text-align: right;
  border: 1px solid #333;
}

.sub-display {
  color: #666;
  font-size: 14px;
  min-height: 20px;
  margin-bottom: 8px;
  font-family: 'Courier New', monospace;
}

.main-display {
  color: #fff;
  font-size: 36px;
  font-weight: 300;
  letter-spacing: -1px;
  font-family: 'Segoe UI', sans-serif;
}

.buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

button {
  border: none;
  border-radius: 8px;
  padding: 18px;
  font-size: 20px;
  cursor: pointer;
  transition: all 0.15s ease;
  background: #2d2d2d;
  color: #fff;
  font-weight: 500;
}

button:hover {
  background: #3d3d3d;
  transform: translateY(-1px);
}

button:active {
  transform: translateY(1px);
}

button.operator {
  background: #ff9500;
  color: #fff;
}

button.operator:hover {
  background: #ffaa33;
}

button:nth-child(1) { /* AC 按钮 */
  background: #ff3b30;
  color: #fff;
}

button:nth-child(1):hover {
  background: #ff453a;
}

button:nth-child(19) { /* = 按钮 */
  background: #34c759;
  color: #fff;
  grid-column: span 2;
}

button:nth-child(19):hover {
  background: #30d158;
}

/* 数字按钮特殊效果 */
button:not(.operator):not(:first-child):not(:last-child) {
  background: #262626;
}

/* 按钮文字阴影 */
button {
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* 输入动画 */
button {
  transition:
    background 0.2s ease,
    transform 0.1s cubic-bezier(0.4, 0, 0.2, 1),
    box-shadow 0.2s ease;
}

/* 键盘聚焦效果 */
button:focus-visible {
  outline: 2px solid #007AFF;
  outline-offset: 2px;
}

/* 显示区域渐变效果 */
.display {
  background: linear-gradient(145deg, #0a0a0a, #000);
}

/* 操作符按钮激活状态 */
button.operator.active {
  background: #ffaa33;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
}

end


http://www.niftyadmin.cn/n/5865380.html

相关文章

Freertos中空闲钩子函数的使用

在嵌入式系统中&#xff0c;FreeRTOS&#xff08;Free Real - Time Operating System&#xff09;是一个广泛使用的开源实时操作系统内核。FreeRTOS 中的空闲钩子函数是一个很有用的特性&#xff0c;下面为你详细介绍其使用方法、知识点以及示例代码。 空闲钩子函数的作用 空…

Zap:Go 的高性能日志库

文章目录 Zap&#xff1a;Go 高性能日志库一、Zap 的核心优势二、快速入门 Zap1. 安装2. 基本用法输出示例 三、Logger 与 SugaredLogger&#xff1a;如何选择&#xff1f;1. **Logger&#xff08;高性能模式&#xff09;**2. **SugaredLogger&#xff08;开发友好模式&#xf…

【聊天室后端服务器开发】功能设计-框架与微服务

服务器功能设计 微服务思想应用 微服务架构 主要组成分析 客户端 客户端通过 HTTP 协议与网关进行交互&#xff0c;进行操作如用户注册、好友申请等客户端只需要知道网关的地址&#xff0c;无需关心后端服务的具体实现 网关 作为系统的统一入口&#xff0c;网关负责接收客…

开源分布式存储系统在云原生数据库领域的实践与应用

本文深入探讨了Curve项目&#xff0c;一个专为云原生环境设计的开源分布式存储系统。文章详细介绍了Curve的块存储架构、其在云原生数据库领域的实际应用&#xff0c;并展望了项目的未来发展方向。 一、Curve项目介绍 Curve项目致力于打造一个云原生、高性能、稳定且易运维的开…

ARCGIS国土超级工具集1.4更新说明

ARCGIS国土超级工具集V1.4版本&#xff0c;功能已增加至54 个。本次更新在V1.3版本的基础上&#xff0c;新增了“拓扑问题修复工具”并同时调整了数据处理工具栏的布局、工具操作界面的选择图层下拉框新增可选择位于图层组内的要素图层功能、数据保存路径新增了可选择数据库内的…

Bybit最大资金盗窃事件技术分析 by CertiK

事件概述 2025年2月21日UTC时间下午02:16:11,Bybit的以太坊冷钱包(0x1db92e2eebc8e0c075a02bea49a2935bcd2dfcf4[1])因恶意合约升级遭到资金盗取。根据Bybit CEO Ben Zhou的声明[2],攻击者通过钓鱼攻击诱骗冷钱包签名者错误签署恶意交易。他提到,该交易被伪装为合法操作:…

【够用就好006】-PC桌面管理ECS服务器的实操步骤

背景介绍解决思路拓展知识 背景介绍 #够用就好#知其然知其所以然#aigc创意人左边 我计划搭建个人网站&#xff0c;计划格式化我的ECS服务器&#xff0c;但是里面有我之前的实践项目&#xff0c;我舍不得删除&#xff0c;我想要保存到本地。 通常我都是在vscode中用remotes ssh…

【react】基础教程

目录 一、React 简介 二、环境搭建 1. 创建 React 项目 2. 项目结构 三、核心概念 1. JSX 语法 2. 组件 (Component) 3. 状态 (State) 与属性 (Props) 4. 事件处理 5. 条件渲染 6. 列表渲染 四、Hooks&#xff08;函数组件的核心&#xff09; 1. useState 2. useE…