Commit d02697be authored by ucifm's avatar ucifm Committed by uxrog
Browse files

added Calculator test

parent 483aecce
1 + 4
1 * 2 * 3 * 4 * 5
(12 + (5 - 2) * 4) * 1
((1 + (2) + (5)))
1 - 2 - 3 - 42 * 5
931 * 956 + -930 - -63 * 708 - -731 * -417 + 998 + -968 + 911 - -996 - 460 + 825 - -656 * 895 + -972 + 700 - -799 * -157 + 142 + 360 + 542 * -107 + -785 * 382 * 243 - -674 + 83 + 249 * -597 + 386 + 251 * -811 + -58 + 804 - 620 + -194 + -565 - -156 - 735 * -22 - -757 - 722 + -280 + 838 - 655 - 801 - 411 * -713 - 678 + 496 * -123 + -186 * -340 * 553 + 530 * -458 - -927 - 624 * 454 - -765 * 889 + 343 * -52 * 332 * 735 * -34 * -973 + 551 + -425 + -315 * 61 * -361 + 544 + 894 - -68 * 683 - -944 - 298 * 821 + 252 * 319 * -775 + -750 + -689 - -637 * -798 + -228 * 997 - 860 - 138 + -868 * -635 + 108 * 467 * 154 + -557 + 552 * 430 - 78 + 223
349 * 146 * -211 - 790 - (-83 - ((-850 + -291 * -890 * 847 * 836 - (416 - 380 * (-566) * 737 + -826 + ((87 - 117 - 563 + 192) * 613 - -33 + -273 * 532 + (63 - ((813 - -161 * 433 - -347 + (-298 + -341 - -546 - 367 + (976) * 22 * 741)) - -918 + 986) - 448 * 828 + 291 - 248) + 520 * -430 + 31 * 662 - -595) + -532) + 44 + -109)) + -510 * -17 - (620 - (-70 - -737 - 410 - -539 - -527 * (550 * (-827 + -428 - -361 - 495 - 703 + 60 + -656)) - -294)) * ((157 + -741 + -637 * -604 + -68) - -194 - 865 - ((-811 * (163 - 310 * 178) - -778 * -549 * -847 - 626 * 118) * (-553 - 728 * -239 + -916 - -583) - -10) - -691) - 978 - 457 + 628 * -106 - -715) * -98 * -904 * -718
87 * (90 * -328 * (85 - 403 + (-9 - -184 + (964 + 659 - -161 + -267 * -362 + -34) - (-134 * (-838 + 919 - (284 * 30 + 90 - 699 + 826 + -821 - 657 + 820 * -598 * -979 - 625) * (909 - 227) - 757 * 374 - -555 - 773 * -987) + 498 * -995 * 53 + (-488 - -739 * (-940 - ((-646 + 38 * 149 - -847 + 143 + (-390 - 745 * 147 * -822 - -559 * 927 + 474 + 261 * 697 - 395) + -508 * 801 + ((517 * -16 * -176 * -56 * 917 - (-3 + -949 + 790 + 853 + -882 + (-276 + -442 - -461 - 334 - 833 - 31 * 379 + 374 - -481) + 491 + (-965 + (-259 - 919 + 812) - (-899) + 155 + 499 + (-26 + 421 * 115 + 376 + -507 * 14 * (478 * 984 * -682 - -894 - -636 - 904 * 879 * 485 * 393 + ((655 * 465 + -819 - (-443 + -508 - 722 - -219 * -940) + -293 + (972 - -943 + 329 - -826 + -543 * 733 - 512 * 407 - 513 + 883) + -335 * 54 - 320 * -926) - -932 * -570 * 532 * 323 - 845 - 232 * 469 + 809 * 814) + -151) * -693 - -183) * -557 * 212 * ((-123 + 61 * -1 * (80 + -898 + 750 + 358 - -801 + 379 + -963 - 151) + -102 - 95 - -444 + 470 * 317 - 445) - -754 * -547 - (228) - (10 * -161 * ((-808 * -165 * -14 + -393 - -63 * 804) + 89 - 342 * -366 - -83 - 151 + -276 + 91 - 612) * -283 * -137 + 798 + -689) * -89))) + -218 - -423 - 6) + 926 + -973 * 420 * -831) - -287 + 451) - -503 * 63 - (785) - 365 * -920 - -384 - 205) + 210) - (-477 * 717 - -276 - 770 * (538 - 169) * 540)) + 544 * -94 - 254 - 353) - -596 - 307) - (-497 * 395 * -518 + 222 + 727 - (-234 - 630 - -566 - -71 + -7) + 159 - 370)) - (429 + -804 - 673 * 914 * -691 * -569 * -312 - (470 + 732 * -798 + 739 - -641 + -311 + -513 - (-939 + 63 + 580 + 99 + 118 - 258 + (-679 - 691) * -906 + (-690)) + (99 * -751 + -494 - 690 - (960 * -700 + -486 + -68) - -66 + -824 * 820 - 999 - -955)) - (297 + -785 - 213 * -356 * 251 - (((-420 + 844 - 168 * 193 + -466 + -645) + -174 + -968 - -567 - (-374 * 563 + -991 + (140 + (-195 + -430) * 460 + 894 + (547 * -949 * -689 - -261 - -184) * -16 * 953 + 378 - -305 * 646 - -103) * 349 - 474 + -167 - -778) * -246 - 388 - -653 + 428) + 34 - (-28 + -305 + -664 - 401 + -184) - -711 - -715 + -812 + 96 + -767 - 136 + 478) * -277 + -507 + -123 + 126)) - -137 - 782 + -27 - -971 + 650) * 398 - ((887 * -940 - -983 + -245 + 767 + -645 + -541) * -985 - 265 - 718 * 3) - 36 + -31 * -735 - 418 + -35
1 + + 1
class TextBuilder {
public Text text;
public int index;
public TextBuilder init(int length) {
this.text = new Text();
text.init(length);
return this;
}
public TextBuilder c(int ch) {
this.text.data[this.index] = ch;
this.index = this.index + 1;
return this;
}
public Text finish() {
return this.text;
}
}
class Text {
public int[] data;
public int length;
public void init(int length) {
this.length = length;
this.data = new int[length];
}
public void print() {
int idx = 0;
while (idx < this.length) {
System.out.write(this.data[idx]);
idx = idx + 1;
}
}
}
class StringLiterals {
public Text ERROR_INVALID_SYNTAX;
public Text RESULT_COLON;
public Text NEWLINE;
public void init() {
ERROR_INVALID_SYNTAX = new TextBuilder().init(21)
.c(69).c(114).c(114).c(111).c(114).c(58).c(32).c(105).c(110)
.c(118).c(97).c(108).c(105).c(100).c(32).c(115).c(121).c(110)
.c(116).c(97).c(120).finish();
RESULT_COLON = new TextBuilder().init(8)
.c(82).c(101).c(115).c(117).c(108).c(116).c(58).c(32).finish();
NEWLINE = new TextBuilder().init(8).c(10).finish();
}
}
class Token {
public int type;
public int data;
public void initNumber(int value) {
this.type = getNumberType();
this.data = value;
}
public void initAdd() {
this.type = getAddType();
}
public void initSub() {
this.type = getSubType();
}
public void initMul() {
this.type = getMulType();
}
public void initLParen() {
this.type = getLParenType();
}
public void initRParen() {
this.type = getRParenType();
}
public void initEOF() {
this.type = getEOFType();
}
public void initNewLine() {
this.type = getNewLineType();
}
public boolean equals(Token other) {
if (other == null) {
return false;
} else if (this.type == getNumberType() && this.type == other.type) {
return this.data == other.data;
} else {
return this.type == other.type;
}
}
public int getNumberType() { return 1; }
public int getAddType() { return 2; }
public int getSubType() { return 3; }
public int getMulType() { return 4; }
public int getLParenType() { return 10; }
public int getRParenType() { return 11; }
public int getNewLineType() { return 50; }
public int getEOFType() { return 100; }
}
class Lexer {
public int currentByte;
public void init() throws Exception {
nextByte();
}
public Token nextToken(Token token) throws Exception {
skipWhitespace();
if (isDigit(currentByte)) {
int value = lexNumber();
token.initNumber(value);
} else if (isPlus(currentByte)) {
token.initAdd();
nextByte();
} else if (isMinus(currentByte)) {
token.initSub();
nextByte();
} else if (isStar(currentByte)) {
token.initMul();
nextByte();
} else if (isLParen(currentByte)) {
token.initLParen();
nextByte();
} else if (isRParen(currentByte)) {
token.initRParen();
nextByte();
} else if (isNewLine(currentByte)) {
token.initNewLine();
} else {
token.initEOF();
}
return token;
}
public void nextByte() throws Exception {
currentByte = System.in.read();
}
public int lexNumber() throws Exception {
int value = 0;
while (isDigit(currentByte)) {
value = 10 * value;
value = value + digitValue(currentByte);
nextByte();
}
return value;
}
public void skipWhitespace() throws Exception {
while (isWhitespace(currentByte)) {
nextByte();
}
}
public boolean isDigit(int i) {
return 48 <= i && i < 58;
}
public int digitValue(int i) {
return i - 48;
}
public boolean isPlus(int i) {
return i == 43;
}
public boolean isMinus(int i) {
return i == 45;
}
public boolean isStar(int i) {
return i == 42;
}
public boolean isLParen(int i) {
return i == 40;
}
public boolean isRParen(int i) {
return i == 41;
}
public boolean isWhitespace(int i) {
return i == 32 || i == 9;
}
public boolean isNewLine(int i) {
return i == 10;
}
}
class Parser {
public Lexer lexer;
public Token currentToken;
public boolean error;
public void init() throws Exception {
this.currentToken = new Token();
this.lexer = new Lexer();
reset();
}
public void reset() throws Exception {
this.error = false;
this.lexer.init();
nextToken();
}
public void nextToken() throws Exception {
this.currentToken = this.lexer.nextToken(this.currentToken);
}
public int parseExpression() throws Exception {
int result = parseTerm();
while (currentToken.type == currentToken.getAddType()
|| currentToken.type == currentToken.getSubType()) {
int opType = currentToken.type;
nextToken();
int rhs = parseExpression();
if (opType == currentToken.getAddType()) {
result = result + rhs;
} else {
result = result - rhs;
}
}
return result;
}
public int parseTerm() throws Exception {
int result = parseFactor();
while (currentToken.type == currentToken.getMulType()) {
nextToken();
int rhs = parseTerm();
result = result * rhs;
}
return result;
}
public int parseFactor() throws Exception {
int result = 0;
if (currentToken.type == currentToken.getNumberType()) {
result = currentToken.data;
nextToken();
} else if (currentToken.type == currentToken.getLParenType()) {
nextToken();
result = parseExpression();
if (currentToken.type == currentToken.getRParenType()) {
nextToken();
} else {
this.error = true;
}
} else if (currentToken.type == currentToken.getSubType()) {
nextToken();
result = parseFactor();
return -result;
} else {
this.error = true;
}
return result;
}
}
class Main {
public static void main(String[] args) throws Exception {
StringLiterals literals = new StringLiterals();
literals.init();
Parser parser = new Parser();
parser.init();
boolean exit = false;
while (!exit) {
System.out.flush();
int result = parser.parseExpression();
if (!parser.error) {
literals.RESULT_COLON.print();
System.out.println(result);
} else {
literals.ERROR_INVALID_SYNTAX.print();
literals.NEWLINE.print();
}
System.out.flush();
parser.reset();
Token curToken = parser.currentToken;
if (curToken.type == curToken.getEOFType()) {
exit = true;
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment