字符串附加罗马数字
string append for roman numerals
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
!它很可能会导致名称碰撞并分开任何东西。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 在将数字随机生成为数组期间从内存输出随机数的数组