Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. import React from 'react';
  2. import { operPriority } from './operPriority';
  3. import Columns from './columns';
  4.  
  5. class Visualization extends React.Component {
  6.  
  7. constructor (props) {
  8. super(props);
  9. //this.handleChange = this.handleChange.bind(this);
  10. this.state = {
  11. inf: this.props.exp,
  12. car: [],
  13. post: [],
  14. msg: '',
  15. tog: this.props.toggle
  16. }
  17. }
  18.  
  19. render() {
  20.  
  21. let letterNumber = /^[0-9a-zA-Z]+$/;
  22.  
  23. for (let i=0; i < this.state.inf.length; i++) {
  24. let c = this.state.inf[i];
  25.  
  26. if (c.match(letterNumber)) {
  27. this.setState({inf: this.state.inf.shift()});
  28. this.setState({msg: 'Είναι αριθμός, συνεπώς περνάει απευθείας στο αποτέλεσμα.'});
  29. this.setState({post: this.state.post.push(c)});
  30. }
  31.  
  32. else if (c === "(") {
  33. this.setState({inf: this.state.inf.shift()});
  34. this.setState({msg: 'Είναι (, συνεπώς παραμένει ως κρατούμενο.'});
  35. this.setState({car: this.state.car.push(c)});
  36. }
  37.  
  38. else if (c === ")") {
  39. while (this.state.car.length > 0 && this.state.car[this.state.car.length-1] !== "(") {
  40. this.setState({inf: this.state.inf.shift()});
  41. this.setState({post: this.state.post.push(this.state.car.pop())});
  42. this.setState({msg: 'Είναι ), συνεπώς η έκφραση έκλεισε και παύει να είναι κρατούμενο.'});
  43. }
  44.  
  45. if (this.state.car.length > 0 && this.state.car[this.state.car.length-1] !== "(") {
  46. this.setState({msg: 'Invalid Expression'});
  47. }
  48. else {
  49. this.setState({inf: this.state.inf.shift()});
  50. this.setState({car: this.state.car.pop()});
  51. this.setState({msg: 'Είναι ), συνεπώς η έκφραση έκλεισε και παύει να είναι κρατούμενο και έχουμε το τελικό αποτέλεσμα.'});
  52. }
  53. }
  54.  
  55. else {
  56. while(this.state.car.length > 0 && operPriority(c) <= operPriority(this.state.car[this.state.car.length-1])) {
  57. this.setState({inf: this.state.inf.shift()});
  58. this.setState({post: this.state.post.push(this.state.car.pop())});
  59. this.setState({msg: 'Ο Operator έχει μεγαλύτερη προταιρεότητα από τον αμέσως προηγούμενο του.'});
  60. }
  61. this.setState({car: this.state.car.push(c)});
  62. this.setState({msg: 'Invalid Expression'});
  63. }
  64. }
  65.  
  66. while (this.state.car.length > 0) {
  67. this.setState({inf: this.state.inf.shift()});
  68. this.setState({post: this.state.post.push(this.state.car.pop())});
  69. this.setState({msg: 'add to carry'});
  70. }
  71.  
  72. return(
  73. <div>
  74. <Columns
  75. spec = 'Specifications: '
  76. infix = {this.state.inf}
  77. carry = {this.state.car}
  78. postfix = {this.state.post}
  79. message = {this.state.msg}
  80. toggle = {this.state.tog}
  81. />
  82. </div>
  83. );
  84. }
  85.  
  86.  
  87. }
  88.  
  89. export default Visualization;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement