2012-12-02 31 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'方法看起來更優雅。 –

相關問題