Android Studio项目(算法计算器)

打印 上一主题 下一主题

主题 982|帖子 982|积分 2946

算法计算器使用文档

详细版本已更新,点击主页即可查看
阐明:本项目对平凡计算器举行了改进部分为:
初始:


  • 平凡计算器为只可对两个整数举行加减乘除
改进部分:


  • 平凡计算器采用栈实现,并可对浮点数举行一系列操作
  • 算法计算器采用均为最优算法,对时间复杂度举行优化,如求幂(正常为for循环逐一相乘,此处采用快速幂算法)等
缺点部分:


  • 本计算器设计时间有限,故全部整形运算均为int类型
  • guass函数还未完成,别的均可使用
页面展示

首页: 分为两个入口,一个是常规的平凡版计算器,一个是联合算法实现的算法版计算器

平凡计算器
使用方法同平凡计算器,均可实现整数以及小数计算,同时,在平凡计算器的基础上添加了栈的实现,可以举行复合运算。

算法计算器
使用方式比力复杂,最后是其使用文档,可以对差别的运算举行快速处理,特别的是,后端的逻辑实现采用的均为算法较优的实现。

实现过程

1、创建一个新文件——Empty Views Activity


2、下面的感叹号是由于我已经创建过一个了(可以忽略)


3、等待Building


4、规划 代码实现部分:

理论上把我写的代码复制到对应位置上面即可运行
MainActivity实现登岸页面,然后对按钮举行跳转实现
MainActivity2实现平凡计算器
MainActivity3实现算法计算器
步骤:


  • 创建MainActivity,然后对activity_main.xml布局
  • 写MainActivity代码逻辑
  • 创建MainActivity2,然后对activity_main2.xml布局
  • 写MainActivity2代码逻辑
  • 创建类MathUtils,完成数学函数编写
  • 创建MainActivity3,然后对activity_main3.xml布局
  • 写MainActivity3代码逻辑
MainActivity——CODE:
  1. package com.example.algoritmcalculator;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import androidx.activity.EdgeToEdge;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import androidx.core.graphics.Insets;
  8. import androidx.core.view.ViewCompat;
  9. import androidx.core.view.WindowInsetsCompat;
  10. public class MainActivity extends AppCompatActivity {
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         EdgeToEdge.enable(this);
  15.         setContentView(R.layout.activity_main);
  16.         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  17.             Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  18.             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  19.             return insets;
  20.         });
  21.         Button button = findViewById(R.id.button);
  22.         button.setOnClickListener(view -> {
  23.             Intent intent = new Intent(this, MainActivity2.class);
  24.             startActivity(intent);
  25.         });
  26.         Button button2 = findViewById(R.id.button2);
  27.         button2.setOnClickListener(view -> {
  28.             Intent intent = new Intent(this, MainActivity3.class);
  29.             startActivity(intent);
  30.         });
  31.     }
  32. }
复制代码
MainActivity2——CODE:
  1. package com.example.algoritmcalculator;
  2. import android.annotation.SuppressLint;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8. import androidx.activity.EdgeToEdge;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.core.graphics.Insets;
  11. import androidx.core.view.ViewCompat;
  12. import androidx.core.view.WindowInsetsCompat;
  13. import java.util.Stack;
  14. public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
  15.     private final static String TAG = "CalculatorActivity";
  16.     private TextView tv_result; // 声明一个文本视图对象
  17.     private String operator = ""; // 运算符
  18.     private String firstNum = ""; // 第一个操作数
  19.     private String secondNum = ""; // 第二个操作数
  20.     private String result = ""; // 当前的计算结果
  21.     private String showText = ""; // 显示的文本内容
  22.     private Stack<Double> num = new Stack<>();
  23.     private Stack<Character> op = new Stack<>();
  24.     private int[] priority = new int[256];
  25.     {
  26.         priority['+'] = priority['-'] = 1;
  27.         priority['*'] = priority['/'] = 2;
  28.     }
  29.     @SuppressLint("MissingInflatedId")
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         EdgeToEdge.enable(this);
  34.         setContentView(R.layout.activity_main2);
  35.         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main2), (v, insets) -> {
  36.             Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  37.             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  38.             return insets;
  39.         });
  40.         // 从布局文件中获取名叫tv_result的文本视图
  41.         tv_result = findViewById(R.id.tv_result);
  42.         // 下面给每个按钮控件都注册了点击监听器
  43.         findViewById(R.id.btn_cancel).setOnClickListener(this); // “取消”
  44.         findViewById(R.id.btn_divide).setOnClickListener(this); // “除法”
  45.         findViewById(R.id.btn_multiply).setOnClickListener(this); // “乘法”
  46.         findViewById(R.id.btn_clear).setOnClickListener(this); // “清除”
  47.         findViewById(R.id.btn_seven).setOnClickListener(this); // 7
  48.         findViewById(R.id.btn_eight).setOnClickListener(this); // 8
  49.         findViewById(R.id.btn_nine).setOnClickListener(this); // 9
  50.         findViewById(R.id.btn_plus).setOnClickListener(this); // “加法”
  51.         findViewById(R.id.btn_four).setOnClickListener(this); // 数字4
  52.         findViewById(R.id.btn_five).setOnClickListener(this); // 数字5
  53.         findViewById(R.id.btn_six).setOnClickListener(this); // 数字6
  54.         findViewById(R.id.btn_minus).setOnClickListener(this); // “减法”
  55.         findViewById(R.id.btn_one).setOnClickListener(this); // 数字1
  56.         findViewById(R.id.btn_two).setOnClickListener(this); // 数字2
  57.         findViewById(R.id.btn_three).setOnClickListener(this); // 数字3
  58.         findViewById(R.id.ib_left).setOnClickListener(this); // (
  59.         findViewById(R.id.btn_zero).setOnClickListener(this); // 数字0
  60.         findViewById(R.id.btn_dot).setOnClickListener(this); // “小数点”
  61.         findViewById(R.id.btn_equal).setOnClickListener(this); // “等号”
  62.         findViewById(R.id.ib_right).setOnClickListener(this); // )
  63.     }
  64.     @Override
  65.     public void onClick(View v) {
  66.         String inputText;
  67.         inputText = ((TextView) v).getText().toString();
  68.         Log.d(TAG, "inputText=" + inputText);
  69.         if (v.getId() == R.id.btn_clear) { // 点击了清除按钮
  70.             clear();
  71.         }
  72.         else if (v.getId() == R.id.btn_cancel) { // 点击了取消按钮
  73.             refreshText(showText.substring(0, showText.length() - 1));
  74.         }
  75.         else if (v.getId() == R.id.btn_plus || v.getId() == R.id.btn_minus || v.getId() == R.id.btn_multiply || v.getId() == R.id.btn_divide) { // +-*/
  76.             operator = inputText;
  77.             refreshText(showText + operator);
  78.         }
  79.         else if (v.getId() == R.id.btn_equal) { // 等号逻辑 ===================
  80.             num.clear();
  81.             op.clear();
  82.             calculate(String.valueOf(showText));
  83.             refreshText(showText);
  84.         }
  85.         else if (v.getId() == R.id.ib_left) { // (
  86.             refreshText(showText + "(");
  87.         }
  88.         else if (v.getId() == R.id.ib_right) { // )
  89.             refreshText(showText + ")");
  90.         }
  91.         else { // 点击了其他按钮,包括数字和小数点
  92.             refreshText(showText + inputText);
  93.         }
  94.     }
  95.     private void eval() {
  96.         Double a = num.peek(); num.pop();
  97.         Double b = num.peek(); num.pop();
  98.         Character p = op.peek(); op.pop();
  99.         Double r = 0.0;
  100.         if(p == '+') r = b + a;
  101.         if(p == '-') r = b - a;
  102.         if(p == '*') r = b * a;
  103.         if(p == '/') r = b / a;
  104.         num.push(r);
  105.     }
  106.     private void calculate(String expression) {
  107.         while(!num.empty()) num.pop();
  108.         while(!op.empty()) op.pop();
  109. //        System.out.println(expression);
  110.         for (int i = 0; i < expression.length(); i++) {
  111.             char c = expression.charAt(i);
  112.             if (Character.isDigit(c) || c == '.') {
  113.                 Double x = 0.0, k = 0.1;
  114.                 int j = i, z = 0;
  115.                 while (j < expression.length() && (Character.isDigit(expression.charAt(j)) || expression.charAt(j) == '.')) {
  116.                     if(z == 1) {
  117.                         x = x + k * (expression.charAt(j) - '0') * k;
  118.                         k *= 0.1;
  119.                     }
  120.                     if(expression.charAt(j) == '.') {
  121.                         z = 1;
  122.                     }
  123.                     if(z == 0) x = x * 10 + (expression.charAt(j) - '0');
  124.                     j++;
  125.                 }
  126.                 i = j - 1;
  127.                 num.push(x);
  128.             } else if (c == '(') {
  129.                 op.push(c);
  130.             } else if (c == ')') {
  131.                 while (op.peek() != '(') eval();
  132.                 op.pop();
  133.             } else {
  134.                 while (op.size() > 0 && priority[op.peek()] >= priority[c]) eval();
  135.                 op.push(c);
  136.             }
  137.         }
  138. //        System.out.println(num.size());
  139.         while (op.size() > 0) eval();
  140.         if (!num.isEmpty()) {
  141.             result = String.valueOf(num.pop());
  142.             refreshText(result);
  143.         }
  144.         System.out.println(result);
  145.     }
  146.     private void refreshText(String text) {
  147.         showText = text;
  148.         tv_result.setText(showText);
  149.     }
  150.     private void clear() {
  151.         num.clear();
  152.         op.clear();
  153.         refreshOperate("");
  154.         refreshText("");
  155.     }
  156.     // 刷新运算结果
  157.     private void refreshOperate(String new_result) {
  158.         result = new_result;
  159.         operator = "";
  160.     }
  161. }
复制代码
MainActivity3——CODE:
  1. package com.example.algoritmcalculator;
  2. import android.annotation.SuppressLint;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8. import androidx.activity.EdgeToEdge;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.core.graphics.Insets;
  11. import androidx.core.view.ViewCompat;
  12. import androidx.core.view.WindowInsetsCompat;
  13. import java.util.Scanner;
  14. public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {
  15.     private final static String TAG = "CalculatorActivity";
  16.     private TextView tv_result; // 声明一个文本视图对象
  17.     private int operator = 0; // 运算符
  18.     private int firstNum = 0; // 第一个操作数
  19.     private int secondNum = 0; // 第二个操作数
  20.     private int thirdNum = 0; // 第三个操作数
  21.     private int res = 0;
  22.     private String result = ""; // 当前的计算结果
  23.     private String showText = ""; // 显示的文本内容
  24.     @SuppressLint("MissingInflatedId")
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         EdgeToEdge.enable(this);
  29.         setContentView(R.layout.activity_main3);
  30.         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main3), (v, insets) -> {
  31.             Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  32.             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  33.             return insets;
  34.         });
  35.         findViewById(R.id.one).setOnClickListener(this); // 数字1
  36.         findViewById(R.id.two).setOnClickListener(this); // 数字2
  37.         findViewById(R.id.three).setOnClickListener(this); // 数字3
  38.         findViewById(R.id.four).setOnClickListener(this); // 数字4
  39.         findViewById(R.id.five).setOnClickListener(this); // 数字5
  40.         findViewById(R.id.six).setOnClickListener(this); // 数字6
  41.         findViewById(R.id.seven).setOnClickListener(this); // 数字7
  42.         findViewById(R.id.eight).setOnClickListener(this); // 数字8
  43.         findViewById(R.id.nine).setOnClickListener(this); // 数字9
  44.         findViewById(R.id.zero).setOnClickListener(this); // 数字0
  45.         findViewById(R.id.pow).setOnClickListener(this);
  46.         findViewById(R.id.pow_modify).setOnClickListener(this);
  47.         findViewById(R.id.is_prime).setOnClickListener(this);
  48.         findViewById(R.id.get_prime).setOnClickListener(this);
  49.         findViewById(R.id.gcd).setOnClickListener(this);
  50.         findViewById(R.id.lcm).setOnClickListener(this);
  51.         findViewById(R.id.gauss).setOnClickListener(this);
  52.         findViewById(R.id.phi).setOnClickListener(this);
  53.         findViewById(R.id.exgcd).setOnClickListener(this);
  54.         findViewById(R.id.equal).setOnClickListener(this);
  55.         findViewById(R.id.space).setOnClickListener(this);
  56.         findViewById(R.id.clear).setOnClickListener(this);
  57.         findViewById(R.id.cancel).setOnClickListener(this);
  58.         tv_result = findViewById(R.id.textView2);
  59.     }
  60.     @Override
  61.     public void onClick(View v) {
  62.         String inputText;
  63.         inputText = ((TextView) v).getText().toString();
  64.         Log.d(TAG, "inputText=" + inputText);
  65.         if (v.getId() == R.id.clear) { // 点击了C按钮
  66.             clear();
  67.         }
  68.         else if (v.getId() == R.id.cancel) { // 点击了取消按钮
  69.             refreshText(showText.substring(0, showText.length() - 1));
  70.         }
  71.         else if(v.getId() == R.id.equal) {
  72.             calculate(String.valueOf(showText));
  73.             refreshText(showText);
  74.         }
  75.         else if(v.getId() == R.id.pow) operator = 1;
  76.         else if(v.getId() == R.id.pow_modify) operator = 2;
  77.         else if(v.getId() == R.id.is_prime) operator = 3;
  78.         else if(v.getId() == R.id.get_prime) operator = 4;
  79.         else if(v.getId() == R.id.gcd) operator = 5;
  80.         else if(v.getId() == R.id.lcm) operator = 6;
  81.         else if(v.getId() == R.id.gauss) operator = 7;
  82.         else if(v.getId() == R.id.phi) operator = 8;
  83.         else if(v.getId() == R.id.exgcd) operator = 9;
  84.         else if(v.getId() == R.id.linear) operator = 10;
  85.         else { // 点击了其他按钮,包括数字和小数点
  86.             refreshText(showText + inputText);
  87.         }
  88. //        refreshText(showText + inputText);
  89.     }
  90.     private void calculate(String expression) {
  91.         int z = 0;
  92.         for (int i = 0; i < expression.length(); i++) {
  93.             char c = expression.charAt(i);
  94.             if (Character.isDigit(c)) {
  95.                 int x = 0;
  96.                 int j = i;
  97.                 while (j < expression.length() && Character.isDigit(expression.charAt(j))) {
  98.                     x = x * 10 + (expression.charAt(j) - '0');
  99.                     j++;
  100.                 }
  101.                 i = j - 1;
  102.                 if(z == 0) {
  103.                     firstNum = x;
  104.                     z = 1;
  105.                 }
  106.                 else if(z == 1) {
  107.                     secondNum = x;
  108.                     z = 2;
  109.                 }
  110.                 else {
  111.                     thirdNum = x;
  112.                     z = 0;
  113.                 }
  114.             }
  115.         }
  116.         switch(operator) {
  117.             case 1:
  118.                 res = MathUtils.qmi(firstNum, secondNum);
  119.                 break;
  120.             case 2:
  121.                 res = MathUtils.qm(firstNum, secondNum, thirdNum);
  122.                 break;
  123.             case 3:
  124.                 boolean isPrime = MathUtils.is_prime(firstNum);
  125.                 result = isPrime ? "Prime" : "Not Prime";
  126.                 break;
  127.             case 4:
  128.                 MathUtils.get_prime(firstNum);
  129.                 // Assuming you want to display the primes in some way
  130.                 StringBuilder primesBuilder = new StringBuilder();
  131.                 for (int j = 0; j < MathUtils.cnt; j++) {
  132.                     primesBuilder.append(MathUtils.primes[j]).append(" ");
  133.                 }
  134.                 result = primesBuilder.toString();
  135.                 break;
  136.             case 5:
  137.                 res = MathUtils.gcd(firstNum, secondNum);
  138.                 break;
  139.             case 6:
  140.                 res = MathUtils.lcm(firstNum, secondNum);
  141.                 break;
  142.             // case 7: gauss function is not implemented
  143.             case 8:
  144.                 res = MathUtils.phi(firstNum);
  145.                 break;
  146.             case 9: {
  147.                 int[] x = new int[1];
  148.                 int[] y = new int[1];
  149.                 MathUtils.exgcd(firstNum, secondNum, x, y);
  150.                 res = x[0]; // or y[0] depending on what you want to return
  151.                 break;
  152.             }
  153.             case 10: {
  154.                 int[] x = new int[1];
  155.                 int[] y = new int[1];
  156.                 long d = MathUtils.linear(firstNum, secondNum, x, y);
  157.                 res = (int)d; // or x[0] or y[0] depending on what you want to return
  158.                 break;
  159.             }
  160.             default:
  161.                 Toast.makeText(this, "Invalid operation", Toast.LENGTH_SHORT).show();
  162.                 return;
  163.         }
  164.         // Display the result
  165.         if (operator != 3 && operator != 4 && operator != 10 && operator != 9) {
  166.             result = String.valueOf(res);
  167.         }
  168.         refreshText(result);
  169.     }
  170.     // 刷新文本显示
  171.     private void refreshText(String text) {
  172.         showText = text;
  173.         CharSequence t = showText;
  174.         Log.d(TAG, "t=" + t);
  175.         tv_result.setText(t);
  176.     }
  177.     // 清空并初始化
  178.     private void clear() {
  179.         //refreshOperate("");
  180.         refreshText("");
  181.     }
  182. }
复制代码
MathUtils——CODE:
  1. package com.example.algoritmcalculator;
  2. public class MathUtils {
  3.     public static boolean[] st = new boolean[100000];
  4.     public static int[] primes = new int[100000];
  5.     public static int cnt;
  6.     public static int qmi(int a, int k) {
  7.         int res = 1;
  8.         while (k != 0) {
  9.             if ((k & 1) != 0) res = res * a;
  10.             k >>>= 1; // 使用无符号右移,确保位移操作的正确性
  11.             a = a * a; // 将a扩大平方倍
  12.         }
  13.         return res;
  14.     }
  15.     public static int qm(int a, int b, int p) {
  16.         int res = 1;
  17.         while (b != 0) {
  18.             if ((b & 1) != 0) res = (res * a) % p;
  19.             a = (a * a) % p;
  20.             b >>= 1;
  21.         }
  22.         return res;
  23.     }
  24.     public static boolean is_prime(int x) {
  25.         if (x < 2) return false;
  26.         for (int i = 2; i <= x / i; i++) {
  27.             if (x % i == 0) return false;
  28.         }
  29.         return true;
  30.     }
  31.     public static void get_prime(int n) {
  32.         for (int i = 2; i <= n; i++) {
  33.             if (!st[i]) primes[cnt++] = i;
  34.             for (int j = 0; primes[j] <= n / i; j++) {
  35.                 st[primes[j] * i] = true;
  36.                 if (i % primes[j] == 0) break;
  37.             }
  38.         }
  39.     }
  40.     public static int gcd(int a, int b) {
  41.         return b != 0 ? gcd(b, a % b) : a;
  42.     }
  43.     public static int lcm(int a, int b) {
  44.         return a * b / gcd(a, b);
  45.     }
  46.     public static int gauss() {
  47.         // 此函数需要一个具体的矩阵a和精度eps来执行高斯消元
  48.         // 由于没有提供具体的矩阵和eps值,这里不实现该函数
  49.         return 0;
  50.     }
  51.     public static int phi(int x) {
  52.         int res = x;
  53.         for (int i = 2; i <= x / i; i++) {
  54.             if (x % i == 0) {
  55.                 res = res / i * (i - 1);
  56.                 while (x % i == 0) x /= i;
  57.             }
  58.         }
  59.         if (x > 1) res = res / x * (x - 1);
  60.         return res;
  61.     }
  62.     public static void exgcd(int a, int b, int[] x, int[] y) {
  63.         if (b == 0) {
  64.             x[0] = 1;
  65.             y[0] = 0;
  66.             return;
  67.         }
  68.         int aa = a, bb = b;
  69.         MathUtils.exgcd(bb, aa % bb, y, x);
  70.         y[0] = y[0] - (a / b) * x[0];
  71.     }
  72.     public static int linear(int a, int b, int[] x, int[] y) {
  73.         if (b == 0) {
  74.             x[0] = 1;
  75.             y[0] = 0;
  76.             return a;
  77.         }
  78.         int d = MathUtils.linear(b, a % b, y, x);
  79.         y[0] -= a / b * x[0];
  80.         return d;
  81.     }
  82. }
复制代码
activity_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/main"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context=".MainActivity">
  9.     <androidx.constraintlayout.widget.Group
  10.         android:id="@+id/group"
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content" />
  13.     <LinearLayout
  14.         android:layout_width="match_parent"
  15.         android:layout_height="match_parent"
  16.         android:orientation="vertical"
  17.         android:gravity="center_horizontal"
  18.         android:background="#2CC0F1"
  19.         >
  20.         <TextView
  21.             android:id="@+id/textView"
  22.             android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content"
  24.             android:text="计算器"
  25.             android:textSize="60dp"
  26.             tools:layout_editor_absoluteX="50dp"
  27.             tools:layout_editor_absoluteY="15dp" />
  28.         <Button
  29.             android:id="@+id/button"
  30.             android:layout_width="300dp"
  31.             android:layout_height="120dp"
  32.             android:layout_marginTop="50sp"
  33.             android:text="普通计算器"
  34.             android:background="#8ed1c0"
  35.             android:textSize="30sp"
  36.             tools:layout_editor_absoluteX="84dp"
  37.             tools:layout_editor_absoluteY="372dp" />
  38.         <Button
  39.             android:id="@+id/button2"
  40.             android:layout_width="300dp"
  41.             android:layout_height="120dp"
  42.             android:text="算法计算器"
  43.             android:background="#ce8ed1"
  44.             android:textSize="30sp"
  45.             android:layout_marginTop="50sp"
  46.             tools:layout_editor_absoluteX="84dp"
  47.             tools:layout_editor_absoluteY="213dp" />
  48.     </LinearLayout>
  49. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码
activity_main2.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/main2"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context=".MainActivity2"
  9.     android:background="#eeeeee"
  10.     android:orientation="vertical"
  11.     android:padding="5dp">
  12.     <ScrollView
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content">
  15.         <LinearLayout
  16.             android:layout_width="match_parent"
  17.             android:layout_height="wrap_content"
  18.             android:orientation="vertical">
  19.             <TextView
  20.                 android:layout_width="360dp"
  21.                 android:layout_height="wrap_content"
  22.                 android:gravity="center"
  23.                 android:text="计算器"
  24.                 android:textColor="#000000"
  25.                 android:textSize="20sp" />
  26.             <LinearLayout
  27.                 android:layout_width="match_parent"
  28.                 android:layout_height="wrap_content"
  29.                 android:orientation="vertical">
  30.                 <TextView
  31.                     android:id="@+id/tv_result"
  32.                     android:layout_width="360dp"
  33.                     android:layout_height="wrap_content"
  34.                     android:background="#ffffff"
  35.                     android:gravity="right|bottom"
  36.                     android:lines="3"
  37.                     android:maxLines="3"
  38.                     android:scrollbars="vertical"
  39.                     android:text="0"
  40.                     android:textColor="#000000"
  41.                     android:textSize="25sp" />
  42.             </LinearLayout>
  43.             <GridLayout
  44.                 android:layout_width="match_parent"
  45.                 android:layout_height="wrap_content"
  46.                 android:columnCount="4">
  47.                 <Button
  48.                     android:id="@+id/btn_cancel"
  49.                     android:layout_width="90dp"
  50.                     android:layout_height="75dp"
  51.                     android:gravity="center"
  52.                     android:text="CE"
  53.                     android:textColor="@color/black"
  54.                     android:textSize="30sp" />
  55.                 <Button
  56.                     android:id="@+id/btn_divide"
  57.                     android:layout_width="90dp"
  58.                     android:layout_height="75dp"
  59.                     android:gravity="center"
  60.                     android:text="/"
  61.                     android:textColor="@color/black"
  62.                     android:textSize="30sp" />
  63.                 <Button
  64.                     android:id="@+id/btn_multiply"
  65.                     android:layout_width="90dp"
  66.                     android:layout_height="75dp"
  67.                     android:gravity="center"
  68.                     android:text="*"
  69.                     android:textColor="@color/black"
  70.                     android:textSize="30sp" />
  71.                 <Button
  72.                     android:id="@+id/btn_clear"
  73.                     android:layout_width="90dp"
  74.                     android:layout_height="75dp"
  75.                     android:gravity="center"
  76.                     android:text="C"
  77.                     android:textColor="@color/black"
  78.                     android:textSize="30sp" />
  79.                 <Button
  80.                     android:id="@+id/btn_seven"
  81.                     android:layout_width="90dp"
  82.                     android:layout_height="75dp"
  83.                     android:gravity="center"
  84.                     android:text="7"
  85.                     android:textColor="@color/black"
  86.                     android:textSize="30sp" />
  87.                 <Button
  88.                     android:id="@+id/btn_eight"
  89.                     android:layout_width="90dp"
  90.                     android:layout_height="75dp"
  91.                     android:gravity="center"
  92.                     android:text="8"
  93.                     android:textColor="@color/black"
  94.                     android:textSize="30sp" />
  95.                 <Button
  96.                     android:id="@+id/btn_nine"
  97.                     android:layout_width="90dp"
  98.                     android:layout_height="75dp"
  99.                     android:gravity="center"
  100.                     android:text="9"
  101.                     android:textColor="@color/black"
  102.                     android:textSize="30sp" />
  103.                 <Button
  104.                     android:id="@+id/btn_plus"
  105.                     android:layout_width="wrap_content"
  106.                     android:layout_height="wrap_content"
  107.                     android:gravity="center"
  108.                     android:text="+"
  109.                     android:textColor="@color/black"
  110.                     android:textSize="30sp" />
  111.                 <Button
  112.                     android:id="@+id/btn_four"
  113.                     android:layout_width="90dp"
  114.                     android:layout_height="75dp"
  115.                     android:gravity="center"
  116.                     android:text="4"
  117.                     android:textColor="@color/black"
  118.                     android:textSize="30sp" />
  119.                 <Button
  120.                     android:id="@+id/btn_five"
  121.                     android:layout_width="90dp"
  122.                     android:layout_height="75dp"
  123.                     android:gravity="center"
  124.                     android:text="5"
  125.                     android:textColor="@color/black"
  126.                     android:textSize="30sp" />
  127.                 <Button
  128.                     android:id="@+id/btn_six"
  129.                     android:layout_width="90dp"
  130.                     android:layout_height="75dp"
  131.                     android:gravity="center"
  132.                     android:text="6"
  133.                     android:textColor="@color/black"
  134.                     android:textSize="30sp" />
  135.                 <Button
  136.                     android:id="@+id/btn_minus"
  137.                     android:layout_width="90dp"
  138.                     android:layout_height="75dp"
  139.                     android:gravity="center"
  140.                     android:text="-"
  141.                     android:textColor="@color/black"
  142.                     android:textSize="30sp" />
  143.                 <Button
  144.                     android:id="@+id/btn_one"
  145.                     android:layout_width="90dp"
  146.                     android:layout_height="75dp"
  147.                     android:gravity="center"
  148.                     android:text="1"
  149.                     android:textColor="@color/black"
  150.                     android:textSize="30sp" />
  151.                 <Button
  152.                     android:id="@+id/btn_two"
  153.                     android:layout_width="90dp"
  154.                     android:layout_height="75dp"
  155.                     android:gravity="center"
  156.                     android:text="2"
  157.                     android:textColor="@color/black"
  158.                     android:textSize="30sp" />
  159.                 <Button
  160.                     android:id="@+id/btn_three"
  161.                     android:layout_width="90dp"
  162.                     android:layout_height="75dp"
  163.                     android:gravity="center"
  164.                     android:text="3"
  165.                     android:textColor="@color/black"
  166.                     android:textSize="30sp" />
  167.                 <Button
  168.                     android:id="@+id/btn_equal"
  169.                     android:layout_width="90dp"
  170.                     android:layout_height="75dp"
  171.                     android:gravity="center"
  172.                     android:text="="
  173.                     android:textColor="@color/black"
  174.                     android:textSize="30sp" />
  175.                 <Button
  176.                     android:id="@+id/btn_dot"
  177.                     android:layout_width="90dp"
  178.                     android:layout_height="75dp"
  179.                     android:gravity="center"
  180.                     android:text="."
  181.                     android:textColor="@color/black"
  182.                     android:textSize="30sp" />
  183.                 <Button
  184.                     android:id="@+id/btn_zero"
  185.                     android:layout_width="90dp"
  186.                     android:layout_height="75dp"
  187.                     android:gravity="center"
  188.                     android:text="0"
  189.                     android:textColor="@color/black"
  190.                     android:textSize="30sp" />
  191.                 <Button
  192.                     android:id="@+id/ib_left"
  193.                     android:layout_width="90dp"
  194.                     android:layout_height="75dp"
  195.                     android:gravity="center"
  196.                     android:text="("
  197.                     android:textColor="@color/black"
  198.                     android:textSize="30sp" />
  199.                 <Button
  200.                     android:id="@+id/ib_right"
  201.                     android:layout_width="89dp"
  202.                     android:layout_height="75dp"
  203.                     android:gravity="center"
  204.                     android:text=")"
  205.                     android:textColor="@color/black"
  206.                     android:textSize="30sp" />
  207.             </GridLayout>
  208.         </LinearLayout>
  209.     </ScrollView>
  210. </LinearLayout>
复制代码
activity_main3.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/main3"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     android:orientation="vertical"
  9.     tools:context=".MainActivity3">
  10.     <TextView
  11.         android:id="@+id/textView2"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="111dp"
  14.         android:textSize="30sp"
  15.         android:layout_margin="10sp"
  16.         android:text="TextView" />
  17.     <GridLayout
  18.         android:layout_width="match_parent"
  19.         android:layout_height="550sp"
  20.         android:columnCount="4"
  21.         android:layout_margin="10sp"
  22.         android:rowCount="5">
  23.         <Button
  24.             android:id="@+id/pow"
  25.             android:layout_width="0dp"
  26.             android:layout_height="0dp"
  27.             android:layout_rowWeight="1"
  28.             android:layout_margin="5sp"
  29.             android:layout_columnWeight="1"
  30.             android:background="#FFB1CD"
  31.             android:text="pow" />
  32.         <Button
  33.             android:id="@+id/pow_modify"
  34.             android:layout_width="0dp"
  35.             android:layout_height="0dp"
  36.             android:layout_rowWeight="1"
  37.             android:layout_margin="5sp"
  38.             android:layout_columnWeight="1"
  39.             android:background="#FFB1CD"
  40.             android:text="pow_modify" />
  41.         <Button
  42.             android:id="@+id/is_prime"
  43.             android:layout_width="0dp"
  44.             android:layout_height="0dp"
  45.             android:layout_rowWeight="1"
  46.             android:layout_margin="5sp"
  47.             android:layout_columnWeight="1"
  48.             android:background="#FFB1CD"
  49.             android:text="is_prime" />
  50.         <Button
  51.             android:id="@+id/get_prime"
  52.             android:layout_width="0dp"
  53.             android:layout_height="0dp"
  54.             android:layout_margin="5sp"
  55.             android:layout_rowWeight="1"
  56.             android:layout_columnWeight="1"
  57.             android:background="#FFB1CD"
  58.             android:text="get_prime" />
  59.         <Button
  60.             android:id="@+id/gcd"
  61.             android:layout_width="0dp"
  62.             android:layout_height="0dp"
  63.             android:layout_rowWeight="1"
  64.             android:layout_columnWeight="1"
  65.             android:layout_margin="5sp"
  66.             android:background="#FFB1CD"
  67.             android:text="gcd" />
  68.         <Button
  69.             android:id="@+id/lcm"
  70.             android:layout_width="0dp"
  71.             android:layout_height="0dp"
  72.             android:layout_rowWeight="1"
  73.             android:layout_columnWeight="1"
  74.             android:layout_margin="5sp"
  75.             android:background="#FFB1CD"
  76.             android:text="lcm" />
  77.         <Button
  78.             android:id="@+id/gauss"
  79.             android:layout_width="0dp"
  80.             android:layout_height="0dp"
  81.             android:layout_rowWeight="1"
  82.             android:layout_columnWeight="1"
  83.             android:layout_margin="5sp"
  84.             android:background="#FFB1CD"
  85.             android:text="gauss" />
  86.         <Button
  87.             android:id="@+id/phi"
  88.             android:layout_width="0dp"
  89.             android:layout_height="0dp"
  90.             android:background="#FFB1CD"
  91.             android:layout_rowWeight="1"
  92.             android:layout_columnWeight="1"
  93.             android:layout_margin="5sp"
  94.             android:text="phi" />
  95.         <Button
  96.             android:id="@+id/exgcd"
  97.             android:layout_width="0dp"
  98.             android:background="#FFB1CD"
  99.             android:layout_height="0dp"
  100.             android:layout_margin="5sp"
  101.             android:layout_rowWeight="1"
  102.             android:layout_columnWeight="1"
  103.             android:text="exgcd" />
  104.         <Button
  105.             android:id="@+id/linear"
  106.             android:layout_width="0dp"
  107.             android:layout_height="0dp"
  108.             android:layout_margin="5sp"
  109.             android:background="#FFB1CD"
  110.             android:layout_rowWeight="1"
  111.             android:layout_columnWeight="1"
  112.             android:text="linear" />
  113.         <Button
  114.             android:id="@+id/cancel"
  115.             android:layout_width="0dp"
  116.             android:layout_height="0dp"
  117.             android:background="#95A3F7"
  118.             android:layout_margin="5sp"
  119.             android:layout_rowWeight="1"
  120.             android:layout_columnWeight="1"
  121.             android:text="CE" />
  122.         <Button
  123.             android:id="@+id/clear"
  124.             android:layout_width="0dp"
  125.             android:background="#95A3F7"
  126.             android:layout_margin="5sp"
  127.             android:layout_height="0dp"
  128.             android:layout_rowWeight="1"
  129.             android:layout_columnWeight="1"
  130.             android:text="C" />
  131.         <Button
  132.             android:id="@+id/one"
  133.             android:layout_width="0dp"
  134.             android:background="#8ebed1"
  135.             android:layout_height="0dp"
  136.             android:layout_margin="5sp"
  137.             android:layout_rowWeight="1"
  138.             android:layout_columnWeight="1"
  139.             android:text="1" />
  140.         <Button
  141.             android:id="@+id/two"
  142.             android:layout_width="0dp"
  143.             android:layout_height="0dp"
  144.             android:layout_margin="5sp"
  145.             android:layout_rowWeight="1"
  146.             android:layout_columnWeight="1"
  147.             android:background="#8ebed1"
  148.             android:text="2" />
  149.         <Button
  150.             android:id="@+id/three"
  151.             android:layout_width="0dp"
  152.             android:layout_height="0dp"
  153.             android:layout_margin="5sp"
  154.             android:layout_rowWeight="1"
  155.             android:background="#8ebed1"
  156.             android:layout_columnWeight="1"
  157.             android:text="3" />
  158.         <Button
  159.             android:id="@+id/zero"
  160.             android:layout_width="0dp"
  161.             android:layout_height="0dp"
  162.             android:layout_margin="5sp"
  163.             android:layout_rowWeight="1"
  164.             android:background="#8ebed1"
  165.             android:layout_columnWeight="1"
  166.             android:text="0" />
  167.         <Button
  168.             android:id="@+id/four"
  169.             android:layout_width="0dp"
  170.             android:layout_height="0dp"
  171.             android:layout_margin="5sp"
  172.             android:background="#8ebed1"
  173.             android:layout_rowWeight="1"
  174.             android:layout_columnWeight="1"
  175.             android:text="4" />
  176.         <Button
  177.             android:id="@+id/five"
  178.             android:layout_width="0sp"
  179.             android:layout_height="0sp"
  180.             android:layout_margin="5sp"
  181.             android:layout_rowWeight="1"
  182.             android:background="#8ebed1"
  183.             android:layout_columnWeight="1"
  184.             android:text="5" />
  185.         <Button
  186.             android:id="@+id/six"
  187.             android:layout_width="0dp"
  188.             android:layout_height="0dp"
  189.             android:layout_margin="5sp"
  190.             android:background="#8ebed1"
  191.             android:layout_rowWeight="1"
  192.             android:layout_columnWeight="1"
  193.             android:text="6" />
  194.         <Button
  195.             android:id="@+id/space"
  196.             android:layout_width="0dp"
  197.             android:layout_height="0dp"
  198.             android:layout_margin="5sp"
  199.             android:background="#8ebed1"
  200.             android:layout_rowWeight="1"
  201.             android:layout_columnWeight="1"
  202.             android:text=" " />
  203.         <Button
  204.             android:id="@+id/seven"
  205.             android:layout_width="0dp"
  206.             android:layout_height="0dp"
  207.             android:layout_margin="5sp"
  208.             android:background="#8ebed1"
  209.             android:layout_rowWeight="1"
  210.             android:layout_columnWeight="1"
  211.             android:text="7" />
  212.         <Button
  213.             android:id="@+id/eight"
  214.             android:layout_width="0dp"
  215.             android:layout_height="0dp"
  216.             android:layout_margin="5sp"
  217.             android:background="#8ebed1"
  218.             android:layout_rowWeight="1"
  219.             android:layout_columnWeight="1"
  220.             android:text="8" />
  221.         <Button
  222.             android:id="@+id/nine"
  223.             android:layout_width="0dp"
  224.             android:layout_height="0dp"
  225.             android:layout_margin="5sp"
  226.             android:layout_rowWeight="1"
  227.             android:background="#8ebed1"
  228.             android:layout_columnWeight="1"
  229.             android:text="9" />
  230.         <Button
  231.             android:id="@+id/equal"
  232.             android:layout_width="0dp"
  233.             android:layout_height="0dp"
  234.             android:layout_margin="5sp"
  235.             android:background="#d1908e"
  236.             android:layout_rowWeight="1"
  237.             android:layout_columnWeight="1"
  238.             android:text="=" />
  239.     </GridLayout>
  240. </LinearLayout>
复制代码
算法实现

   详情函数都是c++代码块,Java代码见上文数学类
  POW:

   输入:两个整数,空格隔开(底数, 指数)
  点击POW按钮后按等号计算(别的函数不再赘述)
  读入底数和指数,通过快速幂算法求出值
  1. bool is_prime(int x){
  2.     if(x < 2) return false;
  3.     for(int i = 2; i <= x / i; i ++ ){
  4.         if(x % i == 0) return false;
  5.     }
  6.     return true;
  7. }
复制代码
POW_MODIFY:

   输入:三个整数,空格隔开(底数,指数,求余mod)
  逆元:除以一个数求余等于乘上这个逆元再求余
【由于求余法则中没有除法的规定,我们只能通过逆元来求余】
乘法逆元的定义
   若整数 b,m 互质,并且对于恣意的整数 a,如果满意 b|a,则存在一个整数 x,使得 a/b≡a×x(mod m),则称 x 为 b 的模 m 乘法逆元,记为 b−1(mod m)。
  b 存在乘法逆元的充要条件是 b 与模数 m 互质。当模数 m 为质数时,b^m−2即为 b 的乘法逆元。
  由此可知,求逆元的方式同快速幂,只需要将指数改为p-2即可
  1. ll qm(ll a, ll b, ll p)
  2. {
  3.     ll res = 1;
  4.     while(b)
  5.     {
  6.         if(b & 1) res = res * a % p;
  7.         a = a * a % p;
  8.         b >>= 1;
  9.     }
  10.     return res;
  11. }
复制代码
IS_PRIME:

   输入:一个整数(判定是否为质数)
  输入一个数,判定是否为质数,质数返回√,非质数返回×
  1. bool is_prime(int x){
  2.     if(x < 2) return false;
  3.     for(int i = 2; i <= x / i; i ++ ){
  4.         if(x % i == 0) return false;
  5.     }
  6.     return true;
  7. }
复制代码
GET_PRIME:

   输入:一个整数(输出小于它的全部质数)
  输入一个数,筛出1~n中的质数,输出为数组,采用线性筛
  1. bool st[N];
  2. int primes[N], cnt;
  3. void get_prime(int n){
  4.     for(int i = 2; i <= n; i++){
  5.         if(!st[i]) primes[cnt ++ ] = i;
  6.         for(int j = 0; primes[j] <= n / i; j ++ ){
  7.             st[primes[j] * i] = true;
  8.             if(i % primes[j] == 0) break;
  9.         }
  10.     }
  11. }
复制代码
GCD:

   输入:两个整数(输出其最大公约数)
  输入两个数,输出最大公约数,采用辗转相除法
  1. int gcd(int a, int b) {
  2.     return b ? gcd(b, a % b) : a; // 辗转相除法
  3. }
复制代码
LCM:

   输入:两个整数(输出其最小公倍数)
  输入两个数,输出最小公倍数
  1. int lcm(int a, int b) {
  2.         return a * b / gcd(a, b);
  3. }
复制代码
GAUSS:

   输入:一个n,接下来n行代表着输入的未知数常数(输出为各个未知数)
  输入常数,输出全部xi的值

  1. int gauss() // 高斯消元
  2. {
  3.     int c, r;
  4.     for(c = 0, r = 0; c < n; c ++ )
  5.     {
  6.         int t = r;
  7.         for(int i = r; i < n; i ++ ) // 找出绝对值最大的行
  8.         {
  9.             if(fabs(a[i][c]) > fabs(a[t][c])) t = i;
  10.         }
  11.         if(fabs(a[t][c]) < eps) continue;
  12.         
  13.         for(int i = c; i <= n; i ++ ) swap(a[t][i], a[r][i]); // 将绝对值最大的行换到最顶端
  14.         for(int i = n; i >= c; i -- ) a[r][i] /= a[r][c]; // 将当前行的首位变成1
  15.         for(int i = r + 1; i < n; i ++ ) // 用当前行将下面所有的列消成0
  16.         {
  17.             if(fabs(a[i][c]) > eps)
  18.             {
  19.                 for(int j = n; j >= c; j -- )
  20.                 {
  21.                     a[i][j] -= a[r][j] * a[i][c];
  22.                 }
  23.             }
  24.         }
  25.         r ++ ;
  26.     }
  27.     if(r < n)
  28.     {
  29.         for(int i = r; i < n; i ++ )
  30.         {
  31.             if(fabs(a[i][n]) > eps) return 2; // 无解,即b不为0
  32.         }
  33.         return 1; // 有无穷多组解
  34.     }
  35.     for(int i = n - 1; i >= 0; i -- )
  36.     {
  37.         for(int j = i + 1; j < n; j ++ )
  38.         {
  39.             a[i][n] -= a[i][j] * a[j][n];
  40.         }
  41.     }
  42.     return 0;
  43. }
复制代码
PHI:

   输入:一个整数(输出其欧拉函数)
  欧拉函数:1~N中与N互质的数的个数被称为欧拉函数,记为 ϕ(N)。
  公式: phi = 质数分之质数-1之积
思绪: 通过试除法找到质数,然后套用公式
  1. int phi(int x) {
  2.         int res = x;
  3.         for(int i = 2; i <= x / i; i++) {
  4.                 if(x % i == 0) {
  5.                         res = res / i * (i - 1);
  6.                         while(x % i == 0) x /= i;
  7.                 }
  8.         }
  9.         if(x > 1) res = res / x * (x - 1);
  10.         return res;
  11. }
复制代码
EXGCD:

   输入:两个整数a,b(输出为x,y的值)
  扩展欧几里得算法:求出一组x,y,使得a * x + b * y == gcd(a, b)
  1. void exgcd(int a, int b, int &x, int &y) {
  2.     if(!b) {
  3.         x = 1, y = 0;
  4.         return ;
  5.     }
  6.     exgcd(b, a % b, y, x);
  7.     y = y - a / b * x;
  8. }
复制代码
LINEAR:

   输入:两个整数a,b(输出x)
  线性同余方程:求一个x,满意a * x == b(mod m)
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. typedef long long ll;
  5. ll linear(ll a, ll b, ll &x, ll &y)
  6. {
  7.     if(!b)
  8.     {
  9.         x = 1, y = 0; // 边界,(或理解为递归的重点)
  10.         return a;
  11.     }
  12.     int d = linear(b, a % b, y, x);// y与b对应,x与a对应
  13.     y -= a / b * x;// 公式推导
  14.     return d;
  15. }
  16. int main()
  17. {
  18.     int n;
  19.     cin >> n;
  20.     while(n -- )
  21.     {
  22.         ll a, b, m;
  23.         cin >> a >> b >> m;
  24.         ll x, y;
  25.         ll t = linear(a, m, x, y);
  26.         if(b % t) puts("impossible");
  27.         else cout << x * (b / t) % m << endl;
  28.     }
  29.     return 0;
  30. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

飞不高

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表