本文介绍了请将我的整数注释为字符串代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我写了以下内容在其字符串中打印一个整数 表示基数为-36到36. 请评论此代码。 #include< iostream> #include< string> 使用std :: abs; 使用std :: cout; 使用std :: endl; 使用std :: reverse; 使用std :: string; char charTable [] =" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;; void printBase(int i ,int base) { if((abs(base)< = 1)||(abs(base)> 36)||(i == 0)) { cout<< " 0" <<结束; 返回; } string str ="" ;; if ((base> 0)&&(i< 0)) { str + =" - " ;; i = -i; } do { if(i%base> = 0) { str + = charTable [i%base]; i = i / base; } else { str + = charTable [i%base - base]; i = i / base + 1; } } while(i); reverse(str.begin(),str.end()); cout<< str<<结束; } int main() { for(int i = - 55; i< = 55; i ++) { cout<< i<< " =" ;; printBase(i,-10); } } 谢谢和问候, manoj。Hi All,I wrote the following to print an integer in its stringrepresentation for base -36 to 36.Please comment on this code.#include <iostream>#include <string>using std::abs;using std::cout;using std::endl;using std::reverse;using std::string;char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";void printBase(int i, int base){if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0)){cout << "0" << endl;return;}string str = "";if ((base > 0) && (i < 0)){str += "-";i = -i;}do {if (i % base >= 0){str += charTable[i % base];i = i / base;} else {str += charTable[i % base - base];i = i /base + 1;}} while(i);reverse(str.begin(), str.end());cout << str << endl;}int main(){for(int i = -55; i <= 55; i++){cout << i << " = ";printBase(i,-10);}}Thanks and Regards,manoj.推荐答案 如何显示数字a *负*基数?????How do you display numbers in a *negative* base????? 如何在*负*基数中显示数字????? How do you display numbers in a *negative* base????? 考虑十字形。 27将是187 自187 = 1 * -10 * -10 + 8 * -10 + 7 = 27。 br />Consider negadecimal.27 will be 187since 187 = 1 * -10 * -10 + 8 * -10 + 7 = 27. 单元测试是最重要的方面节目。你的printBase 应该返回一个字符串,所以你可以测试它,所以它不会结合。 其环境。在这里你有一些户外printBase,还有一些 里面。将所有内容移到外面,然后写下这样的单元测试: 断言(不管== printBase(-55); 断言(无论什么 ; == printBase(-55); 断言(" whatever" == printBase(-55); 断言(" whatever" == printBase( - 55); 现在每次更改程序时,运行那些测试。如果你确定 种错误(但没有任何错误)测试会抓住你,而且你可以使用Undo去除错误。撤消比 调试更有效。 - Phlip http:// www .greencheese.org / ZeekLand < - 不是博客!!!Unit tests are the most important aspect of programming. Your printBaseshould return a string, so you can test it, and so it won''t "couple" withits environment. Here you have some cout outside printBase, and someinside. Move all outside, then write unit tests like these:assert("whatever" == printBase(-55);assert("whatever" == printBase(-55);assert("whatever" == printBase(-55);assert("whatever" == printBase(-55);Now each time you change the program, run those tests. If you make certainkinds of mistakes (but not any mistake) the test will catch you, and youcan use Undo to get rid of the mistake. Undo is much more efficient thandebugging.--Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!! 这篇关于请将我的整数注释为字符串代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-23 20:36