二进制搜索字符串2d数组

时间:2014-04-14 16:55:35

标签: c++ binary-search-tree

我的问题是在2d数组中找到字符串,如果它与显示二进制数字匹配

我的怜悯

string inst[37][3]={{"ld","00001","C2"},{"st","00011","C2"},{"la","00101","C2"},{"ldr","00010","C1"},
                       {"lar","00110","C1"},{"str","00100","C1"},{"add","01100"," "},{"addi","01101","C2"},
                       {"sub","01110"," "},{"neg","01111"," "},{"or","10110"," "},{"ori","10111","C2"},
                       {"and","10100"," "},{"andi","10101","C2"},{"not","11000"," "},{"shr","11010","C3"},
                       {"shra","11011","C3"},{"shl","11100","C3"},{"shc","11101","C3"},{"br","01000","C3"},
                       {"brl","01001","C3"},{"brlnv","01001"," "},{"brzr","01000"," "},{"brlzr","01001"," "},
                       {"not","11000"," "},{"brnz","01000"," "},{"brlnz","01001"," "},{"brpl","01000"," "},
                       {"brmi","01000"," "},{"brlmi","01001"," "},{"nop","00000"," "},{"stop","11111"," "},
                       {"een","01010"," "},{"edi","01011"," "},{"rfi","11110"," "},{"svi","10000"," "},
                       {"ri","10001"," "}};

int last=36, initial=0 , mid, index;

for(int i = 0; i < icount-1; i++)
        {

            //display arrays
            for(int j = 0; j < 4;j++)
            {

                cout << input[i][j] << "     ";
                // this is for check first column that consist inst and then convert to binary code
                if(j==0)
                {
                        while(last>=initial)
                        {
                           mid=(last+initial)/2;

                           if(input[i][0]==inst[mid][0])
                           {   index=mid;

                           }
                           else if(input[i][0]>inst[mid][0])
                           {   initial=mid+1;
                           }
                           else
                               last=mid-1;
                        }

                        cout<<"   "<<inst[index][1]<<"    ";

                   }
}

它类似输出不显示正确的二进制代码。我真的很感激任何帮助。 谢谢。 *我不想使用return mid并创建另一个函数

1 个答案:

答案 0 :(得分:0)

如果您重新组织了数据,那么您的搜索会变得更加简单:

struct Data_Record
{
  string  command;
  string  value;
  string  other;

// Here's what makes the search work better:
  bool operator==(const Data_Record& dr)
  {
    bool is_equal = false;
    if (command == dr.command)
    {
      if (value == dr.value)
      {
         if (other == dr.other)
         {
           is_equal = true;
         }
      }
    }
  }
  bool operator<(const Data_Record& dr)
  {
    return command < dr.command;
  }
};

const Data_Record   inst[37] = { /* ... */};
Data_Record const * p_item_found = NULL;
Data_Record         key = {"and", "", ""};

p_item_found = std::binary_search(&inst[0], &inst[38], key);
if (p_item != &instr[38])
{
  cout << "Found it\n";
}
else
{
  cout << "Item not found.\n";
}

您可以执行其他很酷的操作,例如重载operator<<operator>>以获取自定义输出和输入。

编辑1:OOP层次结构
许多指令集具有共享性的组。例如,跳转或分支指令都具有目标地址。数学指令可能具有相同的操作数。

我建议使用类(或结构)的层次结构。研究&#34; C ++工厂设计模式&#34;。

class Instruction
{
  public:  
    virtual void Print_Annotated(std::ostream& output) = 0;
    virtual void Instruction_Counter Execute(void) = 0;
  protected: 
    std::string opcode;
    std::string instruction_value;
};

class Jump_Instr_Category : public Instruction
{
  protected:  
    unsigned int destination_address;
};

class Math_Instr_Category : public Instruction
{
  protected:
    std::string parameter1;
    std::string parameter2;
};

使用工厂模式返回指向Instruction基类的指针。

用户的程序很简单:

std::vector<Instruction *> program;
相关问题