2012-12-02 35 views
3

请好起来我是一个总newby。给一个单词添加一个数字来调出一个变量

我有一个从num1到num90的变量列表(字符串)。我需要通过将一个int中的数字添加到单词num中来调用这些函数。

任务是将数字转换为'跳入C++'的单词...我可能不会以'正确'的方式进行操作,但这部分现在已经阻止了我一段时间!

我想是这样的:

#include <iostream> 
    #include <string> 

    using namespace std; 

    // Hard code numbers to 20 and then in tens to 90 
    string num1 = "one"; 
    string num2 = "two"; 
    string num3 = "three"; 
    string num4 = "four"; 
    string num5 = "five"; 
    string num6 = "six"; 
    string num7 = "seven"; 
    string num8 = "eight"; 
    string num9 = "nine"; 

等......多达90

int main() 
    { 

    // Break the number down into three digit blocks 
    int num = 0; 

    cout << "Please enter the number: "; 
    cin >> num; 


    while (num > 999) 
    { 
int digit = (num % 1000); 
num = ((num - digit)/1000); 
//cout << digit << '\n'; 
//cout << num << '\n'; 

// For each block of numbers work out hundreds, 


if (digit > 100) 
{ 
    int i = digit; 
    int j = (i % 100); 
    cout << num.append(j) << " hundred"; 
} 

我需要发生的是被标记到存储在“J”的数word num为了调用字符串num *。

这可能吗?

+0

你最终要像'int2words',或有什么功能?写一些输入 - >输出例子。 – Mikhail

回答

3

当然,使用地图:

#include <map> 
#include <string> 
#include <iostream> 

std::map<int, std::string> words { { 1, "one" }, { 2, "two" }, { 3, "three" } }; 

int main() 
{ 
    std::cout << words[1] << std::endl; 
} 

你可能不得不处理一些特殊情况下(多达二十个?),你需要“百”这个词,如果你想让它变得可以国际化,你就必须更加努力地思考。

+0

其实我这样做,在很久以前,并能够产生 英语,法语或德语只是通过改变设置表 的(和标志或二:德国看跌单词之间没有空格的 号码,反转几十套,相比法语或英语 )。单位表实际上有20项,几十 表可以有一个空项,前提是之前的 项不为空;如果条目为空,则使用前一条目的 (并将10添加到单位中以将其编入索引)。所述 实际转换然后通过三位数块('/ 1000' 和'%1000')proceded。 –

+0

哦,你真的不想为一个由小整数索引的表使用'std :: map',对吗? –

+0

@JamesKanze:简介并展示它是瓶颈! :-) –

1

你应该看看使用std :: vector。这给你一个变量,它需要一个指数

std::vector<std::string> lookup; 
lookup.push_back("zero"); // starts at lookup[0] 

lookup.push_back("one"); 
lookup.push_back("two"); 
// etc 

// then 

std::cout << lookup[digit] << std::endl; 
std::cout << lookup[num] << std::endl; 
+0

当你到达21时,这将会中断('硬码号码20,然后在几十到90') –

2

我需要发生的是存储在'j'中的数字被标记到 单词num以调用字符串num *。

该方法的问题在于,您在代码中使用的任何变量名称都不会被编译:程序运行时会操纵变量的值,但它不知道或在意您决定使用名称“num3”而不是“numero3”或“foobar”。

如果你想在一个数字(3)和一个字符串(“三”)之间建立一个链接,那么你可以使用一个向量(如@Mark建议,尽管20后会有问题) (如@Kerrek建议):这些将起作用,因为在两种情况下,字符串是由一个可变的引用(例如数字lookup[digit]的值,或文字值在words[1] ),而不是由名称的一个变量。


编辑:

对于利息,这是一个使用 '如果' 和 '开关' 的一个版本...

#include <iostream> 
#include <string> 
using namespace std; 

string units2word(int units){ 
    switch (units){ 
     case 0: return "zero"; 
     case 1: return "one"; 
     case 2: return "two"; 
     case 3: return "three"; 
     // etc ... 
     case 18: return "eighteen"; 
     case 19: return "nineteen"; 
    } 
} 

string tens2word(int tens){ 
    switch(tens){ 
     case 2: return "twenty"; 
     case 3: return "thirty"; 
     // etc ... 
     case 9: return "ninety"; 
    } 
} 

string num2words(int num) { 
    if (num > 99 && num%100 == 0) return units2word(num/100) + " hundred"; 
    if (num > 99) return units2word(num/100) + " hundred and " + num2words(num%100); 
    if (num < 20) return units2word(num); 
    if (num%10 == 0) return tens2word(num/10); 
    return tens2word(num/10) +"-"+ units2word(num%10); 
} 

int main(int argc, char *argv[]) { 
    int num = -1; 
    while(num < 0 || num > 999){ 
     cout << "Please enter a number between 0 and 999: "; 
     cin >> num; 
    } 
    cout << "You typed: " << num2words(num) << endl; 
} 
+0

谢谢你们...我还没有地图或矢量。当我感觉我拥有合适的工具时,我可能会继续阅读本书并尝试解决问题。或者我可以跳到前面去学习一些关于'地图'的东西,它看起来很有趣,很有用。 –

+0

如果不想地图的解决方案,你可以尝试使用'之开关语句,或者一堆'if'条款... –

+0

我一直在寻找使用的负载'if'条款,但它似乎远繁琐。 'map'方法看起来更优雅。 –

相关问题