50,527
社区成员
发帖
与我相关
我的任务
分享
public class test {
/**
* @author Wangzhg
* @since 2013-08-26
* @param args
*/
public static void main(String[] args) {
int r = program();
System.out.print(r);
}
public static int program() {
int result = 0;
Object[] data = new Object[5];
int i = 0;
char tmp = getChar();
while (tmp != '=') {
switch (tmp) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
StringBuffer sb = new StringBuffer();
sb.append(tmp);
tmp = getChar();
while (tmp != '*' && tmp != '+' && tmp != '=') {
sb.append(tmp);
tmp = getChar();
}
System.out.println(sb);
data[i] = Integer.valueOf(sb.toString());
i++;
break;
}
case '+': {
if (i == 3) {
data[0] = (Integer) data[0] + (Integer) data[2];
System.out.println('+');
System.out.println(data[0]);
i = 1;
} else {
data[i] = tmp;
tmp = getChar();
i++;
}
break;
}
case '*': {
if (i == 1) {
StringBuffer sb = new StringBuffer();
tmp = getChar();
while (tmp != '*' && tmp != '+' && tmp != '=') {
sb.append(tmp);
tmp = getChar();
}
System.out.println('*');
System.out.println(sb);
data[i + 1] = Integer.valueOf(sb.toString());
data[0] = (Integer) data[0] * (Integer) data[2];
System.out.println(data[0]);
}
if (i == 3) {
StringBuffer sb = new StringBuffer();
tmp = getChar();
while (tmp != '*' && tmp != '+' && tmp != '=') {
sb.append(tmp);
tmp = getChar();
}
System.out.println('*');
System.out.println(sb);
data[i + 1] = Integer.valueOf(sb.toString());
data[2] = (Integer) data[2] * (Integer) data[4];
System.out.println(data[2]);
}
if (i != 1 && i != 3) {
data[i] = tmp;
tmp = getChar();
i++;
}
break;
}
default:
return 0;
}
}
if (i > 1)
data[0] = (Integer) data[0] + (Integer) data[2];
result = (Integer) data[0];
return result;
}
static String _char = "1+11*2*3*4+9+9*2+6+7=";
static int _i = -1;
public static char getChar() {
_i++;
return _char.toCharArray()[_i];
}
}
import java.io.IOException;
public class CopyOfA3 {
// number: [0-9]*
// expr: expr0[+]expr0 | expr0[+]expr1
// expr0:expr1- expr1
// expr1:number
// stmt: "="
// - |_print
// stmt_list:stmt[stmt]
// program: stmt_list
static int result = 0;
static char tmp = getChar();// 空间复杂度O(1)
public static int program() {
matchExpr();// 一遍扫描,时间复杂度O(n)
print();
return result;
}
public static void print() {
System.out.println(result);
}
// 匹配加法
public static boolean matchExpr() {
int tmp_result = 0;
int left_num = 0;
int right_num = 0;
if (!(matchExpr0() || matchExpr1())) {// 乘法优先
return false;
}
left_num = result;
if (!matchAdd()) {
return false;
}
if (!(matchExpr0() || matchExpr1())) {
return false;
}
right_num = result;
tmp_result = left_num + right_num;
System.out.println(left_num + "+" + right_num);
result = tmp_result;
left_num = result;
while (matchAdd()) {
if ((matchExpr0() || matchExpr1())) {// 一定能匹配上
right_num = result;
tmp_result = left_num + right_num;
System.out.println(left_num + "+" + right_num);
result = tmp_result;
left_num = result;
}
}
result = tmp_result;
return true;
}
// 匹配乘法
public static boolean matchExpr0() {
int tmp_result = 0;
int left_num = 0;
int right_num = 0;
if (!matchExpr1()) {
return false;
}
left_num = result;
if (!matchM()) {
return false;
}
if (!matchExpr1()) {
return false;
}
right_num = result;
tmp_result = left_num * right_num;
System.out.println(left_num + "*" + right_num);
result = tmp_result;
left_num = result;
while (matchM()) {
matchExpr1();// 一定能够匹配
right_num = result;
tmp_result = left_num * right_num;
System.out.println(left_num + "*" + right_num);
result = tmp_result;
left_num = result;
}
result = tmp_result;
return true;
}
static int Expr1 = 0;
// 匹配数字
public static boolean matchExpr1() {
if (Expr1 == 1)
return true;
char _tmp = tmp;
StringBuffer sb = new StringBuffer();
do {
if (matchNumber()) {
sb.append(_tmp);
} else {
// 识别1个数字
if (sb.length() > 0) {
result = Integer.valueOf(sb.toString()).intValue();
Expr1 = 1;
return true;
} else {
return false;
}
}
} while (true);
}
public static char getChar() {
try {
char tmp = (char) System.in.read();
return tmp;
} catch (IOException e) {
e.printStackTrace();
}
return '=';
}
public static boolean matchNumber() {
if (tmp == '=')
return false;
if (tmp != '*' && tmp != '+') {
// i++;
tmp = getChar();
return true;
}
return false;
}
public static boolean matchM() {
if (tmp == '=')
return false;
if (tmp == '*') {
// i++;
tmp = getChar();
Expr1 = 0;
return true;
}
return false;
}
public static boolean matchAdd() {
if (tmp == '=')
return false;
if (tmp == '+') {
// i++;
tmp = getChar();
Expr1 = 0;
return true;
}
return false;
}
/**
* @param args
*/
public static void main(String[] args) {
program();
}
}