java高难度题 一般人别看 就是写一个类(可以有多个方法),里面包含+,-,x ,%,(),/的数学运算,要求返回一个字符串(并且要进行四舍五入) 也就是用java代码实现 ‘(1+2×3%(5-2))+(6/2)’(要考虑到优先级以及括号是否匹配的问题)。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 要的是这个吗?// =============== Program Description ===============// 程序名称: Stack04.java// 程序目的: 输入一前序表达式,并计算其值。// ===================================================import ConsoleReader.*; // 导入已定义的数据输入类public class Stack04{ public static void main (String args[]) { StackArray Prefix = new StackArray(); // 前序表达式堆栈 StackArray Operand = new StackArray(); // 运算量堆栈 String Expression = new String(); // 声明表达式字串 int Position = 0; // 表达式位置 int Operator1; // 运算符 int Operand1 = 0; // 前运算量 int Operand2 = 0; // 后运算量 int Evaluate = 0; // 运算结果 int Token = 0; // 输出字符 System.out.print("Please input the preorder expression :"); // 读取前序表达式存入字串 ConsoleReader console = new ConsoleReader(System.in); Expression = console.readLine(); while ( true ) { if ( Expression.charAt(Position) < '0' ) Prefix.Push( (int) Expression.charAt(Position)); else Prefix.Push( (int) Expression.charAt(Position)-48); Position++; if ( Position >= Expression.length() ) break; } // 取出表达式堆栈的内容 while ( Prefix.Top != -1 ) { Token = Prefix.Pop(); // 先取出一个数据值 if (Prefix.IsOperator(Token)) // 判断是否为运算符 { Operand1 = Operand.Pop(); Operand2 = Operand.Pop(); // 计算后运算量后存入堆栈 Operand.Push(Operand.TwoResult(Token,Operand1,Operand2) ); } else // 存入运算量堆栈--需做ASCII码转换 Operand.Push(Token); } // 取出表达式的最终结果 Evaluate = Operand.Pop(); System.out.print("The expression [ "+Expression+" ] "); System.out.println("result is "+Evaluate); }}class StackArray{ int MaxSize = 20; int[] AStack = new int[MaxSize]; // 声明堆栈数组 int Top = -1; // 堆栈顶端 //--------------------------------------------// 存入堆栈数据//-------------------------------------------- public void Push(int Value) { int i; if (Top >= MaxSize) // 判断是否已超出堆栈的最大容量 System.out.println("The stack is full!!"); else { Top++; // 堆栈顶端指针+1 AStack[Top] = Value; // 将数据存入堆栈中 } } //--------------------------------------------// 从堆栈中取出数据//-------------------------------------------- public int Pop() { int Temp; // 保存从堆栈取出来的数据 int i; // 循环计数变量 if (Top < 0) // 判断堆栈是否为空 { System.out.println("The stack is empty!!"); return -1; } Temp = AStack[Top]; // 将取出数据保存于变量中 Top--; // 堆栈顶端指标-1 return Temp; } //--------------------------------------------// 判断是否为运算符//-------------------------------------------- public boolean IsOperator(int Operator) { if ( Operator == 43 || Operator == 45 || Operator == 42 || Operator == 47 ) return true; // + - * / 运算符 else return false; } //--------------------------------------------// 判断运算符的优先权//-------------------------------------------- public int Priority(int operator) { if ( operator == 43 || operator == 45 ) // + - 运算符 return 1; else if ( operator == 42 || operator == 47 ) // * / 运算符 return 2; else return 0; } //--------------------------------------------// 计算任两个运算量的值//-------------------------------------------- public int TwoResult(int operator,int operand1,int operand2) { switch (operator) { case 43: return (operand2 + operand1); case 45: return (operand2 - operand1); case 42: return (operand2 * operand1); case 47: return (operand2 / operand1); } return 0; }}-------------------------------------// =============== Program Description ===============// 程序名称: ConsoleReader.java// 程序目的: 本类用于数据的输入及转换。 // ===================================================import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.IOException;public class ConsoleReader{ public ConsoleReader(InputStream inStream) { reader = new BufferedReader (new InputStreamReader(inStream)); } public int readInt() // 整数输入 { String inputString = readLine(); int n = Integer.parseInt(inputString); return n; } public double readDouble() // 浮点数输入 { String inputString = readLine(); double x = Double.parseDouble(inputString); return x; } public String readLine() { String inputLine = ""; try { inputLine = reader.readLine(); } catch(IOException e) { System.out.println(e); System.exit(1); } return inputLine; } private BufferedReader reader; } java基础性问题,适合初学者做练习,栈实现相对简单 这个1多年前上编译原理实验老师要求做过,我是用C写的,因为那时候只会C....首先要将数字从string转成int,怎么实现你自己想吧...计算的话我是先这么做的,先将输入的式子转化成逆波兰,就是后序排列转化规则1.遇到数字直接写到逆波兰式子2.遇到(直接入栈3.遇到)把所有出栈,直到最近的一个(为止,把出栈的东西按序写到逆波兰式子4.+-遇到栈顶为*/的,*/出栈,+-入栈5.同优先级的栈顶出栈写到逆波兰式子,然后入栈大概这样就能得到逆波兰式了...好久了忘了差不多这样根据逆波兰式来计算就简单多了,2个栈,按逆波兰顺序将数字和运算符分开放入,将数字栈栈顶2个数字和运算符栈顶运算符进行计算,然后将结果入栈到数字栈,遇到+/要注意两数字的顺序,这样到最后数字栈里剩下的就是结果可能表达的不清楚吧,话说这个不算什么高难度吧....这都算的话我就不愁毕业找工作了....大四就业压力好大 请看我的帖子.http://topic.csdn.net/u/20080709/12/c92cc549-3dc3-4a0b-b6e8-7c5fe7c4c9d5.html 同问: 这和java有啥关系?? 楼主是骗答案来了吧,这种eval性质的解决方案很多啊。再说像后缀表达式等的东西大学时应该学过吧? 哈哈,以前做个类似的题。现在都很少碰这种题了。话说现在在学servlet... public class Js { public int jisuan(String biaodashi) { int pd1=-1,jishu1=0,jilu1,jieguo; StringBuffer sb=new StringBuffer(),sb1=new StringBuffer(),sb2=new StringBuffer();; String newstr=null,oldstr1=null,oldstr2=null,oldstr3=null; pd1=biaodashi.indexOf('('); if(pd1>=0) { jishu1=1; sb1.append(biaodashi.substring(0, pd1)); System.out.println(sb1.toString()); for(int j=pd1+1;j<biaodashi.length();j++) { if(biaodashi.charAt(j)=='(') { jishu1++; } if(biaodashi.charAt(j)==')') { jishu1--; if(jishu1==0) { jilu1=j; //记住)的位置,以做加入新biaodashi oldstr2=biaodashi.substring(pd1+1,jilu1); oldstr3=biaodashi.substring(jilu1+1); //System.out.println(oldstr2+" "+oldstr3); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!! break; } } sb.append(biaodashi.charAt(j)); } String comtempstr=sb.toString(); int conjieguo=jisuan(comtempstr); //System.out.println(conjieguo+" zanzhi "+String.valueOf(conjieguo)); //!!!!!!!!!!!!!!!!!!!!!!!!!! sb1.append(String.valueOf(conjieguo)); sb1.append(oldstr3); newstr=sb1.toString(); return jisuan(newstr); } else //没有括号 { int jilufuhao=0; for(int i=0;i<biaodashi.length();i++) { if(biaodashi.charAt(i)=='+'||biaodashi.charAt(i)=='-'||biaodashi.charAt(i)=='*'||biaodashi.charAt(i)=='/') { jilufuhao++; } } //System.out.println(jilufuhao); int shuzi[]=new int[jilufuhao+1]; //产生几个数字,让 其都为0 int fuhao[]=new int[jilufuhao]; int j=0,temp=0; //看产生的是* / for(int i=0;i<biaodashi.length();i++) { if(!(biaodashi.charAt(i)=='+'||biaodashi.charAt(i)=='-'||biaodashi.charAt(i)=='*'||biaodashi.charAt(i)=='/')) { shuzi[j]=shuzi[j]*10+Integer.parseInt(String.valueOf(biaodashi.charAt(i))); } else { fuhao[j]=biaodashi.charAt(i); j++; } } int j1=0,is=0,ifu=0,c=0; while(is<jilufuhao) { if(fuhao[ifu]=='+'||fuhao[ifu]=='-') { shuzi[++j1]=shuzi[++is]; if(fuhao[ifu]=='-') shuzi[j1]=-1*shuzi[j1]; ifu++; } else{ if(fuhao[ifu]=='*') { shuzi[j1]=shuzi[j1]*shuzi[++is]; } if(fuhao[ifu]=='/') { shuzi[j1]=shuzi[j1]/shuzi[++is]; } ifu++; } } int zuizhongjieguo=0,front=0; while(front<=j1) { zuizhongjieguo=shuzi[front]+zuizhongjieguo; front++; } return zuizhongjieguo; } } public static void main(String args[]) { new JieMian().show(); }} import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class JieMian extends JFrame implements ActionListener{ private JTextField field; private JLabel label; public JieMian() { field=new JTextField("",30); label=new JLabel("answer"); Container con=this.getContentPane(); con.setLayout(new FlowLayout()); this.setBounds(100, 100, 400, 100); con.add(field); con.add(label); field.addActionListener(this); } public void actionPerformed(ActionEvent e) { Js jisuan=new Js(); String s=field.getText().trim(); int jieguo=jisuan.jisuan(s); label.setText(String.valueOf(jieguo)); }} me too [/Quote] me too 哈哈,高人多啊,怎么没人换种方式避开这个思路。生成一个.java的临时文件,main函数里定义一个Object=你要取值的表达式,输出类型和值;然后用Runtime调用javac.exe编译生成.class文件;再用Runtime调用java.exe执行.class文件获取输出流。...... 我是来回忆大学生活的想当年大学里数据结构老师就逼我们写这个高难度的用栈,看数据结构Java版的应该有讲解 集合框架API文档方法如何使用 为什么是闭包 那位仁兄能给我解释一下“||”的用法? 简单一个题 问个问题!帮我解决一下!在线等!! 我照着例子有点改动怎么不行,谁能帮看看的?? 菜鸟的菜问题,希望指点! 我的map问题,请教! 菜鸟的问题(见笑了) Java中static一般有什么作用 求助!Jcheckbox里面的对号怎么变颜色呢? java中调用sqlserver的存储过程(游标分页的存储过程)
// =============== Program Description ===============
// 程序名称: Stack04.java
// 程序目的: 输入一前序表达式,并计算其值。
// ===================================================
import ConsoleReader.*; // 导入已定义的数据输入类public class Stack04
{
public static void main (String args[])
{
StackArray Prefix = new StackArray(); // 前序表达式堆栈
StackArray Operand = new StackArray(); // 运算量堆栈
String Expression = new String(); // 声明表达式字串
int Position = 0; // 表达式位置
int Operator1; // 运算符
int Operand1 = 0; // 前运算量
int Operand2 = 0; // 后运算量
int Evaluate = 0; // 运算结果
int Token = 0; // 输出字符 System.out.print("Please input the preorder expression :");
// 读取前序表达式存入字串
ConsoleReader console = new ConsoleReader(System.in);
Expression = console.readLine(); while ( true )
{
if ( Expression.charAt(Position) < '0' )
Prefix.Push( (int) Expression.charAt(Position));
else
Prefix.Push( (int) Expression.charAt(Position)-48);
Position++;
if ( Position >= Expression.length() )
break;
}
// 取出表达式堆栈的内容
while ( Prefix.Top != -1 )
{
Token = Prefix.Pop(); // 先取出一个数据值
if (Prefix.IsOperator(Token)) // 判断是否为运算符
{
Operand1 = Operand.Pop();
Operand2 = Operand.Pop();
// 计算后运算量后存入堆栈
Operand.Push(Operand.TwoResult(Token,Operand1,Operand2) );
}
else
// 存入运算量堆栈--需做ASCII码转换
Operand.Push(Token);
}
// 取出表达式的最终结果
Evaluate = Operand.Pop();
System.out.print("The expression [ "+Expression+" ] ");
System.out.println("result is "+Evaluate);
}
}class StackArray
{
int MaxSize = 20;
int[] AStack = new int[MaxSize]; // 声明堆栈数组
int Top = -1; // 堆栈顶端
//--------------------------------------------
// 存入堆栈数据
//--------------------------------------------
public void Push(int Value)
{
int i; if (Top >= MaxSize) // 判断是否已超出堆栈的最大容量
System.out.println("The stack is full!!");
else
{
Top++; // 堆栈顶端指针+1
AStack[Top] = Value; // 将数据存入堆栈中
}
}
//--------------------------------------------
// 从堆栈中取出数据
//--------------------------------------------
public int Pop()
{
int Temp; // 保存从堆栈取出来的数据
int i; // 循环计数变量 if (Top < 0) // 判断堆栈是否为空
{
System.out.println("The stack is empty!!");
return -1;
} Temp = AStack[Top]; // 将取出数据保存于变量中
Top--; // 堆栈顶端指标-1
return Temp;
} //--------------------------------------------
// 判断是否为运算符
//--------------------------------------------
public boolean IsOperator(int Operator)
{
if ( Operator == 43 || Operator == 45
|| Operator == 42 || Operator == 47 )
return true; // + - * / 运算符
else
return false;
}
//--------------------------------------------
// 判断运算符的优先权
//--------------------------------------------
public int Priority(int operator)
{
if ( operator == 43 || operator == 45 ) // + - 运算符
return 1;
else if ( operator == 42 || operator == 47 ) // * / 运算符
return 2;
else
return 0;
}
//--------------------------------------------
// 计算任两个运算量的值
//--------------------------------------------
public int TwoResult(int operator,int operand1,int operand2)
{
switch (operator)
{
case 43:
return (operand2 + operand1);
case 45:
return (operand2 - operand1);
case 42:
return (operand2 * operand1);
case 47:
return (operand2 / operand1);
}
return 0;
}
}
-------------------------------------
// =============== Program Description ===============
// 程序名称: ConsoleReader.java
// 程序目的: 本类用于数据的输入及转换。
// ===================================================
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;public class ConsoleReader
{
public ConsoleReader(InputStream inStream)
{
reader = new BufferedReader (new InputStreamReader(inStream));
}
public int readInt() // 整数输入
{
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() // 浮点数输入
{
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
} public String readLine()
{
String inputLine = "";
try
{
inputLine = reader.readLine();
}
catch(IOException e)
{
System.out.println(e);
System.exit(1);
} return inputLine;
}
private BufferedReader reader;
}
首先要将数字从string转成int,怎么实现你自己想吧...计算的话我是先这么做的,先将输入的式子转化成逆波兰,就是后序排列
转化规则
1.遇到数字直接写到逆波兰式子
2.遇到(直接入栈
3.遇到)把所有出栈,直到最近的一个(为止,把出栈的东西按序写到逆波兰式子
4.+-遇到栈顶为*/的,*/出栈,+-入栈
5.同优先级的栈顶出栈写到逆波兰式子,然后入栈
大概这样就能得到逆波兰式了...好久了忘了差不多这样根据逆波兰式来计算就简单多了,2个栈,按逆波兰顺序将数字和运算符分开放入,将数字栈栈顶2个数字和运算符栈顶运算符进行计算,然后将结果入栈到数字栈,遇到+/要注意两数字的顺序,这样到最后数字栈里剩下的就是结果可能表达的不清楚吧,话说这个不算什么高难度吧....这都算的话我就不愁毕业找工作了....大四就业压力好大
同问: 这和java有啥关系??
再说像后缀表达式等的东西大学时应该学过吧?
现在都很少碰这种题了。
话说现在在学servlet...
public int jisuan(String biaodashi)
{
int pd1=-1,jishu1=0,jilu1,jieguo;
StringBuffer sb=new StringBuffer(),sb1=new StringBuffer(),sb2=new StringBuffer();;
String newstr=null,oldstr1=null,oldstr2=null,oldstr3=null;
pd1=biaodashi.indexOf('(');
if(pd1>=0)
{
jishu1=1;
sb1.append(biaodashi.substring(0, pd1));
System.out.println(sb1.toString());
for(int j=pd1+1;j<biaodashi.length();j++)
{
if(biaodashi.charAt(j)=='(')
{
jishu1++;
}
if(biaodashi.charAt(j)==')')
{
jishu1--;
if(jishu1==0)
{
jilu1=j; //记住)的位置,以做加入新biaodashi
oldstr2=biaodashi.substring(pd1+1,jilu1);
oldstr3=biaodashi.substring(jilu1+1);
//System.out.println(oldstr2+" "+oldstr3); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
break;
}
}
sb.append(biaodashi.charAt(j));
}
String comtempstr=sb.toString();
int conjieguo=jisuan(comtempstr);
//System.out.println(conjieguo+" zanzhi "+String.valueOf(conjieguo)); //!!!!!!!!!!!!!!!!!!!!!!!!!!
sb1.append(String.valueOf(conjieguo));
sb1.append(oldstr3);
newstr=sb1.toString();
return jisuan(newstr);
}
else //没有括号
{
int jilufuhao=0;
for(int i=0;i<biaodashi.length();i++)
{
if(biaodashi.charAt(i)=='+'||biaodashi.charAt(i)=='-'||biaodashi.charAt(i)=='*'||biaodashi.charAt(i)=='/')
{
jilufuhao++;
}
}
//System.out.println(jilufuhao);
int shuzi[]=new int[jilufuhao+1]; //产生几个数字,让 其都为0
int fuhao[]=new int[jilufuhao];
int j=0,temp=0; //看产生的是* /
for(int i=0;i<biaodashi.length();i++)
{
if(!(biaodashi.charAt(i)=='+'||biaodashi.charAt(i)=='-'||biaodashi.charAt(i)=='*'||biaodashi.charAt(i)=='/'))
{
shuzi[j]=shuzi[j]*10+Integer.parseInt(String.valueOf(biaodashi.charAt(i)));
}
else {
fuhao[j]=biaodashi.charAt(i);
j++;
}
}
int j1=0,is=0,ifu=0,c=0;
while(is<jilufuhao)
{
if(fuhao[ifu]=='+'||fuhao[ifu]=='-')
{
shuzi[++j1]=shuzi[++is];
if(fuhao[ifu]=='-')
shuzi[j1]=-1*shuzi[j1];
ifu++;
}
else{
if(fuhao[ifu]=='*')
{
shuzi[j1]=shuzi[j1]*shuzi[++is];
}
if(fuhao[ifu]=='/')
{
shuzi[j1]=shuzi[j1]/shuzi[++is];
}
ifu++;
}
}
int zuizhongjieguo=0,front=0;
while(front<=j1)
{
zuizhongjieguo=shuzi[front]+zuizhongjieguo;
front++;
}
return zuizhongjieguo;
}
}
public static void main(String args[])
{
new JieMian().show();
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JieMian extends JFrame implements ActionListener{
private JTextField field;
private JLabel label;
public JieMian()
{
field=new JTextField("",30);
label=new JLabel("answer");
Container con=this.getContentPane();
con.setLayout(new FlowLayout());
this.setBounds(100, 100, 400, 100);
con.add(field);
con.add(label);
field.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Js jisuan=new Js();
String s=field.getText().trim();
int jieguo=jisuan.jisuan(s);
label.setText(String.valueOf(jieguo));
}}
[/Quote]
me too
生成一个.java的临时文件,main函数里定义一个Object=你要取值的表达式,输出类型和值;
然后用Runtime调用javac.exe编译生成.class文件;
再用Runtime调用java.exe执行.class文件获取输出流。
......
想当年大学里数据结构老师就逼我们写这个高难度的用栈,看数据结构Java版的应该有讲解