简易计算器(加、减、乘、除、括号、小数&负数处理)(Java)

简易计算器(加、减、乘、除、括号、小数&负数处理)(Java)

0.参考

[颜色查询对照表](https://www.114la.com/other/rgb.htm)

1.前言

模仿苹果手机的计算器,以下是效果图
效果图
单行显示字体缩小的效果

#布局
我采用了线性布局(个人认为百分比布局会更好)。
刚看完线性布局部分就迫不及待的开始写界面,写完所有按钮之后试图在一个线性布局中调整按钮的位置,想调整好真的难,后来才知道有线性布局的嵌套,用了嵌套才使得布局美丽了些。

计算器布局嵌套:在一个覆盖全部屏幕的布局中,嵌套两个布局,将屏幕分成两部分,再将下半部分按照行分成分成五个部分(你也可以按照列分),在布局中放入按钮。
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout								//覆盖整个屏幕的部分
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"				

    android:orientation="vertical"				//垂直分布
    android:layout_width="match_parent"			//宽匹配父布局
    android:layout_height="match_parent"		//高匹配父布局
    android:layout_gravity="bottom"				//布局位与屏幕底部
    android:background="#000000">				//布局背景颜色
    <LinearLayout								//textview部分		
        android:orientation="vertical"			//水平分布
        android:layout_width="match_parent"
        android:layout_height="140dp">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@null"					//不显示编辑框中的下划线
            android:cursorVisible="false"				//隐藏光标
            android:gravity="right|bottom"
            android:lines="1"							//限制单行显示
            android:textColor="#FFFFFF"					//字体颜色
            app:autoSizeMaxTextSize="80dp"				//最大字体
            app:autoSizeMinTextSize="20dp"				//最小字体
            app:autoSizeStepGranularity="4dp"			//每次缩小字体的幅度
            app:autoSizeTextType="uniform" />			//打开超过限制自动缩小字体的显示功能
    </LinearLayout>
    <LinearLayout										//按钮部分的那一大块
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="460dp"
        android:layout_gravity="bottom">
        <LinearLayout									//按钮部分按照行嵌套(第一行)
            android:orientation="horizontal"
        android:layout_width="match_parent"
            android:gravity="center"
        android:layout_height="90dp">
            <Button
                android:id="@+id/button_AC"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"		//自定义的圆形按钮
                android:text="AC"
                android:textColor="#000000"
                android:textSize="35dp"
                android:layout_margin="6dp"							//按钮与周围的距离
                android:backgroundTint="#FAFAFA"/>					//不同按钮的不同颜色
            <Button
                android:id="@+id/button_left"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="("
                android:textColor="#000000"
                android:textSize="35dp"
                android:layout_margin="6dp"
                android:backgroundTint="#FCFCFC"/>
            <Button
                android:id="@+id/button_right"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text=")"
                android:textColor="#000000"
                android:textSize="35dp"
                android:layout_margin="6dp"
                android:backgroundTint="#FCFCFC" />
            <Button
                android:id="@+id/button_divide"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="÷"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"
                android:backgroundTint="#FF8C00"/>
    </LinearLayout>											//(第二行)
        <LinearLayout
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="90dp">
            <Button
                android:id="@+id/button_7"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="7"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>
            <Button
                android:id="@+id/button_8"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="8"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>
            <Button
                android:id="@+id/button_9"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="9"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>

            <Button
                android:id="@+id/button_multiple"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:layout_margin="6dp"
                android:background="@drawable/button_circle"
                android:backgroundTint="#FF8C00"
                android:text="×"
                android:textColor="#FCFCFC"
                android:textSize="35dp" />
        </LinearLayout>
        <LinearLayout												//(第三行)
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="90dp">
            <Button
                android:id="@+id/button_4"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="4"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>
            <Button
                android:id="@+id/button_5"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="5"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>
            <Button
                android:id="@+id/button_6"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="6"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>
            <Button
                android:id="@+id/button_minus"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="—"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"
                android:backgroundTint="#FF8C00"/>
        </LinearLayout>
        <LinearLayout													//(第四行)
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="90dp">
            <Button
                android:id="@+id/button_1"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="1"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>
            <Button
                android:id="@+id/button_2"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="2"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>
            <Button
                android:id="@+id/button_3"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="3"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"/>
            <Button
                android:id="@+id/button_add"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="+"
                android:textColor="#FCFCFC"
                android:textSize="35dp"
                android:layout_margin="6dp"
                android:backgroundTint="#FF8C00"/>
        </LinearLayout>
        <LinearLayout														//(第五行)
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="90dp">
            <Button
                android:id="@+id/button_0"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:background="@drawable/button_circle"
                android:text="0"
                android:textColor="#FCFCFC"
                android:textSize=
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一份简易计算器程序,以下是代码及说明: ```c #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;ctype.h&gt; #define MAX_EXPR_LEN 100 // 运算符栈 char op_stack[MAX_EXPR_LEN]; // 数字栈 double num_stack[MAX_EXPR_LEN]; // 运算符栈和数字栈的栈顶指针 int op_top = -1, num_top = -1; // 将运算符压入运算符栈 void push_op(char op) { op_stack[++op_top] = op; } // 将数字压入数字栈 void push_num(double num) { num_stack[++num_top] = num; } // 弹出运算符栈的栈顶元素 char pop_op() { return op_stack[op_top--]; } // 弹出数字栈的栈顶元素 double pop_num() { return num_stack[num_top--]; } // 获取当前栈顶运算符,不存在返回&#39;\0&#39; char peek_op() { return op_top &gt;= 0 ? op_stack[op_top] : &#39;\0&#39;; } // 获取当前栈顶数字,不存在返回0 double peek_num() { return num_top &gt;= 0 ? num_stack[num_top] : 0; } // 判断是否为运算符,是返回1,否则返回0 int is_op(char c) { return c == &#39;+&#39; || c == &#39;-&#39; || c == &#39;*&#39; || c == &#39;/&#39;; } // 获取运算符的优先级,数字越大优先级越高 int op_priority(char op) { if (op == &#39;+&#39; || op == &#39;-&#39;) { return 1; } else if (op == &#39;*&#39; || op == &#39;/&#39;) { return 2; } else { return 0; } } // 计算表达式 double calculate(double left, double right, char op) { switch (op) { case &#39;+&#39;: return left + right; case &#39;-&#39;: return left - right; case &#39;*&#39;: return left * right; case &#39;/&#39;: return left / right; default: return 0; } } // 计算表达式,返回结果 double eval(char *expr) { char *ptr = expr; double num = 0; int is_negative = 0, in_decimal = 0, decimal_pos = 0; while (*ptr != &#39;\0&#39;) { if (isspace(*ptr)) { // 忽略空格 ptr++; } else if (isdigit(*ptr)) { // 处理数字 num = num * 10 + (*ptr - &#39;0&#39;); if (in_decimal) { decimal_pos++; } ptr++; } else if (*ptr == &#39;.&#39;) { // 处理小数点 in_decimal = 1; ptr++; } else if (*ptr == &#39;-&#39;) { // 处理负数 if (ptr == expr || is_op(*(ptr - 1)) || *(ptr - 1) == &#39;(&#39;) { is_negative = 1; } else { push_op(*ptr); } ptr++; } else if (is_op(*ptr)) { // 处理运算符 while (op_top &gt;= 0 &amp;&amp; op_priority(*ptr) &lt;= op_priority(peek_op())) { double right_num = pop_num(); double left_num = pop_num(); char op = pop_op(); push_num(calculate(left_num, right_num, op)); } push_op(*ptr); ptr++; } else if (*ptr == &#39;(&#39;) { // 处理括号 push_op(*ptr); ptr++; } else if (*ptr == &#39;)&#39;) { // 处理括号 while (op_top &gt;= 0 &amp;&amp; peek_op() != &#39;(&#39;) { double right_num = pop_num(); double left_num = pop_num(); char op = pop_op(); push_num(calculate(left_num, right_num, op)); } if (op_top &gt;= 0 &amp;&amp; peek_op() == &#39;(&#39;) { pop_op(); } ptr++; } else { // 非法字符 return 0; } } // 处理最后一个数字 if (in_decimal) { num /= pow(10, decimal_pos); } if (is_negative) { num = -num; } push_num(num); // 计算剩余的表达式 while (op_top &gt;= 0) { double right_num = pop_num(); double left_num = pop_num(); char op = pop_op(); push_num(calculate(left_num, right_num, op)); } return peek_num(); } int main() { char expr[MAX_EXPR_LEN]; printf(&quot;请输入表达式:&quot;); scanf(&quot;%s&quot;, expr); double result = eval(expr); printf(&quot;计算结果:%f\n&quot;, result); return 0; } ``` 该程序基于栈实现了一个简单的表达式求值器,支持带括号负数的四则运算。主要思路是读取表达式中的每一个字符,如果是数字则将其转换成数字并压入数字栈,如果是运算符则将其压入运算符栈,如果是左括号则将其压入运算符栈,如果是右括号则不断弹出运算符栈中的运算符和数字栈中的数字直到遇到左括号为止并将左括号弹出,最后计算剩余的表达式。 由于C语言没有内置的pow函数,因此在处理小数时需要手动计算小数点后的位数。如果表达式中包含非法字符,则计算结果会返回0。 请注意,此程序仅为演示用途,可能存在一些问题和不足,如对于大数的处理、错误输入的处理等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值