即時閱讀問題的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;
}
「backwards」是什麼意思?你能正確使用每句話的第一個字母嗎?撇號在哪裏? –