The command window is displaying the following:
C:\Users\Dell\Desktop\PROGRA~1>node revers~2.js
events.js:160
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open
'C:\Users\Dell\Desktop\PROGRA~1\Input_File.txt'
at Error (native)
I am unsure what this error is referencing, but I suspect it might be related to the Node.js
software used for running my code.
Any assistance you can offer is greatly appreciated. I apologize if this question seems basic or self-explanatory, as I am unable to pinpoint the issue.
It is possible that my code may not be suitable for the task at hand.
The Input_File.txt
contains:
15 12 2 + -4 * + 3 -
This data in the file pertains to a Reverse Polish Function that I am trying to implement and solve through the program.
'use strict' ;
var ArrayStack = require('./ArrayStack');
const fs = require('fs');
const readline = require('readline');
var ArrayStack2 = new ArrayStack();
var readStream = fs.createReadStream('Input_File.txt', 'utf8');
var rl = readline.createInterface({input: readStream});
rl.on('line', function(inputLine) {
console.log(inputLine);
var tokens = inputline.split(' ');
for (var i = 0; i < tokens.length; i++) {
const token = tokens[i];
var tokenCategory = 'operand';
if (token === '+' || token === '-' || token === '/' || token === '*') {
tokenCategory = 'operator';
var B = ArrayStack2.pop();
var A = ArrayStack2.pop();
if (token === '+') {
var answer = A + B;
ArrayStack2.push(answer);
} elseif (token === '-') {
var answer = A - B;
ArrayStack2.push(answer);
} elseif (token === '/') {
var answer = A / B;
ArrayStack2.push(answer);
} elseif (token === '*') {
var answer = A * B;
ArrayStack2.push(answer);
}
} else {
ArrayStack2.push(token);
console.log(ArrayStack);
}
}
});
rl.on('close', function() {
console.log('File now closed.');
});
In order to store and solve the problem, an ArrayStack class is utilized.
'use strict' ;
var EmptyError = require('./EmptyError');
class ArrayStack {
constructor() {
this._data = new Array();
}
isEmpty() {
return (this._data.length === 0);
}
push(toPush) {
this._data.push(toPush);
return this;
}
pop() {
if (this.isEmpty())
throw new EmptyError("Can't pop from an empty stack!");
return this._data.pop();
}
top() {
if (this.isEmpty())
throw new EmptyError('An empty stack has no top!');
return this._data[this._data.length - 1];
}
len() {
return this._data.length;
}
}
module.exports = ArrayStack;
The EmptyError class is referenced in relation to the ArrayStack class.
'use strict';
class EmptyError extends Error {
constructor(message) {
super(message);
}
}
module.exports = EmptyError;