你这个字符串是不是只有一个"+"号?
如果只有一个,那太简单了
public class STest {
public static void main(String[] args) {
try {
String str = "22+44";
String operator = "+";
int i = str.indexOf(operator);
String a = str.substring(0,i);
String b = str.substring(i+1);
System.out.println(a);
System.out.println(b);
System.out.println(operator);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
如果只有一个,那太简单了
public class STest {
public static void main(String[] args) {
try {
String str = "22+44";
String operator = "+";
int i = str.indexOf(operator);
String a = str.substring(0,i);
String b = str.substring(i+1);
System.out.println(a);
System.out.println(b);
System.out.println(operator);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
if(st.hasMoreTokens()){
a=st.nextToken();
}
if(st.hasMoreTokens()){
c=st.nextToken();
}
String a,b,operator;
String temp="";
String numberstring="0123456789.";
Vector v=new Vector();
a=b=operator="";
int flag=0;
for(int i=0;i<s.length();i++){
temp=s.substring(i,i+1);
if (numberstring.indexOf(temp)!=-1){
a=a.concat(temp);
}
else {
if (a.length()!=0) v.add(a);
v.add(temp);
a="";
}
}
if (a.length()!=0) v.add(a);
System.out.println(v.toString());
2)String a,b,operator;
String temp="";
String numberstring="0123456789.";
a=b=operator="";
int flag=0;
for(int i=0;i<s.length();i++){
temp=s.substring(i,i+1);
if (numberstring.indexOf(temp)!=-1){
if (operator.length()==0) a=a.concat(temp);
else b=b.concat(temp);
}
else {
if (b.length()==0) operator=operator.concat(temp);
else break;
}
}
System.out.println(a+" "+b+" "+operator);
String a,b,operator;
String temp="";
String numberstring="0123456789.";//多一个点是处理小数用的
String operatorstring="+-*/()";//这里可以增加你所要的操作符
Vector v=new Vector();
a=b=operator="";
int flag=0;
for(int i=0;i<s.length();i++){
temp=s.substring(i,i+1);
if (numberstring.indexOf(temp)!=-1){
a=a.concat(temp);
}
else if(operatorstring.indexOf(temp)!=-1) {
if (a.length()!=0) v.add(a);
v.add(temp);
a="";
}
else{
if (a.length()!=0) v.add(a);
a="";
}
}
if (a.length()!=0) v.add(a);
System.out.println(v.toString());
还是这个正确
-就当作减号处理 效果一样!
用BNF写几个式子
然后就是老一套了嘛
public class ST {
public static void main(String[] args) {
try {
int i = 0;
int j=0;
String str = "12+23-12*123/12";
String[] operator = {"+","-","*","/"};
String[] block = new String[str.length()];//定义足够长
for(i=0;i<block.length;i++) {
block[i] = "";
}
int max = 0;
i=0;
while(max!=-1) {
max = -1;
int[] num = new int[operator.length];//num = 4;
for(j=0;j<num.length;j++) {
num[j] = str.indexOf(operator[j]);
}
for(j=0;j<num.length;j++) {
if(j==0) {
max = num[0];
}
else {
if(num[j]!=-1) {
if(max==-1) {
max = num[j];
}
else {
if(num[j]<max) {
max = num[j];
}
}
}
}
}
if(max!=-1) {
block[i] = str.substring(0,max);
str = str.substring(max+1);
/*//如果不需要将运算符号也算进来,可以将下面的循环注释掉
for(j=0;j<num.length;j++) {
if(num[j]==max) {
i++;
block[i] = operator[j];
}
}
//*/
i++;
}
else {
block[i] = str;
break;
}
}
for(i=0;i<block.length;i++) {
if(!block[i].equals("")) {
System.out.println(block[i]);
}
} }
catch(Exception e) {
e.printStackTrace();
}
}
}
里面就行
String operatorstring="+-*/()";//这里可以增加你所要的操作符
负号当作减号处理