int proc_color(char *s, char *t, int ind) { int result; int errornum; char holder[SHORTLEN]; char err[MAX_ERR_LENGTH+1]; regmatch_t pmatch[4]; regex_t regone; char *colpat = "\\([0-9XE][0-9XE]*\\),\\([0-9XE][0-9XE]*\\),\\([0-9XE][0-9XE]*\\)"; holder[0] = '\0'; lower_str(s); if (strstr(s,"red")) { allopts[ind].r = 1000; allopts[ind].g = 0; allopts[ind].b = 0; } else if (strstr(s,"blue")) { allopts[ind].r = 0; allopts[ind].g = 0; allopts[ind].b = 1000; } else if (strstr(s,"green")) { allopts[ind].r = 0; allopts[ind].g = 1000; allopts[ind].b = 0; } else if (strstr(s,"yellow")) { allopts[ind].r = 1000; allopts[ind].g = 1000; allopts[ind].b = 0; } else if (strstr(s,"magenta")) { allopts[ind].r = 1000; allopts[ind].g = 0; allopts[ind].b = 1000; } else if (strstr(s,"cyan")) { allopts[ind].r = 0; allopts[ind].g = 1000; allopts[ind].b = 1000; } else if (strstr(s,"black")) { allopts[ind].r = 0; allopts[ind].g = 0; allopts[ind].b = 0; } else if (strstr(s,"white")) { allopts[ind].r = 1000; allopts[ind].g = 1000; allopts[ind].b = 1000; } else { upper_str(s); if ((errornum = regcomp(®one,colpat,0)) != 0) { regerror(errornum,®one,err,MAX_ERR_LENGTH); return -1; } result = regexec(®one,s,4,pmatch,0); regfree(®one); if (result != 0) { /* no match */ if (strstr(t,"FORE")) { allopts[ind].r = 1000; allopts[ind].g = 1000; allopts[ind].b = 1000; } else { allopts[ind].r = 0; allopts[ind].g = 0; allopts[ind].b = 0; } } else { /* match */ sprintf(holder,"%.*s",pmatch[1].rm_eo - pmatch[1].rm_so, s+pmatch[1].rm_so); allopts[ind].r = (int) doztodec(holder); holder[0] = '\0'; sprintf(holder,"%.*s",pmatch[2].rm_eo - pmatch[2].rm_so, s+pmatch[2].rm_so); allopts[ind].g = (int) doztodec(holder); holder[0] = '\0'; sprintf(holder,"%.*s",pmatch[3].rm_eo - pmatch[3].rm_so, s+pmatch[3].rm_so); allopts[ind].b = (int) doztodec(holder); } } return 0; }
int main() { //if the compiler supports c++ 2011 standard, std::unordered_set container is perfect for this scenario due to // the requirement that the replacements should be written in the order of their appearance in the dictionary std::vector<ITEM> dict; int sequence; //A terminating null character is automatically added at the end of the stored sequence. char word[16]; while(1) { scanf("%s", word); sequence++; if(word[0] != '#') { ITEM tmp_item; tmp_item.m_word = word; tmp_item.m_sequence_num = sequence; dict.push_back(tmp_item); } else break; } std::sort(dict.begin(), dict.end()); while(1) { scanf("%s", word); if(word[0] != '#') { printf("%s", word); bool is_correct = false; ITEM equal_item; equal_item.m_word = word; std::pair<std::vector<ITEM>::iterator,std::vector<ITEM>::iterator> bounds = std::equal_range(dict.begin(), dict.end(), equal_item); for(std::vector<ITEM>::iterator equal = bounds.first; equal != bounds.second; ++equal) { if(equal->m_word == word ) { printf(" is correct"); is_correct = true; break; } } if(!is_correct) { printf(":"); std::string lower_str(strlen(word) -1, 'x'); std::string upper_str(strlen(word) +1, 'x'); ITEM lower_item, upper_item; lower_item.m_word = lower_str; upper_item.m_word = upper_str; std::vector<ITEM>::iterator low, up; low = std::lower_bound(dict.begin(), dict.end(), lower_item); up = std::upper_bound(dict.begin(), dict.end(), upper_item); std::vector<ITEM> similar_words; for(low; low != up; ++low) { if(!similarity(low->m_word.c_str(), word) ) similar_words.push_back(*low); } std::sort(similar_words.begin(), similar_words.end(), comp); for(int i = 0; i < similar_words.size(); ++i) printf(" %s", similar_words[i].m_word.c_str()); } printf("\n"); } else break; } return 0; }