传递整数显示字符串 [英] Pass integer display string

查看:80
本文介绍了传递整数显示字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想在函数中传递整数并显示与之相关的字符串。

我的方法是

  string  myNoString( int  no)



所以如果我通过2,它应该显示2,如果我通过14它应该显示onefour



谢谢

解决方案

查看这篇好文章将数字转换为这个词相当于。 [ ^ ]这将帮助你开始。


在一张纸上做练习:

1)写下你将如何做手动

2)以某种控制流形式编写关于手动执行任务的指令,例如

一般想法:从右到左取数字并将它们的单词存储在一个字符串中。 
1)如果数字为零,则结果为零
2)否则
2.1)将结果字符串初始化为空字符串
2.2)取得该部门的提醒数字乘以10
2.3)将该提醒转换为单词等效,并在结果字符串前加上新单词
2.4)将该数字的除数除以10作为新数字值
2.5 )重复2.1)... 2.5)直到新的数字值为零
2.6)结果是结果字符串



......或类似的。 br />
在纸上播放以检查上面的描述(称为伪代码)是否符合要求。

现在你已经足够了解这个问题了。



下一步是将其翻译成C#。

  string  ToWordEquivalent( int  number)
{
if (number == < span class =code-digit> 0 ) return 0;
// gimmik,上面没有描述; - )
if (number < 0 return minus + ToWordEquivalent(-number);
// 可能的技术之一,称为查找表
string [] words = new string [] {< span class =code-string> zero one,...};
string result = string .Empty;
while (number > 0
{
result = ... [number% 10 ] + +结果;
number = ... / ...;
}
return result.Trim();
}





填写缺少的部分,在某些测试用例中调用该函数。



干杯

Andi



PS:这几乎太过帮助...... ;-)


Hello,
I want to pass integer in a function and display string related to it.
my method is

string myNoString(int no)


so if i pass 2, it should display "two" and if i pass 14 it should display "onefour"

Thanks

解决方案

Check this nice article Converting numbers to the word equivalent. [^] this will help you to give a start.


Do the exercise on a sheet of paper:
1) write down how you would do it "manually"
2) write your instructions for "manual" execution of the task in some controll flow form, e.g.

general idea: take the digits from the right to left and store their word equivalent in a string.
1) if the number is zero, the result is "zero"
2) otherwise
   2.1) initialize the result string to empty string
   2.2) take the reminder of the division of the number by 10
   2.3) convert that reminder to the word equivalent and prefix the result string by that new word
   2.4) take the division by 10 of the number as new number value
   2.5) repeat 2.1)...2.5) until the new number value is zero
   2.6) the result is the result string


... or alike.
Play it through on paper to check if the description above (called "pseudo-code") behaves as required.
Now you understand the problem well enough.

Next step is to translate that into C#.

string ToWordEquivalent(int number)
{
   if (number == 0) return "0";
   // gimmik, not described above ;-)
   if (number < 0) return "minus " + ToWordEquivalent(-number);
   // one of the possible technique, called lookup table
   string[] words = new string[] { "zero", "one", ... };
   string result = string.Empty;
   while(number > 0)
   {
       result = ...[number % 10] + " " + result;
       number = ... / ...;
   }
   return result.Trim();
}



Fill in the missing parts, call the function in some test cases.

Cheers
Andi

PS: That''s almost too much helped... ;-)


这篇关于传递整数显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆