字符串附加罗马数字

时间:2016-12-30 06:10:49

标签: c++

我正在阅读使用C ++解决问题并且不理解以下内容:

    第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;
}

1 个答案:

答案 0 :(得分:-1)

  在第27行中的

看起来似乎大于和小于标志是倒退但是当我将它们更改为我认为它们应该是程序确实运行时。请你告诉我为什么&lt;&gt;它们是什么迹象?

因此,如果输入不在1000和3000之间,程序将再次请求integernumber的输入。

  

在从第47行开始的字符串函数中,它如何知道要选择哪个字符串?我想如果我输入1930年,那么900计划以某种方式知道如何通过9个逗号来选择下一个字符串“CM&#39;”同样为30它知道它需要超过3个逗号和选择了&#39; XXX&#39; ...但我不确定?请你知道如何选择&#39; CM&#39; &安培; &#39; XXX&#39;用我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,这很可能会导致名称冲突并破坏任何内容。