2016-12-30 81 views
-3

即時閱讀問題的C++解決和不明白以下幾點:字符串附加羅馬數字

  • 在27行似乎大於和小於標誌是倒退,但是當我改變他們什麼,我想他們應該是程序確實運行。請你能讓我知道他們爲什麼是這樣嗎?

  • 從第47行開始的字符串函數中,它如何知道要選擇哪個字符串?考慮我是否輸入了1930年,對於900這個程序知道什麼時候知道要超過9個逗號來選擇'CM'的下一個字符串,並且同樣對於30它知道它需要超過3個逗號並選擇'XXX'。 ..但我不確定?能不能請您讓我知道它是如何知道使用一年1930

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


//function prototype 
string toRomanLiterals(int); 

int main() 
//program displays Arabic Years as Roman Years 
{ 
//declare integer as number 
int integernumber; 
//declare a character 
char choice; 

//do while loop to continue by y or no 
do 
{ 
    //do while loop to read integer from user 
    //read integer number between 1 and 3000 only 
    do 
    { 
     cout << "Enter a number between 1000 and 3000: "; 
     cin >> integernumber; 
    } 
    while (integernumber < 1000 || integernumber > 3000); 

    if (integernumber > 1000 || integernumber < 3000) 
    { 
     //calling toRomanLiterals function 
     string output= toRomanLiterals(integernumber); 
     cout << output<<endl; 
    } 
    //continue to ask until user wishes to stop 
    cout << "Would you like to continue? [y/n]" << endl; 
    cin >> choice; 

} 
while (choice != 'n'); 
return 0; 
} 

//string toRomanLitrals takes the integer number and converts to a given 
//number to a Roman number if it is between 1000 and 3000 

string toRomanLiterals(int number) 
{ 
string roman; 
int th,h,t,o; 

//string array for numbers 1 to 9 
string ones[] = 
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; 
//string array for numbers 10 to 90 
string tens[] = 
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; 
//string array for numbers 100 to 900 
string hundreds[] = 
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; 
//string array for numbers 1000 to 3000 
string thousands[] = {"", "M", "MM", "MMM"}; 

if (number <= 3000) 
{ 
    //get number of thousands 
    th = number/1000; 
    number = number % 1000; 

    //get number of hundreds 
    h = number/100; 
    number = number % 100; 

    //get number of tens 
    t = number/10; 
    o = number % 10; 

    //concatenate all string values of thousands, hundreds, tens, zeros 
    //concatenate all symbols 

    roman += thousands[th].append(hundreds[h]) 
      .append(tens[t]).append(ones[o]); 

} 
return roman; 
} 
+1

「backwards」是什麼意思?你能正確使用每句話的第一個字母嗎?撇號在哪裏? –

回答

-1

我的例子來選擇「CM」 &「XXX」在27行好像大於和小於比標誌倒退,但當我改變他們到我認爲他們應該是程序運行。請你能讓我知道他們爲什麼是這樣嗎?

因此該計劃將在再次輸入請求integernumber,如果輸入1000和3000

在字符串函數

之間是不是在47行開始,它是如何知道哪些字符串選擇?考慮我是否輸入了1930年,對於900這個程序知道什麼時候知道要超過9個逗號來選擇'CM'的下一個字符串,並且同樣對於30它知道它需要超過3個逗號並選擇'XXX'。 ..但我不確定?能不能請您讓我知道它是如何知道使用一年1930

我的例子對於你的榜樣號,1930年選擇了「CM」 &「XXX」,

th = number/1000; // th gets 1 
number = number % 1000; // number is now 930 

h = number/100; // h gets 9 
number = number % 100; // number is now 30 

t = number/10; // t gets 3 
o = number % 10; // o gets 0 

roman += thousands[th].append(hundreds[h]) 
     .append(tens[t]).append(ones[o]); 
// gets the numerals "M", "CM", "XXX" and "", and appends them together, getting "MCMXXX" 

綜上所述,函數從數字中逐個提取數字,然後爲它們獲取羅馬數字,並追加它們,並得到答案。

另外,請不要使用可怕的using namespace std它很可能會導致名稱衝突並破壞任何東西。

+0

感謝回顧爲我自己的利益,在字符串toRomanLiterals函數中是'th','h','t','o'它指示數組中的數據點使用? – michaelBard

+0

是的。 'th'得到1,因此'千[']''''M'',等等。 –