本文介绍了一种更有效的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 是否有更有效的方法将字符串与不同的单词进行比较? 我这样做: if(strcmp( farge,kvit)== 0) peikar_til_glas_struktur-> farge = KVIT; if(strcmp(farge," raud")== 0) peikar_til_glas_struktur-> farge = RAUD; if(strcmp(farge," bl?")== 0) peikar_til_glas_struktur - > farge = BLAA; if(strcmp(farge," gul")== 0) peikar_til_glas_struktur-> farge = GUL; if(strcmp(farge," gr?n")== 0) peikar_til_glas_struktur-> farge = GROEN; if(strcmp(farge) ,rosa)== 0) peikar_til_glas_struktur-> farge = ROSA; if(strcmp(farge," svart")== 0) peikar_til_glas_struktur-> farge = SVART; if(strcmp(farge," med m?nster")== 0) peikar_til_glas_struktur - > farge = MED_MOENSTER; if(st rcmp(farge,indigo)== 0) peikar_til_glas_struktur-> farge = INDIGO; if(strcmp(farge," lilla")== 0) peikar_til_glas_struktur-> farge = LILLA; 如果你不理解变量名,别担心。这是无关紧要的。无关紧要。无论如何你得到了这一点。 我想知道是否可以使用switch语句, 但是我必须将每种颜色与字符串进行比较,并且在切换声明中,我不能这样做吗?我可以吗? 感谢您的帮助。 我希望这不是偏离主题的。 解决方案 不,虽然你可以'首先阅读常见问题解答。 Ed。 你能做的就是这个。 struct word { char * str; int id; }; / * table必须是按字母顺序* / struct word table [100] = {" aa",AA," ab",AB ..." zyggle",ZYGGLE}; 然后你对你想要的单词执行二进制搜索,并提取id。 这将减少你必须在相当程度上进行比较的数量。 如果你在字典中添加和减去单词那么东西 会变得有点复杂 - 时间去讨论编程。 < snip> 编号请参阅常见问题解答: http://www.eskimo.com/~scs/C-faq/q20.17.html 嗯,虽然我刚才说的是真的,你可以这样做: switch(farge [0]){ case''k'': if(strcmp(farge," kvit")== 0) peikar_til_glas_struktur-> farge = KVIT ; 休息; case''r'': if(strcmp(farge," raud")== 0) peikar_til_glas_struktur-> farge = RAUD; 休息; 案例''b'': if(strcmp(farge," bl?")== 0) peikar_til_glas_struktur-> farge = BLAA; break; case''g'': if(strcmp(farge," gul")== 0) peikar_til_glas_struktur-> farge = GUL; else if(strcmp(farge," gr?n")== 0) peikar_til_glas_struktur-> farge = GROEN; break; case''r'': if(strcmp(farge," rosa")== 0) peikar_til_glas_struktur-> farge = ROSA; 休息; case''s'': if(strcmp(farge," svart")== 0 ) peikar_til_glas_struktur-> farge = SVART; 休息; case''m'': if(strcmp(farge,med m?nster)== 0) peikar_til_glas_struktur-> farge = MED_MOENSTER; break; case''我'': if(strcmp(farge," indigo))== 0) peikar_til_glas_struktur-> farge = INDIGO; 休息; case''l'': if(strcmp(farge," lilla")== 0) peikar_til_glas_struktur-> farge = LILLA; 休息; 默认值: / *无论* / 休息; } 它可能会节省一些周期 - 试试看看。 Ed。 Is there a more efficient way of comparing a string to different words?I''m doing it this way: if(strcmp(farge, "kvit") == 0)peikar_til_glas_struktur->farge = KVIT;if(strcmp(farge, "raud") == 0)peikar_til_glas_struktur->farge = RAUD;if(strcmp(farge, "bl?") == 0)peikar_til_glas_struktur->farge = BLAA;if(strcmp(farge, "gul") == 0)peikar_til_glas_struktur->farge = GUL;if(strcmp(farge, "gr?n") == 0)peikar_til_glas_struktur->farge = GROEN;if(strcmp(farge, "rosa") == 0)peikar_til_glas_struktur->farge = ROSA;if(strcmp(farge, "svart") == 0)peikar_til_glas_struktur->farge = SVART;if(strcmp(farge, "med m?nster") == 0)peikar_til_glas_struktur->farge = MED_MOENSTER;if(strcmp(farge, "indigo") == 0)peikar_til_glas_struktur->farge = INDIGO;if(strcmp(farge, "lilla") == 0)peikar_til_glas_struktur->farge = LILLA; Don''t worry if you don''t understand the variable names. It''sirrelevant. You get the point anyway. I was wondering if maybe I could use the switch statement,but I have to compare each colour to the string, and I can''t dothat in a switch statement, can I? Thanks for any help.I hope this isn''t off-topic. 解决方案 No, though you could''ve read the FAQ first. Ed. What you can do is this. struct word{char *str;int id;}; /* table must be alphabetical */struct word table[100] = { "aa", AA, "ab", AB ... "zyggle", ZYGGLE }; Then you perform a binary search for the word you want, and extract the id.This will cut down the number of comparisons you have to make to aconsiderable degree. If you are adding and subtracting words from the dictionary then thingsbecome a bit more complicated - time to go over to comp.programming. <snip> No. See the FAQ: http://www.eskimo.com/~scs/C-faq/q20.17.html Hmm, although what I just said is true, you COULD do this: switch(farge[0]) {case ''k'':if(strcmp(farge, "kvit") == 0)peikar_til_glas_struktur->farge = KVIT;break;case ''r'':if(strcmp(farge, "raud") == 0)peikar_til_glas_struktur->farge = RAUD;break;case ''b'':if(strcmp(farge, "bl?") == 0)peikar_til_glas_struktur->farge = BLAA;break;case ''g'':if(strcmp(farge, "gul") == 0)peikar_til_glas_struktur->farge = GUL;else if(strcmp(farge, "gr?n") == 0)peikar_til_glas_struktur->farge = GROEN;break;case ''r'':if(strcmp(farge, "rosa") == 0)peikar_til_glas_struktur->farge = ROSA;break;case ''s'':if(strcmp(farge, "svart") == 0)peikar_til_glas_struktur->farge = SVART;break;case ''m'':if(strcmp(farge, "med m?nster") == 0)peikar_til_glas_struktur->farge = MED_MOENSTER;break;case ''i'':if(strcmp(farge, "indigo") == 0)peikar_til_glas_struktur->farge = INDIGO;break;case ''l'':if(strcmp(farge, "lilla") == 0)peikar_til_glas_struktur->farge = LILLA;break;default:/* whatever */break;} It might save you some cycles - try it and see. Ed. 这篇关于一种更有效的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-16 04:13