Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package cu.cs.cpsc2150.project1;
  2.  
  3. public class VariableExpression extends SymbolTable {
  4.  
  5. String var, operator;
  6.  
  7. int num;
  8.  
  9. public String getVar() {
  10. return var;
  11. }
  12.  
  13. public void setVar(String var) {
  14. this.var = var;
  15. }
  16.  
  17. public int getNum() {
  18. return num;
  19. }
  20.  
  21. public void setNum(int num) {
  22. this.num = num;
  23. }
  24.  
  25. public String getOperator() {
  26. return operator;
  27. }
  28.  
  29. public void setOperator(String operator) {
  30. this.operator = operator;
  31. }
  32.  
  33.  
  34.  
  35. public void readVariable(String input){
  36. if (input.indexOf(":=") > -1){
  37. // Assign a value to key in Symbol table
  38.  
  39. String[] elems = input.split("\\s+");
  40.  
  41. setVar(elems[0]);
  42. setNum(Integer.parseInt(elems[2]));
  43.  
  44. SymbolTable.insert(getVar(), getNum());
  45.  
  46. }else if(input.length() == 1){
  47. // check if key has a value
  48. // if so return
  49. //else error
  50.  
  51. Integer value = SymbolTable.getValue(input);
  52.  
  53. if(value != null){
  54. System.out.println(input + " = " + value);
  55. }else{
  56. System.out.println("Variable " + input + "has no value assignment");
  57. }
  58. }else{
  59.  
  60.  
  61. String[] elems = input.split("\\s+");
  62.  
  63. Integer valueOfVar1 = SymbolTable.getValue(elems[0]);
  64. Integer valueOfVar2 = SymbolTable.getValue(elems[2]);
  65.  
  66.  
  67.  
  68. setOperator(elems[1]);
  69.  
  70. if(elems[0].matches(".*[a-zA-Z]+.*")){
  71. if(valueOfVar1 != null){
  72. valueOfVar1 = SymbolTable.getValue(elems[0]);
  73. }else{
  74. System.out.println("Variable " + input + "has no value assignment");
  75. }
  76. }else{
  77. valueOfVar1 = Integer.parseInt(elems[0]);
  78. }
  79.  
  80. if(elems[2].matches(".*[a-zA-Z]+.*")){
  81. if(valueOfVar2 != null){
  82. valueOfVar2 = SymbolTable.getValue(elems[2]);
  83. }else{
  84. System.out.println("Variable " + input + "has no value assignment");
  85. }
  86. }else{
  87. valueOfVar2 = Integer.parseInt(elems[2]);
  88. }
  89.  
  90. double ans = BinaryExpression.calculate(valueOfVar1.intValue(),valueOfVar2.intValue(), getOperator());
  91.  
  92.  
  93. System.out.println(input + " = " + ans);
  94.  
  95. }
  96. }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement