2014-01-11 60 views
0

恢复信息,我有下一个文本文件的工作:我如何从一个文本文件

ABA  INH  1806  2 0 2 
ADCA IMM  89ii  1 1 2 
     DIR  99dd  1 1 2 
     EXT  B9hhll  1 2 3 
     IDX  A9xb  1 1 2 
     IDX1 A9xbff  1 2 3 
     IDX2 A9xbeeff 1 3 4 
     [D,IDX] A9xb  1 1 2 
     [IDX2] A9xbeeff 1 3 4 
ADCB IMM  C9ii  1 1 2 
     DIR  D9dd  1 1 2 
     EXT  F9hhll  1 2 3 
     IDX  E9xb  1 1 2 
     IDX1 E9xbff  1 2 3 
     IDX2 E9xbeeff 1 3 4 
     [D,IDX] E9xb  1 1 2 
     [IDX2] E9xbeeff 1 3 4 
ADDA IMM  8Bii  1 1 2 
     DIR  9Bdd  1 1 2 
     EXT  BBhhll  1 2 3 
     IDX  ABxb  1 1 2 
     IDX1 ABxbff  1 2 3 
     IDX2 ABxbeeff 1 3 4 
     [D,IDX] ABxb  1 1 2 
     [IDX2] ABxbeeff 1 3 4 
ADDB IMM  CBii  1 1 2 
     DIR  DBdd  1 1 2 
     EXT  FBhhll  1 2 3 
     IDX  EBxb  1 1 2 
     IDX1 EBxbff  1 2 3 
     IDX2 EBxbeeff 1 3 4 
     [D,IDX] EBxb  1 1 2 
     [IDX2] EBxbeeff 1 3 4 

我必须做的第一件事就是设计来搜索一个codop(ABA,ADCA的算法, ADCB,...)为此我创建了一个链接列表,其中只包含codop。

一旦你找到了codop你必须打印所有剩下的信息,例如,如果我有它具有打印codop ABA链表上:

INH 1806 2 0 2

,或者如果我有codop ADDA它打印:

ADDA IMM  8Bii  1 1 2 
     DIR  9Bdd  1 1 2 
     EXT BBhhll  1 2 3 
     IDX  ABxb  1 1 2 
     IDX1 ABxbff  1 2 3 
     IDX2 ABxbeeff 1 3 4 
     [D,IDX] ABxb  1 1 2 
     [IDX2] ABxbeeff 1 3 4 

我如何设计一个恢复所有的informationof的codop搜索之后的算法,(该codop是否有信息或更只有一条线)?即使该信息不包含链接的列表中,我创建了一个链接列表,因为它是,我认为这将是一个研究算法

+0

如果你不断读取文件的位置(例如'ftell'或'fgetpos'),它可以很容易地从文件中恢复。 – BLUEPIXY

回答

0

下面是一个伪代码更appropiate方式:1

阶段,建立输入的字典:

codops = {} # dictionary data type, where each entry contains an array 
cur = [] # array of currently processed codop 
for each line of input: 
    codop = first word in positions 1-8 # adjust accordingly 
    # for a new codop, add a new entry to the dictionary 
    if codop != _blank_: 
      cur = [] # allocate new array for this 
      add cur to codops dictionary with key = codop 
    append current line to cur (without positions 1-8) 

第2阶段:搜索和输出

input = codop to search 
using the codops dictionary, lookup the key=input in the codops dictionary 
if the key is in codops: 
     print all the lines contained in the array returned 
else: 
     print "not found" message 

难道这就是你要找的人?