如何在罗马数字到十进制转换器中获得结果

How can i get a result in my Roman numeral to decimal converter?

本文关键字:转换器 结果 十进制 罗马 数字      更新时间:2023-10-16
#include <iostream>
#include <string>
using namespace std;
class rom2dec
{ 
    public: void roman(); 
    int convert(); 
    void print(); 
    void get(); 
    private: int M, D, C, L, X, V, I; 
    char romanNumeral; 
};
    void rom2dec::roman()
{ 
    M = 1000; 
    D = 500; 
    C = 100; 
    L = 50; 
    X = 10; 
    V = 5; 
    I = 1; 
} 
    int rom2dec::convert()
{ 
    if(romanNumeral == 'I' || 'i')
    { cout << 1; }
    else if(romanNumeral == 'V' || 'v')
    { cout << 5; }
    else if(romanNumeral == 'X' || 'x')
    { cout << 10; }
    else if(romanNumeral == 'L' || 'l')
    { cout << 50; }
    else if(romanNumeral == 'C' || 'c')
    { cout << 100; }
    else if(romanNumeral == 'D' || 'd')
    { cout << 500; }
    else if (romanNumeral == 'M' || 'm')
    { cout << 1000; }
    else if(romanNumeral != 'I' && romanNumeral != 'i' && romanNumeral !=  'V' && romanNumeral != 'v' && romanNumeral != 'X' && romanNumeral != 'x' && romanNumeral != 'L' && romanNumeral != 'l' && romanNumeral != 'C' && romanNumeral != 'c' && romanNumeral != 'D' && romanNumeral != 'd' && romanNumeral != 'M' && romanNumeral != 'm')
    { cout << "Error! Not a valid value!" << endl; }
    return romanNumeral; 
}
    void rom2dec::print()
    { cout << romanNumeral << endl; }
    void rom2dec::get(){ }
int main()
{
    char romanNumeral; 
    cout << "Please enter a number in Roman numerals to be converted: " << endl; 
    cin >> romanNumeral;  
    return 0;
}

我在构建程序时没有收到任何错误,但是当我调试并尝试将罗马数字转换为十进制时,我没有得到小数。欢迎所有建议,如果有更简单的方法来编写最后一个,请告诉我。谢谢。

程序中有许多错误:

  1. 类 rom2dec 中缺少构造函数、析构函数
  2. 主函数未调用方法来进行转换
  3. 如果陈述是错误的。

我已经修复了代码:

#include <iostream>
#include <string>
using namespace std;
class rom2dec
{ 
public: 
    rom2dec(char x): romanNumeral(x) {};
    ~rom2dec() {};
        void roman(); 
        int convert(); 
        void print(); 
        void get(); 
private: int M, D, C, L, X, V, I; 
         char romanNumeral; 
};
void rom2dec::roman()
{ 
    M = 1000; 
    D = 500; 
    C = 100; 
    L = 50; 
    X = 10; 
    V = 5; 
    I = 1; 
} 
int rom2dec::convert()
{ 
    if (romanNumeral == 'I' || romanNumeral == 'i')
    { cout << 1; }
    else if (romanNumeral == 'V' || romanNumeral == 'v')
    { cout << 5; }
    else if (romanNumeral == 'X' || romanNumeral == 'x')
    { cout << 10; }
    else if ((romanNumeral == 'L') || romanNumeral == 'l')
    { cout << 50; }
    else if (romanNumeral == 'C' || romanNumeral == 'c')
    { cout << 100; }
    else if (romanNumeral == 'D' || romanNumeral == 'd')
    { cout << 500; }
    else if (romanNumeral == 'M' || romanNumeral == 'm')
    { cout << 1000; }
    else 
    { cout << "Error! Not a valid value!" << endl; }
    return romanNumeral; 
}
void rom2dec::print()
{ cout << romanNumeral << endl; }
void rom2dec::get(){ }
int main()
{
    char romanNumeral; 
    cout << "Please enter a number in Roman numerals to be converted: " << endl; 
    cin >> romanNumeral;  
    rom2dec obj(romanNumeral);
    obj.convert();
    return 0;
}

我稍微重拍了一下。

  • 我没有使用类似面条的if-else-if-else-if控制语句,而是使用无序映射将小写数字映射到 int(大写字符在映射中查找之前转换为小写(。

  • 该类本身现在是一个改进的单例,并用作 char 上的函子

  • 现在有一个重载istream operator>>允许将类实例用作 iStreams 的接收器(std::cin 或我用来模拟用户输入的字符串流(

要以交互方式使用它,只需将#if 0更改为#if 1

#include <iostream>
#include <unordered_map>
#include <string>
#include <sstream>

class rom2dec
{ 
public: 
    using map_type = std::unordered_map<char,int>;
    static rom2dec& instance() {
        static rom2dec r2d;
        return r2d;
    }
    int operator() (char romanNumeral) const; 
    void print(char romanNumeral) const; 
    void get(); // ?
private: 
     rom2dec() = default;
     ~rom2dec() = default;
     rom2dec(const rom2dec&) = delete;
     rom2dec(rom2dec&&) = delete;
     rom2dec& operator=(rom2dec&&) = delete;
     rom2dec& operator=(const rom2dec&) = delete;
     static map_type map_;   
};
rom2dec::map_type rom2dec::map_ = {   
                                    {'i',    1}, 
                                    {'v',    5}, 
                                    {'x',   10}, 
                                    {'l',   50}, 
                                    {'c',  100}, 
                                    {'d',  500}, 
                                    {'m', 1000}
                                  };    

int rom2dec::operator() (char romanNumeral) const
{
    int rv = -1;
    auto it = map_.find(std::tolower(romanNumeral));
    if (it != map_.end()) {    
        rv = it->second; 
    } else {
        std::cerr << "Error! '" << romanNumeral 
                  << "' is not a valid value!" 
                  << std::endl; 
    }
    return rv;
}
void rom2dec::print(char romanNumeral) const
{ 
    std::cout << romanNumeral << "n";
}
void rom2dec::get() {}
std::istream& operator>>(std::istream& is, const rom2dec& r2d) {
    char romanNumeral;
    if (is >> romanNumeral) {
        int dec = r2d(romanNumeral);
        if (dec > 0)
            std::cout << romanNumeral << " = " << dec << "n";
    }
    return is;
}

int main()
{
    auto& r2d = rom2dec::instance();
#if 0
    auto& is = std::cin;
#else     
    std::stringstream is("Cn+n+nMnMnXnInV"); 
#endif    

    do {
        std::cout << "Please enter a single Roman numeral to be converted"
                  << "(press <CTRL>+<D> to terminate):n";
    } while (is >> r2d);  
    return EXIT_SUCCESS;
}

住在科利鲁