从上一次到今天的总结1
在这么长的一段时间中,完成很多事情
第一件事是完成了耗费时间最长的栈运算,要求是把一个字符串算式将数字和算号分开分别放到链表里面,然后通过栈实现后缀表达式
虽然整个代码拆分的还不够细致,但是能够完成四则运算。下面附上源代码
package GOGOGO;
import java.util.*;
public class Stack2 {
public static double getResult(String n,double num1,double num2)
{
double result = 0;
switch (n) {
case "+":
result = num1+num2;
break;
case "-":
result = num1-num2;
break;
case "*":
result = num1*num2;
break;
case "/":
result = num1/num2;
break;
default:
break;
}
return result;
}
public int priority(String oper){
import java.util.*;
public class Stack2 {
public static double getResult(String n,double num1,double num2)
{
double result = 0;
switch (n) {
case "+":
result = num1+num2;
break;
case "-":
result = num1-num2;
break;
case "*":
result = num1*num2;
break;
case "/":
result = num1/num2;
break;
default:
break;
}
return result;
}
public int priority(String oper){
if(oper.contentEquals("*")||oper.contentEquals("/")){
return 1;
}else if (oper.contentEquals("+")||oper.contentEquals("-")) {
return 0;
} else {
return -1; // 假定目前的表达式只有 +, - , * , /
}
}
public ArrayList<String> getArraylist(String n){
String str = "";
int j=0;
ArrayList<String> list = new ArrayList<String>();
for(char a:n.toCharArray()) {
if(Character.isDigit(a)) {
str += a;
}
else {
if(str.contentEquals("")) {
list.add(a+"");
}
else {
list.add(str);
list.add(a+"");
}
str = "";
j= n.lastIndexOf(a);
}
}
list.add(n.substring(j+1));
return list;
}
public double getAnswer(ArrayList<String> list) {
Stack<String> stack1 = new Stack<String>();
Stack<String> stack2 = new Stack<String>();
double num1;
double num2;
int index = 0;
while(true) {
String a2 = list.get(index);;
char ch[] = a2.toCharArray();
if(Character.isDigit(ch[0])) {
stack2.push(a2);
}
else {
if(stack1.isEmpty()) {
stack1.push(a2);
}
else {
if(a2.contentEquals("(")){
stack1.push(a2);
}
public ArrayList<String> getArraylist(String n){
String str = "";
int j=0;
ArrayList<String> list = new ArrayList<String>();
for(char a:n.toCharArray()) {
if(Character.isDigit(a)) {
str += a;
}
else {
if(str.contentEquals("")) {
list.add(a+"");
}
else {
list.add(str);
list.add(a+"");
}
str = "";
j= n.lastIndexOf(a);
}
}
list.add(n.substring(j+1));
return list;
}
public double getAnswer(ArrayList<String> list) {
Stack<String> stack1 = new Stack<String>();
Stack<String> stack2 = new Stack<String>();
double num1;
double num2;
int index = 0;
while(true) {
String a2 = list.get(index);;
char ch[] = a2.toCharArray();
if(Character.isDigit(ch[0])) {
stack2.push(a2);
}
else {
if(stack1.isEmpty()) {
stack1.push(a2);
}
else {
if(a2.contentEquals("(")){
stack1.push(a2);
}
else if(a2.contentEquals(")")){
num1 = Double.parseDouble(stack2.pop());
else if(a2.contentEquals(")")){
num1 = Double.parseDouble(stack2.pop());
num2 = Double.parseDouble(stack2.pop());
stack2.push(""+getResult(stack1.pop(),num2,num1));
stack1.pop();
}
else {
if(priority(a2)<=priority(stack1.peek())) {
num1 = Double.parseDouble(stack2.pop());
stack2.push(""+getResult(stack1.pop(),num2,num1));
stack1.pop();
}
else {
if(priority(a2)<=priority(stack1.peek())) {
num1 = Double.parseDouble(stack2.pop());
num2 = Double.parseDouble(stack2.pop());
stack2.push(""+getResult(stack1.pop(),num2,num1));
stack1.push(a2);
}
else {
stack1.push(a2);
}
}
}
}
index++;
if(index==list.size()){
stack2.push(""+getResult(stack1.pop(),num2,num1));
stack1.push(a2);
}
else {
stack1.push(a2);
}
}
}
}
index++;
if(index==list.size()){
break;
}
System.out.println(stack1);
System.out.print(stack2);
}
while(true) {
if(stack1.isEmpty()){
//当我们的符号栈为空的时候则计算到最后的结果,数栈中只有一个结果那就是我们的结果
break;
}
num1 = Double.parseDouble(stack2.pop());
System.out.println(stack1);
System.out.print(stack2);
}
while(true) {
if(stack1.isEmpty()){
//当我们的符号栈为空的时候则计算到最后的结果,数栈中只有一个结果那就是我们的结果
break;
}
num1 = Double.parseDouble(stack2.pop());
num2 = Double.parseDouble(stack2.pop());
stack2.push(""+getResult(stack1.pop(),num2,num1));
}
return Double.parseDouble((stack2.peek()));
}
public int getRandom(int n){ //get Random
Random a = new Random();
int b = a.nextInt(n);
return b;
}
public ArrayList<String> makeArraylist(ArrayList<String> list,int m){
int f;
f= getRandom(m);
if(f== 0) {
list.add(f,"(");
list.add(f+4,")");
}
else if(f==2){
list.add(f,"(");
list.add(f+4,")");
}
else if(f==4) {
list.add(f,"(");
list.add(f+4,")");
}
else if(f==6) {
list.add(f,"(");
list.add(")");
}
return list;
}
public static void main(String[] args) {
Stack2 stack = new Stack2();
String a = "36+4*3+4/2";
ArrayList li = new ArrayList();
li = stack.makeArraylist(stack.getArraylist(a), 6);
System.out.println(li);
double b = stack.getAnswer(li);
System.out.print(b);
}
}
stack2.push(""+getResult(stack1.pop(),num2,num1));
}
return Double.parseDouble((stack2.peek()));
}
public int getRandom(int n){ //get Random
Random a = new Random();
int b = a.nextInt(n);
return b;
}
public ArrayList<String> makeArraylist(ArrayList<String> list,int m){
int f;
f= getRandom(m);
if(f== 0) {
list.add(f,"(");
list.add(f+4,")");
}
else if(f==2){
list.add(f,"(");
list.add(f+4,")");
}
else if(f==4) {
list.add(f,"(");
list.add(f+4,")");
}
else if(f==6) {
list.add(f,"(");
list.add(")");
}
return list;
}
public static void main(String[] args) {
Stack2 stack = new Stack2();
String a = "36+4*3+4/2";
ArrayList li = new ArrayList();
li = stack.makeArraylist(stack.getArraylist(a), 6);
System.out.println(li);
double b = stack.getAnswer(li);
System.out.print(b);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具