• java静态栈(含回文数和计算机代码展示)


    目录

    一:栈的思想

    二:栈的操作

    三:回文数

    3.1 栈的定义

    3.2 回文数的判断

    四:计算机

    4.1 栈的定义(数字栈、符号栈)

    4.1.1 判断是否是一个运算符

    4.1.2 判断运算符的优先级

    4.2 计算机代码展示 

    4.2.1 栈的定义

    4.2.2 计算机的运算


    一:栈的思想

    后进先出

    二:栈的操作

    压栈

    弹栈 

    三:回文数

    3.1 栈的定义

    1. public class stackproject {
    2. //栈的大小
    3. private int max;
    4. //数组模拟静态栈
    5. private int[] array;
    6. //栈顶元素
    7. private int top = -1;
    8. //栈的大小初始化
    9. public stackproject(int maxstack) {
    10. this.max = maxstack;
    11. array = new int[max];
    12. }
    13. //压栈
    14. public void push(int val) {
    15. //
    16. if(isFull()) {
    17. throw new RuntimeException("栈已经满");
    18. }
    19. top++;
    20. array[top] = val;
    21. }
    22. //弹栈
    23. public int pop() {
    24. if(ifEmpty()) {
    25. throw new RuntimeException("栈已经空");
    26. }
    27. int value = array[top];
    28. top--;
    29. return value;
    30. }
    31. //是否是空栈
    32. public boolean ifEmpty() {
    33. return this.top == -1;
    34. }
    35. //是否是满栈
    36. public boolean isFull() {
    37. return this.top == max -1;
    38. }
    39. //查看栈中所有元素
    40. public void list() {
    41. if(ifEmpty()) {
    42. throw new RuntimeException("栈已经空");
    43. }
    44. for(int i = 0; i < top; i++) {
    45. System.out.print(array[i] + " ");
    46. }
    47. }
    48. //获取栈的长度
    49. public int stacklength() {
    50. return top+1;
    51. }
    52. }

    3.2 回文数的判断

    1. public class TestApp {
    2. public static void main(String[] args) {
    3. System.out.print(decation("aba"));
    4. }
    5. public static boolean decation(String str) {
    6. staticstack stack = new staticstack(10);
    7. for(int i = 0; i < str.length(); i++) {
    8. stack.push(str.charAt(i));
    9. }
    10. String newval = "";
    11. int newlength = stack.length();
    12. for(int i = 0; i < newlength; i++) {
    13. char c = (char)stack.pop();
    14. newval += c;
    15. }
    16. if(newval.equals(str)) {
    17. return true;
    18. }
    19. return false;
    20. }
    21. }

    四:计算机

    4.1 栈的定义(数字栈、符号栈)

    一串表达式中分为数字和字符分别压入数字栈和字符栈,然后通过判断字符的优先级来操作表达式(两个栈)的运算

    4.1.1 判断是否是一个运算符

    1. //判断是否是字符
    2. public boolean isOper(char c) {
    3. return c == '+' || c == '-' || c == '*' || c== '/';
    4. }

    4.1.2 判断运算符的优先级

    1. public int priority(char c) {
    2. if(c == '*' || c == '/')
    3. return 1;
    4. else if(c == '+' || c == '-')
    5. return 0;
    6. else
    7. return -1;
    8. }

    4.2 计算机代码展示 

    4.2.1 栈的定义

    1. package javastack;
    2. public class stackproject {
    3. //栈的大小
    4. private int max;
    5. //数组模拟静态栈
    6. private int[] array;
    7. //栈顶元素
    8. private int top = -1;
    9. //栈的大小初始化
    10. public stackproject(int maxstack) {
    11. this.max = maxstack;
    12. array = new int[max];
    13. }
    14. //压栈
    15. public void push(int val) {
    16. //
    17. if(isFull()) {
    18. throw new RuntimeException("栈已经满");
    19. }
    20. top++;
    21. array[top] = val;
    22. }
    23. //弹栈
    24. public int pop() {
    25. if(ifEmpty()) {
    26. throw new RuntimeException("栈已经空");
    27. }
    28. int value = array[top];
    29. top--;
    30. return value;
    31. }
    32. //是否是空栈
    33. public boolean ifEmpty() {
    34. return this.top == -1;
    35. }
    36. //是否是满栈
    37. public boolean isFull() {
    38. return this.top == max -1;
    39. }
    40. //查看栈中所有元素
    41. public void list() {
    42. if(ifEmpty()) {
    43. throw new RuntimeException("栈已经空");
    44. }
    45. for(int i = 0; i < top; i++) {
    46. System.out.print(array[i] + " ");
    47. }
    48. }
    49. //获取栈的长度
    50. public int stacklength() {
    51. return top+1;
    52. }
    53. //判断是否是字符
    54. public boolean isOper(char c) {
    55. return c == '+' || c == '-' || c == '*' || c== '/';
    56. }
    57. //判断栈的容量的大小
    58. public int stacksize() {
    59. return this.array.length;
    60. }
    61. //获取栈顶元素
    62. public int peek() {
    63. return this.array[top];
    64. }
    65. //判断运算符的优先级
    66. public int priority(char c) {
    67. if(c == '*' || c == '/')
    68. return 1;
    69. else if(c == '+' || c == '-')
    70. return 0;
    71. else
    72. return -1;
    73. }
    74. //计算两个数的结果
    75. public int calculate(int num1, int num2, int oper) {
    76. int result = 0;
    77. switch(oper) {
    78. case '+':
    79. result = num1 + num2;
    80. break;
    81. case '-':
    82. result = num2 - num1;
    83. break;
    84. case '*':
    85. result = num1 * num2;
    86. break;
    87. case '/':
    88. result = num2 / num1;
    89. break;
    90. default:
    91. break;
    92. }
    93. return result;
    94. }
    95. }

    4.2.2 计算机的运算

    1. package javastack;
    2. public class TestApp {
    3. public static void main(String[] args) {
    4. String str = "4+3*2-1";
    5. stackproject numstack = new stackproject(10);
    6. stackproject symbolstack = new stackproject(10);
    7. int length = str.length();
    8. int temp1 = 0;
    9. int temp2 = 0;
    10. int symbolChar = 0;
    11. int result = 0;
    12. String values = "";
    13. for(int i = 0; i < length; i++) {
    14. char c = str.charAt(i);
    15. if(symbolstack.isOper(c)) {
    16. if(!symbolstack.ifEmpty()) {
    17. if(symbolstack.priority(c) <= symbolstack.priority((char)symbolstack.peek())) {
    18. temp1 = numstack.pop();
    19. temp2 = numstack.pop();
    20. symbolChar = symbolstack.pop();
    21. result = numstack.calculate(temp1, temp2, symbolChar);
    22. numstack.push(result);
    23. symbolstack.push(c);
    24. }
    25. else {
    26. symbolstack.push(c);
    27. }
    28. }
    29. else {
    30. symbolstack.push(c);
    31. }
    32. }
    33. else {
    34. //多位数字
    35. values += c;
    36. if(i == length -1) {
    37. numstack.push(Integer.parseInt(values));
    38. }else {
    39. char data = str.substring(i + 1, i + 2).charAt(0);
    40. if(symbolstack.isOper(data)) {
    41. numstack.push(Integer.parseInt(values));
    42. }
    43. }
    44. values = "";
    45. }
    46. }
    47. while(true) {
    48. if(symbolstack.ifEmpty()) {
    49. break;
    50. }
    51. temp1 = numstack.pop();
    52. temp2 = numstack.pop();
    53. symbolChar = symbolstack.pop();
    54. result = numstack.calculate(temp1, temp2, symbolChar);
    55. numstack.push(result);
    56. }
    57. int res = numstack.pop();
    58. System.out.print(res);
    59. }
    60. }

  • 相关阅读:
    2022杭电多校十 1004-Average Replacement(猜结论+并查集)
    C++闲谈03——多线程
    Nginx配置指南:如何定位、解读与优化Linux上的Nginx设置
    基于Python实现的快速的仿手写文字的图片生成器项目源码
    [Python从零到壹] 五十二.图像增强及运算篇之图像掩膜直方图和HS直方图
    python+django协同过滤算法的美食O2O外卖点餐系统vue
    torch.from_numpy()函数(pytorch版)
    Spyder故障基本解决方案 (包括闪退)-超全版本
    css动画基本使用
    flink时间处理语义
  • 原文地址:https://blog.csdn.net/qq_56127002/article/details/126287743