【问题标题】:How can i get a result in my Roman numeral to decimal converter?如何在我的罗马数字到十进制转换器中获得结果?
【发布时间】:2016-02-23 17:00:01
【问题描述】:
#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,请告诉我。谢谢。

【问题讨论】:

  • 你没有在main使用你的班级
  • 你需要调用convert,然后在main()中打印
  • romanNumeral == 'I' || 'i' 这不会做你想做的事!你(很遗憾,我知道)必须完整地写出来。 romanNumeral == 'I' || romanNumeral == 'i'

标签: c++ if-statement converter roman-numerals


【解决方案1】:

你的程序有很多错误:

  1. 类 rom2dec 中缺少构造函数、析构函数
  2. 主函数没有调用方法进行转换
  3. if 语句错误。

我已经修复了代码:

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

【讨论】:

  • 你能解释一下“~”的作用吗?
  • "~rom2dec(){}" 定义类析构函数。析构函数可用于取消分配或释放对象使用的任何资源,否则在程序终止的情况下无法收回。在你的情况下,没有。
  • 哦。感谢所有的帮助。 :)
【解决方案2】:

我稍微重做了一遍。

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

  • 类本身现在是一个改进的singleton,并且在字符上作为functor 工作。

  • 现在有一个重载的istreamoperator&gt;&gt;,它允许将类实例用作 istream 的接收器(std::cin 或我以前用于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's

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    相关资源
    最近更新 更多