字符串附加罗马数字

string append for roman numerals

本文关键字:数字 罗马 字符串      更新时间:2023-10-16

im阅读问题解决问题,不了解以下内容:

  • 在第27行中,似乎比迹象大于倒退,但是当我将它们更改为我认为它们应该是程序确实运行的东西时。请让我知道为什么<>符号是?

  • 在第47行开始的字符串函数中,它如何知道要选择哪个字符串?考虑到我在1930年输入,对于900年,该程序以某种方式知道如何超过9个逗号来选择下一个字符串,即" CM",并且同样30,它也知道它需要超过3个逗号并选择" xxx"。..但是我不确定吗?请让我知道它如何知道选择" CM"&" 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;
}

在第27行中,似乎比迹象大,而小于符号的质量,但是当我将它们更改为我认为它们应该是程序确实运行的东西时。请让我知道为什么&lt;>符号是?

因此,如果输入不在1000到3000之间,则该程序将再次要求integernumber的输入。

在第47行开始的字符串函数中,它如何知道要选择哪个字符串?考虑到我在1930年输入,对于900年,该程序以某种方式知道如何超过9个逗号来选择下一个字符串,即" CM",并且同样30,它也知道它需要超过3个逗号并选择" xxx"。..但是我不确定吗?请让我知道它如何知道选择" CM"&amp;'xxx'使用我的1930年的例子。

对于您的示例号,1930年,

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它很可能会导致名称碰撞并分开任何东西。