我怎样才能在我的罗马数字到十进制转换器中得到结果?

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

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

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

你的程序有很多错误:

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

我已经修复了代码:

#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 控制语句,我使用 unordered map 将小写数字映射到 int (大写字符在查找之前转换为小写地图)。

  • class 本身现在是改进的 singleton, and works as a functor 字符。

  • 现在有一个重载的 istream operator>> 允许使用 class 实例作为 istream 的接收器(std::cin 或我以前使用的 stringstream mock 用户输入)

要交互使用它,只需将 #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("C\n+\n+\nM\nM\nX\nI\nV"); 
#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;
}

live 在 Coliru 的