【发布时间】: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