I developed a program that is an rpn calculator. For those who dont know what an RPN calculator is,it works similar to assembly programming language. An input file would look like this:
PUSH 1
PUSH 2
POP abc
PUSH 2323
ADD
MUL
POP xyz
JUMP 2
...
The program is working fine,i just have to improve efficiency. I´m using fscanf to read each line and then i have a function that computes each command (ADD, MUL,...). The problem is with the JUMP function:
In this example,the program would jump to line 2(PUSH 2). I have a program counter that stores the current line number. But, when i have to jump, 2 things can happen:
1. The line is before the current line: i use rewind(file) then program counter =0, read the commands again until i find the one i want;
2. The line is after the current line: in this case, i just keep reading lines until i find the one i want.
This works,but its not very fast,specially in very large inputs. So,i was thinking in storing the commands in memory while i was reading then and then read the memory instead of the file. The only thing i dont know is what data struture should i use to store things in memory. My teachers talked about an hash table,ok, that would be good, i just dont have any idea of how to hash the lines. They also told me that inserting a line in each position of the array will eventually result in a segmentation fault(the inputs will have millions of commands).
So, i hope someone could give some ideas!