c++ - binary search string 2d arrays -
my question find string in 2d array , if match display binary no.
pience of cide
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 = 0; < icount-1; i++) { //display arrays for(int j = 0; j < 4;j++) { cout << input[i][j] << " "; // check first column consist inst , convert 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]<<" "; } }
it's output not display correct binary code. kind of i'm appreciated. you. * don't want use return mid , create function
your search soooo simpler if reorganized data:
struct data_record { string command; string value; string other; // here's makes 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"; }
you can other cool stuff overloading operator<<
, operator>>
custom output , input.
edit 1: oop hierarchy
many instruction sets have groups share commonality. example, jump or branch instructions have destination address. math instructions may have same operands.
i recommend having hierarchy of classes (or structures). research "c++ factory design pattern".
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; };
you use factory pattern return pointer instruction
base class.
the user's program simple as:
std::vector<instruction *> program;
Comments
Post a Comment