用C++将字符串转换为罗马数字
Converting a string to roman numerals in C++
可能重复:
如何将整数值转换为罗马数字字符串?
第一次在这里提问。我有一个项目就在眼前,我一直在努力,似乎在这里或其他地方都找不到它要求的东西。目标是接受一个没有上限限制的整数,并将其转换为罗马数字。由于整数实际上有一个上界,所以在解析它之前,我必须将其转换为字符串,使用撇号表示放置在子字符串中的每组三个字符。我在概念化循环时遇到了困难,a)根据位置为他们看到的东西指定罗马数字b)通过显示撇号来计算每组三个。
到目前为止,我有:
for(int i=0;i<(int)input.length()/3;i++){
temp=input.substr(i,3);
for(int j = 0; j < (int)temp.length(); j++){
if(j == 0){
if(temp[j] == '9') cout<<"CM";
else if(temp[j] >= '8') cout<<"DCCC";
else if(temp[j] >= '7') cout<<"DCC";
else if(temp[j] >= '6') cout<<"DC";
else if(temp[j] >= '5') cout<<"D";
else if(temp[j] == '4') cout<<"CD";
else if(temp[j] == '3') cout<<"CCC";
else if(temp[j] == '2') cout<<"CC";
else if(temp[j] == '1') cout<<"C";
}
else if(j == 1){
if(temp[j] == '9') cout<<"XC";
else if(temp[j] >= '8') cout<<"LXXX";
else if(temp[j] >= '7') cout<<"LXX";
else if(temp[j] >= '6') cout<<"LX";
else if(temp[j] >= '5') cout<<"L";
else if(temp[j] == '4') cout<<"XL";
else if(temp[j] == '3') cout<<"XXX";
else if(temp[j] == '2') cout<<"XX";
else if(temp[j] == '1') cout<<"X";
}
else if(j ==2){
if(temp[j] == '9') cout<<"IX";
else if(temp[j] == '8') cout<<"VIII";
else if(temp[j] == '7') cout<<"VII";
else if(temp[j] == '6') cout<<"VI";
else if(temp[j] >= '5') cout<<"V";
else if(temp[j] == '4') cout<<"IV";
else if(temp[j] == '3') cout<<"III";
else if(temp[j] == '2') cout<<"II";
else if(temp[j] == '1') cout<<"I";
}
}
}
数字本身显示得很好,但我很难弄清楚如何告诉循环从右边开始,并按三个向左的方式工作,保持数字在输入中的实际位置(例如1234应该显示1为I,而不是C。我还需要弄清楚用撇号写循环。
我能想到的转换为罗马数字的最简单方法是从尽可能大的数字/组合开始检查并向下计算。包括combo,并从最大到最小进行检查,这样说"XC"总是在"L"之前进行检查,你就不必担心"LXXXX"answers"LXL"之类的了。
// This code requires C++11 support. Namely, initializer lists and type inference.
// If your compiler sucks, there's equivalents for the important stuff. What really
// really matters is the order of the value=>digits mappings, and the iteration over
// them in the for loop.
#include <vector>
#include <string>
#include <utility>
std::string romanNumeralFor(int n, int markCount = 0) {
typedef std::pair<int, std::string> valueMapping;
static std::vector<valueMapping> importantNumbers = {
{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
{100, "C"}, { 90, "XC"}, { 50, "L"}, { 40, "XL"},
{10, "X"}, { 9, "IX"}, { 5, "V"}, { 4, "IV"},
{1, "I"},
};
std::string result;
bool needMark = false;
std::string marks(markCount, ''');
for (auto mapping : importantNumbers) {
int value = mapping.first;
std::string &digits = mapping.second;
while (n >= value) {
result += digits;
n -= value;
needMark = true;
}
if ((value == 1000 || value == 100 || value == 10 || value == 1) && needMark) {
result += marks;
needMark = false;
}
}
return result;
}
关于将字符串转换为数字:
// in C++11
int n = std::stoi(str);
// in C++03
std::istringstream iss(str);
int n;
iss >> n;
所以,把你的字符串分成三位数的块(从末尾开始!),并用适当的标记数把它们传进去。
不是最好的解决方案,但它确实有效。
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
int romanNumeralToInt(std::string s) {
std::map<char,int> romanNum = {
{'I',1}, {'V',5}, {'X',10}, {'L',50}, {'C',100},
{'D',500}, {'M',1000}
};
//{"IIIII",5}, {"VV",10}, {"XXXXX",50}, {"LL",100}, {"CCCCC",500}, {"DD",1000}
int g = 0;
std::sort(s.begin(),s.end());
if(s.find("IIIII") != std::string::npos)
replaceAll(s,"IIIII","V");
if(s.find("VV") != std::string::npos)
replaceAll(s,"VV","X");
if(s.find("XXXXX") != std::string::npos)
replaceAll(s,"XXXXX","L");
if(s.find("LL") != std::string::npos)
replaceAll(s,"LL","C");
for(auto& i : s) {
if(romanNum[i] != 0)
g += romanNum[i];
}
return g;
}
int main() {
std::string st = "XXXXXIIIIIVVVXLLD";
int a = romanNumeralToInt(st);
std::cout << a;
}
打印680。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 在将数字随机生成为数组期间从内存输出随机数的数组