最终目标:将整数转化为书面的长手牌。
我已经看到一些有关此问题的讨论。我遇到过高兴地将字符串分解并打印出字符的函数,但是将它们捕获到数组中似乎是不可能的。
似乎单个字符可以通过下标访问,但无法对其进行操作。
如果我删除注释,则以下内容将打印出1,2,9,。,5,0,但是当我运行if循环时,我得到了写入的数字字符串,但顺序错误。
std::string *firstName= new std::string[numOfLines];
std::string *lastName= new std::string[numOfLines];
for (int i = 0; i < numOfLines; ++i)
{
line >> firstName[i];
}
我还试图通过一个函数访问索引功能,尽管每次每次崩溃时它都会完整打印出来。
let sentence = "129.50"
for (character) in sentence {
// print(character)
if character == "0" {
print("zero")
}
if character == "1" {
print("one")
}
if character == "2" {
print("two")
}
etc etc
任何帮助表示赞赏。
答案 0 :(得分:1)
这是您学习TDD的绝佳时机。测试驱动开发。从一个简单的案例开始,您可以想到的最简单的案例...
assert(writtenOut("1") == "one")
完成以上工作,然后添加另一个测试:
assert(writtenOut("1") == "one")
assert(writtenOut("2") == "two")
对所有数字和小数点执行上述操作。您还应该处理错误情况:
assert(writtenOut("d") == "")
然后尝试一些更复杂的事情:
assert(writtenOut("12") == "one two") // or do you want "twelve" in this case?
您可以自己做,从小处开始,然后逐步提高。到您完成时,您将具有一个正常工作的功能以及证明有效的大量测试。
答案 1 :(得分:1)
尝试
let str = "129.50"
let array = Array(str)
print(array)
打印[“ 1”,“ 2”,“ 9”,“。”,“ 5”,“ 0”]
答案 2 :(得分:0)
感谢大家的反馈,我最终得到了一点麻烦,但确实可行:
func radio(_ MHz:Double){
let sentence = String(MHz)
for (character) in sentence {
if character == "0" {
print("zero", terminator:" ")
}
if character == "1" {
print("one", terminator:" ")
}
if character == "2" {
print("two", terminator:" ")
}
if character == "3" {
print("tree", terminator:" ")
}
if character == "4" {
print("fower", terminator:" ")
}
if character == "5" {
print("fife", terminator:" ")
}
if character == "6" {
print("six", terminator:" ")
}
if character == "7" {
print("seven", terminator:" ")
}
if character == "8" {
print("eight", terminator:" ")
}
if character == "9" {
print("niner", terminator:" ")
}
if character == "." {
print("decimal", terminator:" ")
}
}
print()
print()
}
因此广播(118.65)产生
一个八分之一的小内罗夫