public class TestString
{
public static void main(String[] args)
{
String ts = "200-2d+3d6-d3/2+5+3dd6*3d";
//int intaddtmp = 0;
//StringBuffer straddtmp = new StringBuffer();
String[] tadd = ts.split("//+");
for (int i=0;i<tadd.length;i++)
{
String[] tdec = tadd[i].split("-");
for (int i=0;i<tdec.length;i++)
{
String[] tmul = tdec[i].split("//*");
for (int i=0;i<tmul.length;i++)
{
String[] tdiv = tmul[i].split("/");
double[] tdivnum = new double[tdiv.length];
for (int i=0;i<tdiv.length;i++)
{
String[] tdice = tdiv[i].split("d");
int intdiceaddtmp=0;
if (tdice[0].eauals(tdiv[i])
continue;
if (tdice.length > 2)
return "骰子表达式非法";
if (tdice[0].equals(""))
tdice[0]="1";
if (tdice.length = 1 )
{
for (int idice=0;idice<Integer.parseInt(tdice[0]);idice++)
{
int tmp = countDice(20);
intdiceaddtmp+=tmp;
intaddtmp.append("+").append(tmp);
}
}
else
{
for (int idice=0;idice<Integer.parseInt(tdice[0]);idice++)
{
int tmp = countDice(strChangint(tdice[1]));
intdiceaddtmp+=tmp;
intaddtmp.append("+").append(tmp);
}
}
if (tdiv[idiv].eauals(tmul[imul]))
continue; //问题
if (tdiv[idiv].equals(""))
retrun "第一位就是算术操作符或者算术操作符重复";
tdivnum[idiv] = Double.parseDouble(tdiv[idiv]);
if (tdivnum[idiv]==0)
retrun "不支持除法运算中含有0";
}
double tdivtmp = tdivnum[0]
for (idivnum=1;idivnum<tdivnum.length;idivnum++)
{
tdivtmp = tdivtmp/tdivnum[idivnum];
}
tmul[imul]=tdivtmp.toString();
if (tmul[imul].eauals(tsec[idec]))
continue;
if (tmul[imul].equals(""))
retrun "第一位就是算术操作符或者算术操作符重复";
tmulnum[imul] = Double.parseDouble(tmul[imul]);
double tdivtmp = tdivnum[0]
for (idivnum=1;idivnum<tdivnum.length;idivnum++)
{
tdivtmp = tdivtmp/tdivnum[idivnum];
}
tmul[imul]=tdivtmp.toString();
}
}
/*
if (tt[i].equals(""))
tt[i] = "没有内容的字符串";
System.out.println("tt["+i+"]: "+tt[i]);
*/
}
}
}
=========================
今天做了一些改动, 比较好的一个版本
/**
*
*/
/**
* @author rei
*
*/
public class TestString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
String cm = "3-12+10*6/0+5+";
//String cm = "3*2d10/2";
try
{
System.out.println(countExp(cm));
}
catch(NumberFormatException e)
{
System.out.println("存在非法字符");
}
}
public static String countExp(String countmessage) throws NumberFormatException
{
String returncm = "";
String[] tadd = countmessage.split("//+");
double[] taddnum = new double[tadd.length];
double naddtmp = 0;
for (int iadd=0;iadd<tadd.length;iadd++)
{
String[] tdec = tadd[iadd].split("-");
double[] tdecnum = new double[tdec.length];
double ndectmp = 0;
for (int idec=0;idec<tdec.length;idec++)
{
String[] tmul = tdec[idec].split("//*");
double[] tmulnum = new double[tmul.length];
double nmultmp = 0;
for (int imul=0;imul<tmul.length;imul++)
{
//System.out.println(tmul[imul]);
String[] tdiv = tmul[imul].split("/");
double[] tdivnum = new double[tdiv.length];
double ndivtmp = 0;
for (int idiv=0;idiv<tdiv.length;idiv++)
{
if (Double.parseDouble(tdiv[idiv])==0)
return "不支持除法表达式中含有0";
if (tdiv[idiv].equalsIgnoreCase("d"))
tdiv[idiv] = "1d20";
String[] tdice = tdiv[idiv].split("d");
int intdiceaddtmp=0;
if (tdice[0].equals(tdiv[idiv]))
{
intdiceaddtmp=Integer.parseInt(tdiv[idiv]);
//System.out.println(intdiceaddtmp);
}
else
{
if (tdice.length > 2)
return "骰子表达式非法";
if (tdice[0].equals(""))
tdice[0]="1";
if (tdice.length == 1 )
{
for (int idice=0;idice<Integer.parseInt(tdice[0]);idice++)
{
int tmp = countDice(20);
intdiceaddtmp+=tmp;
//intaddtmp.append("+").append(tmp);
}
}
else
{
if (!checkDice(Integer.parseInt(tdice[1])))
{
return "这个骰子面数有点奇怪...";
}
for (int idice=0;idice<Integer.parseInt(tdice[0]);idice++)
{
int tmp = countDice(Integer.parseInt(tdice[1]));
intdiceaddtmp+=tmp;
//intaddtmp.append("+").append(tmp);
}
}
}
tdiv[idiv] = Integer.toString(intdiceaddtmp);
tdivnum[idiv] = Double.parseDouble(tdiv[idiv]);
}
if (tdiv[0].equals(tmul[imul]))
{
tmulnum[imul] = Double.parseDouble(tmul[imul]);//continue
}
else
{
ndivtmp = tdivnum[0];
for (int idivnum=1;idivnum<tdivnum.length;idivnum++)
{
ndivtmp = ndivtmp/tdivnum[idivnum];
}
tmulnum[imul] = ndivtmp;
}
tmul[imul] = Double.toString(tmulnum[imul]);
}
if (tmul[0].equals(tdec[idec]))
{
tdecnum[idec] = Double.parseDouble(tdec[idec]);//continue
}
else
{
nmultmp = tmulnum[0];
for (int imulnum=1;imulnum<tmulnum.length;imulnum++)
{
nmultmp = nmultmp*tmulnum[imulnum];
}
tdecnum[idec] = nmultmp;
//System.out.println(nmultmp);
}
tdec[idec] = Double.toString(tdecnum[idec]);
//System.out.println(tdec[idec]);
}
if (tdec[0].equals(tadd[iadd]))
{
taddnum[iadd] = Double.parseDouble(tadd[iadd]);//continue
}
else
{
ndectmp = tdecnum[0];
for (int idecnum=1;idecnum<tdecnum.length;idecnum++)
{
ndectmp = ndectmp-tdecnum[idecnum];
}
taddnum[iadd] = ndectmp;
}
tadd[iadd] = Double.toString(taddnum[iadd]);
}
if (tadd[0].equals(countmessage))
{
returncm = countmessage + "=" + countmessage;
}
else
{
naddtmp = taddnum[0];
for (int iaddnum=1;iaddnum<taddnum.length;iaddnum++)
{
//System.out.println(naddtmp+":"+iaddnum+":"+taddnum.length);
naddtmp = naddtmp+taddnum[iaddnum];
//System.out.println("+:"+naddtmp+":"+taddnum.length);
}
returncm = countmessage + "=" + Double.toString(naddtmp);
}
return returncm;
}
private static boolean checkDice(int i) {
// TODO 自动生成方法存根
switch(i)
{
case 2:
return true;
case 3:
return true;
case 4:
return true;
case 6:
return true;
case 8:
return true;
case 10:
return true;
case 12:
return true;
case 20:
return true;
case 100:
return true;
default:
return false;
}
}
private static int countDice(int i) {
return (int)(Math.random()*i)+1;
}
}