Android Studio计算器解决三角函数,反三角函数等


前言

需要设计一款通用计算器,包含基本的四则运算,还引入了括号,还要解决一些特殊的三角函数,反三角函数等,浏览了CSDN发现这方面的博客比较少,多为一些只能解决四则运算的简易计算器,功能比较单一。最后在一个引入了括号,只能解决四则运算的计算器的基础上,进行功能的扩充,经过四五天时间,最终写出。即下图这些功能。

在这里插入图片描述


一、XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <!-- 结果显示框 -->
    <EditText android:id="@+id/input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cursorVisible="false"
        android:gravity="right"
        android:editable = "false"
        android:text="0"                               />
    <!-- 接下去采TableRow的格式进行布局设计 -->
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- 用于显示存储结果 -->
        <TextView android:id="@+id/M"
            android:layout_width="53sp"
            android:layout_height="wrap_content"
            android:text="  MEM :"                 />
        <!-- 默认显示字符串0 -->
        <TextView android:id="@+id/mem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="0"                       />
    </TableRow>
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- 显示当前是角度还是弧度,默认是角度 -->
        <TextView android:id="@+id/_drg"
            android:layout_width="53sp"
            android:layout_height="wrap_content"
            android:text="   DEG"                 />
        <!-- 清除存储结果 -->
        <Button android:id="@+id/mc"
            android:text="MC"
            android:layout_width="106sp"
            android:layout_height="wrap_content"   />
        <!-- 清除输出窗口的所有内容 -->
        <Button android:id="@+id/c"
            android:text="C"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"   />
    </TableRow>
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- 在角度和弧度之间切换 -->
        <Button android:id="@+id/drg"
            android:text="DRG"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- 正弦计算 -->
        <Button android:id="@+id/sin"
            android:text="sin"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- 余弦计算 -->
        <Button android:id="@+id/cos"
            android:text="cos"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- 正切计算 -->
        <Button android:id="@+id/tan"
            android:text="tan"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- 阶乘计算 -->
        <Button android:id="@+id/factorial"
            android:text="n!"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- 退格键 -->
        <Button android:id="@+id/bksp"
            android:text="Bksp"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
    </TableRow>
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- 数字7 -->
        <Button android:id="@+id/seven"
            android:text="7"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 数字8 -->
        <Button android:id="@+id/eight"
            android:text="8"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 数字9 -->
        <Button android:id="@+id/nine"
            android:text="9"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 除号 -->
        <Button android:id="@+id/divide"
            android:text="÷"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 左括号 -->
        <Button android:id="@+id/left"
            android:text="("
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 右括号 -->
        <Button android:id="@+id/right"
            android:text=")"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
    </TableRow>
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- 数字4 -->
        <Button android:id="@+id/four"
            android:text="4"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 数字5 -->
        <Button android:id="@+id/five"
            android:text="5"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 数字6 -->
        <Button android:id="@+id/six"
            android:text="6"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 乘号 -->
        <Button android:id="@+id/mul"
            android:text="×"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 开方 -->
        <Button android:id="@+id/sqrt"
            android:text=""
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 乘方 -->
        <Button android:id="@+id/square"
            android:text="^"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
    </TableRow>
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- 数字1 -->
        <Button android:id="@+id/one"
            android:text="1"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 数字2 -->
        <Button android:id="@+id/two"
            android:text="2"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 数字3 -->
        <Button android:id="@+id/three"
            android:text="3"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 减号 -->
        <Button android:id="@+id/sub"
            android:text="-"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 对数 -->
        <Button android:id="@+id/log"
            android:text="log"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
        <!-- 自然对数 -->
        <Button android:id="@+id/ln"
            android:text="ln"
            android:layout_width="53sp"
            android:layout_height="wrap_content" />
    </TableRow>
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="57sp"
        >
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
            <!-- 数字0 -->
            <Button android:id="@+id/zero"
                android:text="0"
                android:layout_width="53sp"
                android:layout_height="wrap_content" />
            <!-- 小数点 -->
            <Button android:id="@+id/dot"
                android:text="."
                android:layout_width="53sp"
                android:layout_height="wrap_content" />
            <!-- 等号 -->
            <Button android:id="@+id/equal"
                android:text="="
                android:layout_width="53sp"
                android:layout_height="wrap_content" />
            <!-- 加号 -->
            <Button android:id="@+id/add"
                android:text="+"
                android:layout_width="53sp"
                android:layout_height="wrap_content" />
            <!-- 退出计算器 -->
            <Button android:id="@+id/exit"
                android:text="exit"
                android:layout_width="106sp"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- 用于提示,告诉用户如何使用计算器的一些功能等 -->
        <TextView android:id="@+id/T"
            android:layout_width="45sp"
            android:layout_height="wrap_content"
            android:text="提示:"                 />
        <TextView android:id="@+id/tip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="欢迎使用!"                 />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- asin -->
        <Button android:id="@+id/asin"
            android:text="asin"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- acos -->
        <Button android:id="@+id/acos"
            android:text="acos"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- atan -->
        <Button android:id="@+id/atan"
            android:text="atan"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- sinh -->
        <Button android:id="@+id/sinh"
            android:text="sinh"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- cosh -->
        <Button android:id="@+id/cosh"
            android:text="cosh"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- tanh -->
        <Button android:id="@+id/tanh"
            android:text="tanh"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <!-- asinh -->
        <Button android:id="@+id/asinh"
            android:text="asinh"
            android:layout_width="0sp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <!-- acosh -->
        <Button android:id="@+id/acosh"
            android:text="acosh"
            android:layout_width="0sp"
            android:layout_weight=
  • 3
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值