在字符串中查找字符时出现问题
Trouble with finding characters in a string
我是c++的新手,虽然我有Java的基本知识,但我正在尝试制作一个程序,转换罗马数字输入,然后找到等效的阿拉伯数字并输出。然而,我遇到了在罗马数字中查找特定前缀的问题。我试图使用str.find函数,然后使用str.substr测试前缀是否存在,如果存在,它会给出阿拉伯值,然后继续使用下一个前缀。但是,我的代码似乎失败或打印出"0"。我想知道我是不是用错了str函数,还是有一种更简单的方法可以在字符串中找到前缀?
这是我当前的代码:
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
/*
*
*/
int main() {
string roman_digits [] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
string roman_tens [] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string roman_hundreds [] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
string roman_thousands [] = {"", "M", "MM", "MMM"};
string line, substr;
int arabic = 0;
// MCCCXXXVII
cout << "Type in a Roman numeral: ";
// Loops through inputted Roman Numerals.
while (cin >> line) {
if (!cin.eof()) {
int i = 0;
// Loops through a Roman numeral and changes it to uppercase.
while (line[i]) {
char c;
c = line[i];
c = (toupper(c));
line[i] = c;
i++;
}
// Loops through checking roman numeral with the thousands array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 4; i++) {
if (line.find("MMM") != string::npos) {
unsigned pos = line.find("MMM");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("MM") != string::npos) {
unsigned pos = line.find("MM");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("M") != string::npos) {
unsigned pos = line.find("M");
substr = line.substr(pos, 1);
line.erase(pos, 1);
}
if (roman_thousands[i] == substr){
arabic = arabic + (i * 1000);
}
}
// Loops through checking roman numeral with the hundreds array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++) {
if (line.find("CM") != string::npos){
unsigned pos = line.find("CM");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("DCCC") != string::npos){
unsigned pos = line.find("DCCC");
substr = line.substr(pos, 4);
line.erase(pos, 4);
} else if (line.find("DCC") != string::npos){
unsigned pos = line.find("DCC");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("DC") != string::npos){
unsigned pos = line.find("DC");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("D") != string::npos){
unsigned pos = line.find("D");
substr = line.substr(pos, 1);
line.erase(pos, 1);
} else if (line.find("CD") != string::npos){
unsigned pos = line.find("CD");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("CCC") != string::npos){
unsigned pos = line.find("CCC");
substr = line.substr(pos, 3);
line.erase(pos, 3);
}else if (line.find("CC") != string::npos){
unsigned pos = line.find("CC");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("C") != string::npos){
unsigned pos = line.find("C");
substr = line.substr(pos, 1);
line.erase(pos, 1);
}
if (roman_hundreds[i] == substr) {
arabic = arabic + (i * 100);
}
}
// Loops through checking roman numeral with the tens array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++) {
if (line.find("XC") != string::npos){
unsigned pos = line.find("XC");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("LXXX") != string::npos){
unsigned pos = line.find("LXXX");
substr = line.substr(pos, 4);
line.erase(pos, 4);
}else if (line.find("LXX") != string::npos){
unsigned pos = line.find("LXX");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("LX") != string::npos){
unsigned pos = line.find("LX");
substr = line.substr(pos, 2);
line.erase(pos, 2);
}else if (line.find("L") != string::npos){
unsigned pos = line.find("L");
substr = line.substr(pos, 1);
line.erase(pos, 1);
}else if (line.find("XL") != string::npos){
unsigned pos = line.find("XL");
substr = line.substr(pos, 2);
line.erase(pos, 2);
}else if (line.find("XXX") != string::npos){
unsigned pos = line.find("XXX");
substr = line.substr(pos, 3);
line.erase(pos, 3);
}else if (line.find("XX") != string::npos){
unsigned pos = line.find("XX");
substr = line.substr(pos, 2);
line.erase(pos, 2);
}else if (line.find("X") != string::npos){
unsigned pos = line.find("X");
substr = line.substr(pos, 1);
line.erase(pos, 1);
}
if (roman_tens[i] == substr) {
arabic = arabic + (i * 10);
}
}
// Loops through checking roman numeral with the digits array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++) {
if (line.find("IX") != string::npos){
unsigned pos = line.find("IX");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("VIII") != string::npos){
unsigned pos = line.find("VIII");
substr = line.substr(pos, 4);
line.erase(pos, 4);
} else if (line.find("VII") != string::npos){
unsigned pos = line.find("VII");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("VI") != string::npos){
unsigned pos = line.find("VI");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("V") != string::npos){
unsigned pos = line.find("V");
substr = line.substr(pos, 1);
line.erase(pos, 1);
} else if (line.find("IV") != string::npos){
unsigned pos = line.find("IV");
substr = line.substr(pos, 2);
line.erase(pos, 2);
} else if (line.find("III") != string::npos){
unsigned pos = line.find("III");
substr = line.substr(pos, 3);
line.erase(pos, 3);
} else if (line.find("II") != string::npos){
unsigned pos = line.find("II");
substr = line.substr(pos, 2);
line.erase(pos, 2);
}else if (line.find("I") != string::npos){
unsigned pos = line.find("I");
substr = line.substr(pos, 1);
}
if (roman_digits[i] == substr) {
arabic = arabic + i;
}
}
cout << "The Arabic equivalent of " << line << " is: " << arabic << endl;
arabic = 0;
} else {
cout << "Invalid Roman numeral." << endl;
}
}
return 0;
}
如有任何帮助,我们将不胜感激。
编辑:所以我接受了建议,一切似乎都很好(代码已经编辑),所以非常感谢。^^
然而,由于它是单独检查"X"并删除它,我的程序将输入"IX"翻译为11,而实际上它是9。我知道这与我的程序在字符串中查找前缀的顺序有关,但我不确定如何修复它,所以在这方面提供任何帮助都会很好。
再次感谢
string::find
不返回布尔(如您在下一行中看到的)。如果找不到字符串,它将返回一个特殊的常量string::npos
。值为0表示字符串是在开头找到的,而不是失败的。
试试这个
string::size_type pos = line.find("MMM");
if (pos != string::npos)
{
...
}
其他意见
这里有一种将字符串转换为大写的单行方法。您需要包括<algorithm>
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
您可以使您的数据数组,如roman_digits const string
,以防止意外修改它们。
尽量在第一次使用时声明变量。例如,char c
只需要在while循环中定义。
std::string::find()
函数不返回布尔值,而是返回找到的子字符串的位置,如果没有这样的位置,则返回std::string::npos
。因此,您的测试可能应该是这样的:
if (line.find("MMM") != std::string::npos) { ... }
当然,四次尝试相同的测试序列并没有多大意义。您可能希望索引i
从数千数组中选择适当的字符串,并且只有一个测试。当然,您可能希望颠倒字符串的顺序或倒计时,这样会很有用。
对于其他检查,您可能也应该使用find()
,因为后面可能还有其他数字。
我认为这段代码更简单,我没有实现错误检查,但它应该不难:
#include <iostream>
#include <map>
using namespace std;
int fromRoman(string n)
{
map<char, int> m;
m['I'] = 1;
m['V'] = 5;
m['X'] = 10;
m['L'] = 50;
m['C'] = 100;
m['D'] = 500;
m['M'] = 1000;
int tmp = 0;
int res = 0;
for (string::iterator i = n.begin(); i != n.end(); ++i)
{
int d = m[*i];
if (d < tmp)
{
res += tmp;
tmp = d;
}
else if (d > tmp)
{
if (tmp == 0)
tmp = d;
else
{
res += d - tmp;
tmp = 0;
}
}
else if (d == tmp)
{
res += tmp + d;
tmp = 0;
}
}
return res + tmp;
}
int main()
{
const char *romanNumbers[] = {
"IV", "VIII", "IX", "XXXI", "XLVI", "XCIX", "DLXXXIII", "DCCCLXXXVIII",
"MDCLXVIII", "MCMLXXXIX", "MMX", "MMXII", "MMMCMXCIX"
};
for (const char **r = romanNumbers; r != romanNumbers + sizeof(romanNumbers) / sizeof(*romanNumbers); ++r)
cout << *r << " is " << fromRoman(*r) << endl;
}
输出:
IV is 4
VIII is 8
IX is 9
XXXI is 31
XLVI is 46
XCIX is 99
DLXXXIII is 583
DCCCLXXXVIII is 888
MDCLXVIII is 1668
MCMLXXXIX is 1989
MMX is 2010
MMXII is 2012
MMMCMXCIX is 3999
- 从矢量C++读取字符时出现问题
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- C++理解计算字符变量的问题
- 将十六进制值设置为用于填充的字符变量时出现问题
- 编写 cin.get() 以接收字符数组时出现问题
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- 字符 * 未从重载运算符或内存管理问题正确返回
- 将utf16宽std::wstring转换为utf8窄std::string以获得罕见字符时出现问题
- 我在使用字符的函数时遇到了一些问题
- 用wchar_t处理 unicode 字符好吗?它不会引起任何问题吗?
- 将 c++ 字符串转换为常量字符 * 时出现问题
- C++程序,将整数输入转换为字符的问题
- 我通过迭代加法将二进制数转换为十进制并检查单个字符(请参阅代码)的方法有什么问题?
- C++ 初级面试问题:仅使用字符指针压缩字符序列的功能
- 通过比较字符设置字符串时出现问题
- 我的编辑距离递归代码中的字符类型有问题
- 获取“boost::文件系统::p ath”字符指针时出现问题
- C Unicode UTF-8解码字符的问题
- 类型转换问题:字符数组的元素转换为整数变量