I have been reading SO for some time now, but I truly cannot find any help for my problem.
I have a c++ assignment to create an IAS Simulator.
Here is some sample code…
0 1 a
1 2 b
2 c
3 1
10 begin
11 . load a, subtract b and offset by -1 for jump+
11 load M(0)
12 sub M(1)
13 sub M(3)
14 halt
Using c++, I need to be able to read these lines and store them in a “memory register” class that I already have constructed…
For example, the first line would need to store “1 a” in register zero.
How can I parse out the number at the line beginning and then store the rest as a string?
I have setup storage using a class that is called using mem.set(int, string);. int is the memory location at the beginning of the line and string is the stored instruction.
Edit: Some Clarifications:
- I must use standard libraries
- the grammar for the input file is here: http://www.cs.uwyo.edu/~seker/courses/2150/iascode.pdf
- The loader will overwrite duplicate line entries. That means the first line 11 in the sample will be overwritten by the second.
Splitting the leading number and the rest of the line is not too difficult of a task. Use something like
getlineto read one line at a time from your input file, and store the line in a stringchar cur_line[]. For each line, try something like this:char* pStringand an integerint line_numstrstrfunction to find the first whitespace character, and assign the result topString.pStringforward one character at a time until it points to a non-whitespace character. This is the start of the string containing the “rest of the line”.atoioncur_lineto convert the first entry in the string to an integer, and store the results inline_nummem.set(line_num, pString)Interpreting those strings is going to be much more difficult, however…
Edit: As Mike DeSimone mentions, you can combine the
strstrandatoisteps above if you use one of thestrto*functions instead ofatoi.